Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 19 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 20 | */ |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 21 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 22 | |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | |
| 26 | import java.util.Arrays; |
| 27 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 28 | import org.junit.Assert; |
| 29 | import org.junit.Test; |
| 30 | |
Yuta HIGUCHI | aa132f5 | 2014-06-26 10:18:39 -0700 | [diff] [blame] | 31 | // CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 32 | /** |
| 33 | * @author David Erickson (daviderickson@cs.stanford.edu) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 34 | */ |
| 35 | public class IPv4Test { |
| 36 | @Test |
| 37 | public void testToIPv4Address() { |
| 38 | int intIp = 0xc0a80001; |
| 39 | String stringIp = "192.168.0.1"; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | byte[] byteIp = new byte[]{(byte) 192, (byte) 168, (byte) 0, (byte) 1}; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 41 | assertEquals(intIp, IPv4.toIPv4Address(stringIp)); |
| 42 | assertEquals(intIp, IPv4.toIPv4Address(byteIp)); |
| 43 | assertTrue(Arrays.equals(byteIp, IPv4.toIPv4AddressBytes(intIp))); |
| 44 | assertTrue(Arrays.equals(byteIp, IPv4.toIPv4AddressBytes(stringIp))); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testToIPv4AddressBytes() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | byte[] expected = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 50 | Assert.assertArrayEquals(expected, IPv4.toIPv4AddressBytes("255.255.255.255")); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | expected = new byte[]{(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80}; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 52 | Assert.assertArrayEquals(expected, IPv4.toIPv4AddressBytes("128.128.128.128")); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | expected = new byte[]{0x7f, 0x7f, 0x7f, 0x7f}; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 54 | Assert.assertArrayEquals(expected, IPv4.toIPv4AddressBytes("127.127.127.127")); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testSerialize() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | byte[] expected = new byte[]{0x45, 0x00, 0x00, 0x14, 0x5e, 0x4e, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 60 | 0x00, 0x00, 0x3f, 0x06, 0x31, 0x2e, (byte) 0xac, 0x18, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | 0x4a, (byte) 0xdf, (byte) 0xab, 0x40, 0x4a, 0x30}; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 62 | IPv4 packet = new IPv4() |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | .setIdentification((short) 24142) |
| 64 | .setTtl((byte) 63) |
| 65 | .setProtocol((byte) 0x06) |
| 66 | .setSourceAddress("172.24.74.223") |
| 67 | .setDestinationAddress("171.64.74.48"); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 68 | byte[] actual = packet.serialize(); |
| 69 | assertTrue(Arrays.equals(expected, actual)); |
| 70 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 72 | @Test |
| 73 | public void testDeserialize() { |
| 74 | // A real TLSv1 packet |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | byte[] pktSerialized = new byte[]{0x45, 0x00, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 76 | 0x00, 0x2e, 0x41, (byte) 0xbe, 0x40, 0x00, 0x40, 0x06, |
| 77 | (byte) 0xd4, (byte) 0xf0, (byte) 0xc0, (byte) 0xa8, 0x02, (byte) 0xdb, (byte) 0xd0, 0x55, |
| 78 | (byte) 0x90, 0x42, (byte) 0xd5, 0x48, 0x01, (byte) 0xbb, (byte) 0xe3, 0x50, |
| 79 | (byte) 0xb2, 0x2f, (byte) 0xfc, (byte) 0xf8, (byte) 0xa8, 0x2c, 0x50, 0x18, |
| 80 | (byte) 0xff, (byte) 0xff, 0x24, 0x3c, 0x00, 0x00, 0x14, 0x03, |
| 81 | 0x01, 0x00, 0x01, 0x01 |
| 82 | }; |
| 83 | IPv4 packet = new IPv4(); |
| 84 | packet.deserialize(pktSerialized, 0, pktSerialized.length); |
| 85 | byte[] pktSerialized1 = packet.serialize(); |
| 86 | assertTrue(Arrays.equals(pktSerialized, pktSerialized1)); |
| 87 | } |
| 88 | } |