1*1eaf0ac3Slogwang /*
2*1eaf0ac3Slogwang  * Copyright (C) 2005 Brooks Davis. All rights reserved.
3*1eaf0ac3Slogwang  *
4*1eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
5*1eaf0ac3Slogwang  * modification, are permitted provided that the following conditions
6*1eaf0ac3Slogwang  * are met:
7*1eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
8*1eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer.
9*1eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
10*1eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
11*1eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
12*1eaf0ac3Slogwang  *
13*1eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*1eaf0ac3Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*1eaf0ac3Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*1eaf0ac3Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17*1eaf0ac3Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*1eaf0ac3Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*1eaf0ac3Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*1eaf0ac3Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*1eaf0ac3Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*1eaf0ac3Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*1eaf0ac3Slogwang  * SUCH DAMAGE.
24*1eaf0ac3Slogwang  */
25*1eaf0ac3Slogwang 
26*1eaf0ac3Slogwang #include <sys/cdefs.h>
27*1eaf0ac3Slogwang __FBSDID("$FreeBSD$");
28*1eaf0ac3Slogwang 
29*1eaf0ac3Slogwang #include <sys/types.h>
30*1eaf0ac3Slogwang #include <sys/param.h>
31*1eaf0ac3Slogwang #include <errno.h>
32*1eaf0ac3Slogwang #include <libutil.h>
33*1eaf0ac3Slogwang #include <stdio.h>
34*1eaf0ac3Slogwang #include <stdlib.h>
35*1eaf0ac3Slogwang #include <string.h>
36*1eaf0ac3Slogwang #include <unistd.h>
37*1eaf0ac3Slogwang 
38*1eaf0ac3Slogwang #define TESTDOMAIN ".domain.example.com"
39*1eaf0ac3Slogwang #define TESTHOST "testhost"
40*1eaf0ac3Slogwang #define TESTFQDN "testhost" TESTDOMAIN
41*1eaf0ac3Slogwang 
42*1eaf0ac3Slogwang int failures = 0;
43*1eaf0ac3Slogwang int tests = 0;
44*1eaf0ac3Slogwang 
45*1eaf0ac3Slogwang /*
46*1eaf0ac3Slogwang  * Evily override gethostname(3) so trimdomain always gets the same result.
47*1eaf0ac3Slogwang  * This makes the tests much easier to write and less likely to fail on
48*1eaf0ac3Slogwang  * oddly configured systems.
49*1eaf0ac3Slogwang  */
50*1eaf0ac3Slogwang int
gethostname(char * name,size_t namelen)51*1eaf0ac3Slogwang gethostname(char *name, size_t namelen)
52*1eaf0ac3Slogwang {
53*1eaf0ac3Slogwang 	if (strlcpy(name, TESTFQDN, namelen) > namelen) {
54*1eaf0ac3Slogwang 		errno = ENAMETOOLONG;
55*1eaf0ac3Slogwang 		return (-1);
56*1eaf0ac3Slogwang 	}
57*1eaf0ac3Slogwang 	return (0);
58*1eaf0ac3Slogwang }
59*1eaf0ac3Slogwang 
60*1eaf0ac3Slogwang void
testit(const char * input,int hostsize,const char * output,const char * test)61*1eaf0ac3Slogwang testit(const char *input, int hostsize, const char *output, const char *test)
62*1eaf0ac3Slogwang {
63*1eaf0ac3Slogwang 	char *testhost;
64*1eaf0ac3Slogwang 	const char *expected = (output == NULL) ? input : output;
65*1eaf0ac3Slogwang 
66*1eaf0ac3Slogwang 	testhost = strdup(input);
67*1eaf0ac3Slogwang 	trimdomain(testhost, hostsize < 0 ? (int)strlen(testhost) : hostsize);
68*1eaf0ac3Slogwang 	tests++;
69*1eaf0ac3Slogwang 	if (strcmp(testhost, expected) != 0) {
70*1eaf0ac3Slogwang 		printf("not ok %d - %s\n", tests, test);
71*1eaf0ac3Slogwang 		printf("# %s -> %s (expected %s)\n", input, testhost, expected);
72*1eaf0ac3Slogwang 	} else
73*1eaf0ac3Slogwang 		printf("ok %d - %s\n", tests, test);
74*1eaf0ac3Slogwang 	free(testhost);
75*1eaf0ac3Slogwang 	return;
76*1eaf0ac3Slogwang }
77*1eaf0ac3Slogwang 
78*1eaf0ac3Slogwang int
main(void)79*1eaf0ac3Slogwang main(void)
80*1eaf0ac3Slogwang {
81*1eaf0ac3Slogwang 
82*1eaf0ac3Slogwang 	printf("1..5\n");
83*1eaf0ac3Slogwang 
84*1eaf0ac3Slogwang 	testit(TESTFQDN, -1, TESTHOST, "self");
85*1eaf0ac3Slogwang 	testit("XXX" TESTDOMAIN, -1, "XXX", "different host, same domain");
86*1eaf0ac3Slogwang 	testit("XXX" TESTDOMAIN, 1, NULL, "short hostsize");
87*1eaf0ac3Slogwang 	testit("bogus.example.net", -1, NULL, "arbitrary host");
88*1eaf0ac3Slogwang 	testit("XXX." TESTFQDN, -1, NULL, "domain is local hostname");
89*1eaf0ac3Slogwang 
90*1eaf0ac3Slogwang 	return (0);
91*1eaf0ac3Slogwang }
92