(root)/
diffutils-3.10/
gnulib-tests/
test-areadlink.h
       1  /* Tests of areadlink and friends.
       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 areadlink(a),
      20     areadlink_with_size(a,b), and areadlinkat(AT_FDCWD,a).  FUNC is the
      21     function to test; a length is always supplied, but may be ignored.
      22     Assumes that BASE and ASSERT are already defined, and that
      23     appropriate headers are already included.  If PRINT, warn before
      24     skipping symlink tests with status 77.  */
      25  
      26  static int
      27  test_areadlink (char * (*func) (char const *, size_t), bool print)
      28  {
      29    /* Sanity checks of failures.  Mingw lacks symlink, but areadlink can
      30       still distinguish between various errors.  */
      31    errno = 0;
      32    ASSERT (func ("no_such", 1) == NULL);
      33    ASSERT (errno == ENOENT);
      34    errno = 0;
      35    ASSERT (func ("no_such/", 1) == NULL);
      36    ASSERT (errno == ENOENT);
      37    errno = 0;
      38    ASSERT (func ("", 1) == NULL);
      39    ASSERT (errno == ENOENT || errno == EINVAL);
      40    errno = 0;
      41    ASSERT (func (".", 1) == NULL);
      42    ASSERT (errno == EINVAL);
      43    errno = 0;
      44    ASSERT (func ("./", 1) == NULL);
      45    ASSERT (errno == EINVAL);
      46    ASSERT (close (creat (BASE "file", 0600)) == 0);
      47    errno = 0;
      48    ASSERT (func (BASE "file", 1) == NULL);
      49    ASSERT (errno == EINVAL);
      50    errno = 0;
      51    ASSERT (func (BASE "file/", 1) == NULL);
      52    ASSERT (errno == ENOTDIR || errno == EINVAL); /* AIX yields EINVAL */
      53    ASSERT (unlink (BASE "file") == 0);
      54  
      55    /* Now test actual symlinks.  */
      56    if (symlink (BASE "dir", BASE "link"))
      57      {
      58        if (print)
      59          fputs ("skipping test: symlinks not supported on this file system\n",
      60                 stderr);
      61        return 77;
      62      }
      63    ASSERT (mkdir (BASE "dir", 0700) == 0);
      64    errno = 0;
      65    ASSERT (func (BASE "link/", 1) == NULL);
      66    ASSERT (errno == EINVAL);
      67    {
      68      /* Too small a guess is okay.  */
      69      char *buf = func (BASE "link", 1);
      70      ASSERT (buf);
      71      ASSERT (strcmp (buf, BASE "dir") == 0);
      72      free (buf);
      73      /* Too large a guess is okay.  */
      74      buf = func (BASE "link", 10000000);
      75      ASSERT (buf);
      76      ASSERT (strcmp (buf, BASE "dir") == 0);
      77      free (buf);
      78    }
      79    ASSERT (rmdir (BASE "dir") == 0);
      80    ASSERT (unlink (BASE "link") == 0);
      81  
      82    return 0;
      83  }