# HG changeset patch # User Bruno Haible # Date 1322924220 -3600 # Node ID cd99c80c513b8d0b78b8b538af4419354861f823 # Parent 5e77939eb443d702bf305f8b2a1f3635bf971274 sethostname tests: Fix link error on mingw. * tests/test-sethostname1.c: New file, extracted from tests/test-sethostname.c. * tests/test-sethostname2.c: New file, extracted from tests/test-sethostname.c. * tests/test-sethostname.c: Remove file. * modules/sethostname-tests (Files): Add tests/test-sethostname1.c, tests/test-sethostname2.c. Remove tests/test-sethostname.c. (Depends-on): Add gethostname. (Makefile.am): Compile both test-sethostname1 and test-sethostname2. Link the latter with $(GETHOSTNAME_LIB). diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2011-12-03 Bruno Haible + sethostname tests: Fix link error on mingw. + * tests/test-sethostname1.c: New file, extracted from + tests/test-sethostname.c. + * tests/test-sethostname2.c: New file, extracted from + tests/test-sethostname.c. + * tests/test-sethostname.c: Remove file. + * modules/sethostname-tests (Files): Add tests/test-sethostname1.c, + tests/test-sethostname2.c. Remove tests/test-sethostname.c. + (Depends-on): Add gethostname. + (Makefile.am): Compile both test-sethostname1 and test-sethostname2. + Link the latter with $(GETHOSTNAME_LIB). + sethostname tests: Fix compilation error on mingw. * tests/test-sethostname.c: Don't include . (geteuid): Use a dummy value without uid_t. diff --git a/modules/sethostname-tests b/modules/sethostname-tests --- a/modules/sethostname-tests +++ b/modules/sethostname-tests @@ -1,13 +1,16 @@ Files: -tests/test-sethostname.c +tests/test-sethostname1.c +tests/test-sethostname2.c tests/signature.h tests/macros.h Depends-on: +gethostname configure.ac: AC_CHECK_FUNCS_ONCE([geteuid]) Makefile.am: -TESTS += test-sethostname -check_PROGRAMS += test-sethostname +TESTS += test-sethostname1 test-sethostname2 +check_PROGRAMS += test-sethostname1 test-sethostname2 +test_sethostname2_LDADD = $(LDADD) @GETHOSTNAME_LIB@ diff --git a/tests/test-sethostname.c b/tests/test-sethostname.c deleted file mode 100644 --- a/tests/test-sethostname.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (C) 2011 Free Software Foundation, Inc. - * Written by Ben Walton. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . */ - -#include - -#include - -#include "signature.h" -SIGNATURE_CHECK (sethostname, int, (const char *, size_t)); - -/* for HOST_NAME_MAX */ -#include -/* for strlen */ -#include - -#include -#include - -#include "macros.h" - -#define TESTHOSTNAME "gnulib-hostname" - -/* mingw and MSVC 9 lack geteuid, so setup a dummy value. */ -#if !HAVE_GETEUID -# define geteuid() 0 -#endif - -int -main (int argc, char *argv[] _GL_UNUSED) -{ - char origname[HOST_NAME_MAX]; - char newname[HOST_NAME_MAX]; - char longname[HOST_NAME_MAX + 2]; - int rcs, i; - - /* skip the tests if we don't have root privilege. this does not - consider things like CAP_SYS_ADMIN (linux) or PRIV_SYS_ADMIN - (solaris), etc. systems without a working geteuid (mingw, MSVC - 9) will always skip this test. */ - if (geteuid () != 0) - { - fprintf (stderr, "Skipping test: insufficient permissions.\n"); - return 77; - } - - /* we want to ensure we can do a get/set/get check to ensure the - change is accepted. record the current name so it can be restored - later */ - ASSERT (gethostname (origname, sizeof (origname)) == 0); - - /* try setting a valid hostname. if it fails -1/ENOSYS, we will - skip the test for long names as this is an indication we're using - the stub function that doesn't do anything on this platform. */ - rcs = sethostname (TESTHOSTNAME, strlen (TESTHOSTNAME)); - - if (rcs != 0) - { - if (rcs == -1 && errno == ENOSYS) - { - fprintf (stderr, - "Skipping test: sethostname is not really implemented.\n"); - return 77; - } - else - { - fprintf (stderr, "error setting valid hostname.\n"); - return 1; - } - } - else - { - ASSERT (gethostname (newname, sizeof (newname)) == 0); - - /* if we don't get back what we put in, there is no need to - restore the original name as we will assume it was not - properly changed. */ - if (strcmp (newname, TESTHOSTNAME) != 0) - { - fprintf (stderr, "set/get comparison failed.\n"); - return 1; - } - } - - /* glibc does allow setting a zero length name, so the lower bound - needs no test. validate that we are constrained by - HOST_NAME_MAX */ - for (i = 0; i < (HOST_NAME_MAX + 1); i++) - longname[i] = 'a'; - - longname[i] = '\0'; - - rcs = sethostname (longname, (HOST_NAME_MAX + 1)); - - if (rcs != -1) - { - /* attempt to restore the original name. */ - ASSERT (sethostname (origname, strlen (origname)) == 0); - fprintf (stderr, "setting a too long hostname succeeded.\n"); - return 1; - } - - /* restore the original name. */ - ASSERT (sethostname (origname, strlen (origname)) == 0); - - return 0; -} diff --git a/tests/test-sethostname1.c b/tests/test-sethostname1.c new file mode 100644 --- /dev/null +++ b/tests/test-sethostname1.c @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2011 Free Software Foundation, Inc. + * Written by Ben Walton. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +#include + +#include + +#include "signature.h" +SIGNATURE_CHECK (sethostname, int, (const char *, size_t)); + +int do_dangerous_things; + +int +main () +{ + /* Some code that has a link-time dependency to the sethostname() function + and that is likely not optimized away by compilers. */ + if (do_dangerous_things) + /* Never executed. */ + sethostname ("oprah", 5); + + return 0; +} diff --git a/tests/test-sethostname2.c b/tests/test-sethostname2.c new file mode 100644 --- /dev/null +++ b/tests/test-sethostname2.c @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2011 Free Software Foundation, Inc. + * Written by Ben Walton. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +#include + +#include + +/* for HOST_NAME_MAX */ +#include +/* for strlen */ +#include + +#include +#include + +#include "macros.h" + +#define TESTHOSTNAME "gnulib-hostname" + +/* mingw and MSVC 9 lack geteuid, so setup a dummy value. */ +#if !HAVE_GETEUID +# define geteuid() 0 +#endif + +int +main (int argc, char *argv[] _GL_UNUSED) +{ + char origname[HOST_NAME_MAX]; + char newname[HOST_NAME_MAX]; + char longname[HOST_NAME_MAX + 2]; + int rcs, i; + + /* skip the tests if we don't have root privilege. this does not + consider things like CAP_SYS_ADMIN (linux) or PRIV_SYS_ADMIN + (solaris), etc. systems without a working geteuid (mingw, MSVC + 9) will always skip this test. */ + if (geteuid () != 0) + { + fprintf (stderr, "Skipping test: insufficient permissions.\n"); + return 77; + } + + /* we want to ensure we can do a get/set/get check to ensure the + change is accepted. record the current name so it can be restored + later */ + ASSERT (gethostname (origname, sizeof (origname)) == 0); + + /* try setting a valid hostname. if it fails -1/ENOSYS, we will + skip the test for long names as this is an indication we're using + the stub function that doesn't do anything on this platform. */ + rcs = sethostname (TESTHOSTNAME, strlen (TESTHOSTNAME)); + + if (rcs != 0) + { + if (rcs == -1 && errno == ENOSYS) + { + fprintf (stderr, + "Skipping test: sethostname is not really implemented.\n"); + return 77; + } + else + { + fprintf (stderr, "error setting valid hostname.\n"); + return 1; + } + } + else + { + ASSERT (gethostname (newname, sizeof (newname)) == 0); + + /* if we don't get back what we put in, there is no need to + restore the original name as we will assume it was not + properly changed. */ + if (strcmp (newname, TESTHOSTNAME) != 0) + { + fprintf (stderr, "set/get comparison failed.\n"); + return 1; + } + } + + /* glibc does allow setting a zero length name, so the lower bound + needs no test. validate that we are constrained by + HOST_NAME_MAX */ + for (i = 0; i < (HOST_NAME_MAX + 1); i++) + longname[i] = 'a'; + + longname[i] = '\0'; + + rcs = sethostname (longname, (HOST_NAME_MAX + 1)); + + if (rcs != -1) + { + /* attempt to restore the original name. */ + ASSERT (sethostname (origname, strlen (origname)) == 0); + fprintf (stderr, "setting a too long hostname succeeded.\n"); + return 1; + } + + /* restore the original name. */ + ASSERT (sethostname (origname, strlen (origname)) == 0); + + return 0; +}