blob: a5ac1be0168a898028b088d532a889df5cee96d1 [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -07001package org.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.openflow.exceptions.OFParseError;
10import org.openflow.exceptions.OFShortRead;
11
12public class IPv4Test {
13 byte[][] testAddresses = new byte[][] {
14 {0x01, 0x02, 0x03, 0x04 },
15 {127, 0, 0, 1},
16 {(byte) 192, (byte) 168, 0, 100 },
17 {(byte) 255, (byte) 255, (byte) 255, (byte) 255 }
18 };
19
20 String[] testStrings = {
21 "1.2.3.4",
22 "127.0.0.1",
23 "192.168.0.100",
24 "255.255.255.255"
25 };
26
27 int[] testInts = {
28 0x01020304,
29 0x7f000001,
30 (192 << 24) | (168 << 16) | 100,
31 0xffffffff
32 };
33
34 String[] invalidIPs = {
35 "",
36 ".",
37 "1.2..3.4",
38 "1.2.3.4.",
39 "257.11.225.1",
40 "-1.2.3.4",
41 "1.2.3.4.5",
42 "1.x.3.4",
43 "1.2x.3.4"
44 };
Yotam Harchold7b84202013-07-26 16:08:10 -070045
46 String[] ipsWithMask = {
47 "1.2.3.4/24",
48 "192.168.130.140/255.255.192.0",
49 "127.0.0.1/8",
50 "8.8.8.8",
51 };
52
53 boolean[] hasMask = {
54 true,
55 true,
56 true,
57 false
58 };
59
60 byte[][][] ipsWithMaskValues = {
61 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
62 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
63 new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } },
64 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, null }
65 };
Andreas Wundsam40e14f72013-05-06 14:49:08 -070066
67
68 @Test
69 public void testOfString() {
70 for(int i=0; i < testAddresses.length; i++ ) {
71 IPv4 ip = IPv4.of(testStrings[i]);
72 assertEquals(testInts[i], ip.getInt());
73 assertArrayEquals(testAddresses[i], ip.getBytes());
74 assertEquals(testStrings[i], ip.toString());
75 }
76 }
77
78 @Test
79 public void testOfByteArray() {
80 for(int i=0; i < testAddresses.length; i++ ) {
81 IPv4 ip = IPv4.of(testAddresses[i]);
82 assertEquals(testInts[i], ip.getInt());
83 assertArrayEquals(testAddresses[i], ip.getBytes());
84 assertEquals(testStrings[i], ip.toString());
85 }
86 }
87
88 @Test
89 public void testReadFrom() throws OFParseError, OFShortRead {
90 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchold7b84202013-07-26 16:08:10 -070091 IPv4 ip = IPv4.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
Andreas Wundsam40e14f72013-05-06 14:49:08 -070092 assertEquals(testInts[i], ip.getInt());
93 assertArrayEquals(testAddresses[i], ip.getBytes());
94 assertEquals(testStrings[i], ip.toString());
95 }
96 }
97
98
99 @Test
100 public void testInvalidIPs() throws OFParseError, OFShortRead {
101 for(String invalid : invalidIPs) {
102 try {
103 IPv4.of(invalid);
104 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
105 } catch(IllegalArgumentException e) {
106 // ok
107 }
108 }
109 }
Yotam Harchold7b84202013-07-26 16:08:10 -0700110
Yotam Harchold7b84202013-07-26 16:08:10 -0700111 @Test
Yotam Harchol6fccde62013-08-15 12:04:52 -0700112 public void testOfMasked() throws OFParseError, OFShortRead {
Yotam Harchold7b84202013-07-26 16:08:10 -0700113 for (int i = 0; i < ipsWithMask.length; i++) {
Yotam Harchol6fccde62013-08-15 12:04:52 -0700114 IPv4WithMask value = IPv4WithMask.of(ipsWithMask[i]);
115 if (!hasMask[i]) {
116 IPv4 ip = value.getValue();
Yotam Harchold7b84202013-07-26 16:08:10 -0700117 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
Yotam Harchol6fccde62013-08-15 12:04:52 -0700118 } else if (hasMask[i]) {
119 byte[] ipBytes = new byte[4];
120 System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
121 assertEquals(ipBytes.length, value.getValue().getBytes().length);
122 for (int j = 0; j < ipBytes.length; j++) {
123 ipBytes[j] &= ipsWithMaskValues[i][1][j];
Yotam Harchold7b84202013-07-26 16:08:10 -0700124 }
Yotam Harchol6fccde62013-08-15 12:04:52 -0700125
126 assertArrayEquals(ipBytes, value.getValue().getBytes());
127 assertArrayEquals(ipsWithMaskValues[i][1], value.getMask().getBytes());
Yotam Harchold7b84202013-07-26 16:08:10 -0700128 }
129 }
130 }
Andreas Wundsam40e14f72013-05-06 14:49:08 -0700131}