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