249c064c00db29c4f39b9ccc811a2265c6df0985
[gnulib.git] / tests / test-sethostname.c
1 /*
2  * Copyright (C) 2011 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 #include "signature.h"
23 SIGNATURE_CHECK (sethostname, int, (const char *, size_t));
24
25 /* for HOST_NAME_MAX */
26 #include <limits.h>
27 /* for uid_t */
28 #include <sys/types.h>
29 /* for strlen */
30 #include <string.h>
31
32 #include <errno.h>
33 #include <stdio.h>
34
35 #include "macros.h"
36
37 #define TESTHOSTNAME "gnulib-hostname"
38
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*/
42 #if !HAVE_GETEUID
43 # define geteuid() ((uid_t) -1)
44 #endif
45
46 int
47 main (int argc, char *argv[] _GL_UNUSED)
48 {
49   char origname[HOST_NAME_MAX];
50   char newname[HOST_NAME_MAX];
51   char longname[HOST_NAME_MAX + 2];
52   int rcg, rcs, i;
53
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. */
58   if (geteuid() != 0)
59     return 0;
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         return 0;
75       else
76         {
77           printf ("error setting valid hostname.\n");
78           return 1;
79         }
80     }
81   else
82     {
83       ASSERT (gethostname (newname, sizeof (newname)) == 0);
84
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
87          properly changed. */
88       if (strcmp (newname, TESTHOSTNAME) != 0)
89         {
90           printf ("set/get comparison failed.\n");
91           return 1;
92         }
93     }
94
95   /* glibc does allow setting a zero length name, so the lower bound
96      needs no test. validate that we are constrained by
97      HOST_NAME_MAX */
98   for (i = 0; i < (HOST_NAME_MAX + 1); i++)
99     longname[i] = 'a';
100
101   longname[i] = '\0';
102
103   rcs = sethostname ((const char *) longname, (HOST_NAME_MAX + 1));
104
105   if (rcs != -1)
106     {
107       /* attempt to restore the original name. */
108       sethostname (origname, strlen (origname));
109       printf ("expected failure when setting very long hostname.\n");
110       return 1;
111     }
112
113   /* restore the original name. */
114   sethostname (origname, strlen (origname));
115
116   return 0;
117 }