blob: 8627a9bc17c43cfc59d25909f9c688fd76416185 [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
2 * Copyright 2014 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
17
18
19package org.onlab.packet;
20
21import org.junit.BeforeClass;
22import org.junit.Test;
23
24import static org.hamcrest.Matchers.is;
25import static org.junit.Assert.*;
26
27/**
28 * Tests for class {@link NeighborAdvertisement}.
29 */
30public class NeighborAdvertisementTest {
31 private static final byte[] TARGET_ADDRESS = {
32 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
33 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
34 };
35 private static Data data;
36 private static byte[] bytePacket;
37
38 @BeforeClass
39 public static void setUpBeforeClass() throws Exception {
40 data = new Data();
41 data.setData("".getBytes());
42
43 byte[] bytePayload = data.serialize();
44 byte[] byteHeader = {
45 (byte) 0xe0, (byte) 0x00, (byte) 0x00, (byte) 0x00,
46 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
47 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
48 };
49 bytePacket = new byte[byteHeader.length + bytePayload.length];
50 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
51 System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
52 }
53
54 /**
55 * Tests serialize and setters.
56 */
57 @Test
58 public void testSerialize() {
59 NeighborAdvertisement na = new NeighborAdvertisement();
60 na.setRouterFlag((byte) 1);
61 na.setSolicitedFlag((byte) 1);
62 na.setOverrideFlag((byte) 1);
63 na.setTargetAddress(TARGET_ADDRESS);
64
65 assertArrayEquals(na.serialize(), bytePacket);
66 }
67
68 /**
69 * Tests deserialize and getters.
70 */
71 @Test
72 public void testDeserialize() {
73 NeighborAdvertisement na = new NeighborAdvertisement();
74 na.deserialize(bytePacket, 0, bytePacket.length);
75
76 assertThat(na.getRouterFlag(), is((byte) 1));
77 assertThat(na.getSolicitedFlag(), is((byte) 1));
78 assertThat(na.getOverrideFlag(), is((byte) 1));
79 assertArrayEquals(na.getTargetAddress(), TARGET_ADDRESS);
80 }
81
82 /**
83 * Tests comparator.
84 */
85 @Test
86 public void testEqual() {
87 NeighborAdvertisement na1 = new NeighborAdvertisement();
88 na1.setRouterFlag((byte) 1);
89 na1.setSolicitedFlag((byte) 1);
90 na1.setOverrideFlag((byte) 1);
91 na1.setTargetAddress(TARGET_ADDRESS);
92
93 NeighborAdvertisement na2 = new NeighborAdvertisement();
94 na2.setRouterFlag((byte) 1);
95 na2.setSolicitedFlag((byte) 1);
96 na2.setOverrideFlag((byte) 0);
97 na2.setTargetAddress(TARGET_ADDRESS);
98
99 assertTrue(na1.equals(na1));
100 assertFalse(na1.equals(na2));
101 }
102}