xref: /freebsd/tests/sys/kern/unix_stream.c (revision 6655bec4e268eeb779f5e57e5914761cf86b5f8a)
1*6655bec4SGleb Smirnoff /*-
2*6655bec4SGleb Smirnoff  * SPDX-License-Identifier: BSD-2-Clause
3*6655bec4SGleb Smirnoff  *
4*6655bec4SGleb Smirnoff  * Copyright (c) 2018 Alan Somers
5*6655bec4SGleb Smirnoff  *
6*6655bec4SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
7*6655bec4SGleb Smirnoff  * modification, are permitted provided that the following conditions
8*6655bec4SGleb Smirnoff  * are met:
9*6655bec4SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
10*6655bec4SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
11*6655bec4SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
12*6655bec4SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
13*6655bec4SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
14*6655bec4SGleb Smirnoff  *
15*6655bec4SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*6655bec4SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*6655bec4SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*6655bec4SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19*6655bec4SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*6655bec4SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*6655bec4SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*6655bec4SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*6655bec4SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*6655bec4SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*6655bec4SGleb Smirnoff  * SUCH DAMAGE.
26*6655bec4SGleb Smirnoff  */
27*6655bec4SGleb Smirnoff 
28*6655bec4SGleb Smirnoff #include <sys/cdefs.h>
29*6655bec4SGleb Smirnoff #include <errno.h>
30*6655bec4SGleb Smirnoff #include <fcntl.h>
31*6655bec4SGleb Smirnoff #include <pthread.h>
32*6655bec4SGleb Smirnoff #include <signal.h>
33*6655bec4SGleb Smirnoff #include <sys/socket.h>
34*6655bec4SGleb Smirnoff #include <sys/un.h>
35*6655bec4SGleb Smirnoff 
36*6655bec4SGleb Smirnoff #include <stdio.h>
37*6655bec4SGleb Smirnoff 
38*6655bec4SGleb Smirnoff #include <atf-c.h>
39*6655bec4SGleb Smirnoff 
40*6655bec4SGleb Smirnoff static void
41*6655bec4SGleb Smirnoff do_socketpair(int *sv)
42*6655bec4SGleb Smirnoff {
43*6655bec4SGleb Smirnoff 	int s;
44*6655bec4SGleb Smirnoff 
45*6655bec4SGleb Smirnoff 	s = socketpair(PF_LOCAL, SOCK_STREAM, 0, sv);
46*6655bec4SGleb Smirnoff 	ATF_REQUIRE_EQ(0, s);
47*6655bec4SGleb Smirnoff 	ATF_REQUIRE(sv[0] >= 0);
48*6655bec4SGleb Smirnoff 	ATF_REQUIRE(sv[1] >= 0);
49*6655bec4SGleb Smirnoff 	ATF_REQUIRE(sv[0] != sv[1]);
50*6655bec4SGleb Smirnoff }
51*6655bec4SGleb Smirnoff 
52*6655bec4SGleb Smirnoff /* getpeereid(3) should work with stream sockets created via socketpair(2) */
53*6655bec4SGleb Smirnoff ATF_TC_WITHOUT_HEAD(getpeereid);
54*6655bec4SGleb Smirnoff ATF_TC_BODY(getpeereid, tc)
55*6655bec4SGleb Smirnoff {
56*6655bec4SGleb Smirnoff 	int sv[2];
57*6655bec4SGleb Smirnoff 	uid_t real_euid, euid;
58*6655bec4SGleb Smirnoff 	gid_t real_egid, egid;
59*6655bec4SGleb Smirnoff 
60*6655bec4SGleb Smirnoff 	real_euid = geteuid();
61*6655bec4SGleb Smirnoff 	real_egid = getegid();
62*6655bec4SGleb Smirnoff 
63*6655bec4SGleb Smirnoff 	do_socketpair(sv);
64*6655bec4SGleb Smirnoff 
65*6655bec4SGleb Smirnoff 	ATF_REQUIRE_EQ(0, getpeereid(sv[0], &euid, &egid));
66*6655bec4SGleb Smirnoff 	ATF_CHECK_EQ(real_euid, euid);
67*6655bec4SGleb Smirnoff 	ATF_CHECK_EQ(real_egid, egid);
68*6655bec4SGleb Smirnoff 
69*6655bec4SGleb Smirnoff 	ATF_REQUIRE_EQ(0, getpeereid(sv[1], &euid, &egid));
70*6655bec4SGleb Smirnoff 	ATF_CHECK_EQ(real_euid, euid);
71*6655bec4SGleb Smirnoff 	ATF_CHECK_EQ(real_egid, egid);
72*6655bec4SGleb Smirnoff 
73*6655bec4SGleb Smirnoff 	close(sv[0]);
74*6655bec4SGleb Smirnoff 	close(sv[1]);
75*6655bec4SGleb Smirnoff }
76*6655bec4SGleb Smirnoff 
77*6655bec4SGleb Smirnoff /* Sending zero bytes should succeed (once regressed in aba79b0f4a3f). */
78*6655bec4SGleb Smirnoff ATF_TC_WITHOUT_HEAD(send_0);
79*6655bec4SGleb Smirnoff ATF_TC_BODY(send_0, tc)
80*6655bec4SGleb Smirnoff {
81*6655bec4SGleb Smirnoff 	int sv[2];
82*6655bec4SGleb Smirnoff 
83*6655bec4SGleb Smirnoff 	do_socketpair(sv);
84*6655bec4SGleb Smirnoff 	ATF_REQUIRE(send(sv[0], sv, 0, 0) == 0);
85*6655bec4SGleb Smirnoff 	close(sv[0]);
86*6655bec4SGleb Smirnoff 	close(sv[1]);
87*6655bec4SGleb Smirnoff }
88*6655bec4SGleb Smirnoff 
89*6655bec4SGleb Smirnoff 
90*6655bec4SGleb Smirnoff ATF_TP_ADD_TCS(tp)
91*6655bec4SGleb Smirnoff {
92*6655bec4SGleb Smirnoff 	ATF_TP_ADD_TC(tp, getpeereid);
93*6655bec4SGleb Smirnoff 	ATF_TP_ADD_TC(tp, send_0);
94*6655bec4SGleb Smirnoff 
95*6655bec4SGleb Smirnoff 	return atf_no_error();
96*6655bec4SGleb Smirnoff }
97