2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
3 * Written by Ben Walton.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* for HOST_NAME_MAX */
32 #define TESTHOSTNAME "gnulib-hostname"
34 /* mingw and MSVC 9 lack geteuid, so setup a dummy value.
35 On Cygwin, geteuid() may return non-zero even for user accounts with
36 administrator privileges, so use a dummy value as well. */
37 #if !HAVE_GETEUID || defined __CYGWIN__
42 main (int argc, char *argv[] _GL_UNUSED)
44 char origname[HOST_NAME_MAX];
45 char newname[HOST_NAME_MAX];
46 char longname[HOST_NAME_MAX + 2];
49 /* skip the tests if we don't have root privilege. this does not
50 consider things like CAP_SYS_ADMIN (linux) or PRIV_SYS_ADMIN
51 (solaris), etc. systems without a working geteuid (mingw, MSVC
52 9) will always skip this test. */
55 fprintf (stderr, "Skipping test: insufficient permissions.\n");
59 /* we want to ensure we can do a get/set/get check to ensure the
60 change is accepted. record the current name so it can be restored
62 ASSERT (gethostname (origname, sizeof (origname)) == 0);
64 /* try setting a valid hostname. if it fails -1/ENOSYS, we will
65 skip the test for long names as this is an indication we're using
66 the stub function that doesn't do anything on this platform. */
67 rcs = sethostname (TESTHOSTNAME, strlen (TESTHOSTNAME));
71 if (rcs == -1 && errno == ENOSYS)
74 "Skipping test: sethostname is not really implemented.\n");
77 else if (rcs == -1 && errno == EPERM)
79 fprintf (stderr, "Skipping test: insufficient permissions.\n");
84 fprintf (stderr, "error setting valid hostname.\n");
90 ASSERT (gethostname (newname, sizeof (newname)) == 0);
92 /* On Windows, a hostname change becomes effective only after
94 #if !((defined _WIN32 || defined __WIN32__) || defined __CYGWIN__)
96 /* if we don't get back what we put in, there is no need to
97 restore the original name as we will assume it was not
99 if (strcmp (newname, TESTHOSTNAME) != 0)
101 fprintf (stderr, "set/get comparison failed.\n");
107 /* glibc does allow setting a zero length name, so the lower bound
108 needs no test. validate that we are constrained by
110 for (i = 0; i < (HOST_NAME_MAX + 1); i++)
115 rcs = sethostname (longname, (HOST_NAME_MAX + 1));
119 /* attempt to restore the original name. */
120 ASSERT (sethostname (origname, strlen (origname)) == 0);
121 fprintf (stderr, "setting a too long hostname succeeded.\n");
125 /* restore the original name. */
126 ASSERT (sethostname (origname, strlen (origname)) == 0);