blob: a6edc5dabd93b8f6ef0fba93fe0d82f0ed51bc6d [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
19import org.junit.BeforeClass;
20import org.junit.Test;
21import org.onlab.packet.Data;
22import org.onlab.packet.UDP;
23
24import static org.hamcrest.Matchers.is;
25import static org.junit.Assert.assertArrayEquals;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertThat;
28import static org.junit.Assert.assertTrue;
29
30/**
31 * Tests for class {@link Authentication}.
32 */
33public class AuthenticationTest {
34 private static Data data;
35 private static UDP udp;
36 private static byte[] icv = {
37 (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
38 };
39 private static byte[] bytePacket;
40
41 @BeforeClass
42 public static void setUpBeforeClass() throws Exception {
43 data = new Data();
44 data.setData("testSerialize".getBytes());
45 udp = new UDP();
46 udp.setPayload(data);
47
48 byte[] bytePayload = udp.serialize();
49 byte[] byteHeader = {
50 (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x00,
51 (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68,
52 (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00,
53 (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
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
60 /**
61 * Tests serialize and setters.
62 */
63 @Test
64 public void testSerialize() {
65 Authentication auth = new Authentication();
66 auth.setNextHeader((byte) 0x11);
67 auth.setPayloadLength((byte) 0x02);
68 auth.setSecurityParamIndex(0x13572468);
69 auth.setSequence(0xffff00);
70 auth.setIngegrityCheck(icv);
71 auth.setPayload(udp);
72
73 assertArrayEquals(auth.serialize(), bytePacket);
74 }
75
76 /**
77 * Tests deserialize and getters.
78 */
79 @Test
80 public void testDeserialize() {
81 Authentication auth = new Authentication();
82 auth.deserialize(bytePacket, 0, bytePacket.length);
83
84 assertThat(auth.getNextHeader(), is((byte) 0x11));
85 assertThat(auth.getPayloadLength(), is((byte) 0x02));
86 assertThat(auth.getSecurityParamIndex(), is(0x13572468));
87 assertThat(auth.getSequence(), is(0xffff00));
88 assertArrayEquals(auth.getIntegrityCheck(), icv);
89 }
90
91 /**
92 * Tests comparator.
93 */
94 @Test
95 public void testEqual() {
96 Authentication auth1 = new Authentication();
97 auth1.setNextHeader((byte) 0x11);
98 auth1.setPayloadLength((byte) 0x02);
99 auth1.setSecurityParamIndex(0x13572468);
100 auth1.setSequence(0xffff00);
101 auth1.setIngegrityCheck(icv);
102
103 Authentication auth2 = new Authentication();
104 auth2.setNextHeader((byte) 0x11);
105 auth2.setPayloadLength((byte) 0x02);
106 auth2.setSecurityParamIndex(0x13572467);
107 auth2.setSequence(0xffff00);
108 auth2.setIngegrityCheck(icv);
109
110 assertTrue(auth1.equals(auth1));
111 assertFalse(auth1.equals(auth2));
112 }
113}