Mercurial > hg > octave-shane > gnulib-hg
annotate tests/test-sethostname2.c @ 17530:8981b79e1271
error: make the module depend on vfprintf-posix
* modules/error (Depends-on): Add vfprintf-posix, since error
calls vfprintf unconditionally, via error_tail.
author | Jim Meyering <meyering@fb.com> |
---|---|
date | Sat, 12 Oct 2013 19:28:40 -0700 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
rev | line source |
---|---|
16152
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16951
diff
changeset
|
2 * Copyright (C) 2011-2013 Free Software Foundation, Inc. |
16152
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 * Written by Ben Walton. |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
4 * |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
5 * This program is free software: you can redistribute it and/or modify |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 * it under the terms of the GNU General Public License as published by |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 * the Free Software Foundation; either version 3 of the License, or |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 * (at your option) any later version. |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 * |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 * This program is distributed in the hope that it will be useful, |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 * GNU General Public License for more details. |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
14 * |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 #include <config.h> |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 #include <unistd.h> |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 /* for HOST_NAME_MAX */ |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 #include <limits.h> |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 /* for strlen */ |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
25 #include <string.h> |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
26 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
27 #include <errno.h> |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 #include <stdio.h> |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 |
16951 | 30 #include "root-uid.h" |
31 | |
16152
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
32 #include "macros.h" |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
33 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 #define TESTHOSTNAME "gnulib-hostname" |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 |
16156
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
36 /* mingw and MSVC 9 lack geteuid, so setup a dummy value. |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
37 On Cygwin, geteuid() may return non-zero even for user accounts with |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
38 administrator privileges, so use a dummy value as well. */ |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
39 #if !HAVE_GETEUID || defined __CYGWIN__ |
16951 | 40 # define geteuid() ROOT_UID |
16152
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
41 #endif |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
42 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
43 int |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
44 main (int argc, char *argv[] _GL_UNUSED) |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
45 { |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
46 char origname[HOST_NAME_MAX]; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
47 char newname[HOST_NAME_MAX]; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 char longname[HOST_NAME_MAX + 2]; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 int rcs, i; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
50 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
51 /* skip the tests if we don't have root privilege. this does not |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
52 consider things like CAP_SYS_ADMIN (linux) or PRIV_SYS_ADMIN |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
53 (solaris), etc. systems without a working geteuid (mingw, MSVC |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
54 9) will always skip this test. */ |
16951 | 55 if (geteuid () != ROOT_UID) |
16152
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
56 { |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
57 fprintf (stderr, "Skipping test: insufficient permissions.\n"); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
58 return 77; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
59 } |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
61 /* we want to ensure we can do a get/set/get check to ensure the |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 change is accepted. record the current name so it can be restored |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 later */ |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 ASSERT (gethostname (origname, sizeof (origname)) == 0); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
66 /* try setting a valid hostname. if it fails -1/ENOSYS, we will |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
67 skip the test for long names as this is an indication we're using |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
68 the stub function that doesn't do anything on this platform. */ |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
69 rcs = sethostname (TESTHOSTNAME, strlen (TESTHOSTNAME)); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
70 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
71 if (rcs != 0) |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
72 { |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
73 if (rcs == -1 && errno == ENOSYS) |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
74 { |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
75 fprintf (stderr, |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
76 "Skipping test: sethostname is not really implemented.\n"); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
77 return 77; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
78 } |
16156
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
79 else if (rcs == -1 && errno == EPERM) |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
80 { |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
81 fprintf (stderr, "Skipping test: insufficient permissions.\n"); |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
82 return 77; |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
83 } |
16152
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
84 else |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
85 { |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
86 fprintf (stderr, "error setting valid hostname.\n"); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
87 return 1; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
88 } |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
89 } |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
90 else |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
91 { |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
92 ASSERT (gethostname (newname, sizeof (newname)) == 0); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
93 |
16156
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
94 /* On Windows, a hostname change becomes effective only after |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
95 a reboot. */ |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
96 #if !((defined _WIN32 || defined __WIN32__) || defined __CYGWIN__) |
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
97 |
16152
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
98 /* if we don't get back what we put in, there is no need to |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
99 restore the original name as we will assume it was not |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
100 properly changed. */ |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
101 if (strcmp (newname, TESTHOSTNAME) != 0) |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
102 { |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
103 fprintf (stderr, "set/get comparison failed.\n"); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
104 return 1; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
105 } |
16156
034184e9b5ea
sethostname: Port to Windows platforms.
Bruno Haible <bruno@clisp.org>
parents:
16152
diff
changeset
|
106 #endif |
16152
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
107 } |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
108 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
109 /* glibc does allow setting a zero length name, so the lower bound |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
110 needs no test. validate that we are constrained by |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
111 HOST_NAME_MAX */ |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
112 for (i = 0; i < (HOST_NAME_MAX + 1); i++) |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
113 longname[i] = 'a'; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
114 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
115 longname[i] = '\0'; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
116 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
117 rcs = sethostname (longname, (HOST_NAME_MAX + 1)); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
118 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
119 if (rcs != -1) |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
120 { |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
121 /* attempt to restore the original name. */ |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
122 ASSERT (sethostname (origname, strlen (origname)) == 0); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
123 fprintf (stderr, "setting a too long hostname succeeded.\n"); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
124 return 1; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
125 } |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
126 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
127 /* restore the original name. */ |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
128 ASSERT (sethostname (origname, strlen (origname)) == 0); |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
129 |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
130 return 0; |
cd99c80c513b
sethostname tests: Fix link error on mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
131 } |