def test_fail_to_decode_missing_required(self): packet = \ b'M-SEARCH * HTTP/1.1\r\n' \ b'Host: 239.255.255.250:1900\r\n' \ b'ST: ssdp:all\r\n' \ b'MX: 5\r\n' \ b'\r\n' with self.assertRaises(UPnPError): SSDPDatagram.decode(packet)
def test_parse_m_search(self): packet = SSDPDatagram.decode(self.datagram) self.assertTrue(packet._packet_type, packet._M_SEARCH) self.assertEqual(packet.host, '239.255.255.250:1900') self.assertEqual( packet.st, 'urn:schemas-upnp-org:device:InternetGatewayDevice:1') self.assertEqual(packet.man, 'ssdp:discover') self.assertEqual(packet.mx, 1)
def test_parse_m_search_response(self): packet = SSDPDatagram.decode(self.datagram) self.assertTrue(packet._packet_type, packet._OK) self.assertEqual(packet.cache_control, 'max-age=1800') self.assertEqual(packet.location, 'http://10.1.10.1:49152/IGDdevicedesc_brlan0.xml') self.assertEqual( packet.server, 'Linux/3.14.28-Prod_17.2, UPnP/1.0, Portable SDK for UPnP devices/1.6.22' ) self.assertEqual(packet.st, UPNP_ORG_IGD)
def test_parse_m_search_response(self): packet = SSDPDatagram.decode(self.datagram) self.assertTrue(packet._packet_type, packet._OK) self.assertEqual(packet.cache_control, 'max-age=1800') self.assertEqual(packet.location, 'http://10.0.0.1:49152/InternetGatewayDevice.xml') self.assertEqual(packet.server, 'Linux, UPnP/1.0, DIR-890L Ver 1.20') self.assertEqual( packet.st, 'urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1') self.assertEqual( packet.usn, 'uuid:00000000-0000-0000-0000-000000000000::urn:' 'schemas-upnp-org:service:WANCommonInterfaceConfig:1')
def datagram_received(self, data: bytes, addr: Tuple[str, int]) -> None: # type: ignore if addr[0] == self.bind_address: return None try: packet = SSDPDatagram.decode(data) log.debug("decoded packet from %s:%i: %s", addr[0], addr[1], packet) except UPnPError as err: log.warning("failed to decode SSDP packet from %s:%i (%s): %s", addr[0], addr[1], err, binascii.hexlify(data)) return None if packet._packet_type == packet._OK: self._callback_m_search_ok(addr[0], packet) return None
def test_parse_notify(self): packet = SSDPDatagram.decode(self.datagram) self.assertTrue(packet._packet_type, packet._NOTIFY) self.assertEqual(packet.host, '239.255.255.250:1900') self.assertEqual(packet.cache_control, 'max-age=180') # this is an optional field self.assertEqual( packet.location, 'http://192.168.1.1:5431/dyndev/uuid:000c-29ea-247500c00068') self.assertEqual(packet.nt, 'upnp:rootdevice') self.assertEqual(packet.nts, 'ssdp:alive') self.assertEqual(packet.server, 'LINUX/2.4 UPnP/1.0 BRCM400/1.0') self.assertEqual(packet.usn, 'uuid:000c-29ea-247500c00068::upnp:rootdevice')
def datagram_received(self, data, addr) -> None: if addr[0] == self.bind_address: return try: packet = SSDPDatagram.decode(data) log.debug("decoded packet from %s:%i: %s", addr[0], addr[1], packet) except UPnPError as err: log.error("failed to decode SSDP packet from %s:%i (%s): %s", addr[0], addr[1], err, binascii.hexlify(data)) return if packet._packet_type == packet._OK: self._callback_m_search_ok(addr[0], packet) return
def test_deserialize_and_reserialize(self): packet1 = SSDPDatagram.decode(self.packet) packet2 = SSDPDatagram("M-SEARCH", self.datagram_args) self.assertEqual(packet2.encode(), packet1.encode())
def test_fail_to_decode_one_line(self): packet = b'M-SEARCH * HTTP/1.1' with self.assertRaises(UPnPError): SSDPDatagram.decode(packet)
def test_fail_to_decode_blank(self): packet = b'' with self.assertRaises(UPnPError): SSDPDatagram.decode(packet)
def test_key_error(self): packet = SSDPDatagram.decode(self.datagram) self.assertRaises(KeyError, lambda: packet['Cache Control'])
def test_get_case_insensitive(self): packet = SSDPDatagram.decode(self.datagram) self.assertEqual('max-age=1800', packet['Cache_Control'])
def test_serialize_m_search(self): packet = SSDPDatagram.decode(self.datagram) self.assertEqual(packet.encode().encode(), self.datagram)