1 /*	$NetBSD: t_getsockname.c,v 1.1 2016/07/30 11:03:54 njoly Exp $	*/
2 /*
3  * Copyright (c) 2016 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
16  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include <atf-c.h>
36 
37 ATF_TC(getsockname_unix);
38 
39 ATF_TC_HEAD(getsockname_unix, tc)
40 {
41 	atf_tc_set_md_var(tc, "descr", "Checks getsockname with UNIX domain");
42 }
43 
44 ATF_TC_BODY(getsockname_unix, tc)
45 {
46 	const char *path = "sock.unix";
47 	int sd;
48 	socklen_t len;
49 	struct sockaddr_un sun;
50 
51 	sd = socket(AF_UNIX, SOCK_STREAM, 0);
52 	ATF_REQUIRE(sd != -1);
53 
54 	len = sizeof(sun);
55 	memset(&sun, 0, sizeof(sun));
56 	ATF_REQUIRE(getsockname(sd, (struct sockaddr *)&sun, &len) != -1);
57 	ATF_CHECK(sun.sun_family == AF_UNIX);
58 	ATF_CHECK(strcmp(sun.sun_path, "") == 0);
59 
60 	len = sizeof(sun);
61 	memset(&sun, 0, sizeof(sun));
62 	sun.sun_family = AF_UNIX;
63 	strcpy(sun.sun_path, path);
64 	ATF_REQUIRE(bind(sd, (struct sockaddr *)&sun, len) != -1);
65 
66 	len = sizeof(sun);
67 	memset(&sun, 0, sizeof(sun));
68 	ATF_REQUIRE(getsockname(sd, (struct sockaddr *)&sun, &len) != -1);
69 	ATF_CHECK(sun.sun_family == AF_UNIX);
70 	ATF_CHECK(strcmp(sun.sun_path, path) == 0);
71 
72 	ATF_REQUIRE(close(sd) != -1);
73 	ATF_REQUIRE(unlink(path) != -1);
74 }
75 
76 ATF_TP_ADD_TCS(tp)
77 {
78 
79 	ATF_TP_ADD_TC(tp, getsockname_unix);
80 
81 	return atf_no_error();
82 }
83