blob: ce6a1e375306ad27067d782ffdfd27a2827c5f25 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
Ayaka Koshibe16698a32014-09-13 22:19:02 -070016package org.onlab.packet;
17
18import static org.junit.Assert.assertEquals;
Jonathan Hartb7a2ac32014-09-19 10:42:27 -070019import static org.junit.Assert.assertFalse;
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070020import static org.junit.Assert.assertTrue;
Ayaka Koshibe16698a32014-09-13 22:19:02 -070021
22import java.util.Arrays;
23
24import org.junit.Test;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070025import org.onlab.packet.IpAddress.Version;
Ayaka Koshibe16698a32014-09-13 22:19:02 -070026
27import com.google.common.testing.EqualsTester;
28
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070029public class IpPrefixTest {
Ayaka Koshibe16698a32014-09-13 22:19:02 -070030
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070031 private static final byte [] BYTES1 = new byte [] {0xa, 0x0, 0x0, 0xa};
32 private static final byte [] BYTES2 = new byte [] {0xa, 0x0, 0x0, 0xb};
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070033 private static final int INTVAL0 = 0x0a000000;
34 private static final int INTVAL1 = 0x0a00000a;
35 private static final int INTVAL2 = 0x0a00000b;
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070036 private static final String STRVAL = "10.0.0.12/16";
37 private static final int MASK_LENGTH = 16;
Ayaka Koshibe16698a32014-09-13 22:19:02 -070038
39 @Test
40 public void testEquality() {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070041 IpPrefix ip1 = IpPrefix.valueOf(BYTES1, IpPrefix.MAX_INET_MASK_LENGTH);
42 IpPrefix ip2 = IpPrefix.valueOf(INTVAL1, IpPrefix.MAX_INET_MASK_LENGTH);
43 IpPrefix ip3 = IpPrefix.valueOf(BYTES2, IpPrefix.MAX_INET_MASK_LENGTH);
44 IpPrefix ip4 = IpPrefix.valueOf(INTVAL2, IpPrefix.MAX_INET_MASK_LENGTH);
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070045 IpPrefix ip5 = IpPrefix.valueOf(STRVAL);
Ayaka Koshibe16698a32014-09-13 22:19:02 -070046
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070047 new EqualsTester().addEqualityGroup(ip1, ip2)
48 .addEqualityGroup(ip3, ip4)
49 .addEqualityGroup(ip5)
Ayaka Koshibe16698a32014-09-13 22:19:02 -070050 .testEquals();
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070051
52 // string conversions
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070053 IpPrefix ip6 = IpPrefix.valueOf(BYTES1, MASK_LENGTH);
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070054 IpPrefix ip7 = IpPrefix.valueOf("10.0.0.10/16");
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070055 IpPrefix ip8 = IpPrefix.valueOf(new byte [] {0xa, 0x0, 0x0, 0xc}, 16);
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070056 assertEquals("incorrect address conversion", ip6, ip7);
57 assertEquals("incorrect address conversion", ip5, ip8);
Ayaka Koshibe16698a32014-09-13 22:19:02 -070058 }
59
60 @Test
61 public void basics() {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070062 IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK_LENGTH);
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070063 final byte [] bytes = new byte [] {0xa, 0x0, 0x0, 0x0};
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070064
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070065 // check fields
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070066 assertEquals("incorrect IP Version", Version.INET, ip1.version());
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070067 assertEquals("incorrect netmask", 16, ip1.prefixLength());
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070068 assertTrue("faulty toOctets()",
69 Arrays.equals(bytes, ip1.address().toOctets()));
70 assertEquals("faulty toInt()", INTVAL0, ip1.address().toInt());
71 assertEquals("faulty toString()", "10.0.0.0/16", ip1.toString());
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070072 }
73
74 @Test
75 public void netmasks() {
76 // masked
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070077 IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK_LENGTH);
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070078 IpPrefix ip2 = IpPrefix.valueOf("10.0.0.10/16");
79 IpPrefix ip3 = IpPrefix.valueOf("10.0.0.0/16");
80 assertEquals("incorrect binary masked address",
81 ip1.toString(), "10.0.0.0/16");
82 assertEquals("incorrect string masked address",
83 ip2.toString(), "10.0.0.0/16");
84 assertEquals("incorrect network address",
85 ip2.toString(), "10.0.0.0/16");
Ayaka Koshibe16698a32014-09-13 22:19:02 -070086 }
Jonathan Hartb7a2ac32014-09-19 10:42:27 -070087
88 @Test
Jonathan Hart70da5122014-10-01 16:37:42 -070089 public void testContainsIpPrefix() {
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070090 IpPrefix slash31 = IpPrefix.valueOf(BYTES1, 31);
91 IpPrefix slash32 = IpPrefix.valueOf(BYTES1, 32);
92 IpPrefix differentSlash32 = IpPrefix.valueOf(BYTES2, 32);
Jonathan Hartb7a2ac32014-09-19 10:42:27 -070093
94 assertTrue(slash31.contains(differentSlash32));
95 assertFalse(differentSlash32.contains(slash31));
96
97 assertTrue(slash31.contains(slash32));
98 assertFalse(slash32.contains(differentSlash32));
99 assertFalse(differentSlash32.contains(slash32));
100
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700101 IpPrefix zero = IpPrefix.valueOf("0.0.0.0/0");
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700102 assertTrue(zero.contains(differentSlash32));
103 assertFalse(differentSlash32.contains(zero));
104
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700105 IpPrefix slash8 = IpPrefix.valueOf("10.0.0.0/8");
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700106 assertTrue(slash8.contains(slash31));
107 assertFalse(slash31.contains(slash8));
108 }
Jonathan Hart70da5122014-10-01 16:37:42 -0700109
110 @Test
111 public void testContainsIpAddress() {
112 IpPrefix slash31 = IpPrefix.valueOf(BYTES1, 31);
Pavlin Radoslavove7b5bd72014-10-27 20:17:24 -0700113 IpAddress addr32 = IpAddress.valueOf(BYTES1);
Jonathan Hart70da5122014-10-01 16:37:42 -0700114
Pavlin Radoslavove7b5bd72014-10-27 20:17:24 -0700115 assertTrue(slash31.contains(addr32));
Jonathan Hart70da5122014-10-01 16:37:42 -0700116
117 IpPrefix intf = IpPrefix.valueOf("192.168.10.101/24");
118 IpAddress addr = IpAddress.valueOf("192.168.10.1");
119
120 assertTrue(intf.contains(addr));
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700121
122 IpPrefix intf1 = IpPrefix.valueOf("10.0.0.101/24");
123 IpAddress addr1 = IpAddress.valueOf("10.0.0.4");
124
125 assertTrue(intf1.contains(addr1));
Jonathan Hart70da5122014-10-01 16:37:42 -0700126 }
Ayaka Koshibe16698a32014-09-13 22:19:02 -0700127}