blob: 3518c160a4374ddaa1a72efa119d32e0c63e47f0 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Ayaka Koshibe16698a32014-09-13 22:19:02 -070019package org.onlab.packet;
20
21import static org.junit.Assert.assertEquals;
Jonathan Hartb7a2ac32014-09-19 10:42:27 -070022import static org.junit.Assert.assertFalse;
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070023import static org.junit.Assert.assertTrue;
Ayaka Koshibe16698a32014-09-13 22:19:02 -070024
25import java.util.Arrays;
26
27import org.junit.Test;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070028import org.onlab.packet.IpPrefix.Version;
Ayaka Koshibe16698a32014-09-13 22:19:02 -070029
30import com.google.common.testing.EqualsTester;
31
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070032public class IpPrefixTest {
Ayaka Koshibe16698a32014-09-13 22:19:02 -070033
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070034 private static final byte [] BYTES1 = new byte [] {0xa, 0x0, 0x0, 0xa};
35 private static final byte [] BYTES2 = new byte [] {0xa, 0x0, 0x0, 0xb};
36 private static final int INTVAL1 = 167772170;
37 private static final int INTVAL2 = 167772171;
38 private static final String STRVAL = "10.0.0.12";
39 private static final int MASK = 16;
Ayaka Koshibe16698a32014-09-13 22:19:02 -070040
41 @Test
42 public void testEquality() {
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070043 IpPrefix ip1 = IpPrefix.valueOf(BYTES1);
44 IpPrefix ip2 = IpPrefix.valueOf(INTVAL1);
45 IpPrefix ip3 = IpPrefix.valueOf(BYTES2);
46 IpPrefix ip4 = IpPrefix.valueOf(INTVAL2);
47 IpPrefix ip5 = IpPrefix.valueOf(STRVAL);
Ayaka Koshibe16698a32014-09-13 22:19:02 -070048
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070049 new EqualsTester().addEqualityGroup(ip1, ip2)
50 .addEqualityGroup(ip3, ip4)
51 .addEqualityGroup(ip5)
Ayaka Koshibe16698a32014-09-13 22:19:02 -070052 .testEquals();
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070053
54 // string conversions
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070055 IpPrefix ip6 = IpPrefix.valueOf(BYTES1, MASK);
56 IpPrefix ip7 = IpPrefix.valueOf("10.0.0.10/16");
57 IpPrefix ip8 = IpPrefix.valueOf(new byte [] {0xa, 0x0, 0x0, 0xc});
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070058 assertEquals("incorrect address conversion", ip6, ip7);
59 assertEquals("incorrect address conversion", ip5, ip8);
Ayaka Koshibe16698a32014-09-13 22:19:02 -070060 }
61
62 @Test
63 public void basics() {
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070064 IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK);
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070065 final byte [] bytes = new byte [] {0xa, 0x0, 0x0, 0xa};
66
67 //check fields
68 assertEquals("incorrect IP Version", Version.INET, ip1.version());
69 assertEquals("incorrect netmask", 16, ip1.netmask);
70 assertTrue("faulty toOctets()", Arrays.equals(bytes, ip1.toOctets()));
71 assertEquals("faulty toInt()", INTVAL1, ip1.toInt());
72 assertEquals("faulty toString()", "10.0.0.10/16", ip1.toString());
73 }
74
75 @Test
76 public void netmasks() {
77 // masked
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070078 IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK);
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070079
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070080 IpPrefix host = IpPrefix.valueOf("0.0.0.10/16");
81 IpPrefix network = IpPrefix.valueOf("10.0.0.0/16");
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070082 assertEquals("incorrect host address", host, ip1.host());
83 assertEquals("incorrect network address", network, ip1.network());
84 assertEquals("incorrect netmask", "255.255.0.0", ip1.netmask().toString());
85
86 //unmasked
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070087 IpPrefix ip2 = IpPrefix.valueOf(BYTES1);
88 IpPrefix umhost = IpPrefix.valueOf("10.0.0.10/0");
89 IpPrefix umnet = IpPrefix.valueOf("0.0.0.0/0");
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070090 assertEquals("incorrect host address", umhost, ip2.host());
91 assertEquals("incorrect host address", umnet, ip2.network());
92 assertTrue("incorrect netmask",
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070093 Arrays.equals(IpPrefix.ANY, ip2.netmask().toOctets()));
Ayaka Koshibe16698a32014-09-13 22:19:02 -070094 }
Jonathan Hartb7a2ac32014-09-19 10:42:27 -070095
96 @Test
Jonathan Hart70da5122014-10-01 16:37:42 -070097 public void testContainsIpPrefix() {
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070098 IpPrefix slash31 = IpPrefix.valueOf(BYTES1, 31);
99 IpPrefix slash32 = IpPrefix.valueOf(BYTES1, 32);
100 IpPrefix differentSlash32 = IpPrefix.valueOf(BYTES2, 32);
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700101
102 assertTrue(slash31.contains(differentSlash32));
103 assertFalse(differentSlash32.contains(slash31));
104
105 assertTrue(slash31.contains(slash32));
106 assertFalse(slash32.contains(differentSlash32));
107 assertFalse(differentSlash32.contains(slash32));
108
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700109 IpPrefix zero = IpPrefix.valueOf("0.0.0.0/0");
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700110 assertTrue(zero.contains(differentSlash32));
111 assertFalse(differentSlash32.contains(zero));
112
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700113 IpPrefix slash8 = IpPrefix.valueOf("10.0.0.0/8");
Jonathan Hartb7a2ac32014-09-19 10:42:27 -0700114 assertTrue(slash8.contains(slash31));
115 assertFalse(slash31.contains(slash8));
116 }
Jonathan Hart70da5122014-10-01 16:37:42 -0700117
118 @Test
119 public void testContainsIpAddress() {
120 IpPrefix slash31 = IpPrefix.valueOf(BYTES1, 31);
121 IpAddress slash32 = IpAddress.valueOf(BYTES1, 32);
122
123 assertTrue(slash31.contains(slash32));
124
125 IpPrefix intf = IpPrefix.valueOf("192.168.10.101/24");
126 IpAddress addr = IpAddress.valueOf("192.168.10.1");
127
128 assertTrue(intf.contains(addr));
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700129
130 IpPrefix intf1 = IpPrefix.valueOf("10.0.0.101/24");
131 IpAddress addr1 = IpAddress.valueOf("10.0.0.4");
132
133 assertTrue(intf1.contains(addr1));
Jonathan Hart70da5122014-10-01 16:37:42 -0700134 }
Ayaka Koshibe16698a32014-09-13 22:19:02 -0700135}