blob: be71b185582598f7770ca6961893cc442b8db873 [file] [log] [blame]
Chetan Gaonkercfcce782016-05-10 10:10:42 -07001#
2# Copyright 2016-present Ciena Corporation
3#
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#
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080016import sys, os
17from EapolAAA import *
18from enum import *
19import noseMd5AuthHolder as md5AuthHolder
20from socket import *
21from struct import *
22from md5 import md5
23from scapy.all import *
24from nose.tools import *
25from CordTestBase import CordTester
26
27class MD5AuthTest(EapolPacket, CordTester):
28
29 md5StateTable = Enumeration("MD5StateTable", ("ST_EAP_SETUP",
30 "ST_EAP_START",
31 "ST_EAP_ID_REQ",
32 "ST_EAP_MD5_CHALLENGE",
33 "ST_EAP_STATUS",
34 "ST_EAP_MD5_DONE"
35 )
36 )
37 md5EventTable = Enumeration("MD5EventTable", ("EVT_EAP_SETUP",
38 "EVT_EAP_START",
39 "EVT_EAP_ID_REQ",
40 "EVT_EAP_MD5_CHALLENGE",
41 "EVT_EAP_STATUS",
42 "EVT_EAP_MD5_DONE"
43 )
44 )
45 def __init__(self, intf = 'veth0', password = "password", required_status = "EAP_SUCCESS"):
46 self.passwd = password
47 self.req_status = required_status
48 self.fsmTable = md5AuthHolder.initMd5AuthHolderFsmTable(self, self.md5StateTable, self.md5EventTable)
49 EapolPacket.__init__(self, intf)
50 CordTester.__init__(self, self.fsmTable, self.md5StateTable.ST_EAP_MD5_DONE)
51 self.currentState = self.md5StateTable.ST_EAP_SETUP
52 self.currentEvent = self.md5EventTable.EVT_EAP_SETUP
53 self.nextState = None
54 self.nextEvent = None
55
56 def _eapSetup(self):
57 print 'Inside EAP Setup'
58 self.setup()
59 self.nextEvent = self.md5EventTable.EVT_EAP_START
60
61 def _eapStart(self):
62 print 'Inside EAP Start'
63 self.eapol_start()
64 self.nextEvent = self.md5EventTable.EVT_EAP_ID_REQ
65
66 def _eapIdReq(self):
67 print 'Inside EAP ID Req'
68 p = self.eapol_recv()
69 code, pkt_id, eaplen = unpack("!BBH", p[0:4])
70 print "Code %d, id %d, len %d" %(code, pkt_id, eaplen)
71 assert_equal(code, EAP_REQUEST)
72 reqtype = unpack("!B", p[4:5])[0]
73 reqdata = p[5:4+eaplen]
74 assert_equal(reqtype, EAP_TYPE_ID)
75 print "<====== Send EAP Response with identity = %s ================>" % USER
76 self.eapol_id_req(pkt_id, USER)
77 self.nextEvent = self.md5EventTable.EVT_EAP_MD5_CHALLENGE
78
79 def _eapMd5Challenge(self):
80 print 'Inside EAP MD5 Challenge Exchange'
81 challenge,pkt_id =self.eap_md5_challenge_recv(self.passwd)
82 resp=md5(challenge).digest()
83 resp=chr(len(resp))+resp
84 length= 5+len(resp)
85 print "Generated MD5 challenge is %s Length : %d" % (resp,length)
86 print "--> Send EAP response with MD5 challenge"
87 eap_payload = self.eap(EAP_RESPONSE, pkt_id, EAP_TYPE_MD5, str(resp))
88 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
89 self.nextEvent = self.md5EventTable.EVT_EAP_STATUS
90
91 def _eapStatus(self):
92 print 'Inside EAP Status -- Sucess/Failure'
93 if self.req_status == "EAP_SUCCESS":
94 status=self.eap_Status()
95 print "<============EAP code received is = %d ====================>" % status
96 assert_equal(status, EAP_SUCCESS)
97 print"Received EAP SUCCESS"
98 else:
99 print 'Inside EAP Status -- Sucess/Failure ===> SUCCESS should not be received , Since Negative Testcase'
100 self.s.settimeout(10)
101 assert_equal(self.s.gettimeout(), 10)
102 print "Check if the socket timed out ====> Since negative testcase socket should timeout because ONOS is not sending the EAP FAILURE Message"
103 assert_raises(socket.error, self.s.recv, 1024)
104 self.nextEvent = self.md5EventTable.EVT_EAP_MD5_DONE
105
106 def _wrong_password(self):
107 print 'Start Testcase for EAP-MD5 Wrong Password'
108 #self._eap_md5_states()
109 self.__init__(intf = 'veth0', password = "wrong_password", required_status = "EAP_FAILURE")
110
111