blob: d52278e69cf28d88846b583f2960b6c17227f04a [file] [log] [blame]
Charles M.C. Chan94f37372015-01-10 17:53:42 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Charles M.C. Chan94f37372015-01-10 17:53:42 +08003 *
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
Jian Li5fc14292015-12-04 11:30:46 -080019import org.apache.commons.lang3.StringUtils;
Jonathan Hart2a655752015-04-07 16:46:33 -070020import org.junit.Before;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080021import org.junit.BeforeClass;
22import org.junit.Test;
23import org.onlab.packet.Data;
Jonathan Hart2a655752015-04-07 16:46:33 -070024import org.onlab.packet.DeserializationException;
25import org.onlab.packet.Deserializer;
26
Charles M.C. Chan94f37372015-01-10 17:53:42 +080027import java.util.Arrays;
28
29import static org.hamcrest.Matchers.is;
30import static org.junit.Assert.assertArrayEquals;
31import static org.junit.Assert.assertFalse;
32import static org.junit.Assert.assertThat;
33import static org.junit.Assert.assertTrue;
34
35/**
36 * Tests for class {@link EncapSecurityPayload}.
37 */
38public class EncapSecurityPayloadTest {
39 private static Data data;
40 private static byte[] dataByte = new byte[32];
41 private static byte[] bytePacket;
42
Jonathan Hart2a655752015-04-07 16:46:33 -070043 private Deserializer<EncapSecurityPayload> deserializer;
44
Charles M.C. Chan94f37372015-01-10 17:53:42 +080045 @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 Hart2a655752015-04-07 16:46:33 -070060 @Before
61 public void setUp() {
62 deserializer = EncapSecurityPayload.deserializer();
63 }
64
Charles M.C. Chan94f37372015-01-10 17:53:42 +080065 /**
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 Hart2a655752015-04-07 16:46:33 -070082 public void testDeserialize() throws DeserializationException {
83 EncapSecurityPayload esp = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan94f37372015-01-10 17:53:42 +080084
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 Li5fc14292015-12-04 11:30:46 -0800105
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. Chan94f37372015-01-10 17:53:42 +0800117}