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