Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 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 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.StringUtils; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 21 | import org.junit.BeforeClass; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Data; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 24 | import org.onlab.packet.DeserializationException; |
| 25 | import org.onlab.packet.Deserializer; |
| 26 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 27 | import java.util.Arrays; |
| 28 | |
| 29 | import static org.hamcrest.Matchers.is; |
| 30 | import static org.junit.Assert.assertArrayEquals; |
| 31 | import static org.junit.Assert.assertFalse; |
| 32 | import static org.junit.Assert.assertThat; |
| 33 | import static org.junit.Assert.assertTrue; |
| 34 | |
| 35 | /** |
| 36 | * Tests for class {@link EncapSecurityPayload}. |
| 37 | */ |
| 38 | public class EncapSecurityPayloadTest { |
| 39 | private static Data data; |
| 40 | private static byte[] dataByte = new byte[32]; |
| 41 | private static byte[] bytePacket; |
| 42 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 43 | private Deserializer<EncapSecurityPayload> 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 | Arrays.fill(dataByte, (byte) 0xff); |
| 48 | data = new Data().setData(dataByte); |
| 49 | |
| 50 | byte[] bytePayload = data.serialize(); |
| 51 | byte[] byteHeader = { |
| 52 | (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68, |
| 53 | (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00 |
| 54 | }; |
| 55 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 56 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 57 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 58 | } |
| 59 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 60 | @Before |
| 61 | public void setUp() { |
| 62 | deserializer = EncapSecurityPayload.deserializer(); |
| 63 | } |
| 64 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 65 | /** |
| 66 | * Tests serialize and setters. |
| 67 | */ |
| 68 | @Test |
| 69 | public void testSerialize() { |
| 70 | EncapSecurityPayload esp = new EncapSecurityPayload(); |
| 71 | esp.setSecurityParamIndex(0x13572468); |
| 72 | esp.setSequence(0xffff00); |
| 73 | esp.setPayload(data); |
| 74 | |
| 75 | assertArrayEquals(esp.serialize(), bytePacket); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests deserialize and getters. |
| 80 | */ |
| 81 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 82 | public void testDeserialize() throws DeserializationException { |
| 83 | EncapSecurityPayload esp = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 84 | |
| 85 | assertThat(esp.getSecurityParamIndex(), is(0x13572468)); |
| 86 | assertThat(esp.getSequence(), is(0xffff00)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Tests comparator. |
| 91 | */ |
| 92 | @Test |
| 93 | public void testEqual() { |
| 94 | EncapSecurityPayload esp1 = new EncapSecurityPayload(); |
| 95 | esp1.setSecurityParamIndex(0x13572468); |
| 96 | esp1.setSequence(0xffff00); |
| 97 | |
| 98 | EncapSecurityPayload esp2 = new EncapSecurityPayload(); |
| 99 | esp2.setSecurityParamIndex(0x13572468); |
| 100 | esp2.setSequence(0xfffff0); |
| 101 | |
| 102 | assertTrue(esp1.equals(esp1)); |
| 103 | assertFalse(esp1.equals(esp2)); |
| 104 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Tests toString. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testToStringESP() throws Exception { |
| 111 | EncapSecurityPayload esp = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
| 112 | String str = esp.toString(); |
| 113 | |
| 114 | assertTrue(StringUtils.contains(str, "securityParamIndex=" + 0x13572468)); |
| 115 | assertTrue(StringUtils.contains(str, "sequence=" + 0xffff00)); |
| 116 | } |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 117 | } |