def test5_msearch_device(self): """Test with four verbose requests, data only on third request.""" self.o_mock_socket.recvfrom.side_effect = [ socket.timeout(), socket.timeout(), (SDATAGRAM1, SADDR1), socket.timeout(), socket.timeout() ] o_msearch_device = MsearchDevice(verbose=True) o_msearch_device.request(retries=4) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 1) self.assertRegex(result, r'^0000\.00\d\ds 2\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 2) self.assertRegex(result, r'^0000\.00\d\ds 3\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 3) self.assertRegex(result[:9], r'^0000\.00\d\d$$') self.assertEqual(result[9:], 's 3 192.168.10.119:47383\r\n' + SDATAGRAM1.decode()) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 4) self.assertRegex(result, r'^0000\.00\d\ds 4\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 5) self.assertRegex(result, r'^0000\.00\d\ds 0\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 5) self.assertIsNone(result)
def test3_msearch_device(self): """Test with two requests and data on each request.""" self.o_mock_socket.recvfrom.side_effect = [(SDATAGRAM1, SADDR1), socket.timeout(), (SDATAGRAM2, SADDR2), (SDATAGRAM1, SADDR1), socket.timeout()] o_msearch_device = MsearchDevice() o_msearch_device.request(retries=2) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 1) self.assertRegex(result, (r"^0000\.0\d\d\ds 1 192\.168\.10\.119:47383 " r"uuid:3b2867a3-b55f-8e77-5ad8-a6d0c6990277 " r"Linux/3\.10\.79, UPnP/1\.0, Portable SDK for UPnP " r"devices/1\.6\.18\r\n$")) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 2) self.assertRegex(result, r'^0000\.00..s 2\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 3) self.assertRegex(result, (r"^0000\.0\d\d\ds 2 192\.168\.49\.1:34731 " r"uuid:f48c8d92-c3c0-6f29-0000-00004e74db48 " r"Linux/3\.10\.54 UPnP/1\.0 Cling/2\.0\r\n$")) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 5) self.assertRegex(result, r'^0000\.00..s 0\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 5) self.assertIsNone(result)
def test1_msearch_device(self): """Test with no request and requests with negative and 0 retries.""" o_msearch_device = MsearchDevice() result = o_msearch_device.get() self.assertIsNone(result) self.assertEqual(self.o_mock_socket.recvfrom.call_count, 0) o_msearch_device.request(retries=-1) result = o_msearch_device.get() self.assertIsNone(result) self.assertEqual(self.o_mock_socket.recvfrom.call_count, 0) o_msearch_device.request(retries=0) result = o_msearch_device.get() self.assertIsNone(result) self.assertEqual(self.o_mock_socket.recvfrom.call_count, 0)
def test_print_it(self): """Test if the output works.""" # set three timeouts because default retries = 3 self.o_mock_socket.recvfrom.side_effect = [(SDATAGRAM2, SADDR2), socket.timeout(), socket.timeout(), (SDATAGRAM3, SADDR3), socket.timeout()] with mock.patch('sys.stdout', new=StringIO()) as fake_output: print_it(MsearchDevice()) self.assertRegex( fake_output.getvalue(), (r"^0000\.0\d\d\ds 1 192\.168\.49\.1:34731 " r"uuid:f48c8d92-c3c0-6f29-0000-00004e74db48 " r"Linux/3\.10\.54 UPnP/1\.0 Cling/2\.0\r\n" r"0000\.0\d\d\ds 2\r\n" r"0000\.0\d\d\ds 3\r\n" r"0000\.0\d\d\ds 3 192\.168\.10\.3:1900 " r"uuid:123402409-bccb-40e7-8e6c-3481C4FC71A9 " r"fritz-box UPnP/1\.0 AVM FRITZ!Box 7490 113\.07\.01\r\n" r"0000\.0\d\d\ds 0\r\n$"))
def test2_msearch_device(self): """Test with one request but subsequent pending data.""" self.o_mock_socket.recvfrom.side_effect = [(SDATAGRAM1, SADDR1), socket.timeout(), (SDATAGRAM2, SADDR2)] o_msearch_device = MsearchDevice() o_msearch_device.request(retries=1) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 1) self.assertRegex(result, (r"^0000\.0\d\d\ds 1 192\.168\.10\.119:47383 " r"uuid:3b2867a3-b55f-8e77-5ad8-a6d0c6990277 " r"Linux/3\.10\.79, UPnP/1\.0, Portable SDK for UPnP " r"devices/1\.6\.18\r\n$")) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 2) self.assertRegex(result, r'^0000\.00..s 0\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 2) self.assertIsNone(result)
def test6_msearch_device(self): """Test with three requests but no data.""" self.o_mock_socket.recvfrom.side_effect = [ socket.timeout(), socket.timeout(), socket.timeout() ] o_msearch_device = MsearchDevice() o_msearch_device.request(retries=3) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 1) self.assertRegex(result, r'^0000\.00\d\ds 2\r\n$') self.assertNotEqual(result[:9], '0000.0000') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 2) self.assertRegex(result, r'^0000\.00\d\ds 3\r\n$') self.assertNotEqual(result[:9], '0000.0000') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 3) self.assertRegex(result, r'^0000\.00\d\ds 0\r\n$') self.assertNotEqual(result[:9], '0000.0000') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 3) self.assertIsNone(result)
def test4_msearch_device(self): """Test with three requests, data only on second request.""" self.o_mock_socket.recvfrom.side_effect = [ socket.timeout(), (SDATAGRAM1, SADDR1), (SDATAGRAM2, SADDR2), (SDATAGRAM1, SADDR1), (SDATAGRAM1, SADDR1), (SDATAGRAM2, SADDR2), (SDATAGRAM3, SADDR3), socket.timeout(), socket.timeout() ] o_msearch_device = MsearchDevice() o_msearch_device.request(retries=3) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 1) self.assertRegex(result, r'^0000\.00\d\ds 2\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 2) self.assertRegex(result, (r"^0000\.0\d\d\ds 2 192\.168\.10\.119:47383 " r"uuid:3b2867a3-b55f-8e77-5ad8-a6d0c6990277 " r"Linux/3\.10\.79, UPnP/1\.0, Portable SDK for UPnP " r"devices/1\.6\.18\r\n$")) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 3) self.assertRegex(result, (r"^0000\.0\d\d\ds 2 192\.168\.49\.1:34731 " r"uuid:f48c8d92-c3c0-6f29-0000-00004e74db48 " r"Linux/3\.10\.54 UPnP/1\.0 Cling/2\.0\r\n$")) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 7) self.assertRegex( result, (r"^0000\.0\d\d\ds 2 192\.168\.10\.3:1900 " r"uuid:123402409-bccb-40e7-8e6c-3481C4FC71A9 " r"fritz-box UPnP/1\.0 AVM FRITZ!Box 7490 113\.07\.01\r\n$")) result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 8) self.assertRegex(result, r'^0000\.00\d\ds 3\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 9) self.assertRegex(result, r'^0000\.00\d\ds 0\r\n$') result = o_msearch_device.get() self.assertEqual(self.o_mock_socket.recvfrom.call_count, 9) self.assertIsNone(result)