blob: e0e99191a85e3a1d3e3f32aec4d4d5161ec32458 [file] [log] [blame]
Charles M.C. Chan94f37372015-01-10 17:53:42 +08001/*
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
17package org.onlab.packet.ipv6;
18
Jonathan Hart2a655752015-04-07 16:46:33 -070019import org.junit.Before;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080020import org.junit.BeforeClass;
21import org.junit.Test;
22import org.onlab.packet.Data;
Jonathan Hart2a655752015-04-07 16:46:33 -070023import org.onlab.packet.DeserializationException;
24import org.onlab.packet.Deserializer;
25
Charles M.C. Chan94f37372015-01-10 17:53:42 +080026import java.util.Arrays;
27
28import static org.hamcrest.Matchers.is;
29import static org.junit.Assert.assertArrayEquals;
30import static org.junit.Assert.assertFalse;
31import static org.junit.Assert.assertThat;
32import static org.junit.Assert.assertTrue;
33
34/**
35 * Tests for class {@link EncapSecurityPayload}.
36 */
37public class EncapSecurityPayloadTest {
38 private static Data data;
39 private static byte[] dataByte = new byte[32];
40 private static byte[] bytePacket;
41
Jonathan Hart2a655752015-04-07 16:46:33 -070042 private Deserializer<EncapSecurityPayload> deserializer;
43
Charles M.C. Chan94f37372015-01-10 17:53:42 +080044 @BeforeClass
45 public static void setUpBeforeClass() throws Exception {
46 Arrays.fill(dataByte, (byte) 0xff);
47 data = new Data().setData(dataByte);
48
49 byte[] bytePayload = data.serialize();
50 byte[] byteHeader = {
51 (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68,
52 (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00
53 };
54 bytePacket = new byte[byteHeader.length + bytePayload.length];
55 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
56 System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
57 }
58
Jonathan Hart2a655752015-04-07 16:46:33 -070059 @Before
60 public void setUp() {
61 deserializer = EncapSecurityPayload.deserializer();
62 }
63
Charles M.C. Chan94f37372015-01-10 17:53:42 +080064 /**
65 * Tests serialize and setters.
66 */
67 @Test
68 public void testSerialize() {
69 EncapSecurityPayload esp = new EncapSecurityPayload();
70 esp.setSecurityParamIndex(0x13572468);
71 esp.setSequence(0xffff00);
72 esp.setPayload(data);
73
74 assertArrayEquals(esp.serialize(), bytePacket);
75 }
76
77 /**
78 * Tests deserialize and getters.
79 */
80 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070081 public void testDeserialize() throws DeserializationException {
82 EncapSecurityPayload esp = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan94f37372015-01-10 17:53:42 +080083
84 assertThat(esp.getSecurityParamIndex(), is(0x13572468));
85 assertThat(esp.getSequence(), is(0xffff00));
86 }
87
88 /**
89 * Tests comparator.
90 */
91 @Test
92 public void testEqual() {
93 EncapSecurityPayload esp1 = new EncapSecurityPayload();
94 esp1.setSecurityParamIndex(0x13572468);
95 esp1.setSequence(0xffff00);
96
97 EncapSecurityPayload esp2 = new EncapSecurityPayload();
98 esp2.setSecurityParamIndex(0x13572468);
99 esp2.setSequence(0xfffff0);
100
101 assertTrue(esp1.equals(esp1));
102 assertFalse(esp1.equals(esp2));
103 }
104}