Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 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 | |
| 17 | package org.onlab.packet.ipv6; |
| 18 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 19 | import org.junit.Before; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 20 | import org.junit.BeforeClass; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.Data; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Deserializer; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 24 | import org.onlab.packet.UDP; |
| 25 | |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.junit.Assert.assertArrayEquals; |
| 28 | import static org.junit.Assert.assertFalse; |
| 29 | import static org.junit.Assert.assertThat; |
| 30 | import static org.junit.Assert.assertTrue; |
| 31 | |
| 32 | /** |
| 33 | * Tests for class {@link Authentication}. |
| 34 | */ |
| 35 | public class AuthenticationTest { |
| 36 | private static Data data; |
| 37 | private static UDP udp; |
| 38 | private static byte[] icv = { |
| 39 | (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 |
| 40 | }; |
| 41 | private static byte[] bytePacket; |
| 42 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 43 | private Deserializer<Authentication> deserializer; |
| 44 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 45 | @BeforeClass |
| 46 | public static void setUpBeforeClass() throws Exception { |
| 47 | data = new Data(); |
| 48 | data.setData("testSerialize".getBytes()); |
| 49 | udp = new UDP(); |
| 50 | udp.setPayload(data); |
| 51 | |
| 52 | byte[] bytePayload = udp.serialize(); |
| 53 | byte[] byteHeader = { |
| 54 | (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x00, |
| 55 | (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68, |
| 56 | (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00, |
| 57 | (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 |
| 58 | }; |
| 59 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 60 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 61 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 62 | } |
| 63 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 64 | @Before |
| 65 | public void setUp() { |
| 66 | deserializer = Authentication.deserializer(); |
| 67 | } |
| 68 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 69 | /** |
| 70 | * Tests serialize and setters. |
| 71 | */ |
| 72 | @Test |
| 73 | public void testSerialize() { |
| 74 | Authentication auth = new Authentication(); |
| 75 | auth.setNextHeader((byte) 0x11); |
| 76 | auth.setPayloadLength((byte) 0x02); |
| 77 | auth.setSecurityParamIndex(0x13572468); |
| 78 | auth.setSequence(0xffff00); |
| 79 | auth.setIngegrityCheck(icv); |
| 80 | auth.setPayload(udp); |
| 81 | |
| 82 | assertArrayEquals(auth.serialize(), bytePacket); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Tests deserialize and getters. |
| 87 | */ |
| 88 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 89 | public void testDeserialize() throws Exception { |
| 90 | Authentication auth = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 91 | |
| 92 | assertThat(auth.getNextHeader(), is((byte) 0x11)); |
| 93 | assertThat(auth.getPayloadLength(), is((byte) 0x02)); |
| 94 | assertThat(auth.getSecurityParamIndex(), is(0x13572468)); |
| 95 | assertThat(auth.getSequence(), is(0xffff00)); |
| 96 | assertArrayEquals(auth.getIntegrityCheck(), icv); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests comparator. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testEqual() { |
| 104 | Authentication auth1 = new Authentication(); |
| 105 | auth1.setNextHeader((byte) 0x11); |
| 106 | auth1.setPayloadLength((byte) 0x02); |
| 107 | auth1.setSecurityParamIndex(0x13572468); |
| 108 | auth1.setSequence(0xffff00); |
| 109 | auth1.setIngegrityCheck(icv); |
| 110 | |
| 111 | Authentication auth2 = new Authentication(); |
| 112 | auth2.setNextHeader((byte) 0x11); |
| 113 | auth2.setPayloadLength((byte) 0x02); |
| 114 | auth2.setSecurityParamIndex(0x13572467); |
| 115 | auth2.setSequence(0xffff00); |
| 116 | auth2.setIngegrityCheck(icv); |
| 117 | |
| 118 | assertTrue(auth1.equals(auth1)); |
| 119 | assertFalse(auth1.equals(auth2)); |
| 120 | } |
| 121 | } |