Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | package org.onosproject.lisp.msg.authentication; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | import static org.hamcrest.Matchers.is; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 23 | import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.NONE; |
| 24 | import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.SHA1; |
| 25 | import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.SHA256; |
| 26 | import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.UNKNOWN; |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Test case for LISP authentication. |
| 30 | */ |
| 31 | public class LispAuthenticationTest { |
| 32 | |
| 33 | private LispAuthenticationFactory factory; |
| 34 | |
| 35 | @Before |
| 36 | public void setup() { |
| 37 | factory = LispAuthenticationFactory.getInstance(); |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | public void testAuthData() { |
| 42 | |
| 43 | String authKey = "testKey"; |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 44 | byte[] noneAuthData = factory.createAuthenticationData(NONE, authKey, new byte[0]); |
| 45 | byte[] unknownAuthData = factory.createAuthenticationData(UNKNOWN, authKey, new byte[0]); |
| 46 | byte[] sha1AuthData = factory.createAuthenticationData(SHA1, authKey, new byte[0]); |
| 47 | byte[] sha256AuthData = factory.createAuthenticationData(SHA256, authKey, new byte[0]); |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 48 | |
| 49 | assertThat(noneAuthData, is(new byte[0])); |
| 50 | assertThat(unknownAuthData, is(new byte[0])); |
| 51 | assertThat(sha1AuthData.length, is(20)); |
| 52 | assertThat(sha256AuthData.length, is(32)); |
| 53 | } |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 54 | |
| 55 | @Test(expected = IllegalArgumentException.class) |
| 56 | public void testInvalidAuthType() { |
| 57 | LispAuthenticationKeyEnum authType = LispAuthenticationKeyEnum.valueOf((short) 0); |
| 58 | LispMacAuthentication macAuth = new LispMacAuthentication(authType); |
| 59 | |
| 60 | macAuth.getAuthenticationData("onos", new byte[0]); |
| 61 | } |
| 62 | |
| 63 | @Test(expected = NullPointerException.class) |
| 64 | public void testNullAuthKey() { |
| 65 | LispAuthenticationKeyEnum authType = LispAuthenticationKeyEnum.valueOf((short) 1); |
| 66 | |
| 67 | LispMacAuthentication macAuth = new LispMacAuthentication(authType); |
| 68 | macAuth.getAuthenticationData(null, new byte[0]); |
| 69 | } |
| 70 | |
| 71 | @Test(expected = IllegalArgumentException.class) |
| 72 | public void testInvalidAuthKey() { |
| 73 | LispAuthenticationKeyEnum authType = LispAuthenticationKeyEnum.valueOf((short) 1); |
| 74 | |
| 75 | LispMacAuthentication macAuth = new LispMacAuthentication(authType); |
| 76 | macAuth.getAuthenticationData("", new byte[0]); |
| 77 | } |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 78 | } |