xref: /freebsd/tests/sys/netgraph/socket.c (revision 367e8adb4be91ca3c0d40c085eaf47467d0ad25d)
1*367e8adbSGleb Smirnoff /*-
2*367e8adbSGleb Smirnoff  * SPDX-License-Identifier: BSD-2-Clause
3*367e8adbSGleb Smirnoff  *
4*367e8adbSGleb Smirnoff  * Copyright (c) 2023 Gleb Smirnoff <glebius@FreeBSD.org>
5*367e8adbSGleb Smirnoff  *
6*367e8adbSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
7*367e8adbSGleb Smirnoff  * modification, are permitted provided that the following conditions
8*367e8adbSGleb Smirnoff  * are met:
9*367e8adbSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
10*367e8adbSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
11*367e8adbSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
12*367e8adbSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
13*367e8adbSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
14*367e8adbSGleb Smirnoff  *
15*367e8adbSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*367e8adbSGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*367e8adbSGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*367e8adbSGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*367e8adbSGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*367e8adbSGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*367e8adbSGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*367e8adbSGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*367e8adbSGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*367e8adbSGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*367e8adbSGleb Smirnoff  * SUCH DAMAGE.
26*367e8adbSGleb Smirnoff  */
27*367e8adbSGleb Smirnoff 
28*367e8adbSGleb Smirnoff #include <sys/socket.h>
29*367e8adbSGleb Smirnoff #include <netgraph.h>
30*367e8adbSGleb Smirnoff #include <netgraph/ng_socket.h>
31*367e8adbSGleb Smirnoff 
32*367e8adbSGleb Smirnoff #include <errno.h>
33*367e8adbSGleb Smirnoff 
34*367e8adbSGleb Smirnoff #include <atf-c.h>
35*367e8adbSGleb Smirnoff 
36*367e8adbSGleb Smirnoff ATF_TC_WITHOUT_HEAD(getsockname);
37*367e8adbSGleb Smirnoff ATF_TC_BODY(getsockname, tc)
38*367e8adbSGleb Smirnoff {
39*367e8adbSGleb Smirnoff 	struct sockaddr_ng sg;
40*367e8adbSGleb Smirnoff 	socklen_t len = sizeof(struct sockaddr_ng);
41*367e8adbSGleb Smirnoff #define	NAME	"0123456789012345678901234567891"	/* 31 chars */
42*367e8adbSGleb Smirnoff 	char name[NG_NODESIZ] = NAME;
43*367e8adbSGleb Smirnoff 	int cs;
44*367e8adbSGleb Smirnoff 
45*367e8adbSGleb Smirnoff #if 0
46*367e8adbSGleb Smirnoff 	/* Unnamed node. */
47*367e8adbSGleb Smirnoff 	ATF_REQUIRE(NgMkSockNode(NULL, &cs, NULL) == 0);
48*367e8adbSGleb Smirnoff 	ATF_REQUIRE(getsockname(cs, (struct sockaddr *)&sg, &len) == 0);
49*367e8adbSGleb Smirnoff 	close(cs);
50*367e8adbSGleb Smirnoff 	/* Unnamed node doesn't return any name/ID now. */
51*367e8adbSGleb Smirnoff #endif
52*367e8adbSGleb Smirnoff 
53*367e8adbSGleb Smirnoff 	/* Named node. */
54*367e8adbSGleb Smirnoff 	ATF_REQUIRE(NgMkSockNode(name, &cs, NULL) == 0);
55*367e8adbSGleb Smirnoff 	ATF_REQUIRE(getsockname(cs, (struct sockaddr *)&sg, &len) == 0);
56*367e8adbSGleb Smirnoff #if 0
57*367e8adbSGleb Smirnoff 	/* sockaddr_ng truncates name now. */
58*367e8adbSGleb Smirnoff 	ATF_REQUIRE(strcmp(sg.sg_data, NAME) == 0);
59*367e8adbSGleb Smirnoff #else
60*367e8adbSGleb Smirnoff 	ATF_REQUIRE(strncmp(sg.sg_data, NAME, sizeof(sg.sg_data)) == 0);
61*367e8adbSGleb Smirnoff #endif
62*367e8adbSGleb Smirnoff }
63*367e8adbSGleb Smirnoff 
64*367e8adbSGleb Smirnoff ATF_TP_ADD_TCS(tp)
65*367e8adbSGleb Smirnoff {
66*367e8adbSGleb Smirnoff 	ATF_TP_ADD_TC(tp, getsockname);
67*367e8adbSGleb Smirnoff 
68*367e8adbSGleb Smirnoff 	return (atf_no_error());
69*367e8adbSGleb Smirnoff }
70