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