Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 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 | |
| 16 | |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 17 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 18 | # Copyright 2016-present Ciena Corporation |
| 19 | # |
| 20 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | # you may not use this file except in compliance with the License. |
| 22 | # You may obtain a copy of the License at |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 23 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 25 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 26 | # Unless required by applicable law or agreed to in writing, software |
| 27 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | # See the License for the specific language governing permissions and |
| 30 | # limitations under the License. |
| 31 | # |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 32 | import sys, os |
| 33 | from EapolAAA import * |
A R Karthick | 74d0031 | 2017-04-18 14:26:01 -0700 | [diff] [blame] | 34 | from Enum import * |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 35 | import noseMd5AuthHolder as md5AuthHolder |
| 36 | from socket import * |
| 37 | from struct import * |
| 38 | from md5 import md5 |
| 39 | from scapy.all import * |
| 40 | from nose.tools import * |
| 41 | from CordTestBase import CordTester |
| 42 | |
| 43 | class MD5AuthTest(EapolPacket, CordTester): |
| 44 | |
| 45 | md5StateTable = Enumeration("MD5StateTable", ("ST_EAP_SETUP", |
| 46 | "ST_EAP_START", |
| 47 | "ST_EAP_ID_REQ", |
| 48 | "ST_EAP_MD5_CHALLENGE", |
| 49 | "ST_EAP_STATUS", |
| 50 | "ST_EAP_MD5_DONE" |
| 51 | ) |
| 52 | ) |
| 53 | md5EventTable = Enumeration("MD5EventTable", ("EVT_EAP_SETUP", |
| 54 | "EVT_EAP_START", |
| 55 | "EVT_EAP_ID_REQ", |
| 56 | "EVT_EAP_MD5_CHALLENGE", |
| 57 | "EVT_EAP_STATUS", |
| 58 | "EVT_EAP_MD5_DONE" |
| 59 | ) |
| 60 | ) |
| 61 | def __init__(self, intf = 'veth0', password = "password", required_status = "EAP_SUCCESS"): |
| 62 | self.passwd = password |
| 63 | self.req_status = required_status |
| 64 | self.fsmTable = md5AuthHolder.initMd5AuthHolderFsmTable(self, self.md5StateTable, self.md5EventTable) |
| 65 | EapolPacket.__init__(self, intf) |
| 66 | CordTester.__init__(self, self.fsmTable, self.md5StateTable.ST_EAP_MD5_DONE) |
| 67 | self.currentState = self.md5StateTable.ST_EAP_SETUP |
| 68 | self.currentEvent = self.md5EventTable.EVT_EAP_SETUP |
| 69 | self.nextState = None |
| 70 | self.nextEvent = None |
| 71 | |
| 72 | def _eapSetup(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 73 | print('Inside EAP Setup') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 74 | self.setup() |
| 75 | self.nextEvent = self.md5EventTable.EVT_EAP_START |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 76 | |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 77 | def _eapStart(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 78 | print('Inside EAP Start') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 79 | self.eapol_start() |
| 80 | self.nextEvent = self.md5EventTable.EVT_EAP_ID_REQ |
| 81 | |
| 82 | def _eapIdReq(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 83 | print('Inside EAP ID Req') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 84 | p = self.eapol_recv() |
| 85 | code, pkt_id, eaplen = unpack("!BBH", p[0:4]) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 86 | print("Code %d, id %d, len %d" %(code, pkt_id, eaplen)) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 87 | assert_equal(code, EAP_REQUEST) |
| 88 | reqtype = unpack("!B", p[4:5])[0] |
| 89 | reqdata = p[5:4+eaplen] |
| 90 | assert_equal(reqtype, EAP_TYPE_ID) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 91 | print("<====== Send EAP Response with identity = %s ================>" % USER) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 92 | self.eapol_id_req(pkt_id, USER) |
| 93 | self.nextEvent = self.md5EventTable.EVT_EAP_MD5_CHALLENGE |
| 94 | |
| 95 | def _eapMd5Challenge(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 96 | print('Inside EAP MD5 Challenge Exchange') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 97 | challenge,pkt_id =self.eap_md5_challenge_recv(self.passwd) |
| 98 | resp=md5(challenge).digest() |
| 99 | resp=chr(len(resp))+resp |
| 100 | length= 5+len(resp) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 101 | print("Generated MD5 challenge is %s Length : %d" % (resp,length)) |
| 102 | print("--> Send EAP response with MD5 challenge") |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 103 | eap_payload = self.eap(EAP_RESPONSE, pkt_id, EAP_TYPE_MD5, str(resp)) |
| 104 | self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
| 105 | self.nextEvent = self.md5EventTable.EVT_EAP_STATUS |
| 106 | |
| 107 | def _eapStatus(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 108 | print('Inside EAP Status -- Sucess/Failure') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 109 | if self.req_status == "EAP_SUCCESS": |
| 110 | status=self.eap_Status() |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 111 | print("<============EAP code received is = %d ====================>" % status) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 112 | assert_equal(status, EAP_SUCCESS) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 113 | print("Received EAP SUCCESS") |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 114 | else: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 115 | print('Inside EAP Status -- Sucess/Failure ===> SUCCESS should not be received , Since Negative Testcase') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 116 | self.s.settimeout(10) |
| 117 | assert_equal(self.s.gettimeout(), 10) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 118 | print("Check if the socket timed out ====> Since negative testcase socket should timeout because ONOS is not sending the EAP FAILURE Message") |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 119 | assert_raises(socket.error, self.s.recv, 1024) |
| 120 | self.nextEvent = self.md5EventTable.EVT_EAP_MD5_DONE |
| 121 | |
| 122 | def _wrong_password(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 123 | print('Start Testcase for EAP-MD5 Wrong Password') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 124 | #self._eap_md5_states() |
| 125 | self.__init__(intf = 'veth0', password = "wrong_password", required_status = "EAP_FAILURE") |