blob: c00fad4e50ddf25d09c214885536ed15daa02a27 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Andreas Wundsam12cea882013-12-15 15:05:50 -08003import static org.hamcrest.CoreMatchers.equalTo;
Yotam Harcholf3f11152013-09-05 16:47:16 -07004import static org.junit.Assert.assertArrayEquals;
5import static org.junit.Assert.assertEquals;
Andreas Wundsame04c86f2013-11-05 17:13:18 -08006import static org.junit.Assert.assertThat;
Yotam Harcholf3f11152013-09-05 16:47:16 -07007import static org.junit.Assert.fail;
8
Andreas Wundsame04c86f2013-11-05 17:13:18 -08009import org.hamcrest.CoreMatchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070010import org.jboss.netty.buffer.ChannelBuffers;
11import org.junit.Test;
12import org.projectfloodlight.openflow.exceptions.OFParseError;
13
Yotam Harchola289d552013-09-16 10:10:40 -070014public class IPv4AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070015 byte[][] testAddresses = new byte[][] {
16 {0x01, 0x02, 0x03, 0x04 },
17 {127, 0, 0, 1},
18 {(byte) 192, (byte) 168, 0, 100 },
19 {(byte) 255, (byte) 255, (byte) 255, (byte) 255 }
20 };
21
22 String[] testStrings = {
23 "1.2.3.4",
24 "127.0.0.1",
25 "192.168.0.100",
26 "255.255.255.255"
27 };
28
29 int[] testInts = {
30 0x01020304,
31 0x7f000001,
32 (192 << 24) | (168 << 16) | 100,
33 0xffffffff
34 };
35
36 String[] invalidIPs = {
37 "",
38 ".",
39 "1.2..3.4",
40 "1.2.3.4.",
41 "257.11.225.1",
42 "-1.2.3.4",
43 "1.2.3.4.5",
44 "1.x.3.4",
45 "1.2x.3.4"
46 };
47
48 String[] ipsWithMask = {
49 "1.2.3.4/24",
50 "192.168.130.140/255.255.192.0",
51 "127.0.0.1/8",
52 "8.8.8.8",
Andreas Wundsame04c86f2013-11-05 17:13:18 -080053 "0.0.0.0/0"
Yotam Harcholf3f11152013-09-05 16:47:16 -070054 };
55
56 boolean[] hasMask = {
57 true,
58 true,
59 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080060 false,
61 true
Yotam Harcholf3f11152013-09-05 16:47:16 -070062 };
63
64 byte[][][] ipsWithMaskValues = {
65 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
66 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
67 new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } },
Andreas Wundsame04c86f2013-11-05 17:13:18 -080068 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, null },
69 new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } }
Yotam Harcholf3f11152013-09-05 16:47:16 -070070 };
71
Andreas Wundsam12cea882013-12-15 15:05:50 -080072 @Test
73 public void testMaskedMatchesCidr() {
74 IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("10.0.42.16/28");
75
76 String[] notContained = {"0.0.0.0", "11.0.42.16", "10.0.41.1", "10.0.42.0", "10.0.42.15",
77 "10.0.42.32", "255.255.255.255" };
78
79 for(String n: notContained) {
80 assertThat(String.format("slash 28 %s should not contain address %s",
81 slash28, n),
82 slash28.matches(IPv4Address.of(n)), equalTo(false));
83 }
84 for(int i=16; i < 32; i++) {
85 IPv4Address c = IPv4Address.of(String.format("10.0.42.%d", i));
86 assertThat(String.format("slash 28 %s should contain address %s",
87 slash28, c),
88 slash28.matches(c), equalTo(true));
89 }
90 }
91
92 @Test
93 public void testMaskedMatchesArbitrary() {
94 // irregular octect on the 3rd bitmask requires '1'bit to be set
95 // 4 bit unset, all others arbitrary
96 IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("1.2.1.4/255.255.5.255");
97
98 String[] notContained = {"0.0.0.0", "1.2.3.5", "1.2.3.3",
99 "1.2.0.4", "1.2.2.4", "1.2.4.4", "1.2.5.4", "1.2.6.4", "1.2.7.4",
100 "1.2.8.4", "1.2.12.4", "1.2.13.4"
101 };
102 String[] contained = {"1.2.1.4", "1.2.3.4", "1.2.9.4", "1.2.11.4", "1.2.251.4",
103 };
104
105 for(String n: notContained) {
106 assertThat(String.format("slash 28 %s should not contain address %s",
107 slash28, n),
108 slash28.matches(IPv4Address.of(n)), equalTo(false));
109 }
110 for(String c: contained) {
111 IPv4Address addr = IPv4Address.of(c);
112 assertThat(String.format("slash 28 %s should contain address %s",
113 slash28, addr),
114 slash28.matches(addr), equalTo(true));
115 }
116
117 }
118
Yotam Harcholf3f11152013-09-05 16:47:16 -0700119
120 @Test
121 public void testOfString() {
122 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700123 IPv4Address ip = IPv4Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700124 assertEquals(testInts[i], ip.getInt());
125 assertArrayEquals(testAddresses[i], ip.getBytes());
126 assertEquals(testStrings[i], ip.toString());
127 }
128 }
129
130 @Test
131 public void testOfByteArray() {
132 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700133 IPv4Address ip = IPv4Address.of(testAddresses[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700134 assertEquals(testInts[i], ip.getInt());
135 assertArrayEquals(testAddresses[i], ip.getBytes());
136 assertEquals(testStrings[i], ip.toString());
137 }
138 }
139
140 @Test
141 public void testReadFrom() throws OFParseError {
142 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700143 IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700144 assertEquals(testInts[i], ip.getInt());
145 assertArrayEquals(testAddresses[i], ip.getBytes());
146 assertEquals(testStrings[i], ip.toString());
147 }
148 }
149
150
151 @Test
152 public void testInvalidIPs() throws OFParseError {
153 for(String invalid : invalidIPs) {
154 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700155 IPv4Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700156 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
157 } catch(IllegalArgumentException e) {
158 // ok
159 }
160 }
161 }
162
163 @Test
164 public void testOfMasked() throws OFParseError {
165 for (int i = 0; i < ipsWithMask.length; i++) {
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700166 IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700167 if (!hasMask[i]) {
Yotam Harchola289d552013-09-16 10:10:40 -0700168 IPv4Address ip = value.getValue();
Yotam Harcholf3f11152013-09-05 16:47:16 -0700169 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
170 } else if (hasMask[i]) {
171 byte[] ipBytes = new byte[4];
172 System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
173 assertEquals(ipBytes.length, value.getValue().getBytes().length);
174 for (int j = 0; j < ipBytes.length; j++) {
175 ipBytes[j] &= ipsWithMaskValues[i][1][j];
176 }
177
178 assertArrayEquals(ipBytes, value.getValue().getBytes());
Andreas Wundsame04c86f2013-11-05 17:13:18 -0800179 assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
180 value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700181 }
182 }
183 }
184}