Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.assertFalse; |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.assertTrue; |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 21 | |
| 22 | import java.util.Arrays; |
| 23 | |
| 24 | import org.junit.Test; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix.Version; |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 26 | |
| 27 | import com.google.common.testing.EqualsTester; |
| 28 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 29 | public class IpPrefixTest { |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 30 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 31 | private static final byte [] BYTES1 = new byte [] {0xa, 0x0, 0x0, 0xa}; |
| 32 | private static final byte [] BYTES2 = new byte [] {0xa, 0x0, 0x0, 0xb}; |
| 33 | private static final int INTVAL1 = 167772170; |
| 34 | private static final int INTVAL2 = 167772171; |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 35 | private static final String STRVAL = "10.0.0.12/16"; |
| 36 | private static final int MASK_LENGTH = 16; |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 37 | |
| 38 | @Test |
| 39 | public void testEquality() { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 40 | IpPrefix ip1 = IpPrefix.valueOf(BYTES1, IpPrefix.MAX_INET_MASK_LENGTH); |
| 41 | IpPrefix ip2 = IpPrefix.valueOf(INTVAL1, IpPrefix.MAX_INET_MASK_LENGTH); |
| 42 | IpPrefix ip3 = IpPrefix.valueOf(BYTES2, IpPrefix.MAX_INET_MASK_LENGTH); |
| 43 | IpPrefix ip4 = IpPrefix.valueOf(INTVAL2, IpPrefix.MAX_INET_MASK_LENGTH); |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 44 | IpPrefix ip5 = IpPrefix.valueOf(STRVAL); |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 45 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 46 | new EqualsTester().addEqualityGroup(ip1, ip2) |
| 47 | .addEqualityGroup(ip3, ip4) |
| 48 | .addEqualityGroup(ip5) |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 49 | .testEquals(); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 50 | |
| 51 | // string conversions |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 52 | IpPrefix ip6 = IpPrefix.valueOf(BYTES1, MASK_LENGTH); |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 53 | IpPrefix ip7 = IpPrefix.valueOf("10.0.0.10/16"); |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 54 | IpPrefix ip8 = IpPrefix.valueOf(new byte [] {0xa, 0x0, 0x0, 0xc}, 16); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 55 | assertEquals("incorrect address conversion", ip6, ip7); |
| 56 | assertEquals("incorrect address conversion", ip5, ip8); |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void basics() { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 61 | IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK_LENGTH); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 62 | final byte [] bytes = new byte [] {0xa, 0x0, 0x0, 0xa}; |
| 63 | |
| 64 | //check fields |
| 65 | assertEquals("incorrect IP Version", Version.INET, ip1.version()); |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 66 | assertEquals("incorrect netmask", 16, ip1.prefixLength()); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 67 | assertTrue("faulty toOctets()", Arrays.equals(bytes, ip1.toOctets())); |
| 68 | assertEquals("faulty toInt()", INTVAL1, ip1.toInt()); |
| 69 | assertEquals("faulty toString()", "10.0.0.10/16", ip1.toString()); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void netmasks() { |
| 74 | // masked |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 75 | IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK_LENGTH); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 76 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 77 | IpPrefix host = IpPrefix.valueOf("0.0.0.10/16"); |
| 78 | IpPrefix network = IpPrefix.valueOf("10.0.0.0/16"); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 79 | assertEquals("incorrect host address", host, ip1.host()); |
| 80 | assertEquals("incorrect network address", network, ip1.network()); |
| 81 | assertEquals("incorrect netmask", "255.255.0.0", ip1.netmask().toString()); |
| 82 | |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 83 | } |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 84 | |
| 85 | @Test |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 86 | public void testContainsIpPrefix() { |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 87 | IpPrefix slash31 = IpPrefix.valueOf(BYTES1, 31); |
| 88 | IpPrefix slash32 = IpPrefix.valueOf(BYTES1, 32); |
| 89 | IpPrefix differentSlash32 = IpPrefix.valueOf(BYTES2, 32); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 90 | |
| 91 | assertTrue(slash31.contains(differentSlash32)); |
| 92 | assertFalse(differentSlash32.contains(slash31)); |
| 93 | |
| 94 | assertTrue(slash31.contains(slash32)); |
| 95 | assertFalse(slash32.contains(differentSlash32)); |
| 96 | assertFalse(differentSlash32.contains(slash32)); |
| 97 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 98 | IpPrefix zero = IpPrefix.valueOf("0.0.0.0/0"); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 99 | assertTrue(zero.contains(differentSlash32)); |
| 100 | assertFalse(differentSlash32.contains(zero)); |
| 101 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 102 | IpPrefix slash8 = IpPrefix.valueOf("10.0.0.0/8"); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 103 | assertTrue(slash8.contains(slash31)); |
| 104 | assertFalse(slash31.contains(slash8)); |
| 105 | } |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 106 | |
| 107 | @Test |
| 108 | public void testContainsIpAddress() { |
| 109 | IpPrefix slash31 = IpPrefix.valueOf(BYTES1, 31); |
Pavlin Radoslavov | e7b5bd7 | 2014-10-27 20:17:24 -0700 | [diff] [blame] | 110 | IpAddress addr32 = IpAddress.valueOf(BYTES1); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 111 | |
Pavlin Radoslavov | e7b5bd7 | 2014-10-27 20:17:24 -0700 | [diff] [blame] | 112 | assertTrue(slash31.contains(addr32)); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 113 | |
| 114 | IpPrefix intf = IpPrefix.valueOf("192.168.10.101/24"); |
| 115 | IpAddress addr = IpAddress.valueOf("192.168.10.1"); |
| 116 | |
| 117 | assertTrue(intf.contains(addr)); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 118 | |
| 119 | IpPrefix intf1 = IpPrefix.valueOf("10.0.0.101/24"); |
| 120 | IpAddress addr1 = IpAddress.valueOf("10.0.0.4"); |
| 121 | |
| 122 | assertTrue(intf1.contains(addr1)); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 123 | } |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 124 | } |