1367e8adbSGleb Smirnoff /*-
2367e8adbSGleb Smirnoff * SPDX-License-Identifier: BSD-2-Clause
3367e8adbSGleb Smirnoff *
4367e8adbSGleb Smirnoff * Copyright (c) 2023 Gleb Smirnoff <glebius@FreeBSD.org>
5367e8adbSGleb Smirnoff *
6367e8adbSGleb Smirnoff * Redistribution and use in source and binary forms, with or without
7367e8adbSGleb Smirnoff * modification, are permitted provided that the following conditions
8367e8adbSGleb Smirnoff * are met:
9367e8adbSGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
10367e8adbSGleb Smirnoff * notice, this list of conditions and the following disclaimer.
11367e8adbSGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
12367e8adbSGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
13367e8adbSGleb Smirnoff * documentation and/or other materials provided with the distribution.
14367e8adbSGleb Smirnoff *
15367e8adbSGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16367e8adbSGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17367e8adbSGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18367e8adbSGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19367e8adbSGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20367e8adbSGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21367e8adbSGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22367e8adbSGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23367e8adbSGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24367e8adbSGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25367e8adbSGleb Smirnoff * SUCH DAMAGE.
26367e8adbSGleb Smirnoff */
27367e8adbSGleb Smirnoff
28367e8adbSGleb Smirnoff #include <sys/socket.h>
29367e8adbSGleb Smirnoff #include <netgraph.h>
30367e8adbSGleb Smirnoff #include <netgraph/ng_socket.h>
31367e8adbSGleb Smirnoff
32367e8adbSGleb Smirnoff #include <errno.h>
33367e8adbSGleb Smirnoff
34367e8adbSGleb Smirnoff #include <atf-c.h>
35367e8adbSGleb Smirnoff
36367e8adbSGleb Smirnoff ATF_TC_WITHOUT_HEAD(getsockname);
ATF_TC_BODY(getsockname,tc)37367e8adbSGleb Smirnoff ATF_TC_BODY(getsockname, tc)
38367e8adbSGleb Smirnoff {
39367e8adbSGleb Smirnoff struct sockaddr_ng sg;
40367e8adbSGleb Smirnoff socklen_t len = sizeof(struct sockaddr_ng);
41367e8adbSGleb Smirnoff #define NAME "0123456789012345678901234567891" /* 31 chars */
42367e8adbSGleb Smirnoff char name[NG_NODESIZ] = NAME;
43367e8adbSGleb Smirnoff int cs;
44367e8adbSGleb Smirnoff
45*d2de66a9SGleb Smirnoff /* Unnamed node returns its ID as name. */
46367e8adbSGleb Smirnoff ATF_REQUIRE(NgMkSockNode(NULL, &cs, NULL) == 0);
47367e8adbSGleb Smirnoff ATF_REQUIRE(getsockname(cs, (struct sockaddr *)&sg, &len) == 0);
48*d2de66a9SGleb Smirnoff ATF_REQUIRE(strspn(sg.sg_data, "[0123456789abcdef]") >= 3 &&
49*d2de66a9SGleb Smirnoff sg.sg_data[strspn(sg.sg_data, "[0123456789abcdef]")] == '\0');
50367e8adbSGleb Smirnoff close(cs);
51367e8adbSGleb Smirnoff
52367e8adbSGleb Smirnoff /* Named node. */
53367e8adbSGleb Smirnoff ATF_REQUIRE(NgMkSockNode(name, &cs, NULL) == 0);
54367e8adbSGleb Smirnoff ATF_REQUIRE(getsockname(cs, (struct sockaddr *)&sg, &len) == 0);
55367e8adbSGleb Smirnoff ATF_REQUIRE(strcmp(sg.sg_data, NAME) == 0);
56367e8adbSGleb Smirnoff }
57367e8adbSGleb Smirnoff
ATF_TP_ADD_TCS(tp)58367e8adbSGleb Smirnoff ATF_TP_ADD_TCS(tp)
59367e8adbSGleb Smirnoff {
60367e8adbSGleb Smirnoff ATF_TP_ADD_TC(tp, getsockname);
61367e8adbSGleb Smirnoff
62367e8adbSGleb Smirnoff return (atf_no_error());
63367e8adbSGleb Smirnoff }
64