Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 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 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.StringUtils; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 21 | import org.junit.BeforeClass; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Data; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 24 | import org.onlab.packet.DeserializationException; |
| 25 | import org.onlab.packet.Deserializer; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 26 | import org.onlab.packet.UDP; |
| 27 | |
| 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.junit.Assert.assertArrayEquals; |
| 30 | import static org.junit.Assert.assertFalse; |
| 31 | import static org.junit.Assert.assertThat; |
| 32 | import static org.junit.Assert.assertTrue; |
| 33 | |
| 34 | /** |
| 35 | * Tests for class {@link Fragment}. |
| 36 | */ |
| 37 | public class FragmentTest { |
| 38 | private static Data data; |
| 39 | private static UDP udp; |
| 40 | private static byte[] bytePacket; |
| 41 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 42 | private Deserializer<Fragment> deserializer; |
| 43 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 44 | @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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 61 | @Before |
| 62 | public void setUp() { |
| 63 | deserializer = Fragment.deserializer(); |
| 64 | } |
| 65 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 66 | /** |
| 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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 85 | public void testDeserialize() throws DeserializationException { |
| 86 | Fragment frag = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 87 | |
| 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 Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 114 | |
| 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. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 128 | } |