blob: cee6898818c6b3fab6b973870724a23d1eba1154 [file] [log] [blame]
Jonathan Hart8f6dc092014-04-18 15:56:43 -07001package net.onrc.onos.apps.sdnip;
Jonathan Hart8f5f4682013-08-07 22:13:39 +12002
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
Jonathan Hart8f5f4682013-08-07 22:13:39 +12006
7import java.util.Arrays;
8
9import org.junit.After;
10import org.junit.Before;
11import org.junit.Test;
12
13public class PrefixTest {
14
Ray Milkey269ffb92014-04-03 14:43:30 -070015 @Before
16 public void setUp() throws Exception {
17 }
Jonathan Hart8f5f4682013-08-07 22:13:39 +120018
Ray Milkey269ffb92014-04-03 14:43:30 -070019 @After
20 public void tearDown() throws Exception {
21 }
Jonathan Hart8f5f4682013-08-07 22:13:39 +120022
Ray Milkey269ffb92014-04-03 14:43:30 -070023 @Test
24 public void testPrefixByteArray() {
25 byte[] b1 = new byte[]{(byte) 0x8f, (byte) 0xa0, (byte) 0x00, (byte) 0x00};
26 byte[] b2 = new byte[]{(byte) 0x8f, (byte) 0xa0, (byte) 0xff, (byte) 0xff};
27 byte[] b3 = new byte[]{(byte) 0x8f, (byte) 0xac, (byte) 0x00, (byte) 0x00};
28 byte[] b4 = new byte[]{(byte) 0x8f, (byte) 0xa0, (byte) 0x00, (byte) 0x00};
Jonathan Hart8f5f4682013-08-07 22:13:39 +120029
Ray Milkey269ffb92014-04-03 14:43:30 -070030 Prefix p1 = new Prefix(b1, 12);
31 Prefix p2 = new Prefix(b2, 12);
32 Prefix p3 = new Prefix(b3, 12);
33 Prefix p4 = new Prefix(b4, 11);
Jonathan Hart8f5f4682013-08-07 22:13:39 +120034
Ray Milkey269ffb92014-04-03 14:43:30 -070035 //Have different byte arrays, but should be equal after construction
36 assertTrue(p1.equals(p2));
37 assertTrue(p2.equals(p3));
38
39 //Same byte array, but should be false
40 assertFalse(p1.equals(p4));
41
42 assertTrue(Arrays.equals(p1.getAddress(), p3.getAddress()));
43 assertTrue(p1.toString().equals(p2.toString()));
44 assertTrue(Arrays.equals(p1.getAddress(), p4.getAddress()));
45 assertFalse(p1.toString().equals(p4.toString()));
46 }
47
48 @Test
49 public void testPrefixString() {
50 Prefix p1 = new Prefix("192.168.166.0", 24);
51 Prefix p2 = new Prefix("192.168.166.0", 23);
52 Prefix p3 = new Prefix("192.168.166.128", 24);
53 Prefix p4 = new Prefix("192.168.166.128", 25);
54
55 assertFalse(p1.equals(p2));
56 assertTrue(Arrays.equals(p1.getAddress(), p2.getAddress()));
57
58 assertTrue(p1.equals(p3));
59 assertTrue(Arrays.equals(p1.getAddress(), p2.getAddress()));
60
61 assertFalse(p3.equals(p4));
62 assertFalse(Arrays.equals(p3.getAddress(), p4.getAddress()));
63
64 assertTrue(p1.toString().equals(p3.toString()));
65 assertEquals(p1.hashCode(), p3.hashCode());
66 }
67
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -070068 private static final int MAX_PREFIX_LENGTH = 32;
69
Ray Milkey269ffb92014-04-03 14:43:30 -070070 @Test
71 public void testPrefixReturnsSame() {
72 //Create a prefix of all 1s for each prefix length.
73 //Check that Prefix doesn't mangle it
Ray Milkey269ffb92014-04-03 14:43:30 -070074 for (int prefixLength = 1; prefixLength <= MAX_PREFIX_LENGTH; prefixLength++) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070075 byte[] address = new byte[MAX_PREFIX_LENGTH / Byte.SIZE];
Ray Milkey269ffb92014-04-03 14:43:30 -070076
77 int lastByte = (prefixLength - 1) / Byte.SIZE;
78 int lastBit = (prefixLength - 1) % Byte.SIZE;
79
80 for (int j = 0; j < address.length; j++) {
81 if (j < lastByte) {
82 address[j] = (byte) 0xff;
83 } else if (j == lastByte) {
84 byte b = 0;
85 byte msb = (byte) 0x80;
86 for (int k = 0; k < Byte.SIZE; k++) {
87 if (k <= lastBit) {
88 b |= (msb >> k);
89 }
90 }
91 address[j] = b;
92 } else {
93 address[j] = 0;
94 }
95 }
96
97 Prefix p = new Prefix(address, prefixLength);
98 System.out.println(p.printAsBits());
99 assertTrue(Arrays.equals(address, p.getAddress()));
100 }
101 }
Jonathan Hart8f5f4682013-08-07 22:13:39 +1200102}