def test_mask__negative__try_to_mask_not_existing_layer_or_field( self, elements_to_ignore, scapy_simple_tcp_packet): masked_packet = Mask(scapy_simple_tcp_packet) # type: Mask with pytest.raises(MaskException): masked_packet.set_do_not_care_packet(elements_to_ignore[0], elements_to_ignore[1]) assert not masked_packet.valid
def runTest(self): pkt = "ab" * 20 pkt = pkt.encode() testutils.send_packet(self, (0, 1), pkt) print("packet sent") testutils.verify_any_packet_any_port(self, pkts=[pkt], ports=[3, 1], device_number=1) # negative test: if the packet is indeed received, but not on one of the # expected ports, the test should fail with self.assertRaises(AssertionError): testutils.send_packet(self, (0, 1), pkt) print("packet sent") testutils.verify_any_packet_any_port(self, pkts=[pkt], ports=[0, 2, 3], device_number=1) print("Verify masked packets") pkt1 = testutils.simple_udp_packet(eth_dst="00:11:11:11:11:11") pkt2 = testutils.simple_udp_packet(eth_dst="00:22:22:22:22:22") exp_pkt = Mask(pkt2) exp_pkt.set_do_not_care_packet(Ether, 'dst') testutils.send_packet(self, (0, 1), pkt1) print("Packet sent") # pkt2 will not be received # pkt2 with masked eth_dst field will match testutils.verify_any_packet_any_port(self, pkts=[pkt2, exp_pkt], ports=[0, 1], device_number=1) # negative tests with self.assertRaises(AssertionError): testutils.send_packet(self, (0, 1), pkt1) print("Packet sent") # incorrect ports testutils.verify_any_packet_any_port(self, pkts=[exp_pkt], ports=[0, 2, 3], device_number=1) with self.assertRaises(AssertionError): testutils.send_packet(self, (0, 1), pkt1) print("Packet sent") # incorrect packet testutils.verify_any_packet_any_port(self, pkts=[pkt2], ports=[0, 1], device_number=1)
def test_mask__conditional_field__gpid_should_be_masked_correctly( self, scapy_simple_vxlan_packet): simple_vxlan = scapy_simple_vxlan_packet # type: Packet simple_vxlan[VXLAN].flags = "G" masked_simple_vxlan = Mask(simple_vxlan) # type: Mask masked_simple_vxlan.set_do_not_care_packet(VXLAN, "gpid") # UDP chksum will be different when we change VXLAN masked_simple_vxlan.set_do_not_care_packet(UDP, "chksum") packet_wth_custom_gpid = simple_vxlan.copy() # type: Packet packet_wth_custom_gpid[VXLAN].gpid = 0x15 # 21 assert masked_simple_vxlan.pkt_match( packet_wth_custom_gpid), "Packets should match"
def test_mask__check_masking_conditional_field(self, scapy_simple_vxlan_packet): simple_vxlan = scapy_simple_vxlan_packet simple_vxlan[VXLAN].flags = "G" masked_simple_vxlan = Mask(simple_vxlan) masked_simple_vxlan.set_do_not_care_packet(VXLAN, "gpid") # gpflags, gpid second_masked_packet = Mask(simple_vxlan) second_masked_packet.set_do_not_care_packet(VXLAN, "gpflags") assert (masked_simple_vxlan.mask != second_masked_packet.mask), "Masks should not be equal"
def test_mask__mask_simple_packet(self, scapy_simple_tcp_packet): packet = scapy_simple_tcp_packet mask_packet = Mask(packet) assert mask_packet.pkt_match(packet) modified_packet = packet.copy() modified_packet[TCP].sport = 97 assert not mask_packet.pkt_match(modified_packet) # try to mask only sport (still packet will be different on chksum!) mask_packet.set_do_not_care_packet(TCP, "sport") assert not mask_packet.pkt_match(modified_packet) mask_packet.set_do_not_care_packet(TCP, "chksum") assert mask_packet.pkt_match(modified_packet)
def test_mask__mask_has_problem_with_conditional_fields(self): pkt = VXLAN(flags=0x80, gpflags=0x23, gpid=0x1234, vni=0x1337) mask_pkt = Mask(pkt) mask_pkt.set_do_not_care_packet(VXLAN, "gpid") assert mask_pkt.mask == [ 255, 255, 0, 0, 255, 255, 255, 255, ], "Only gpid field should be masked"
def runTest(self): print "Sending L2 packet - port 1 -> port 2 [trunk vlan=10])" switch_init(self.client) vlan_id = 10 port1 = port_list[1] port2 = port_list[2] mac1 = '00:11:11:11:11:11' mac2 = '00:22:22:22:22:22' mac_action = 1 self.client.sai_thrift_create_vlan(vlan_id) vlan_port1 = sai_thrift_vlan_port_t(port_id=port1, tagging_mode=1) vlan_port2 = sai_thrift_vlan_port_t(port_id=port2, tagging_mode=0) self.client.sai_thrift_add_ports_to_vlan(vlan_id, [vlan_port1, vlan_port2]) sai_thrift_create_fdb(self.client, vlan_id, mac1, port1, mac_action) sai_thrift_create_fdb(self.client, vlan_id, mac2, port2, mac_action) pkt = simple_tcp_packet(eth_dst='00:11:11:11:11:11', eth_src='00:22:22:22:22:22', ip_dst='10.0.0.1', ip_id=102, ip_ttl=64) exp_pkt = simple_tcp_packet(eth_dst='00:11:11:11:11:11', eth_src='00:22:22:22:22:22', ip_dst='10.0.0.1', dl_vlan_enable=True, vlan_vid=10, ip_id=102, ip_ttl=64, pktlen=104) # illustrates how to use a mask even if no impact here m = Mask(exp_pkt) m.set_do_not_care_packet(IP, 'ttl') try: send_packet(self, 2, pkt) verify_packets(self, m, [1]) finally: sai_thrift_delete_fdb(self.client, vlan_id, mac1, port1) sai_thrift_delete_fdb(self.client, vlan_id, mac2, port2) self.client.sai_thrift_remove_ports_from_vlan( vlan_id, [vlan_port1, vlan_port2]) self.client.sai_thrift_delete_vlan(vlan_id)
def test_mask__validate_str_conversion(self, scapy_simple_tcp_packet): masked_packet = Mask(scapy_simple_tcp_packet) # type: Mask masked_packet.set_do_not_care_packet(IP, "chksum") assert str(masked_packet) == EXPECTED_MASKED_PACKET