2 # Copyright (c) 2017 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 This module is used to generate pcap file used to execute UDP test cases.
19 from scapy.layers.inet import Ether, IP, UDP, fragment
20 from scapy.layers.inet6 import IPv6
21 from scapy.utils import PcapWriter
22 from resources.libraries.python.TLDK.TLDKConstants import TLDKConstants as con
24 def create_packet(psz, is_ipv6, frag_size=None):
25 """Create a packet to use scapy send to DUT."""
27 packet = Ether()/IP()/UDP()/("X" * psz)
28 packet[IP].src = "192.168.1.56"
29 packet[IP].dst = "192.168.1.233"
31 packet = Ether()/IPv6()/UDP()/("X" * psz)
32 packet[IPv6].src = "2001:4860:b002::56"
33 packet[IPv6].dst = "2001:4860:b002::28"
34 packet[Ether].src = "DE:AD:BE:EF:02:01"
35 packet[Ether].dst = "DE:AD:BE:EF:01:02"
36 packet[UDP].sport = 1111
37 packet[UDP].dport = 32768
39 packet = fragment(packet, fragsize=frag_size)
42 def gen_ipv4_checksum_pcap():
43 """Generate ipv4 checksum test case input pcap file."""
44 writer = PcapWriter(con.TLDK_TESTCONFIG + "/test_ipv4_checksum_rx.pcap",
46 for i in range(1, 1474):
47 packets = create_packet(i, False)
48 for packet in packets:
52 def gen_ipv6_checksum_pcap():
53 """Generate ipv6 checksum test case input pcap file."""
54 writer = PcapWriter(con.TLDK_TESTCONFIG + "/test_ipv6_checksum_rx.pcap",
56 for i in range(1, 1454):
57 packets = create_packet(i, True)
58 for packet in packets:
62 def gen_ipv4_fragment_pcap():
63 """Generate ipv4 fragment test case input pcap file."""
64 writer = PcapWriter(con.TLDK_TESTCONFIG + "/test_ipv4_fragment_rx.pcap",
66 for i in range(1, 1474):
67 packets = create_packet(i, False)
68 for packet in packets:
72 def gen_ipv6_fragment_pcap():
73 """Generate ipv6 fragment test case input pcap file."""
74 writer = PcapWriter(con.TLDK_TESTCONFIG + "/test_ipv6_fragment_rx.pcap",
76 for i in range(1, 1454):
77 packets = create_packet(i, True)
78 for packet in packets:
82 def gen_ipv4_assemble_pcap():
83 """Generate ipv4 assemble test case input pcap file."""
84 writer = PcapWriter(con.TLDK_TESTCONFIG + "/test_ipv4_assemble_rx.pcap",
86 packets = create_packet(1066, False, 1024)
87 for packet in packets:
92 """Generate all test cases input pcap file."""
93 gen_ipv4_checksum_pcap()
94 gen_ipv6_checksum_pcap()
95 gen_ipv4_fragment_pcap()
96 gen_ipv6_fragment_pcap()
97 gen_ipv4_assemble_pcap()
99 if __name__ == "__main__":