(root)/
gettext-0.22.4/
gettext-tools/
gnulib-tests/
test-mkdir.h
       1  /* Test of mkdir() function.
       2     Copyright (C) 2009-2023 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  /* This file is designed to test both mkdir(a,b) and
      18     mkdirat(AT_FDCWD,a,b).  FUNC is the function to test.  Assumes that
      19     BASE and ASSERT are already defined, and that appropriate headers
      20     are already included.  If PRINT, warn before skipping tests with
      21     status 77 when symlinks are unsupported.  */
      22  
      23  static int
      24  test_mkdir (int (*func) (char const *, mode_t), bool print)
      25  {
      26    /* Test basic error handling.  */
      27    ASSERT (close (creat (BASE "file", 0600)) == 0);
      28    errno = 0;
      29    ASSERT (func (BASE "file", 0700) == -1);
      30    ASSERT (errno == EEXIST);
      31    errno = 0;
      32    ASSERT (func (BASE "file/", 0700) == -1);
      33    ASSERT (errno == ENOTDIR || errno == EEXIST);
      34    errno = 0;
      35    ASSERT (func (BASE "file/dir", 0700) == -1);
      36    ASSERT (errno == ENOTDIR || errno == ENOENT || errno == EOPNOTSUPP);
      37    ASSERT (unlink (BASE "file") == 0);
      38    errno = 0;
      39    ASSERT (func ("", 0700) == -1);
      40    ASSERT (errno == ENOENT);
      41    errno = 0;
      42    ASSERT (func (BASE "dir/sub", 0700) == -1);
      43    ASSERT (errno == ENOENT);
      44    errno = 0;
      45    ASSERT (func (BASE "dir/.", 0700) == -1);
      46    ASSERT (errno == ENOENT);
      47    errno = 0;
      48    ASSERT (func (BASE "dir/.//", 0700) == -1);
      49    ASSERT (errno == ENOENT);
      50  
      51    /* Test trailing slash handling.  */
      52    ASSERT (func (BASE "dir", 0700) == 0);
      53    errno = 0;
      54    ASSERT (func (BASE "dir", 0700) == -1);
      55    ASSERT (errno == EEXIST);
      56    ASSERT (rmdir (BASE "dir") == 0);
      57    ASSERT (func (BASE "dir/", 0700) == 0);
      58    errno = 0;
      59    ASSERT (func (BASE "dir/", 0700) == -1);
      60    ASSERT (errno == EEXIST);
      61    ASSERT (rmdir (BASE "dir") == 0);
      62  
      63    /* Test symlink behavior.  POSIX requires the creation of
      64       directories through a dangling symlink with trailing slash, but
      65       GNU does not yet implement that, so we support either behavior
      66       for now.  */
      67    if (symlink (BASE "dir", BASE "link"))
      68      {
      69        if (print)
      70          fputs ("skipping test: symlinks not supported on this file system\n",
      71                 stderr);
      72        return 77;
      73      }
      74    errno = 0;
      75    ASSERT (func (BASE "link", 0700) == -1);
      76    ASSERT (errno == EEXIST);
      77    {
      78      int result;
      79      errno = 0;
      80      result = func (BASE "link/", 0700);
      81      if (!result)
      82        ASSERT (rmdir (BASE "dir") == 0);
      83      else
      84        {
      85          ASSERT (result == -1);
      86          ASSERT (errno == EEXIST);
      87          errno = 0;
      88          ASSERT (rmdir (BASE "dir") == -1);
      89          ASSERT (errno == ENOENT);
      90        }
      91    }
      92    errno = 0;
      93    ASSERT (func (BASE "link/.", 0700) == -1);
      94    ASSERT (errno == ENOENT);
      95    ASSERT (unlink (BASE "link") == 0);
      96  
      97    return 0;
      98  }