Merge branch 'master' of https://github.cyanoptics.com/cord-lab/cord-tester into test
diff --git a/src/test/builder/noseDnsTest.yaml b/src/test/builder/noseDnsTest.yaml
new file mode 100644
index 0000000..0b85b4e
--- /dev/null
+++ b/src/test/builder/noseDnsTest.yaml
@@ -0,0 +1,9 @@
+States:
+    ST_DNS_SND_REC:
+        Events:
+            EVT_DNS_SND_REC:
+                Actions:
+                    - _dns_snd_rec 
+                NextState: ST_DNS_FINAL
+        
+
diff --git a/src/test/dhcp/__init__.py b/src/test/dhcp/__init__.py
new file mode 100644
index 0000000..a881eb6
--- /dev/null
+++ b/src/test/dhcp/__init__.py
@@ -0,0 +1,7 @@
+import os,sys
+##add the python path to lookup the utils
+working_dir = os.path.dirname(os.path.realpath(sys.argv[-1]))
+utils_dir = os.path.join(working_dir, '../utils')
+fsm_dir = os.path.join(working_dir, '../fsm')
+__path__.append(utils_dir)
+__path__.append(fsm_dir)
diff --git a/src/test/dns/dnsTest.py b/src/test/dns/dnsTest.py
new file mode 100644
index 0000000..ee3d321
--- /dev/null
+++ b/src/test/dns/dnsTest.py
@@ -0,0 +1,50 @@
+import unittest
+import os,sys
+CORD_TEST_UTILS = 'utils'
+test_root = os.getenv('CORD_TEST_ROOT') or './'
+sys.path.append(test_root + CORD_TEST_UTILS)
+from Dns_new import DNSTest
+class dns_exchange(unittest.TestCase):
+     
+      def test_dns_ptr_query(self):
+          obj=DNSTest()
+          t = obj._dns_snd_ptr()
+
+      def test_dns_A(self):
+          obj=DNSTest()
+          t = obj._dns_snd_rec()
+      def test_dns_invalid_url(self):
+          obj=DNSTest()
+          t = obj._dns_snd_inv()
+
+      def test_dns_invalid_reverse_query(self):
+          obj=DNSTest()
+          t = obj._dns_snd_ptr_inv()
+      def test_dns_AAAA(self):
+          obj=DNSTest()
+          t = obj._dns_snd_AAAA()
+
+      def test_dns_CNAME(self):
+          obj=DNSTest()
+          t = obj._dns_snd_CNAME()
+
+      def test_dns_ptr_query(self):
+          obj=DNSTest()
+          t = obj._dns_snd_ptr()
+
+
+
+
+if __name__ == '__main__':
+          t = DNSTest()
+          t._dns_snd_ptr()
+          t._dns_snd_rec()
+          t._dns_snd_inv()
+          t._dns_snd_ptr_inv()
+          t._dns_snd_AAAA()
+          t._dns_snd_CNAME()
+          t._dns_snd_ptr()
+
+
+
+
diff --git a/src/test/fsm/noseDnsHolder.py b/src/test/fsm/noseDnsHolder.py
new file mode 100644
index 0000000..cde1a44
--- /dev/null
+++ b/src/test/fsm/noseDnsHolder.py
@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+
+def initDnsHolderFsmTable(obj,St,Ev):
+    return {
+
+    ## CurrentState                Event                            Actions               NextState
+
+      (St.ST_DNS_SND_REC,          Ev.EVT_DNS_SND_REC           ):( (obj._dns_snd_rec,),  St.ST_DNS_FINAL),
+
+}
+
diff --git a/src/test/igmp/__init__.py b/src/test/igmp/__init__.py
new file mode 100644
index 0000000..900be31
--- /dev/null
+++ b/src/test/igmp/__init__.py
@@ -0,0 +1,5 @@
+import os,sys
+##add the python path to lookup the utils
+working_dir = os.path.dirname(os.path.realpath(sys.argv[-1]))
+utils_dir = os.path.join(working_dir, '../utils')
+__path__.append(utils_dir)
diff --git a/src/test/md5/__init__.py b/src/test/md5/__init__.py
new file mode 100644
index 0000000..a881eb6
--- /dev/null
+++ b/src/test/md5/__init__.py
@@ -0,0 +1,7 @@
+import os,sys
+##add the python path to lookup the utils
+working_dir = os.path.dirname(os.path.realpath(sys.argv[-1]))
+utils_dir = os.path.join(working_dir, '../utils')
+fsm_dir = os.path.join(working_dir, '../fsm')
+__path__.append(utils_dir)
+__path__.append(fsm_dir)
diff --git a/src/test/pap/__init__.py b/src/test/pap/__init__.py
new file mode 100644
index 0000000..a881eb6
--- /dev/null
+++ b/src/test/pap/__init__.py
@@ -0,0 +1,7 @@
+import os,sys
+##add the python path to lookup the utils
+working_dir = os.path.dirname(os.path.realpath(sys.argv[-1]))
+utils_dir = os.path.join(working_dir, '../utils')
+fsm_dir = os.path.join(working_dir, '../fsm')
+__path__.append(utils_dir)
+__path__.append(fsm_dir)
diff --git a/src/test/tls/__init__.py b/src/test/tls/__init__.py
new file mode 100644
index 0000000..a881eb6
--- /dev/null
+++ b/src/test/tls/__init__.py
@@ -0,0 +1,7 @@
+import os,sys
+##add the python path to lookup the utils
+working_dir = os.path.dirname(os.path.realpath(sys.argv[-1]))
+utils_dir = os.path.join(working_dir, '../utils')
+fsm_dir = os.path.join(working_dir, '../fsm')
+__path__.append(utils_dir)
+__path__.append(fsm_dir)
diff --git a/src/test/utils/Channels.py b/src/test/utils/Channels.py
new file mode 100644
index 0000000..90d4f3c
--- /dev/null
+++ b/src/test/utils/Channels.py
@@ -0,0 +1,221 @@
+import threading
+import sys
+import os
+import time
+import monotonic
+import random
+from scapy.all import *
+from McastTraffic import *
+from IGMP import *
+from OnosCtrl import OnosCtrl
+from nose.tools import *
+log.setLevel('DEBUG')
+
+conf.verb = 0
+
+class IgmpChannel:
+
+    IGMP_DST_MAC = "01:00:5e:00:01:01"
+    IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1"
+    IP_SRC = '1.2.3.4'
+    IP_DST = '224.0.1.1'
+    igmp_eth = Ether(dst = IGMP_DST_MAC, src = IGMP_SRC_MAC, type = ETH_P_IP)
+    igmp_ip = IP(dst = IP_DST, src = IP_SRC)
+
+    def __init__(self, iface = 'veth0', src_list = ['1.2.3.4'], delay = 2):
+        self.iface = iface
+        self.src_list = src_list
+        self.delay = delay
+        self.onos_ctrl = OnosCtrl('org.onosproject.igmp')
+        self.onos_ctrl.activate()
+
+    def igmp_join(self, groups):
+        self.ssm_table_load(groups)
+        igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
+                      gaddr='224.0.1.1')
+        for g in groups:
+              gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
+              gr.sources = self.src_list
+              igmp.grps.append(gr)
+
+        pkt = self.igmp_eth/self.igmp_ip/igmp
+        IGMPv3.fixup(pkt)
+        sendp(pkt, iface=self.iface)
+        if self.delay != 0:
+            time.sleep(self.delay)
+
+    def igmp_leave(self, groups):
+        igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
+                      gaddr='224.0.1.1')
+        for g in groups:
+              gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g)
+              gr.sources = self.src_list
+              igmp.grps.append(gr)
+
+        pkt = self.igmp_eth/self.igmp_ip/igmp
+        IGMPv3.fixup(pkt)
+        sendp(pkt, iface = self.iface)
+        if self.delay != 0:
+            time.sleep(self.delay)
+
+    def onos_load_config(self, config):
+        status, code = self.onos_ctrl.config(config)
+        if status is False:
+            log.info('JSON config request for app %s returned status %d' %code)
+        time.sleep(2)
+
+    def ssm_table_load(self, groups):
+          ssm_dict = {'apps' : { 'org.onosproject.igmp' : { 'ssmTranslate' : [] } } }
+          ssm_xlate_list = ssm_dict['apps']['org.onosproject.igmp']['ssmTranslate']
+          for g in groups:
+                for s in self.src_list:
+                      d = {}
+                      d['source'] = s
+                      d['group'] = g
+                      ssm_xlate_list.append(d)
+          self.onos_load_config(ssm_dict)
+
+class Channels(IgmpChannel):
+    Stopped = 0
+    Started = 1
+    Idle = 0
+    Joined = 1
+    def __init__(self, num, iface = 'veth0', iface_mcast = 'veth2', mcast_cb = None):
+        self.num = num
+        self.channels = self.generate(self.num)
+        self.state = self.Stopped
+        self.streams = None
+        self.channel_states = {}
+        self.last_chan = None
+        self.recv_sock = L2Socket(iface = iface, type = ETH_P_IP)
+        self.iface_mcast = iface_mcast
+        self.mcast_cb = mcast_cb
+        for c in range(self.num):
+            self.channel_states[c] = [self.Idle]
+
+        IgmpChannel.__init__(self, iface=iface)
+
+    def generate(self, num):
+        start = (224 << 24) | 1
+        end = start + num + num/256 
+        group_addrs = []
+        for i in range(start, end):
+            if i&255:
+                g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
+                log.debug('Adding group %s' %g)
+                group_addrs.append(g)
+        return group_addrs
+
+    def start(self):
+        if self.state == self.Stopped:
+            if self.streams:
+                self.streams.stop()
+            self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
+            self.streams.start()
+            self.state = self.Started
+
+    def join(self, chan = None):
+        if chan is None:
+            chan = random.randint(0, self.num)
+        else:
+            if chan >= self.num:
+                chan = 0
+
+        if self.get_state(chan) == self.Joined:
+            return chan
+
+        groups = [self.channels[chan]]
+        self.igmp_join(groups)
+        self.set_state(chan, self.Joined)
+        self.last_chan = chan
+        return chan
+
+    def leave(self, chan):
+        if chan is None:
+            chan = self.last_chan
+        if chan is None or chan >= self.num:
+            return False
+        if self.get_state(chan) != self.Joined:
+            return False
+        groups = [self.channels[chan]]
+        self.igmp_leave(groups)
+        self.set_state(chan, self.Idle)
+        if chan == self.last_chan:
+            self.last_chan = None
+        return True
+    
+    def join_next(self, chan = None):
+        if chan is None:
+            chan = self.last_chan
+            if chan is None:
+                return None
+            leave = chan
+            join = chan+1
+        else:
+            leave = chan - 1
+            join = chan
+        
+        if join >= self.num:
+            join = 0
+
+        if leave >= 0 and leave != join:
+            self.leave(leave)
+
+        return self.join(join)
+
+    def jump(self):
+        chan = self.last_chan
+        if chan is not None:
+            self.leave(chan)
+            s_next = chan
+        else:
+            s_next = 0
+        chan = random.randint(s_next, self.num)
+        return self.join(chan)
+
+    def gaddr(self, chan):
+        if chan >= self.num:
+            return None
+        return self.channels[chan]
+
+    def recv_cb(self, pkt):
+        '''Default channel receive callback'''
+        log.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
+        send_time = float(pkt[IP].payload.load)
+        recv_time = monotonic.monotonic()
+        log.debug('Packet received in %.3f usecs' %(recv_time - send_time))
+
+    def recv(self, chan, cb = None, count = 1):
+        if chan is None:
+            return None
+        if type(chan) == type([]) or type(chan) == type(()):
+            channel_list=filter(lambda c: c < self.num, chan)
+            groups = map(lambda c: self.gaddr(c), channel_list)
+        else:
+            groups = (self.gaddr(chan),)
+        if cb is None:
+            cb = self.recv_cb
+        sniff(prn = cb, count=count, lfilter = lambda p: p[IP].dst in groups, opened_socket = self.recv_sock)
+
+    def stop(self):
+        if self.streams:
+            self.streams.stop()
+        self.state = self.Stopped
+
+    def get_state(self, chan):
+        return self.channel_states[chan][0]
+
+    def set_state(self, chan, state):
+        self.channel_states[chan][0] = state
+
+if __name__ == '__main__':
+    num = 2
+    channels = Channels(num)
+    channels.start()
+    for i in range(num):
+        channels.join(i)
+    for i in range(num):
+        channels.recv(i)
+    for i in range(num):
+        channels.leave(i)
+    channels.stop()
diff --git a/src/test/utils/EapMD5.py b/src/test/utils/EapMD5.py
new file mode 100644
index 0000000..61ca405
--- /dev/null
+++ b/src/test/utils/EapMD5.py
@@ -0,0 +1,96 @@
+import sys, os
+from EapolAAA import *
+from enum import *
+import noseMd5AuthHolder as md5AuthHolder
+from socket import *
+from struct import *
+from md5 import md5
+from scapy.all import *
+from nose.tools import *
+from CordTestBase import CordTester
+
+class MD5AuthTest(EapolPacket, CordTester):
+
+    md5StateTable = Enumeration("MD5StateTable", ("ST_EAP_SETUP",
+                                                  "ST_EAP_START",
+                                                  "ST_EAP_ID_REQ",
+                                                  "ST_EAP_MD5_CHALLENGE",
+                                                  "ST_EAP_STATUS",
+                                                  "ST_EAP_MD5_DONE"
+                                                  )
+                                )
+    md5EventTable = Enumeration("MD5EventTable", ("EVT_EAP_SETUP",
+                                                  "EVT_EAP_START",
+                                                  "EVT_EAP_ID_REQ",
+                                                  "EVT_EAP_MD5_CHALLENGE",
+                                                  "EVT_EAP_STATUS",
+                                                  "EVT_EAP_MD5_DONE"
+                                                  )
+                                )
+    def __init__(self, intf = 'veth0', password = "password", required_status = "EAP_SUCCESS"):
+        self.passwd = password
+        self.req_status = required_status
+        self.fsmTable = md5AuthHolder.initMd5AuthHolderFsmTable(self, self.md5StateTable, self.md5EventTable)
+        EapolPacket.__init__(self, intf)
+        CordTester.__init__(self, self.fsmTable, self.md5StateTable.ST_EAP_MD5_DONE)
+        self.currentState = self.md5StateTable.ST_EAP_SETUP
+        self.currentEvent = self.md5EventTable.EVT_EAP_SETUP
+        self.nextState = None
+        self.nextEvent = None
+
+    def _eapSetup(self):
+        print 'Inside EAP Setup'
+        self.setup()
+        self.nextEvent = self.md5EventTable.EVT_EAP_START
+        
+    def _eapStart(self):
+        print 'Inside EAP Start'
+        self.eapol_start()
+        self.nextEvent = self.md5EventTable.EVT_EAP_ID_REQ
+
+    def _eapIdReq(self):
+        print 'Inside EAP ID Req'
+        p = self.eapol_recv()
+        code, pkt_id, eaplen = unpack("!BBH", p[0:4])
+        print "Code %d, id %d, len %d" %(code, pkt_id, eaplen)
+        assert_equal(code, EAP_REQUEST)
+        reqtype = unpack("!B", p[4:5])[0]
+        reqdata = p[5:4+eaplen]
+        assert_equal(reqtype, EAP_TYPE_ID)
+        print "<====== Send EAP Response with identity = %s ================>" % USER
+        self.eapol_id_req(pkt_id, USER)
+        self.nextEvent = self.md5EventTable.EVT_EAP_MD5_CHALLENGE
+
+    def _eapMd5Challenge(self):
+        print 'Inside EAP MD5 Challenge Exchange'
+        challenge,pkt_id =self.eap_md5_challenge_recv(self.passwd)
+        resp=md5(challenge).digest()
+        resp=chr(len(resp))+resp
+        length= 5+len(resp)
+        print "Generated MD5 challenge is %s Length : %d" % (resp,length)
+        print "--> Send EAP response with MD5 challenge"
+        eap_payload = self.eap(EAP_RESPONSE, pkt_id, EAP_TYPE_MD5, str(resp))
+        self.eapol_send(EAPOL_EAPPACKET, eap_payload)
+        self.nextEvent = self.md5EventTable.EVT_EAP_STATUS
+
+    def _eapStatus(self):
+       print 'Inside EAP Status -- Sucess/Failure'
+       if self.req_status == "EAP_SUCCESS":
+         status=self.eap_Status()
+         print "<============EAP code received is = %d ====================>" % status
+         assert_equal(status, EAP_SUCCESS)
+         print"Received EAP SUCCESS"
+       else:
+         print 'Inside EAP Status -- Sucess/Failure ===> SUCCESS should not be received , Since Negative Testcase'
+         self.s.settimeout(10)
+         assert_equal(self.s.gettimeout(), 10)
+         print "Check if the socket timed out ====> Since negative testcase socket should timeout because ONOS is not sending the EAP FAILURE Message"
+         assert_raises(socket.error, self.s.recv, 1024)
+       self.nextEvent = self.md5EventTable.EVT_EAP_MD5_DONE
+
+    def _wrong_password(self):
+       print 'Start Testcase for EAP-MD5 Wrong Password'
+       #self._eap_md5_states()
+       self.__init__(intf = 'veth0', password = "wrong_password", required_status = "EAP_FAILURE")
+      
+
diff --git a/src/test/utils/__init__.py b/src/test/utils/__init__.py
new file mode 100644
index 0000000..6bbc949
--- /dev/null
+++ b/src/test/utils/__init__.py
@@ -0,0 +1,5 @@
+import os,sys
+##add the python path to lookup the utils
+working_dir = os.path.dirname(os.path.realpath(sys.argv[-1]))
+utils_dir = os.path.join(working_dir, '../fsm')
+__path__.append(utils_dir)