Merge "Submit initial test framework skeleton."
[vpp.git] / vnet / vnet / map / examples / mt-test.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2009-2014 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import threading
17 import time
18 from scapy.all import *
19 from Queue import *
20
21 iface = 'veth1'
22
23 class SnifferThread(threading.Thread) :
24     def __init__(self,q,iface,flt,timeout) :
25         threading.Thread.__init__(self)
26         self.q = q
27         self.iface = iface
28         self.timeout = timeout
29         self.flt = flt
30         print("Sniffers reporting for service on ",self.iface)
31  
32     def run(self) :
33         conf.iface=self.iface
34         conf.iface6=self.iface
35
36         r = sniff(filter=self.flt,iface=self.iface,timeout=self.timeout,prn=lambda x: x.summary())
37         self.q.put(r)
38
39
40
41 # New "SR" function
42 #   Fire off thread with filter and expected answer packet(s).
43 # Fire off sniffer thread, main thread sends packet
44 #   Returns true if found
45
46 def sr2(answer, *args, **kwargs):
47     q = Queue()
48     print("Creating SnifferThreadWorkerThread")
49     flt='ip proto 41'
50     iface='veth1'
51     sniffer = SnifferThread(q,iface,flt,1)
52     sniffer.setDaemon(True)
53     sniffer.start()
54
55     print "Sending packet:"
56     send(*args, **kwargs)
57     sniffer.join()
58     ps = q.get()
59     
60 #    ps.summary()
61     print "Number of packets sniffed:", len(ps)
62
63     for p in ps:
64         ip = p.getlayer(1)
65         print "Comparing", ip.summary(), "and", answer.summary()
66         if ip == answer:
67             print "We have a match!!"
68             return True
69     return False
70
71 aip6 = IPv6(dst='2002:0a0a:0a0a::12')/ICMPv6EchoRequest()
72 answer= IP(src="10.0.0.100",dst="10.10.10.10",ttl=63)/aip6
73 packet = IPv6(dst='2002:0a0a:0a0a::12')/ICMPv6EchoRequest()
74
75 # From IPv6
76 sr2(answer, packet,iface='veth1')
77
78 #From IPv4
79 packet = IP(src='10.10.10.10',dst='10.0.0.100')/IPv6(src='2002:0a0a:0a0a::12',dst='1::2')/ICMPv6EchoRequest()
80 sr2(answer, packet,iface='veth1')