2 * Copyright (C) 2011 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 #include "signature.h"
23 SIGNATURE_CHECK (sethostname, int, (const char *, size_t));
25 /* for HOST_NAME_MAX */
28 #include <sys/types.h>
37 #define TESTHOSTNAME "gnulib-hostname"
39 /* mingw and MSVC 9 lack geteuid, so setup a value that will indicate
40 we don't have root privilege since we wouldn't know whether to
41 expect success or failure when setting a name anyway*/
43 # define geteuid() ((uid_t) -1)
47 main (int argc, char *argv[] _GL_UNUSED)
49 char origname[HOST_NAME_MAX];
50 char newname[HOST_NAME_MAX];
51 char longname[HOST_NAME_MAX + 2];
54 /* skip the tests if we don't have root privilege. this does not
55 consider things like CAP_SYS_ADMIN (linux) or PRIV_SYS_ADMIN
56 (solaris), etc. systems without a working geteuid (mingw, MSVC
57 9) will always skip this test. */
61 /* we want to ensure we can do a get/set/get check to ensure the
62 change is accepted. record the current name so it can be restored
64 ASSERT(gethostname (origname, sizeof (origname)) == 0);
66 /* try setting a valid hostname. if it fails -1/ENOSYS, we will
67 skip the test for long names as this is an indication we're using
68 the stub function that doesn't do anything on this platform. */
69 rcs = sethostname (TESTHOSTNAME, strlen (TESTHOSTNAME));
73 if (rcs == -1 && errno == ENOSYS)
77 printf ("error setting valid hostname.\n");
83 ASSERT (gethostname (newname, sizeof (newname)) == 0);
85 /* if we don't get back what we put in, there is no need to
86 restore the original name as we will assume it was not
88 if (strcmp (newname, TESTHOSTNAME) != 0)
90 printf ("set/get comparison failed.\n");
95 /* glibc does allow setting a zero length name, so the lower bound
96 needs no test. validate that we are constrained by
98 for (i = 0; i < (HOST_NAME_MAX + 1); i++)
103 rcs = sethostname ((const char *) longname, (HOST_NAME_MAX + 1));
107 /* attempt to restore the original name. */
108 sethostname (origname, strlen (origname));
109 printf ("expected failure when setting very long hostname.\n");
113 /* restore the original name. */
114 sethostname (origname, strlen (origname));