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