blob: f2d5e48e90462a7508676d788b6db03b21c847e7 [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
Jonathan Hart2a655752015-04-07 16:46:33 -070019import org.junit.Before;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080020import org.junit.BeforeClass;
21import org.junit.Test;
22import org.onlab.packet.Data;
Jonathan Hart2a655752015-04-07 16:46:33 -070023import org.onlab.packet.DeserializationException;
24import org.onlab.packet.Deserializer;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080025import org.onlab.packet.UDP;
26
27import static org.hamcrest.Matchers.is;
28import static org.junit.Assert.assertArrayEquals;
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertThat;
31import static org.junit.Assert.assertTrue;
32
33/**
34 * Tests for class {@link Fragment}.
35 */
36public class FragmentTest {
37 private static Data data;
38 private static UDP udp;
39 private static byte[] bytePacket;
40
Jonathan Hart2a655752015-04-07 16:46:33 -070041 private Deserializer<Fragment> deserializer;
42
Charles M.C. Chan94f37372015-01-10 17:53:42 +080043 @BeforeClass
44 public static void setUpBeforeClass() throws Exception {
45 data = new Data();
46 data.setData("testSerialize".getBytes());
47 udp = new UDP();
48 udp.setPayload(data);
49
50 byte[] bytePayload = udp.serialize();
51 byte[] byteHeader = {
52 (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0xf9,
53 (byte) 0x00, (byte) 0x00, (byte) 0x13, (byte) 0x57
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 = Fragment.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 Fragment frag = new Fragment();
71 frag.setNextHeader((byte) 0x11);
72 frag.setFragmentOffset((short) 0x1f);
73 frag.setMoreFragment((byte) 1);
74 frag.setIdentification(0x1357);
75 frag.setPayload(udp);
76
77 assertArrayEquals(frag.serialize(), bytePacket);
78 }
79
80 /**
81 * Tests deserialize and getters.
82 */
83 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070084 public void testDeserialize() throws DeserializationException {
85 Fragment frag = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan94f37372015-01-10 17:53:42 +080086
87 assertThat(frag.getNextHeader(), is((byte) 0x11));
88 assertThat(frag.getFragmentOffset(), is((short) 0x1f));
89 assertThat(frag.getMoreFragment(), is((byte) 1));
90 assertThat(frag.getIdentification(), is(0x1357));
91 }
92
93 /**
94 * Tests comparator.
95 */
96 @Test
97 public void testEqual() {
98 Fragment frag1 = new Fragment();
99 frag1.setNextHeader((byte) 0x11);
100 frag1.setFragmentOffset((short) 0x1f);
101 frag1.setMoreFragment((byte) 1);
102 frag1.setIdentification(0x1357);
103
104 Fragment frag2 = new Fragment();
105 frag2.setNextHeader((byte) 0x11);
106 frag2.setFragmentOffset((short) 0x1f);
107 frag2.setMoreFragment((byte) 1);
108 frag2.setIdentification(0x1358);
109
110 assertTrue(frag1.equals(frag1));
111 assertFalse(frag1.equals(frag2));
112 }
113}