blob: d3440215112e4e18c3fcf73f8e16ed72dfa740c7 [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
111 @SuppressWarnings("unchecked")
112 @Test
113 public void testOfPossiblyMasked() throws OFParseError, OFShortRead {
114 for (int i = 0; i < ipsWithMask.length; i++) {
115 OFValueType value = IPv4WithMask.ofPossiblyMasked(ipsWithMask[i]);
116 if (value instanceof IPv4 && !hasMask[i]) {
117 // Types OK, check values
118 IPv4 ip = (IPv4)value;
119 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
120 } else if (value instanceof Masked && hasMask[i]) {
121 Masked<IPv4> ip = null;
122 try {
123 ip = (Masked<IPv4>)value;
124 } catch (ClassCastException e) {
125 fail("Invalid Masked<T> type.");
126 }
127 // Types OK, check values
128 assertArrayEquals(ipsWithMaskValues[i][0], ip.getValue().getBytes());
129 assertArrayEquals(ipsWithMaskValues[i][1], ip.getMask().getBytes());
130 } else if (value instanceof IPv4) {
131 fail("Expected masked IPv4, got unmasked IPv4.");
132 } else {
133 fail("Expected unmasked IPv4, got masked IPv4.");
134 }
135 }
136 }
Andreas Wundsam40e14f72013-05-06 14:49:08 -0700137}