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 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 19 | import org.junit.Before; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 20 | import org.junit.BeforeClass; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.Data; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 23 | import org.onlab.packet.DeserializationException; |
| 24 | import org.onlab.packet.Deserializer; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 25 | import org.onlab.packet.UDP; |
| 26 | |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.junit.Assert.assertArrayEquals; |
| 29 | import static org.junit.Assert.assertFalse; |
| 30 | import static org.junit.Assert.assertThat; |
| 31 | import static org.junit.Assert.assertTrue; |
| 32 | |
| 33 | /** |
| 34 | * Tests for class {@link Fragment}. |
| 35 | */ |
| 36 | public class FragmentTest { |
| 37 | private static Data data; |
| 38 | private static UDP udp; |
| 39 | private static byte[] bytePacket; |
| 40 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 41 | private Deserializer<Fragment> deserializer; |
| 42 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 43 | @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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 60 | @Before |
| 61 | public void setUp() { |
| 62 | deserializer = Fragment.deserializer(); |
| 63 | } |
| 64 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 65 | /** |
| 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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 84 | public void testDeserialize() throws DeserializationException { |
| 85 | Fragment frag = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 86 | |
| 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 | } |