blob: f1a7b0d8e2b9d79acd5249efc6376d840c41b160 [file] [log] [blame]
Ayaka Koshibe16698a32014-09-13 22:19:02 -07001package org.onlab.packet;
2
3import static org.junit.Assert.assertEquals;
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -07004import static org.junit.Assert.assertTrue;
Ayaka Koshibe16698a32014-09-13 22:19:02 -07005
6import java.util.Arrays;
7
8import org.junit.Test;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -07009import org.onlab.packet.IpAddress.Version;
Ayaka Koshibe16698a32014-09-13 22:19:02 -070010
11import com.google.common.testing.EqualsTester;
12
13public class IPAddressTest {
14
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070015 private static final byte [] BYTES1 = new byte [] {0xa, 0x0, 0x0, 0xa};
16 private static final byte [] BYTES2 = new byte [] {0xa, 0x0, 0x0, 0xb};
17 private static final int INTVAL1 = 167772170;
18 private static final int INTVAL2 = 167772171;
19 private static final String STRVAL = "10.0.0.12";
20 private static final int MASK = 16;
Ayaka Koshibe16698a32014-09-13 22:19:02 -070021
22 @Test
23 public void testEquality() {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070024 IpAddress ip1 = IpAddress.valueOf(BYTES1);
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070025 IpAddress ip2 = IpAddress.valueOf(INTVAL1);
26 IpAddress ip3 = IpAddress.valueOf(BYTES2);
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070027 IpAddress ip4 = IpAddress.valueOf(INTVAL2);
28 IpAddress ip5 = IpAddress.valueOf(STRVAL);
Ayaka Koshibe16698a32014-09-13 22:19:02 -070029
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070030 new EqualsTester().addEqualityGroup(ip1, ip2)
31 .addEqualityGroup(ip3, ip4)
32 .addEqualityGroup(ip5)
Ayaka Koshibe16698a32014-09-13 22:19:02 -070033 .testEquals();
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070034
35 // string conversions
36 IpAddress ip6 = IpAddress.valueOf(BYTES1, MASK);
37 IpAddress ip7 = IpAddress.valueOf("10.0.0.10/16");
38 IpAddress ip8 = IpAddress.valueOf(new byte [] {0xa, 0x0, 0x0, 0xc});
39 assertEquals("incorrect address conversion", ip6, ip7);
40 assertEquals("incorrect address conversion", ip5, ip8);
Ayaka Koshibe16698a32014-09-13 22:19:02 -070041 }
42
43 @Test
44 public void basics() {
Ayaka Koshibe40e7fec2014-09-16 22:32:19 -070045 IpAddress ip1 = IpAddress.valueOf(BYTES1, MASK);
46 final byte [] bytes = new byte [] {0xa, 0x0, 0x0, 0xa};
47
48 //check fields
49 assertEquals("incorrect IP Version", Version.INET, ip1.version());
50 assertEquals("incorrect netmask", 16, ip1.netmask);
51 assertTrue("faulty toOctets()", Arrays.equals(bytes, ip1.toOctets()));
52 assertEquals("faulty toInt()", INTVAL1, ip1.toInt());
53 assertEquals("faulty toString()", "10.0.0.10/16", ip1.toString());
54 }
55
56 @Test
57 public void netmasks() {
58 // masked
59 IpAddress ip1 = IpAddress.valueOf(BYTES1, MASK);
60
61 IpAddress host = IpAddress.valueOf("0.0.0.10/16");
62 IpAddress network = IpAddress.valueOf("10.0.0.0/16");
63 assertEquals("incorrect host address", host, ip1.host());
64 assertEquals("incorrect network address", network, ip1.network());
65 assertEquals("incorrect netmask", "255.255.0.0", ip1.netmask().toString());
66
67 //unmasked
68 IpAddress ip2 = IpAddress.valueOf(BYTES1);
69 IpAddress umhost = IpAddress.valueOf("10.0.0.10/0");
70 IpAddress umnet = IpAddress.valueOf("0.0.0.0/0");
71 assertEquals("incorrect host address", umhost, ip2.host());
72 assertEquals("incorrect host address", umnet, ip2.network());
73 assertTrue("incorrect netmask",
74 Arrays.equals(IpAddress.ANY, ip2.netmask().toOctets()));
Ayaka Koshibe16698a32014-09-13 22:19:02 -070075 }
76}