Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onlab.packet.ipv6; |
| 18 | |
| 19 | import org.junit.BeforeClass; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.Data; |
| 22 | import org.onlab.packet.UDP; |
| 23 | |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | import static org.junit.Assert.assertArrayEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
| 27 | import static org.junit.Assert.assertThat; |
| 28 | import static org.junit.Assert.assertTrue; |
| 29 | |
| 30 | /** |
| 31 | * Tests for class {@link Fragment}. |
| 32 | */ |
| 33 | public class FragmentTest { |
| 34 | private static Data data; |
| 35 | private static UDP udp; |
| 36 | private static byte[] bytePacket; |
| 37 | |
| 38 | @BeforeClass |
| 39 | public static void setUpBeforeClass() throws Exception { |
| 40 | data = new Data(); |
| 41 | data.setData("testSerialize".getBytes()); |
| 42 | udp = new UDP(); |
| 43 | udp.setPayload(data); |
| 44 | |
| 45 | byte[] bytePayload = udp.serialize(); |
| 46 | byte[] byteHeader = { |
| 47 | (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0xf9, |
| 48 | (byte) 0x00, (byte) 0x00, (byte) 0x13, (byte) 0x57 |
| 49 | }; |
| 50 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 51 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 52 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Tests serialize and setters. |
| 57 | */ |
| 58 | @Test |
| 59 | public void testSerialize() { |
| 60 | Fragment frag = new Fragment(); |
| 61 | frag.setNextHeader((byte) 0x11); |
| 62 | frag.setFragmentOffset((short) 0x1f); |
| 63 | frag.setMoreFragment((byte) 1); |
| 64 | frag.setIdentification(0x1357); |
| 65 | frag.setPayload(udp); |
| 66 | |
| 67 | assertArrayEquals(frag.serialize(), bytePacket); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests deserialize and getters. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testDeserialize() { |
| 75 | Fragment frag = new Fragment(); |
| 76 | frag.deserialize(bytePacket, 0, bytePacket.length); |
| 77 | |
| 78 | assertThat(frag.getNextHeader(), is((byte) 0x11)); |
| 79 | assertThat(frag.getFragmentOffset(), is((short) 0x1f)); |
| 80 | assertThat(frag.getMoreFragment(), is((byte) 1)); |
| 81 | assertThat(frag.getIdentification(), is(0x1357)); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests comparator. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testEqual() { |
| 89 | Fragment frag1 = new Fragment(); |
| 90 | frag1.setNextHeader((byte) 0x11); |
| 91 | frag1.setFragmentOffset((short) 0x1f); |
| 92 | frag1.setMoreFragment((byte) 1); |
| 93 | frag1.setIdentification(0x1357); |
| 94 | |
| 95 | Fragment frag2 = new Fragment(); |
| 96 | frag2.setNextHeader((byte) 0x11); |
| 97 | frag2.setFragmentOffset((short) 0x1f); |
| 98 | frag2.setMoreFragment((byte) 1); |
| 99 | frag2.setIdentification(0x1358); |
| 100 | |
| 101 | assertTrue(frag1.equals(frag1)); |
| 102 | assertFalse(frag1.equals(frag2)); |
| 103 | } |
| 104 | } |