xref: /xnu-8792.81.2/tests/so_linger.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions /*
2*19c3b8c2SApple OSS Distributions  * Copyright (c) 2021 Apple Inc. All rights reserved.
3*19c3b8c2SApple OSS Distributions  *
4*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*19c3b8c2SApple OSS Distributions  *
6*19c3b8c2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*19c3b8c2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*19c3b8c2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*19c3b8c2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*19c3b8c2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*19c3b8c2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*19c3b8c2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*19c3b8c2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*19c3b8c2SApple OSS Distributions  *
15*19c3b8c2SApple OSS Distributions  * Please obtain a copy of the License at
16*19c3b8c2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*19c3b8c2SApple OSS Distributions  *
18*19c3b8c2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*19c3b8c2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*19c3b8c2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*19c3b8c2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*19c3b8c2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*19c3b8c2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*19c3b8c2SApple OSS Distributions  * limitations under the License.
25*19c3b8c2SApple OSS Distributions  *
26*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*19c3b8c2SApple OSS Distributions  */
28*19c3b8c2SApple OSS Distributions 
29*19c3b8c2SApple OSS Distributions #include <sys/socket.h>
30*19c3b8c2SApple OSS Distributions #include <sys/sysctl.h>
31*19c3b8c2SApple OSS Distributions #include <sys/time.h>
32*19c3b8c2SApple OSS Distributions 
33*19c3b8c2SApple OSS Distributions #include <darwintest.h>
34*19c3b8c2SApple OSS Distributions 
35*19c3b8c2SApple OSS Distributions T_DECL(so_linger_negative, "SO_LINGER negative")
36*19c3b8c2SApple OSS Distributions {
37*19c3b8c2SApple OSS Distributions 	int s = -1;
38*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)");
39*19c3b8c2SApple OSS Distributions 
40*19c3b8c2SApple OSS Distributions 	struct linger set_l = {};
41*19c3b8c2SApple OSS Distributions 	set_l.l_onoff = 1;
42*19c3b8c2SApple OSS Distributions 	set_l.l_linger = -1;
43*19c3b8c2SApple OSS Distributions 	T_LOG("l_linger %d", set_l.l_linger);
44*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER,
45*19c3b8c2SApple OSS Distributions 	    &set_l, sizeof(struct linger)),
46*19c3b8c2SApple OSS Distributions 	    "setsockopt SO_LINGER");
47*19c3b8c2SApple OSS Distributions 
48*19c3b8c2SApple OSS Distributions 	struct linger get_l = {};
49*19c3b8c2SApple OSS Distributions 	socklen_t len = sizeof(struct linger);
50*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER,
51*19c3b8c2SApple OSS Distributions 	    &get_l, &len),
52*19c3b8c2SApple OSS Distributions 	    "getsockopt SO_LINGER");
53*19c3b8c2SApple OSS Distributions 
54*19c3b8c2SApple OSS Distributions 	T_EXPECT_EQ(set_l.l_linger, get_l.l_linger,
55*19c3b8c2SApple OSS Distributions 	    "SO_LINGER negative l_linger %d == %d", set_l.l_linger, get_l.l_linger);
56*19c3b8c2SApple OSS Distributions }
57*19c3b8c2SApple OSS Distributions 
58*19c3b8c2SApple OSS Distributions T_DECL(so_linger_overflow, "SO_LINGER overflow")
59*19c3b8c2SApple OSS Distributions {
60*19c3b8c2SApple OSS Distributions 	int s = -1;
61*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)");
62*19c3b8c2SApple OSS Distributions 
63*19c3b8c2SApple OSS Distributions 	struct linger set_l = {};
64*19c3b8c2SApple OSS Distributions 	set_l.l_onoff = 1;
65*19c3b8c2SApple OSS Distributions 	set_l.l_linger = SHRT_MAX + 1;
66*19c3b8c2SApple OSS Distributions 	T_LOG("l_linger %d", set_l.l_linger);
67*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER,
68*19c3b8c2SApple OSS Distributions 	    &set_l, sizeof(struct linger)),
69*19c3b8c2SApple OSS Distributions 	    "setsockopt SO_LINGER");
70*19c3b8c2SApple OSS Distributions 
71*19c3b8c2SApple OSS Distributions 	struct linger get_l = {};
72*19c3b8c2SApple OSS Distributions 	socklen_t len = sizeof(struct linger);
73*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER,
74*19c3b8c2SApple OSS Distributions 	    &get_l, &len),
75*19c3b8c2SApple OSS Distributions 	    "getsockopt SO_LINGER");
76*19c3b8c2SApple OSS Distributions 
77*19c3b8c2SApple OSS Distributions 	/*
78*19c3b8c2SApple OSS Distributions 	 * Test passes based on the knowledge that l_linger is stored
79*19c3b8c2SApple OSS Distributions 	 * as a short signed integer
80*19c3b8c2SApple OSS Distributions 	 */
81*19c3b8c2SApple OSS Distributions 	T_EXPECT_EQ((short)set_l.l_linger, (short)get_l.l_linger,
82*19c3b8c2SApple OSS Distributions 	    "SO_LINGER overflow l_linger (short) %d == (short) %d",
83*19c3b8c2SApple OSS Distributions 	    set_l.l_linger, get_l.l_linger);
84*19c3b8c2SApple OSS Distributions }
85*19c3b8c2SApple OSS Distributions 
86*19c3b8c2SApple OSS Distributions T_DECL(so_linger_500, "SO_LINGER 500")
87*19c3b8c2SApple OSS Distributions {
88*19c3b8c2SApple OSS Distributions 	int s = -1;
89*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)");
90*19c3b8c2SApple OSS Distributions 
91*19c3b8c2SApple OSS Distributions 	struct clockinfo clkinfo;
92*19c3b8c2SApple OSS Distributions 	size_t oldlen = sizeof(struct clockinfo);
93*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.clockrate", &clkinfo, &oldlen, NULL, 0),
94*19c3b8c2SApple OSS Distributions 	    "sysctlbyname(kern.clockrate)");
95*19c3b8c2SApple OSS Distributions 
96*19c3b8c2SApple OSS Distributions 	struct linger set_l = {};
97*19c3b8c2SApple OSS Distributions 	set_l.l_onoff = 1;
98*19c3b8c2SApple OSS Distributions 	set_l.l_linger = 500;
99*19c3b8c2SApple OSS Distributions 
100*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER,
101*19c3b8c2SApple OSS Distributions 	    &set_l, sizeof(struct linger)),
102*19c3b8c2SApple OSS Distributions 	    "setsockopt SO_LINGER");
103*19c3b8c2SApple OSS Distributions 
104*19c3b8c2SApple OSS Distributions 	struct linger get_l = {};
105*19c3b8c2SApple OSS Distributions 	socklen_t len = sizeof(struct linger);
106*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER,
107*19c3b8c2SApple OSS Distributions 	    &get_l, &len),
108*19c3b8c2SApple OSS Distributions 	    "getsockopt SO_LINGER");
109*19c3b8c2SApple OSS Distributions 
110*19c3b8c2SApple OSS Distributions 	T_EXPECT_EQ(set_l.l_linger, get_l.l_linger,
111*19c3b8c2SApple OSS Distributions 	    "SO_LINGER 500 l_linger %d == %d", set_l.l_linger, get_l.l_linger);
112*19c3b8c2SApple OSS Distributions }
113*19c3b8c2SApple OSS Distributions 
114*19c3b8c2SApple OSS Distributions T_DECL(so_linger_sec_negative, "SO_LINGER_SEC negative")
115*19c3b8c2SApple OSS Distributions {
116*19c3b8c2SApple OSS Distributions 	int s = -1;
117*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)");
118*19c3b8c2SApple OSS Distributions 
119*19c3b8c2SApple OSS Distributions 	struct clockinfo clkinfo;
120*19c3b8c2SApple OSS Distributions 	size_t oldlen = sizeof(struct clockinfo);
121*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.clockrate", &clkinfo, &oldlen, NULL, 0),
122*19c3b8c2SApple OSS Distributions 	    "sysctlbyname(kern.clockrate)");
123*19c3b8c2SApple OSS Distributions 
124*19c3b8c2SApple OSS Distributions 	struct linger set_l = {};
125*19c3b8c2SApple OSS Distributions 	set_l.l_onoff = 1;
126*19c3b8c2SApple OSS Distributions 	set_l.l_linger = -1;
127*19c3b8c2SApple OSS Distributions 	T_LOG("l_linger %d * clkinfo.hz %d = %d", set_l.l_linger, clkinfo.hz, (set_l.l_linger * clkinfo.hz));
128*19c3b8c2SApple OSS Distributions 
129*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER_SEC,
130*19c3b8c2SApple OSS Distributions 	    &set_l, sizeof(struct linger)),
131*19c3b8c2SApple OSS Distributions 	    "setsockopt SO_LINGER_SEC");
132*19c3b8c2SApple OSS Distributions 
133*19c3b8c2SApple OSS Distributions 	struct linger get_l = {};
134*19c3b8c2SApple OSS Distributions 	socklen_t len = sizeof(struct linger);
135*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER_SEC,
136*19c3b8c2SApple OSS Distributions 	    &get_l, &len),
137*19c3b8c2SApple OSS Distributions 	    "getsockopt SO_LINGER_SEC");
138*19c3b8c2SApple OSS Distributions 
139*19c3b8c2SApple OSS Distributions 	T_EXPECT_EQ(set_l.l_linger, get_l.l_linger,
140*19c3b8c2SApple OSS Distributions 	    "SO_LINGER_SEC negative l_linger %d == %d", set_l.l_linger, get_l.l_linger);
141*19c3b8c2SApple OSS Distributions }
142*19c3b8c2SApple OSS Distributions 
143*19c3b8c2SApple OSS Distributions T_DECL(so_linger_sec_5_seconds, "SO_LINGER_SEC 5 seconds")
144*19c3b8c2SApple OSS Distributions {
145*19c3b8c2SApple OSS Distributions 	int s = -1;
146*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)");
147*19c3b8c2SApple OSS Distributions 
148*19c3b8c2SApple OSS Distributions 	struct clockinfo clkinfo;
149*19c3b8c2SApple OSS Distributions 	size_t oldlen = sizeof(struct clockinfo);
150*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.clockrate", &clkinfo, &oldlen, NULL, 0),
151*19c3b8c2SApple OSS Distributions 	    "sysctlbyname(kern.clockrate)");
152*19c3b8c2SApple OSS Distributions 
153*19c3b8c2SApple OSS Distributions 	struct linger set_l = {};
154*19c3b8c2SApple OSS Distributions 	set_l.l_onoff = 1;
155*19c3b8c2SApple OSS Distributions 	set_l.l_linger = 5;
156*19c3b8c2SApple OSS Distributions 	T_LOG("l_linger %d * clkinfo.hz %d = %d", set_l.l_linger, clkinfo.hz, (set_l.l_linger * clkinfo.hz));
157*19c3b8c2SApple OSS Distributions 
158*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER_SEC,
159*19c3b8c2SApple OSS Distributions 	    &set_l, sizeof(struct linger)),
160*19c3b8c2SApple OSS Distributions 	    "setsockopt SO_LINGER_SEC");
161*19c3b8c2SApple OSS Distributions 
162*19c3b8c2SApple OSS Distributions 	struct linger get_l = {};
163*19c3b8c2SApple OSS Distributions 	socklen_t len = sizeof(struct linger);
164*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER_SEC,
165*19c3b8c2SApple OSS Distributions 	    &get_l, &len),
166*19c3b8c2SApple OSS Distributions 	    "getsockopt SO_LINGER_SEC");
167*19c3b8c2SApple OSS Distributions 
168*19c3b8c2SApple OSS Distributions 	T_EXPECT_EQ(set_l.l_linger, get_l.l_linger,
169*19c3b8c2SApple OSS Distributions 	    "SO_LINGER_SEC 5 seconds l_linger %d == %d", set_l.l_linger, get_l.l_linger);
170*19c3b8c2SApple OSS Distributions }
171