blob: 5ca7ea478d07a99c349e03d9f31467603298b8b7 [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
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.Deserializer;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080025import org.onlab.packet.UDP;
26
Jian Li5fc14292015-12-04 11:30:46 -080027import java.util.Arrays;
28
Charles M.C. Chan94f37372015-01-10 17:53:42 +080029import static org.hamcrest.Matchers.is;
Jian Li5fc14292015-12-04 11:30:46 -080030import static org.junit.Assert.*;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080031
32/**
33 * Tests for class {@link Authentication}.
34 */
35public 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 Hart2a655752015-04-07 16:46:33 -070043 private Deserializer<Authentication> deserializer;
44
Charles M.C. Chan94f37372015-01-10 17:53:42 +080045 @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 Hart2a655752015-04-07 16:46:33 -070064 @Before
65 public void setUp() {
66 deserializer = Authentication.deserializer();
67 }
68
Charles M.C. Chan94f37372015-01-10 17:53:42 +080069 /**
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 Hart2a655752015-04-07 16:46:33 -070089 public void testDeserialize() throws Exception {
90 Authentication auth = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan94f37372015-01-10 17:53:42 +080091
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 }
Jian Li5fc14292015-12-04 11:30:46 -0800121
122 /**
123 * Tests toString.
124 */
125 @Test
126 public void testToStringAuthentication() throws Exception {
127 Authentication auth = deserializer.deserialize(bytePacket, 0, bytePacket.length);
128 String str = auth.toString();
129
130 assertTrue(StringUtils.contains(str, "nextHeader=" + (byte) 0x11));
131 assertTrue(StringUtils.contains(str, "payloadLength=" + (byte) 0x02));
132 assertTrue(StringUtils.contains(str, "securityParamIndex=" + 0x13572468));
133 assertTrue(StringUtils.contains(str, "sequence=" + 0xffff00));
134 assertTrue(StringUtils.contains(str, "integrityCheck=" + Arrays.toString(icv)));
135 }
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800136}