(root)/
coreutils-9.4/
gnulib-tests/
test-symlink.h
       1  /* Tests of symlink.
       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  /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
      18  
      19  /* This file is designed to test both symlink(a,b) and
      20     symlinkat(a,AT_FDCWD,b).  FUNC is the function to test.  Assumes
      21     that BASE and ASSERT are already defined, and that appropriate
      22     headers are already included.  If PRINT, warn before skipping
      23     symlink tests with status 77.  */
      24  
      25  static int
      26  test_symlink (int (*func) (char const *, char const *), bool print)
      27  {
      28    if (func ("nowhere", BASE "link1"))
      29      {
      30        if (print)
      31          fputs ("skipping test: symlinks not supported on this file system\n",
      32                 stderr);
      33        return 77;
      34      }
      35  
      36    /* Some systems allow the creation of 0-length symlinks as a synonym
      37       for "."; but most reject it.  */
      38    {
      39      int status;
      40      errno = 0;
      41      status = func ("", BASE "link2");
      42      if (status == -1)
      43        ASSERT (errno == ENOENT || errno == EINVAL);
      44      else
      45        {
      46          ASSERT (status == 0);
      47          ASSERT (unlink (BASE "link2") == 0);
      48        }
      49    }
      50  
      51    /* Sanity checks of failures.  */
      52    errno = 0;
      53    ASSERT (func ("nowhere", "") == -1);
      54    ASSERT (errno == ENOENT);
      55    errno = 0;
      56    ASSERT (func ("nowhere", ".") == -1);
      57    ASSERT (errno == EEXIST || errno == EINVAL);
      58    errno = 0;
      59    ASSERT (func ("somewhere", BASE "link1") == -1);
      60    ASSERT (errno == EEXIST);
      61    errno = 0;
      62    ASSERT (func ("nowhere", BASE "link2/") == -1);
      63    ASSERT (errno == ENOTDIR || errno == ENOENT);
      64    ASSERT (mkdir (BASE "dir", 0700) == 0);
      65    errno = 0;
      66    ASSERT (func ("nowhere", BASE "dir") == -1);
      67    ASSERT (errno == EEXIST);
      68    errno = 0;
      69    ASSERT (func ("nowhere", BASE "dir/") == -1);
      70    ASSERT (errno == EEXIST || errno == EINVAL
      71            || errno == ENOENT /* Lustre FS on Linux */);
      72    ASSERT (close (creat (BASE "file", 0600)) == 0);
      73    errno = 0;
      74    ASSERT (func ("nowhere", BASE "file") == -1);
      75    ASSERT (errno == EEXIST);
      76    errno = 0;
      77    ASSERT (func ("nowhere", BASE "file/") == -1);
      78    ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
      79  
      80    /* Trailing slash must always be rejected.  */
      81    ASSERT (unlink (BASE "link1") == 0);
      82    ASSERT (func (BASE "link2", BASE "link1") == 0);
      83    errno = 0;
      84    ASSERT (func (BASE "nowhere", BASE "link1/") == -1);
      85    ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
      86    errno = 0;
      87    ASSERT (unlink (BASE "link2") == -1);
      88    ASSERT (errno == ENOENT);
      89  
      90    /* Cleanup.  */
      91    ASSERT (rmdir (BASE "dir") == 0);
      92    ASSERT (unlink (BASE "file") == 0);
      93    ASSERT (unlink (BASE "link1") == 0);
      94  
      95    return 0;
      96  }