1 #!/usr/bin/env python3.4 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 17 import mock 18 import socket 19 import unittest 20 21 from acts.controllers.utils_lib import host_utils 22 23 24 class ActsHostUtilsTest(unittest.TestCase): 25 """This test class has unit tests for the implementation of everything 26 under acts.controllers.adb. 27 """ 28 29 def test_is_port_available_positive(self): 30 # Unfortunately, we cannot do this test reliably for SOCK_STREAM 31 # because the kernel allow this socket to linger about for some 32 # small amount of time. We're not using SO_REUSEADDR because we 33 # are working around behavior on darwin where binding to localhost 34 # on some port and then binding again to the wildcard address 35 # with SO_REUSEADDR seems to be allowed. 36 test_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 37 test_s.bind(('localhost', 0)) 38 port = test_s.getsockname()[1] 39 test_s.close() 40 self.assertTrue(host_utils.is_port_available(port)) 41 42 def test_detects_udp_port_in_use(self): 43 test_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 44 test_s.bind(('localhost', 0)) 45 port = test_s.getsockname()[1] 46 try: 47 self.assertFalse(host_utils.is_port_available(port)) 48 finally: 49 test_s.close() 50 51 def test_detects_tcp_port_in_use(self): 52 test_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 53 test_s.bind(('localhost', 0)) 54 port = test_s.getsockname()[1] 55 try: 56 self.assertFalse(host_utils.is_port_available(port)) 57 finally: 58 test_s.close() 59 60 61 if __name__ == "__main__": 62 unittest.main() 63