1#!/usr/bin/python
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    net_test.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("", data)
47    self.assertEqual(None, addr)
48
49
50if __name__ == "__main__":
51  unittest.main()
52