blob: 4f3f968602367c3ee7a86287004d7f7f343faa65 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import static org.junit.Assert.assertArrayEquals;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.fail;
6
7import org.jboss.netty.buffer.ChannelBuffers;
8import org.junit.Test;
9import org.projectfloodlight.openflow.exceptions.OFParseError;
10
Yotam Harchola289d552013-09-16 10:10:40 -070011public class IPv4AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070012 byte[][] testAddresses = new byte[][] {
13 {0x01, 0x02, 0x03, 0x04 },
14 {127, 0, 0, 1},
15 {(byte) 192, (byte) 168, 0, 100 },
16 {(byte) 255, (byte) 255, (byte) 255, (byte) 255 }
17 };
18
19 String[] testStrings = {
20 "1.2.3.4",
21 "127.0.0.1",
22 "192.168.0.100",
23 "255.255.255.255"
24 };
25
26 int[] testInts = {
27 0x01020304,
28 0x7f000001,
29 (192 << 24) | (168 << 16) | 100,
30 0xffffffff
31 };
32
33 String[] invalidIPs = {
34 "",
35 ".",
36 "1.2..3.4",
37 "1.2.3.4.",
38 "257.11.225.1",
39 "-1.2.3.4",
40 "1.2.3.4.5",
41 "1.x.3.4",
42 "1.2x.3.4"
43 };
44
45 String[] ipsWithMask = {
46 "1.2.3.4/24",
47 "192.168.130.140/255.255.192.0",
48 "127.0.0.1/8",
49 "8.8.8.8",
50 };
51
52 boolean[] hasMask = {
53 true,
54 true,
55 true,
56 false
57 };
58
59 byte[][][] ipsWithMaskValues = {
60 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
61 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
62 new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } },
63 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, null }
64 };
65
66
67 @Test
68 public void testOfString() {
69 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -070070 IPv4Address ip = IPv4Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -070071 assertEquals(testInts[i], ip.getInt());
72 assertArrayEquals(testAddresses[i], ip.getBytes());
73 assertEquals(testStrings[i], ip.toString());
74 }
75 }
76
77 @Test
78 public void testOfByteArray() {
79 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -070080 IPv4Address ip = IPv4Address.of(testAddresses[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -070081 assertEquals(testInts[i], ip.getInt());
82 assertArrayEquals(testAddresses[i], ip.getBytes());
83 assertEquals(testStrings[i], ip.toString());
84 }
85 }
86
87 @Test
88 public void testReadFrom() throws OFParseError {
89 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -070090 IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
Yotam Harcholf3f11152013-09-05 16:47:16 -070091 assertEquals(testInts[i], ip.getInt());
92 assertArrayEquals(testAddresses[i], ip.getBytes());
93 assertEquals(testStrings[i], ip.toString());
94 }
95 }
96
97
98 @Test
99 public void testInvalidIPs() throws OFParseError {
100 for(String invalid : invalidIPs) {
101 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700102 IPv4Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700103 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
104 } catch(IllegalArgumentException e) {
105 // ok
106 }
107 }
108 }
109
110 @Test
111 public void testOfMasked() throws OFParseError {
112 for (int i = 0; i < ipsWithMask.length; i++) {
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700113 IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700114 if (!hasMask[i]) {
Yotam Harchola289d552013-09-16 10:10:40 -0700115 IPv4Address ip = value.getValue();
Yotam Harcholf3f11152013-09-05 16:47:16 -0700116 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
117 } else if (hasMask[i]) {
118 byte[] ipBytes = new byte[4];
119 System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
120 assertEquals(ipBytes.length, value.getValue().getBytes().length);
121 for (int j = 0; j < ipBytes.length; j++) {
122 ipBytes[j] &= ipsWithMaskValues[i][1][j];
123 }
124
125 assertArrayEquals(ipBytes, value.getValue().getBytes());
126 assertArrayEquals(ipsWithMaskValues[i][1], value.getMask().getBytes());
127 }
128 }
129 }
130}