Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.bgproute; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.util.Arrays; |
| 6 | |
| 7 | import org.junit.After; |
| 8 | import org.junit.Before; |
| 9 | import org.junit.Test; |
| 10 | |
| 11 | public class PrefixTest { |
| 12 | |
| 13 | @Before |
| 14 | public void setUp() throws Exception { |
| 15 | } |
| 16 | |
| 17 | @After |
| 18 | public void tearDown() throws Exception { |
| 19 | } |
| 20 | |
| 21 | @Test |
| 22 | public void testPrefixByteArray() { |
| 23 | byte[] b1 = new byte[] {(byte)0x8f, (byte)0xa0, (byte)0x00, (byte)0x00}; |
| 24 | byte[] b2 = new byte[] {(byte)0x8f, (byte)0xa0, (byte)0xff, (byte)0xff}; |
| 25 | byte[] b3 = new byte[] {(byte)0x8f, (byte)0xac, (byte)0x00, (byte)0x00}; |
| 26 | byte[] b4 = new byte[] {(byte)0x8f, (byte)0xa0, (byte)0x00, (byte)0x00}; |
| 27 | |
| 28 | Prefix p1 = new Prefix(b1, 12); |
| 29 | Prefix p2 = new Prefix(b2, 12); |
| 30 | Prefix p3 = new Prefix(b3, 12); |
| 31 | Prefix p4 = new Prefix(b4, 11); |
| 32 | |
| 33 | //Have different byte arrays, but should be equal after construction |
| 34 | assertTrue(p1.equals(p2)); |
| 35 | assertTrue(p2.equals(p3)); |
| 36 | |
| 37 | //Same byte array, but should be false |
| 38 | assertFalse(p1.equals(p4)); |
| 39 | |
| 40 | assertTrue(Arrays.equals(p1.getAddress(), p3.getAddress())); |
| 41 | assertTrue(p1.toString().equals(p2.toString())); |
| 42 | assertTrue(Arrays.equals(p1.getAddress(), p4.getAddress())); |
| 43 | assertFalse(p1.toString().equals(p4.toString())); |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void testPrefixString() { |
| 48 | Prefix p1 = new Prefix("192.168.166.0", 24); |
| 49 | Prefix p2 = new Prefix("192.168.166.0", 23); |
| 50 | Prefix p3 = new Prefix("192.168.166.128", 24); |
| 51 | Prefix p4 = new Prefix("192.168.166.128", 25); |
| 52 | |
| 53 | assertFalse(p1.equals(p2)); |
| 54 | assertTrue(Arrays.equals(p1.getAddress(), p2.getAddress())); |
| 55 | |
| 56 | assertTrue(p1.equals(p3)); |
| 57 | assertTrue(Arrays.equals(p1.getAddress(), p2.getAddress())); |
| 58 | |
| 59 | assertFalse(p3.equals(p4)); |
| 60 | assertFalse(Arrays.equals(p3.getAddress(), p4.getAddress())); |
| 61 | |
| 62 | assertTrue(p1.toString().equals(p3.toString())); |
| 63 | assertEquals(p1.hashCode(), p3.hashCode()); |
| 64 | } |
| 65 | |
Jonathan Hart | 9ea3121 | 2013-08-12 21:40:34 +1200 | [diff] [blame^] | 66 | @Test |
| 67 | public void testPrefixReturnsSame() { |
| 68 | //Create a prefix of all 1s for each prefix length. |
| 69 | //Check that Prefix doesn't mangle it |
| 70 | int MAX_PREFIX_LENGTH = 32; |
| 71 | for (int prefixLength = 1; prefixLength <= MAX_PREFIX_LENGTH; prefixLength++) { |
| 72 | byte address[] = new byte[MAX_PREFIX_LENGTH/Byte.SIZE]; |
| 73 | |
| 74 | int lastByte = (prefixLength - 1) / Byte.SIZE; |
| 75 | int lastBit = (prefixLength - 1) % Byte.SIZE; |
| 76 | |
| 77 | for (int j = 0; j < address.length; j++) { |
| 78 | if (j < lastByte) { |
| 79 | address[j] = (byte)0xff; |
| 80 | } |
| 81 | else if (j == lastByte) { |
| 82 | byte b = 0; |
| 83 | byte msb = (byte) 0x80; |
| 84 | for (int k = 0; k < Byte.SIZE; k++) { |
| 85 | if (k <= lastBit) { |
| 86 | b |= (msb >> k); |
| 87 | } |
| 88 | } |
| 89 | address[j] = b; |
| 90 | } |
| 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 | } |