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