blob: 2fd1815cc9f12d23dc4b8541ad32925f489c26cc [file] [log] [blame]
Charles M.C. Chanea5aa472015-01-03 13:40:39 +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.ndp;
20
21import org.junit.BeforeClass;
22import org.junit.Test;
23import org.onlab.packet.Data;
24
25import static org.hamcrest.Matchers.is;
26import static org.junit.Assert.assertArrayEquals;
27import static org.junit.Assert.assertFalse;
28import static org.junit.Assert.assertThat;
29import static org.junit.Assert.assertTrue;
30
31/**
32 * Tests for class {@link RouterAdvertisement}.
33 */
34public class RouterAdvertisementTest {
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) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58,
46 (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8,
47 (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4
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 RouterAdvertisement ra = new RouterAdvertisement();
60 ra.setCurrentHopLimit((byte) 3);
61 ra.setMFlag((byte) 1);
62 ra.setOFlag((byte) 1);
63 ra.setRouterLifetime((short) 0x258);
64 ra.setReachableTime(0x3e8);
65 ra.setRetransmitTimer(0x1f4);
66 ra.setPayload(data);
67
68 assertArrayEquals(ra.serialize(), bytePacket);
69 }
70
71 /**
72 * Tests deserialize and getters.
73 */
74 @Test
75 public void testDeserialize() {
76 RouterAdvertisement ra = new RouterAdvertisement();
77 ra.deserialize(bytePacket, 0, bytePacket.length);
78
79 assertThat(ra.getCurrentHopLimit(), is((byte) 3));
80 assertThat(ra.getMFlag(), is((byte) 1));
81 assertThat(ra.getOFlag(), is((byte) 1));
82 assertThat(ra.getRouterLifetime(), is((short) 0x258));
83 assertThat(ra.getReachableTime(), is(0x3e8));
84 assertThat(ra.getRetransmitTimer(), is(0x1f4));
85 }
86
87 /**
88 * Tests comparator.
89 */
90 @Test
91 public void testEqual() {
92 RouterAdvertisement ra1 = new RouterAdvertisement();
93 ra1.setCurrentHopLimit((byte) 3);
94 ra1.setMFlag((byte) 1);
95 ra1.setOFlag((byte) 1);
96 ra1.setRouterLifetime((short) 0x258);
97 ra1.setReachableTime(0x3e8);
98 ra1.setRetransmitTimer(0x1f4);
99
100 RouterAdvertisement ra2 = new RouterAdvertisement();
101 ra2.setCurrentHopLimit((byte) 3);
102 ra2.setMFlag((byte) 0);
103 ra2.setOFlag((byte) 0);
104 ra2.setRouterLifetime((short) 0x1f4);
105 ra2.setReachableTime(0x3e8);
106 ra2.setRetransmitTimer(0x1f4);
107
108 assertTrue(ra1.equals(ra1));
109 assertFalse(ra1.equals(ra2));
110 }
111}