xref: /freebsd/lib/libutil/tests/trimdomain_test.c (revision cf8e5289a110954600f135024d1515a77d0ae34d)
15aa45fcbSEnji Cooper /*
25aa45fcbSEnji Cooper  * Copyright (C) 2005 Brooks Davis. All rights reserved.
35aa45fcbSEnji Cooper  *
45aa45fcbSEnji Cooper  * Redistribution and use in source and binary forms, with or without
55aa45fcbSEnji Cooper  * modification, are permitted provided that the following conditions
65aa45fcbSEnji Cooper  * are met:
75aa45fcbSEnji Cooper  * 1. Redistributions of source code must retain the above copyright
85aa45fcbSEnji Cooper  *    notice, this list of conditions and the following disclaimer.
95aa45fcbSEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
105aa45fcbSEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
115aa45fcbSEnji Cooper  *    documentation and/or other materials provided with the distribution.
125aa45fcbSEnji Cooper  *
135aa45fcbSEnji Cooper  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
145aa45fcbSEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
155aa45fcbSEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
165aa45fcbSEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
175aa45fcbSEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
185aa45fcbSEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
195aa45fcbSEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
205aa45fcbSEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
215aa45fcbSEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
225aa45fcbSEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
235aa45fcbSEnji Cooper  * SUCH DAMAGE.
245aa45fcbSEnji Cooper  */
255aa45fcbSEnji Cooper 
265aa45fcbSEnji Cooper #include <sys/types.h>
275aa45fcbSEnji Cooper #include <sys/param.h>
285aa45fcbSEnji Cooper #include <errno.h>
295aa45fcbSEnji Cooper #include <libutil.h>
305aa45fcbSEnji Cooper #include <stdio.h>
315aa45fcbSEnji Cooper #include <stdlib.h>
325aa45fcbSEnji Cooper #include <string.h>
335aa45fcbSEnji Cooper #include <unistd.h>
345aa45fcbSEnji Cooper 
35*cf8e5289SKyle Evans #include <ssp/ssp.h>
36*cf8e5289SKyle Evans 
375aa45fcbSEnji Cooper #define TESTDOMAIN ".domain.example.com"
385aa45fcbSEnji Cooper #define TESTHOST "testhost"
395aa45fcbSEnji Cooper #define TESTFQDN "testhost" TESTDOMAIN
405aa45fcbSEnji Cooper 
415aa45fcbSEnji Cooper int failures = 0;
425aa45fcbSEnji Cooper int tests = 0;
435aa45fcbSEnji Cooper 
445aa45fcbSEnji Cooper /*
455aa45fcbSEnji Cooper  * Evily override gethostname(3) so trimdomain always gets the same result.
465aa45fcbSEnji Cooper  * This makes the tests much easier to write and less likely to fail on
475aa45fcbSEnji Cooper  * oddly configured systems.
485aa45fcbSEnji Cooper  */
495aa45fcbSEnji Cooper int
__ssp_real(gethostname)50*cf8e5289SKyle Evans __ssp_real(gethostname)(char *name, size_t namelen)
515aa45fcbSEnji Cooper {
525aa45fcbSEnji Cooper 	if (strlcpy(name, TESTFQDN, namelen) > namelen) {
535aa45fcbSEnji Cooper 		errno = ENAMETOOLONG;
545aa45fcbSEnji Cooper 		return (-1);
555aa45fcbSEnji Cooper 	}
565aa45fcbSEnji Cooper 	return (0);
575aa45fcbSEnji Cooper }
585aa45fcbSEnji Cooper 
595aa45fcbSEnji Cooper void
testit(const char * input,int hostsize,const char * output,const char * test)605aa45fcbSEnji Cooper testit(const char *input, int hostsize, const char *output, const char *test)
615aa45fcbSEnji Cooper {
625aa45fcbSEnji Cooper 	char *testhost;
635aa45fcbSEnji Cooper 	const char *expected = (output == NULL) ? input : output;
645aa45fcbSEnji Cooper 
655aa45fcbSEnji Cooper 	testhost = strdup(input);
665aa45fcbSEnji Cooper 	trimdomain(testhost, hostsize < 0 ? (int)strlen(testhost) : hostsize);
675aa45fcbSEnji Cooper 	tests++;
685aa45fcbSEnji Cooper 	if (strcmp(testhost, expected) != 0) {
695aa45fcbSEnji Cooper 		printf("not ok %d - %s\n", tests, test);
705aa45fcbSEnji Cooper 		printf("# %s -> %s (expected %s)\n", input, testhost, expected);
715aa45fcbSEnji Cooper 	} else
725aa45fcbSEnji Cooper 		printf("ok %d - %s\n", tests, test);
735aa45fcbSEnji Cooper 	free(testhost);
745aa45fcbSEnji Cooper 	return;
755aa45fcbSEnji Cooper }
765aa45fcbSEnji Cooper 
775aa45fcbSEnji Cooper int
main(void)785aa45fcbSEnji Cooper main(void)
795aa45fcbSEnji Cooper {
805aa45fcbSEnji Cooper 
815aa45fcbSEnji Cooper 	printf("1..5\n");
825aa45fcbSEnji Cooper 
835aa45fcbSEnji Cooper 	testit(TESTFQDN, -1, TESTHOST, "self");
845aa45fcbSEnji Cooper 	testit("XXX" TESTDOMAIN, -1, "XXX", "different host, same domain");
855aa45fcbSEnji Cooper 	testit("XXX" TESTDOMAIN, 1, NULL, "short hostsize");
865aa45fcbSEnji Cooper 	testit("bogus.example.net", -1, NULL, "arbitrary host");
875aa45fcbSEnji Cooper 	testit("XXX." TESTFQDN, -1, NULL, "domain is local hostname");
885aa45fcbSEnji Cooper 
895aa45fcbSEnji Cooper 	return (0);
905aa45fcbSEnji Cooper }
91