blob: a0005644a288f7c2fbdc25832d2a2ef025ccf7a0 [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
68 @Test
69 public void testPrefixReturnsSame() {
70 //Create a prefix of all 1s for each prefix length.
71 //Check that Prefix doesn't mangle it
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070072 final int MAX_PREFIX_LENGTH = 32;
Ray Milkey269ffb92014-04-03 14:43:30 -070073 for (int prefixLength = 1; prefixLength <= MAX_PREFIX_LENGTH; prefixLength++) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070074 byte[] address = new byte[MAX_PREFIX_LENGTH / Byte.SIZE];
Ray Milkey269ffb92014-04-03 14:43:30 -070075
76 int lastByte = (prefixLength - 1) / Byte.SIZE;
77 int lastBit = (prefixLength - 1) % Byte.SIZE;
78
79 for (int j = 0; j < address.length; j++) {
80 if (j < lastByte) {
81 address[j] = (byte) 0xff;
82 } else if (j == lastByte) {
83 byte b = 0;
84 byte msb = (byte) 0x80;
85 for (int k = 0; k < Byte.SIZE; k++) {
86 if (k <= lastBit) {
87 b |= (msb >> k);
88 }
89 }
90 address[j] = b;
91 } else {
92 address[j] = 0;
93 }
94 }
95
96 Prefix p = new Prefix(address, prefixLength);
97 System.out.println(p.printAsBits());
98 assertTrue(Arrays.equals(address, p.getAddress()));
99 }
100 }
Jonathan Hart8f5f4682013-08-07 22:13:39 +1200101}