Jian Li | 1270aea | 2016-09-15 13:32:24 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 1270aea | 2016-09-15 13:32:24 +0900 | [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 | package org.onlab.packet; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | |
| 23 | import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | import static org.hamcrest.Matchers.instanceOf; |
| 25 | import static org.hamcrest.Matchers.is; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for IP class. |
| 29 | */ |
| 30 | public class IpTest { |
| 31 | private byte v4Version = 4; |
| 32 | private byte v4HeaderLength = 6; |
| 33 | private byte badVersion = 5; |
| 34 | private byte badHeaderLength = 6; |
| 35 | private static Data data; |
| 36 | private static UDP udp; |
| 37 | private byte[] v4HeaderBytes; |
| 38 | private byte[] v6HeaderBytes; |
| 39 | private byte[] badHeaderBytes; |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() throws Exception { |
| 43 | |
| 44 | ByteBuffer v4bb = ByteBuffer.allocate(v4HeaderLength * 4); |
| 45 | v4bb.put((byte) ((v4Version & 0xf) << 4)); |
| 46 | v4HeaderBytes = v4bb.array(); |
| 47 | |
| 48 | ByteBuffer badBb = ByteBuffer.allocate(badHeaderLength * 4); |
| 49 | badBb.put((byte) ((badVersion & 0xf) << 4)); |
| 50 | badHeaderBytes = badBb.array(); |
| 51 | |
| 52 | data = new Data(); |
| 53 | data.setData("testSerialize".getBytes()); |
| 54 | udp = new UDP(); |
| 55 | udp.setPayload(data); |
| 56 | |
| 57 | byte[] bytePayload = udp.serialize(); |
| 58 | byte[] byteHeader = { |
| 59 | (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79, |
| 60 | (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff), |
| 61 | (byte) 0x11, (byte) 0x20, |
| 62 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 63 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, |
| 64 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 65 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8, |
| 66 | }; |
| 67 | v6HeaderBytes = new byte[byteHeader.length + bytePayload.length]; |
| 68 | System.arraycopy(byteHeader, 0, v6HeaderBytes, 0, byteHeader.length); |
| 69 | System.arraycopy(bytePayload, 0, v6HeaderBytes, byteHeader.length, bytePayload.length); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testDeserialize() throws Exception { |
| 74 | Deserializer ipDeserializer = IP.deserializer(); |
| 75 | IPacket v4Packet = ipDeserializer.deserialize(v4HeaderBytes, 0, v4HeaderLength * 4); |
| 76 | IPacket v6Packet = ipDeserializer.deserialize(v6HeaderBytes, 0, v6HeaderBytes.length); |
| 77 | assertThat(v4Packet, is(instanceOf(IPv4.class))); |
| 78 | assertThat(v6Packet, is(instanceOf(IPv6.class))); |
| 79 | |
| 80 | IPv6 ipv6 = (IPv6) v6Packet; |
| 81 | assertThat(ipv6.getVersion(), is((byte) 6)); |
| 82 | assertThat(ipv6.getTrafficClass(), is((byte) 0x93)); |
| 83 | assertThat(ipv6.getFlowLabel(), is(0x13579)); |
| 84 | assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP)); |
| 85 | assertThat(ipv6.getHopLimit(), is((byte) 32)); |
| 86 | } |
| 87 | |
| 88 | @Test(expected = DeserializationException.class) |
| 89 | public void testBadIpVersion() throws Exception { |
| 90 | Deserializer ipDeserializer = IP.deserializer(); |
| 91 | ipDeserializer.deserialize(badHeaderBytes, 0, badHeaderLength * 4); |
| 92 | } |
| 93 | } |