f049ff3e8df471f2054054b2f3db28962adcda46
[platform/upstream/diffutils.git] / gnulib-tests / test-symlink.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Tests of symlink.
4    Copyright (C) 2009-2011 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
20
21 /* This file is designed to test both symlink(a,b) and
22    symlinkat(a,AT_FDCWD,b).  FUNC is the function to test.  Assumes
23    that BASE and ASSERT are already defined, and that appropriate
24    headers are already included.  If PRINT, warn before skipping
25    symlink tests with status 77.  */
26
27 static int
28 test_symlink (int (*func) (char const *, char const *), bool print)
29 {
30   if (func ("nowhere", BASE "link1"))
31     {
32       if (print)
33         fputs ("skipping test: symlinks not supported on this file system\n",
34                stderr);
35       return 77;
36     }
37
38   /* Some systems allow the creation of 0-length symlinks as a synonym
39      for "."; but most reject it.  */
40   {
41     int status;
42     errno = 0;
43     status = func ("", BASE "link2");
44     if (status == -1)
45       ASSERT (errno == ENOENT || errno == EINVAL);
46     else
47       {
48         ASSERT (status == 0);
49         ASSERT (unlink (BASE "link2") == 0);
50       }
51   }
52
53   /* Sanity checks of failures.  */
54   errno = 0;
55   ASSERT (func ("nowhere", "") == -1);
56   ASSERT (errno == ENOENT);
57   errno = 0;
58   ASSERT (func ("nowhere", ".") == -1);
59   ASSERT (errno == EEXIST || errno == EINVAL);
60   errno = 0;
61   ASSERT (func ("somewhere", BASE "link1") == -1);
62   ASSERT (errno == EEXIST);
63   errno = 0;
64   ASSERT (func ("nowhere", BASE "link2/") == -1);
65   ASSERT (errno == ENOTDIR || errno == ENOENT);
66   ASSERT (mkdir (BASE "dir", 0700) == 0);
67   errno = 0;
68   ASSERT (func ("nowhere", BASE "dir") == -1);
69   ASSERT (errno == EEXIST);
70   errno = 0;
71   ASSERT (func ("nowhere", BASE "dir/") == -1);
72   ASSERT (errno == EEXIST || errno == EINVAL);
73   ASSERT (close (creat (BASE "file", 0600)) == 0);
74   errno = 0;
75   ASSERT (func ("nowhere", BASE "file") == -1);
76   ASSERT (errno == EEXIST);
77   errno = 0;
78   ASSERT (func ("nowhere", BASE "file/") == -1);
79   ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
80
81   /* Trailing slash must always be rejected.  */
82   ASSERT (unlink (BASE "link1") == 0);
83   ASSERT (func (BASE "link2", BASE "link1") == 0);
84   errno = 0;
85   ASSERT (func (BASE "nowhere", BASE "link1/") == -1);
86   ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
87   errno = 0;
88   ASSERT (unlink (BASE "link2") == -1);
89   ASSERT (errno == ENOENT);
90
91   /* Cleanup.  */
92   ASSERT (rmdir (BASE "dir") == 0);
93   ASSERT (unlink (BASE "file") == 0);
94   ASSERT (unlink (BASE "link1") == 0);
95
96   return 0;
97 }