Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.bgproute; |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 2 | |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertTrue; |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 6 | |
| 7 | import java.util.Arrays; |
| 8 | |
| 9 | import org.junit.After; |
| 10 | import org.junit.Before; |
| 11 | import org.junit.Test; |
| 12 | |
| 13 | public class PrefixTest { |
| 14 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 15 | @Before |
| 16 | public void setUp() throws Exception { |
| 17 | } |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 18 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 19 | @After |
| 20 | public void tearDown() throws Exception { |
| 21 | } |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 22 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 23 | @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 Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 29 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | 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 Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 34 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | //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 |
| 72 | int MAX_PREFIX_LENGTH = 32; |
| 73 | for (int prefixLength = 1; prefixLength <= MAX_PREFIX_LENGTH; prefixLength++) { |
| 74 | byte address[] = new byte[MAX_PREFIX_LENGTH / Byte.SIZE]; |
| 75 | |
| 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 Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 101 | } |