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