def test_dst_port_abcd(self, ): # Pack f = heymac.APv6Udp(src_port=0xF0B0, dst_port=0xABCD) b = bytes(f) self.assertEqual(b, b"\xF6\xB0\xAB\xCD") # Unpack f = heymac.APv6Udp(b) self.assertEqual(f.hdr_type, 0b11110) self.assertEqual(f.hdr_co, 1) self.assertEqual(f.hdr_ports, 0b10) self.assertEqual(f.chksum, b"") self.assertEqual(f.src_port, 0xF0B0) self.assertEqual(f.dst_port, 0xABCD)
def test_src_port_f0b3(self, ): # Pack f = heymac.APv6Udp(src_port=0xF0B3, dst_port=0xF0B0) b = bytes(f) self.assertEqual(b, b"\xF7\x30") # Unpack f = heymac.APv6Udp(b) self.assertEqual(f.hdr_type, 0b11110) self.assertEqual(f.hdr_co, 1) self.assertEqual(f.hdr_ports, 0b11) self.assertEqual(f.chksum, b"") self.assertEqual(f.src_port, 0xF0B3) self.assertEqual(f.dst_port, 0xF0B0)
def test_src_port_f009(self, ): # Pack f = heymac.APv6Udp(src_port=0xF009, dst_port=0xF0B0) b = bytes(f) self.assertEqual(b, b"\xF6\x09\xF0\xB0") # Unpack f = heymac.APv6Udp(b) self.assertEqual(f.hdr_type, 0b11110) self.assertEqual(f.hdr_co, 1) self.assertEqual(f.hdr_ports, 0b10) self.assertEqual(f.chksum, b"") self.assertEqual(f.src_port, 0xF009) self.assertEqual(f.dst_port, 0xF0B0)
def test_chksum_src_dst(self, ): # Pack f = heymac.APv6Udp(chksum=0xC1C2, src_port=0x1112, dst_port=0x2122) b = bytes(f) self.assertEqual(b, b"\xF0\xC1\xC2\x11\x12\x21\x22") # Unpack f = heymac.APv6Udp(b) self.assertEqual(f.hdr_type, 0b11110) self.assertEqual(f.hdr_co, 0) self.assertEqual(f.hdr_ports, 0b00) self.assertEqual(f.chksum, 0xC1C2) self.assertEqual(f.src_port, 0x1112) self.assertEqual(f.dst_port, 0x2122)
def test_chksum(self, ): # Pack f = heymac.APv6Udp(chksum=0x2A3B, src_port=0xF0B0, dst_port=0xF0B0) b = bytes(f) self.assertEqual(b, b"\xF3\x2A\x3B\x00") # Unpack f = heymac.APv6Udp(b) self.assertEqual(f.hdr_type, 0b11110) self.assertEqual(f.hdr_co, 0) self.assertEqual(f.hdr_ports, 0b11) self.assertEqual(f.chksum, 0x2A3B) self.assertEqual(f.src_port, 0xF0B0) self.assertEqual(f.dst_port, 0xF0B0)
def test_regression_serialize_without_port(self, ): """An assertion should be thrown if the coder does not give src/dst ports """ import struct f = heymac.APv6Udp() with self.assertRaises(struct.error): b = bytes(f) f = heymac.APv6Udp(src_port=0x1234) with self.assertRaises(struct.error): b = bytes(f) f = heymac.APv6Udp(dst_port=0x1234) with self.assertRaises(struct.error): b = bytes(f)
def test_mac_net_udp_to_google(self, ): # Pack fmac = heymac.HeyMacFrame( fctl_type=heymac.HeyMacFrame.FCTL_TYPE_NET, pend=0, saddr=b"\x35\x16", ) google_ipv6_addr = b"\x20\x01\x48\x60\x48\x60\x00\x00\x00\x00\x00\x00\x00\x00\x88\x88" fnet = heymac.APv6Frame(dst=google_ipv6_addr, ) fudp = heymac.APv6Udp(src_port=0xF0B0, dst_port=53, data=b"DnsRequest") fmac.data = fnet fnet.data = fudp b = bytes(fmac) self.assertEqual( b, b"\xe2\x81\x35\x16\xD6" + google_ipv6_addr + b"\xF6\xB0\x00\x35DnsRequest") # Unpack HeyMacFrame fmac = heymac.HeyMacFrame(b) self.assertEqual(fmac.pv_pid, fmac.PV_PID_HEYMAC) self.assertEqual(fmac.pv_ver, fmac.PV_VER_HEYMAC2) self.assertEqual(fmac.fctl_type, heymac.HeyMacFrame.FCTL_TYPE_NET) self.assertEqual(fmac.fctl_l, 0) self.assertEqual(fmac.fctl_p, 0) self.assertEqual(fmac.fctl_n, 0) self.assertEqual(fmac.fctl_d, 0) self.assertEqual(fmac.fctl_i, 0) self.assertEqual(fmac.fctl_s, 1) self.assertEqual(fmac.exttype, b"") self.assertEqual(fmac.netid, b"") self.assertEqual(fmac.daddr, b"") self.assertEqual(fmac.saddr, b"\x35\x16") self.assertTrue(len(fmac.data) > 0) # Unpack APv6Frame fnet = fmac.data self.assertEqual(type(fnet), heymac.APv6Frame) self.assertEqual(fnet.iphc_prefix, 6) self.assertEqual(fnet.iphc_nhc, 1) self.assertEqual(fnet.iphc_hlim, 1) self.assertEqual(fnet.iphc_sam, 1) self.assertEqual(fnet.iphc_dam, 0) self.assertEqual(fnet.hops, 1) self.assertEqual(fnet.src, b"") self.assertEqual(fnet.dst, google_ipv6_addr) # Unpack UDP fudp = fnet.data self.assertEqual(type(fudp), heymac.APv6Udp) self.assertEqual(fudp.hdr_type, 0b11110) self.assertEqual(fudp.hdr_co, 1) self.assertEqual(fudp.hdr_ports, 0b10) self.assertEqual(fudp.chksum, b"") self.assertEqual(fudp.src_port, 0xF0B0) self.assertEqual(fudp.dst_port, 53) self.assertEqual(fudp.data, b"DnsRequest")
def test_mac_net_udp_to_node(self, ): # Pack fmac = heymac.HeyMacFrame(fctl_type=heymac.HeyMacFrame.FCTL_TYPE_NET, pend=0, saddr=b"\x35\x16", daddr=b"\x83\x11") fnet = heymac.APv6Frame() fudp = heymac.APv6Udp(src_port=0xF0BA, dst_port=0xF0BF, data=b"nodedata") fmac.data = fnet fnet.data = fudp b = bytes(fmac) self.assertEqual(b, b"\xe2\x85\x83\x11\x35\x16\xD7\xF7\xAFnodedata") # Unpack HeyMacFrame fmac = heymac.HeyMacFrame(b) self.assertEqual(fmac.pv_pid, fmac.PV_PID_HEYMAC) self.assertEqual(fmac.pv_ver, fmac.PV_VER_HEYMAC2) self.assertEqual(fmac.fctl_type, heymac.HeyMacFrame.FCTL_TYPE_NET) self.assertEqual(fmac.fctl_l, 0) self.assertEqual(fmac.fctl_p, 0) self.assertEqual(fmac.fctl_n, 0) self.assertEqual(fmac.fctl_d, 1) self.assertEqual(fmac.fctl_i, 0) self.assertEqual(fmac.fctl_s, 1) self.assertEqual(fmac.exttype, b"") self.assertEqual(fmac.netid, b"") self.assertEqual(fmac.daddr, b"\x83\x11") self.assertEqual(fmac.saddr, b"\x35\x16") self.assertTrue(len(fmac.data) > 0) # Unpack APv6Frame fnet = fmac.data self.assertEqual(type(fnet), heymac.APv6Frame) self.assertEqual(fnet.iphc_prefix, 6) self.assertEqual(fnet.iphc_nhc, 1) self.assertEqual(fnet.iphc_hlim, 1) self.assertEqual(fnet.iphc_sam, 1) self.assertEqual(fnet.iphc_dam, 1) self.assertEqual(fnet.hops, 1) self.assertEqual(fnet.src, b"") self.assertEqual(fnet.dst, b"") # Unpack UDP fudp = fnet.data self.assertEqual(type(fudp), heymac.APv6Udp) self.assertEqual(fudp.hdr_type, 0b11110) self.assertEqual(fudp.hdr_co, 1) self.assertEqual(fudp.hdr_ports, 0b11) self.assertEqual(fudp.chksum, b"") self.assertEqual(fudp.src_port, 0xF0BA) self.assertEqual(fudp.dst_port, 0xF0BF) self.assertEqual(fudp.data, b"nodedata")
def test_regression_serialize_twice(self, ): """Problem: serializing an APv6Udp() a second time caused an assertion:: >>> u = heymac.APv6Udp() >>> u.src_port = 0xF0B1 >>> print(u) b'\xf7\x10' >>> u.dst_port = 0xF0B9 >>> print(u) Traceback (most recent call last): ...<snip>... struct.error: required argument is not an integer """ # Pack f = heymac.APv6Udp(src_port=0x1112, dst_port=0xF0B0) b1 = bytes(f) f.dst_port = 0x2122 b2 = bytes(f)
def test_regression_give_chksum_as_bytes(self, ): f = heymac.APv6Udp(src_port=0x1234, dst_port=0x5678, chksum=b"\x9A\xBC") b = bytes(f)