Trending: new daily set
[csit.git] / resources / libraries / python / TLDK / gen_pcap.py
1 #! /usr/bin/python
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:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 """
16 This module is used to generate pcap file used to execute UDP test cases.
17 """
18
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
23
24 def create_packet(psz, is_ipv6, frag_size=None):
25     """Create a packet to use scapy send to DUT."""
26     if is_ipv6 != True:
27         packet = Ether()/IP()/UDP()/("X" * psz)
28         packet[IP].src = "192.168.1.56"
29         packet[IP].dst = "192.168.1.233"
30     else:
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
38     if frag_size != None:
39         packet = fragment(packet, fragsize=frag_size)
40     return packet
41
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",
45                         append=False)
46     for i in range(1, 1474):
47         packets = create_packet(i, False)
48         for packet in packets:
49             writer.write(packet)
50     writer.close()
51
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",
55                         append=False)
56     for i in range(1, 1454):
57         packets = create_packet(i, True)
58         for packet in packets:
59             writer.write(packet)
60     writer.close()
61
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",
65                         append=False)
66     for i in range(1, 1474):
67         packets = create_packet(i, False)
68         for packet in packets:
69             writer.write(packet)
70     writer.close()
71
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",
75                         append=False)
76     for i in range(1, 1454):
77         packets = create_packet(i, True)
78         for packet in packets:
79             writer.write(packet)
80     writer.close()
81
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",
85                         append=False)
86     packets = create_packet(1066, False, 1024)
87     for packet in packets:
88         writer.write(packet)
89     writer.close()
90
91 def gen_all_pcap():
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()
98
99 if __name__ == "__main__":
100     gen_all_pcap()