1*2f2c4c7aSAndroid Build Coastguard Worker#!/usr/bin/python3 2*2f2c4c7aSAndroid Build Coastguard Worker# 3*2f2c4c7aSAndroid Build Coastguard Worker# Copyright 2016 The Android Open Source Project 4*2f2c4c7aSAndroid Build Coastguard Worker# 5*2f2c4c7aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*2f2c4c7aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*2f2c4c7aSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*2f2c4c7aSAndroid Build Coastguard Worker# 9*2f2c4c7aSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*2f2c4c7aSAndroid Build Coastguard Worker# 11*2f2c4c7aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*2f2c4c7aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*2f2c4c7aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*2f2c4c7aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*2f2c4c7aSAndroid Build Coastguard Worker# limitations under the License. 16*2f2c4c7aSAndroid Build Coastguard Worker 17*2f2c4c7aSAndroid Build Coastguard Workerfrom errno import * # pylint: disable=wildcard-import 18*2f2c4c7aSAndroid Build Coastguard Workerfrom socket import * # pylint: disable=wildcard-import 19*2f2c4c7aSAndroid Build Coastguard Workerimport threading 20*2f2c4c7aSAndroid Build Coastguard Workerimport time 21*2f2c4c7aSAndroid Build Coastguard Workerimport unittest 22*2f2c4c7aSAndroid Build Coastguard Worker 23*2f2c4c7aSAndroid Build Coastguard Workerimport csocket 24*2f2c4c7aSAndroid Build Coastguard Workerimport net_test 25*2f2c4c7aSAndroid Build Coastguard Worker 26*2f2c4c7aSAndroid Build Coastguard Worker 27*2f2c4c7aSAndroid Build Coastguard Workerclass LeakTest(net_test.NetworkTest): 28*2f2c4c7aSAndroid Build Coastguard Worker 29*2f2c4c7aSAndroid Build Coastguard Worker def testRecvfromLeak(self): 30*2f2c4c7aSAndroid Build Coastguard Worker s = socket(AF_INET6, SOCK_DGRAM, 0) 31*2f2c4c7aSAndroid Build Coastguard Worker s.bind(("::1", 0)) 32*2f2c4c7aSAndroid Build Coastguard Worker 33*2f2c4c7aSAndroid Build Coastguard Worker # Call shutdown on another thread while a recvfrom is in progress. 34*2f2c4c7aSAndroid Build Coastguard Worker csocket.SetSocketTimeout(s, 2000) 35*2f2c4c7aSAndroid Build Coastguard Worker def ShutdownSocket(): 36*2f2c4c7aSAndroid Build Coastguard Worker time.sleep(0.5) 37*2f2c4c7aSAndroid Build Coastguard Worker self.assertRaisesErrno(ENOTCONN, s.shutdown, SHUT_RDWR) 38*2f2c4c7aSAndroid Build Coastguard Worker 39*2f2c4c7aSAndroid Build Coastguard Worker t = threading.Thread(target=ShutdownSocket) 40*2f2c4c7aSAndroid Build Coastguard Worker t.start() 41*2f2c4c7aSAndroid Build Coastguard Worker 42*2f2c4c7aSAndroid Build Coastguard Worker # This could have been written with just "s.recvfrom", but because we're 43*2f2c4c7aSAndroid Build Coastguard Worker # testing for a bug where the kernel returns garbage, it's probably safer 44*2f2c4c7aSAndroid Build Coastguard Worker # to call the syscall directly. 45*2f2c4c7aSAndroid Build Coastguard Worker data, addr = csocket.Recvfrom(s, 4096) 46*2f2c4c7aSAndroid Build Coastguard Worker self.assertEqual(b"", data) 47*2f2c4c7aSAndroid Build Coastguard Worker self.assertEqual(None, addr) 48*2f2c4c7aSAndroid Build Coastguard Worker s.close() 49*2f2c4c7aSAndroid Build Coastguard Worker 50*2f2c4c7aSAndroid Build Coastguard Worker 51*2f2c4c7aSAndroid Build Coastguard Workerclass ForceSocketBufferOptionTest(net_test.NetworkTest): 52*2f2c4c7aSAndroid Build Coastguard Worker 53*2f2c4c7aSAndroid Build Coastguard Worker SO_SNDBUFFORCE = 32 54*2f2c4c7aSAndroid Build Coastguard Worker SO_RCVBUFFORCE = 33 55*2f2c4c7aSAndroid Build Coastguard Worker 56*2f2c4c7aSAndroid Build Coastguard Worker def CheckForceSocketBufferOption(self, option, force_option): 57*2f2c4c7aSAndroid Build Coastguard Worker s = socket(AF_INET6, SOCK_DGRAM, 0) 58*2f2c4c7aSAndroid Build Coastguard Worker 59*2f2c4c7aSAndroid Build Coastguard Worker # Find the minimum buffer value. 60*2f2c4c7aSAndroid Build Coastguard Worker s.setsockopt(SOL_SOCKET, option, 0) 61*2f2c4c7aSAndroid Build Coastguard Worker minbuf = s.getsockopt(SOL_SOCKET, option) 62*2f2c4c7aSAndroid Build Coastguard Worker 63*2f2c4c7aSAndroid Build Coastguard Worker # Check that the force option works to set reasonable values. 64*2f2c4c7aSAndroid Build Coastguard Worker val = 4097 65*2f2c4c7aSAndroid Build Coastguard Worker self.assertGreater(2 * val, minbuf) 66*2f2c4c7aSAndroid Build Coastguard Worker s.setsockopt(SOL_SOCKET, force_option, val) 67*2f2c4c7aSAndroid Build Coastguard Worker self.assertEqual(2 * val, s.getsockopt(SOL_SOCKET, option)) 68*2f2c4c7aSAndroid Build Coastguard Worker 69*2f2c4c7aSAndroid Build Coastguard Worker # Check that the force option sets at least the minimum value instead 70*2f2c4c7aSAndroid Build Coastguard Worker # of a negative value on integer overflow. Because the kernel multiplies 71*2f2c4c7aSAndroid Build Coastguard Worker # passed-in values by 2, pick a value that becomes a small negative number 72*2f2c4c7aSAndroid Build Coastguard Worker # if treated as unsigned. 73*2f2c4c7aSAndroid Build Coastguard Worker bogusval = 2 ** 31 - val 74*2f2c4c7aSAndroid Build Coastguard Worker s.setsockopt(SOL_SOCKET, force_option, bogusval) 75*2f2c4c7aSAndroid Build Coastguard Worker self.assertLessEqual(minbuf, s.getsockopt(SOL_SOCKET, option)) 76*2f2c4c7aSAndroid Build Coastguard Worker s.close() 77*2f2c4c7aSAndroid Build Coastguard Worker 78*2f2c4c7aSAndroid Build Coastguard Worker def testRcvBufForce(self): 79*2f2c4c7aSAndroid Build Coastguard Worker self.CheckForceSocketBufferOption(SO_RCVBUF, self.SO_RCVBUFFORCE) 80*2f2c4c7aSAndroid Build Coastguard Worker 81*2f2c4c7aSAndroid Build Coastguard Worker def testSndBufForce(self): 82*2f2c4c7aSAndroid Build Coastguard Worker self.CheckForceSocketBufferOption(SO_SNDBUF, self.SO_SNDBUFFORCE) 83*2f2c4c7aSAndroid Build Coastguard Worker 84*2f2c4c7aSAndroid Build Coastguard Worker 85*2f2c4c7aSAndroid Build Coastguard Workerif __name__ == "__main__": 86*2f2c4c7aSAndroid Build Coastguard Worker unittest.main() 87