blob: 60d314e8af17d1e0926062febc9af9c01aabaf97 [file] [log] [blame]
Chetan Gaonker25470972016-02-26 08:52:15 -08001import unittest
2from nose.tools import *
3from nose.twistedtools import reactor, deferred
4from twisted.internet import defer
5from scapy.all import *
6import time
7import os, sys
8CORD_TEST_UTILS = 'utils'
9test_root = os.getenv('CORD_TEST_ROOT') or './'
10sys.path.append(test_root + CORD_TEST_UTILS)
11from IGMP import *
12
13class IGMPTestState:
14
15 def __init__(self, groups = [], df = None, state = 0):
16 self.df = df
17 self.state = state
18 self.counter = 0
19 self.groups = groups
20 self.group_map = {} ##create a send/recv count map
21 for g in groups:
22 self.group_map[g] = [0, 0]
23
24 def update(self, group, tx = 0, rx = 0):
25 index = 0 if rx == 0 else 1
26 v = tx if rx == 0 else rx
27 if self.group_map.has_key(group):
28 self.group_map[group][index] += v
29
30 def update_state(self):
31 self.state = self.state ^ 1
32 self.counter += 1
33
34class igmp_exchange(unittest.TestCase):
35
36 def igmp_verify(self, igmpState):
37 ## check if the send is received for the groups
38 for g in igmpState.groups:
39 tx = igmpState.group_map[g][0]
40 rx = igmpState.group_map[g][1]
41 assert_greater(tx, 0)
42 assert_equal(tx, rx)
43 print 'IGMP test verification success'
44
45 def igmp_send(self, igmpState, iface = 'veth2'):
46 dst_mac = '01:00:5e:00:01:01'
47 src_mac = '02:88:b4:e4:90:77'
48 src_ip = '1.2.3.4'
49 eth = Ether(dst = dst_mac, src = src_mac)
50 data = repr(time.time())
51 for g in igmpState.groups:
52 ip = IP(dst = g, src = src_ip)
53 sendp(eth/ip/data, iface=iface)
54 igmpState.update(g, tx = 1)
55
56 ##done sending. Bounce the states
57 igmpState.update_state()
58 return 0
59
60 def igmp_recv(self, igmpState, iface = 'veth0'):
61 for g in igmpState.groups:
62 igmpState.update(g, rx = 1)
63 ##done receiving. Bounce the state back to send
64 igmpState.update_state()
65 return 0
66
67 def send_igmp_join(self, igmpState, iface = 'veth0'):
68 src_list = ['1.2.3.4']
69 for g in igmpState.groups:
70 igmp = IGMP(mtype = IGMPV3_REPORT,
71 group = g,
72 rtype = IGMP_EXCLUDE,
73 src_list = src_list)
74 sendp(igmp.scapify(), iface = iface)
75
76 @deferred(timeout=10)
77 def test_igmp_sendrecv(self):
78 groups = ['224.0.1.1', '225.0.0.1']
79 def igmp_srp_task(stateObject):
80 if stateObject.state == 0:
81 result = self.igmp_send(stateObject)
82 else:
83 result = self.igmp_recv(stateObject)
84
85 if stateObject.counter < 20:
86 reactor.callLater(0, igmp_srp_task, stateObject)
87 else:
88 self.igmp_verify(stateObject)
89 stateObject.df.callback(0)
90
91 df = defer.Deferred()
92 igmpState = IGMPTestState(groups = groups, df = df)
93 self.send_igmp_join(igmpState)
94 reactor.callLater(0, igmp_srp_task, igmpState)
95 return df
96