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