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 | |
| 19 | import org.junit.BeforeClass; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.Data; |
| 22 | import java.util.Arrays; |
| 23 | |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | import static org.junit.Assert.assertArrayEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
| 27 | import static org.junit.Assert.assertThat; |
| 28 | import static org.junit.Assert.assertTrue; |
| 29 | |
| 30 | /** |
| 31 | * Tests for class {@link EncapSecurityPayload}. |
| 32 | */ |
| 33 | public class EncapSecurityPayloadTest { |
| 34 | private static Data data; |
| 35 | private static byte[] dataByte = new byte[32]; |
| 36 | private static byte[] bytePacket; |
| 37 | |
| 38 | @BeforeClass |
| 39 | public static void setUpBeforeClass() throws Exception { |
| 40 | Arrays.fill(dataByte, (byte) 0xff); |
| 41 | data = new Data().setData(dataByte); |
| 42 | |
| 43 | byte[] bytePayload = data.serialize(); |
| 44 | byte[] byteHeader = { |
| 45 | (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68, |
| 46 | (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00 |
| 47 | }; |
| 48 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 49 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 50 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Tests serialize and setters. |
| 55 | */ |
| 56 | @Test |
| 57 | public void testSerialize() { |
| 58 | EncapSecurityPayload esp = new EncapSecurityPayload(); |
| 59 | esp.setSecurityParamIndex(0x13572468); |
| 60 | esp.setSequence(0xffff00); |
| 61 | esp.setPayload(data); |
| 62 | |
| 63 | assertArrayEquals(esp.serialize(), bytePacket); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Tests deserialize and getters. |
| 68 | */ |
| 69 | @Test |
| 70 | public void testDeserialize() { |
| 71 | EncapSecurityPayload esp = new EncapSecurityPayload(); |
| 72 | esp.deserialize(bytePacket, 0, bytePacket.length); |
| 73 | |
| 74 | assertThat(esp.getSecurityParamIndex(), is(0x13572468)); |
| 75 | assertThat(esp.getSequence(), is(0xffff00)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests comparator. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testEqual() { |
| 83 | EncapSecurityPayload esp1 = new EncapSecurityPayload(); |
| 84 | esp1.setSecurityParamIndex(0x13572468); |
| 85 | esp1.setSequence(0xffff00); |
| 86 | |
| 87 | EncapSecurityPayload esp2 = new EncapSecurityPayload(); |
| 88 | esp2.setSecurityParamIndex(0x13572468); |
| 89 | esp2.setSequence(0xfffff0); |
| 90 | |
| 91 | assertTrue(esp1.equals(esp1)); |
| 92 | assertFalse(esp1.equals(esp2)); |
| 93 | } |
| 94 | } |