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