blob: 7e5dc15cd66e0a88ef1d7c1bee5b5d2dbb56703e [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.DeserializationException;
25import org.onlab.packet.Deserializer;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080026import org.onlab.packet.UDP;
27
28import static org.hamcrest.Matchers.is;
29import static org.junit.Assert.assertArrayEquals;
30import static org.junit.Assert.assertFalse;
31import static org.junit.Assert.assertThat;
32import static org.junit.Assert.assertTrue;
33
34/**
35 * Tests for class {@link Fragment}.
36 */
37public class FragmentTest {
38 private static Data data;
39 private static UDP udp;
40 private static byte[] bytePacket;
41
Jonathan Hart2a655752015-04-07 16:46:33 -070042 private Deserializer<Fragment> deserializer;
43
Charles M.C. Chan94f37372015-01-10 17:53:42 +080044 @BeforeClass
45 public static void setUpBeforeClass() throws Exception {
46 data = new Data();
47 data.setData("testSerialize".getBytes());
48 udp = new UDP();
49 udp.setPayload(data);
50
51 byte[] bytePayload = udp.serialize();
52 byte[] byteHeader = {
53 (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0xf9,
54 (byte) 0x00, (byte) 0x00, (byte) 0x13, (byte) 0x57
55 };
56 bytePacket = new byte[byteHeader.length + bytePayload.length];
57 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
58 System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
59 }
60
Jonathan Hart2a655752015-04-07 16:46:33 -070061 @Before
62 public void setUp() {
63 deserializer = Fragment.deserializer();
64 }
65
Charles M.C. Chan94f37372015-01-10 17:53:42 +080066 /**
67 * Tests serialize and setters.
68 */
69 @Test
70 public void testSerialize() {
71 Fragment frag = new Fragment();
72 frag.setNextHeader((byte) 0x11);
73 frag.setFragmentOffset((short) 0x1f);
74 frag.setMoreFragment((byte) 1);
75 frag.setIdentification(0x1357);
76 frag.setPayload(udp);
77
78 assertArrayEquals(frag.serialize(), bytePacket);
79 }
80
81 /**
82 * Tests deserialize and getters.
83 */
84 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070085 public void testDeserialize() throws DeserializationException {
86 Fragment frag = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan94f37372015-01-10 17:53:42 +080087
88 assertThat(frag.getNextHeader(), is((byte) 0x11));
89 assertThat(frag.getFragmentOffset(), is((short) 0x1f));
90 assertThat(frag.getMoreFragment(), is((byte) 1));
91 assertThat(frag.getIdentification(), is(0x1357));
92 }
93
94 /**
95 * Tests comparator.
96 */
97 @Test
98 public void testEqual() {
99 Fragment frag1 = new Fragment();
100 frag1.setNextHeader((byte) 0x11);
101 frag1.setFragmentOffset((short) 0x1f);
102 frag1.setMoreFragment((byte) 1);
103 frag1.setIdentification(0x1357);
104
105 Fragment frag2 = new Fragment();
106 frag2.setNextHeader((byte) 0x11);
107 frag2.setFragmentOffset((short) 0x1f);
108 frag2.setMoreFragment((byte) 1);
109 frag2.setIdentification(0x1358);
110
111 assertTrue(frag1.equals(frag1));
112 assertFalse(frag1.equals(frag2));
113 }
Jian Li5fc14292015-12-04 11:30:46 -0800114
115 /**
116 * Tests toString.
117 */
118 @Test
119 public void testToStringFragment() throws Exception {
120 Fragment frag = deserializer.deserialize(bytePacket, 0, bytePacket.length);
121 String str = frag.toString();
122
123 assertTrue(StringUtils.contains(str, "nextHeader=" + (byte) 0x11));
124 assertTrue(StringUtils.contains(str, "fragmentOffset=" + (short) 0x1f));
125 assertTrue(StringUtils.contains(str, "moreFragment=" + (byte) 1));
126 assertTrue(StringUtils.contains(str, "identification=" + 0x1357));
127 }
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800128}