blob: 8467a7804f62cbc46dc5251a3fe63cff9bd3f0ae [file] [log] [blame]
A R Karthick76a497a2017-04-12 10:59:39 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# 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
A R Karthick76a497a2017-04-12 10:59:39 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick76a497a2017-04-12 10:59:39 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# 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):
A R Karthick76a497a2017-04-12 10:59:39 -070057 print('Inside EAP Setup')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080058 self.setup()
59 self.nextEvent = self.md5EventTable.EVT_EAP_START
A R Karthick76a497a2017-04-12 10:59:39 -070060
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080061 def _eapStart(self):
A R Karthick76a497a2017-04-12 10:59:39 -070062 print('Inside EAP Start')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080063 self.eapol_start()
64 self.nextEvent = self.md5EventTable.EVT_EAP_ID_REQ
65
66 def _eapIdReq(self):
A R Karthick76a497a2017-04-12 10:59:39 -070067 print('Inside EAP ID Req')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080068 p = self.eapol_recv()
69 code, pkt_id, eaplen = unpack("!BBH", p[0:4])
A R Karthick76a497a2017-04-12 10:59:39 -070070 print("Code %d, id %d, len %d" %(code, pkt_id, eaplen))
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080071 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)
A R Karthick76a497a2017-04-12 10:59:39 -070075 print("<====== Send EAP Response with identity = %s ================>" % USER)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080076 self.eapol_id_req(pkt_id, USER)
77 self.nextEvent = self.md5EventTable.EVT_EAP_MD5_CHALLENGE
78
79 def _eapMd5Challenge(self):
A R Karthick76a497a2017-04-12 10:59:39 -070080 print('Inside EAP MD5 Challenge Exchange')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080081 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)
A R Karthick76a497a2017-04-12 10:59:39 -070085 print("Generated MD5 challenge is %s Length : %d" % (resp,length))
86 print("--> Send EAP response with MD5 challenge")
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080087 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):
A R Karthick76a497a2017-04-12 10:59:39 -070092 print('Inside EAP Status -- Sucess/Failure')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080093 if self.req_status == "EAP_SUCCESS":
94 status=self.eap_Status()
A R Karthick76a497a2017-04-12 10:59:39 -070095 print("<============EAP code received is = %d ====================>" % status)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080096 assert_equal(status, EAP_SUCCESS)
A R Karthick76a497a2017-04-12 10:59:39 -070097 print("Received EAP SUCCESS")
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080098 else:
A R Karthick76a497a2017-04-12 10:59:39 -070099 print('Inside EAP Status -- Sucess/Failure ===> SUCCESS should not be received , Since Negative Testcase')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800100 self.s.settimeout(10)
101 assert_equal(self.s.gettimeout(), 10)
A R Karthick76a497a2017-04-12 10:59:39 -0700102 print("Check if the socket timed out ====> Since negative testcase socket should timeout because ONOS is not sending the EAP FAILURE Message")
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800103 assert_raises(socket.error, self.s.recv, 1024)
104 self.nextEvent = self.md5EventTable.EVT_EAP_MD5_DONE
105
106 def _wrong_password(self):
A R Karthick76a497a2017-04-12 10:59:39 -0700107 print('Start Testcase for EAP-MD5 Wrong Password')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800108 #self._eap_md5_states()
109 self.__init__(intf = 'veth0', password = "wrong_password", required_status = "EAP_FAILURE")