pty: Activate the signature wrapper of forkpty.
[gnulib.git] / tests / test-sethostname2.c
1 /*
2  * Copyright (C) 2011-2013 Free Software Foundation, Inc.
3  * Written by Ben Walton.
4  *
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.
9  *
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.
14  *
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/>.  */
17
18 #include <config.h>
19
20 #include <unistd.h>
21
22 /* for HOST_NAME_MAX */
23 #include <limits.h>
24 /* for strlen */
25 #include <string.h>
26
27 #include <errno.h>
28 #include <stdio.h>
29
30 #include "root-uid.h"
31
32 #include "macros.h"
33
34 #define TESTHOSTNAME "gnulib-hostname"
35
36 /* mingw and MSVC 9 lack geteuid, so setup a dummy value.
37    On Cygwin, geteuid() may return non-zero even for user accounts with
38    administrator privileges, so use a dummy value as well.  */
39 #if !HAVE_GETEUID || defined __CYGWIN__
40 # define geteuid() ROOT_UID
41 #endif
42
43 int
44 main (int argc, char *argv[] _GL_UNUSED)
45 {
46   char origname[HOST_NAME_MAX];
47   char newname[HOST_NAME_MAX];
48   char longname[HOST_NAME_MAX + 2];
49   int rcs, i;
50
51   /* skip the tests if we don't have root privilege.  this does not
52      consider things like CAP_SYS_ADMIN (linux) or PRIV_SYS_ADMIN
53      (solaris), etc.  systems without a working geteuid (mingw, MSVC
54      9) will always skip this test. */
55   if (geteuid () != ROOT_UID)
56     {
57       fprintf (stderr, "Skipping test: insufficient permissions.\n");
58       return 77;
59     }
60
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
63      later */
64   ASSERT (gethostname (origname, sizeof (origname)) == 0);
65
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));
70
71   if (rcs != 0)
72     {
73       if (rcs == -1 && errno == ENOSYS)
74         {
75           fprintf (stderr,
76                    "Skipping test: sethostname is not really implemented.\n");
77           return 77;
78         }
79       else if (rcs == -1 && errno == EPERM)
80         {
81           fprintf (stderr, "Skipping test: insufficient permissions.\n");
82           return 77;
83         }
84       else
85         {
86           fprintf (stderr, "error setting valid hostname.\n");
87           return 1;
88         }
89     }
90   else
91     {
92       ASSERT (gethostname (newname, sizeof (newname)) == 0);
93
94       /* On Windows, a hostname change becomes effective only after
95          a reboot.  */
96 #if !((defined _WIN32 || defined __WIN32__) || defined __CYGWIN__)
97
98       /* if we don't get back what we put in, there is no need to
99          restore the original name as we will assume it was not
100          properly changed. */
101       if (strcmp (newname, TESTHOSTNAME) != 0)
102         {
103           fprintf (stderr, "set/get comparison failed.\n");
104           return 1;
105         }
106 #endif
107     }
108
109   /* glibc does allow setting a zero length name, so the lower bound
110      needs no test. validate that we are constrained by
111      HOST_NAME_MAX */
112   for (i = 0; i < (HOST_NAME_MAX + 1); i++)
113     longname[i] = 'a';
114
115   longname[i] = '\0';
116
117   rcs = sethostname (longname, (HOST_NAME_MAX + 1));
118
119   if (rcs != -1)
120     {
121       /* attempt to restore the original name. */
122       ASSERT (sethostname (origname, strlen (origname)) == 0);
123       fprintf (stderr, "setting a too long hostname succeeded.\n");
124       return 1;
125     }
126
127   /* restore the original name. */
128   ASSERT (sethostname (origname, strlen (origname)) == 0);
129
130   return 0;
131 }