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.assertEquals; |
| 4 | import static org.junit.Assert.assertTrue; |
| 5 | import static org.junit.Assert.fail; |
| 6 | |
| 7 | import java.net.InetAddress; |
| 8 | import java.net.UnknownHostException; |
| 9 | import java.util.Arrays; |
| 10 | import java.util.HashMap; |
| 11 | import java.util.Iterator; |
| 12 | import java.util.Map; |
| 13 | |
| 14 | import org.junit.After; |
| 15 | import org.junit.Before; |
| 16 | import org.junit.Ignore; |
| 17 | import org.junit.Test; |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import com.google.common.net.InetAddresses; |
| 22 | |
| 23 | public class PtreeTest { |
| 24 | |
| 25 | private Logger log = LoggerFactory.getLogger(PtreeTest.class); |
| 26 | |
| 27 | private Ptree ptree; |
| 28 | private PatriciaTrie ooptrie; |
| 29 | |
| 30 | private Map<String, byte[]> byteAddresses; |
| 31 | |
| 32 | @Before |
| 33 | public void setUp() throws Exception { |
| 34 | ptree = new Ptree(32); |
| 35 | ooptrie = new PatriciaTrie(32); |
| 36 | |
| 37 | String[] strPrefixes = { |
| 38 | "192.168.10.0/24", |
| 39 | "192.168.10.0/23", |
| 40 | "192.168.10.0/22", |
| 41 | "192.0.0.0/7", |
| 42 | "192.168.11.0/24", |
| 43 | "10.0.23.128/25", |
| 44 | "206.17.144.0/20", |
| 45 | "9.17.0.0/12", |
| 46 | "192.168.0.0/16" |
| 47 | }; |
| 48 | |
| 49 | byteAddresses = new HashMap<String, byte[]>(strPrefixes.length+10); |
| 50 | for (String prefix : strPrefixes) { |
| 51 | String address = prefix.split("/")[0]; |
| 52 | int prefixLength = Integer.parseInt(prefix.split("/")[1]); |
| 53 | byteAddresses.put(prefix, InetAddresses.forString(address).getAddress()); |
| 54 | |
| 55 | PtreeNode node = ptree.acquire(byteAddresses.get(prefix), prefixLength); |
| 56 | node.rib = new Rib("192.168.10.101", "192.168.60.1", prefixLength); |
| 57 | ooptrie.put(new Prefix(byteAddresses.get(prefix), prefixLength), |
| 58 | new Rib("192.168.10.101", "192.168.60.1", prefixLength)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @After |
| 63 | public void tearDown() throws Exception { |
| 64 | } |
| 65 | |
| 66 | @Ignore |
| 67 | @Test |
| 68 | public void testAcquireByteArray() { |
| 69 | fail("Not yet implemented"); |
| 70 | } |
| 71 | |
| 72 | @Ignore |
| 73 | @Test |
| 74 | public void testAcquireByteArrayInt() { |
| 75 | //First let's test an empty Ptree |
| 76 | Ptree localPtree = new Ptree(32); |
| 77 | PtreeNode node = localPtree.acquire(new byte[] {0x00, 0x00, 0x00, 0x00}); |
| 78 | assertTrue(node != null && node.rib == null); |
| 79 | |
| 80 | //Now let's look at the prepopulated tree |
| 81 | String testPrefix = "206.17.144.0/20"; |
| 82 | PtreeNode existingNode = ptree.acquire(byteAddresses.get(testPrefix), 20); |
| 83 | printByteArray(existingNode.key); |
| 84 | printByteArray(byteAddresses.get(testPrefix)); |
| 85 | assertTrue(existingNode != null && existingNode.rib == null); |
| 86 | |
| 87 | assertTrue(Arrays.equals(existingNode.key, byteAddresses.get(testPrefix))); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testLookup() { |
| 92 | String prefix1 = "192.168.10.12"; |
| 93 | int length1 = 29; |
| 94 | PtreeNode node1 = ptree.lookup(InetAddresses.forString(prefix1).getAddress(), length1); |
| 95 | |
| 96 | //There should be no direct match |
| 97 | assertTrue(node1 == null); |
| 98 | |
| 99 | log.debug("{} null: {}", "node1", node1 == null ? "true" : "false"); |
| 100 | |
| 101 | String prefix2 = "206.17.144.0"; |
| 102 | int length2 = 20; |
| 103 | PtreeNode node2 = ptree.lookup(InetAddresses.forString(prefix2).getAddress(), length2); |
| 104 | |
| 105 | assertTrue(node2 != null); |
| 106 | assertTrue(Arrays.equals(node2.key, byteAddresses.get(prefix2 + "/" + length2))); |
| 107 | |
| 108 | log.debug("{} null: {}", "node2", node2 == null ? "true" : "false"); |
| 109 | if (node2 != null) { |
| 110 | log.debug("{} key: {}, keybits: {}", new Object[] {"node2", node2.key, node2.keyBits}); |
| 111 | } |
| 112 | |
| 113 | String prefix3 = "192.0.0.0"; |
| 114 | int length3 = 7; |
| 115 | PtreeNode node3 = ptree.lookup(InetAddresses.forString(prefix3).getAddress(), length3); |
| 116 | assertTrue(node3 != null); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testMatch() { |
| 121 | String prefix1 = "192.168.10.12"; |
| 122 | int length1 = 29; |
| 123 | PtreeNode node1 = ptree.match(InetAddresses.forString(prefix1).getAddress(), length1); |
| 124 | |
| 125 | //There should be no direct match, but we should get the covering prefix |
| 126 | assertTrue(node1 != null); |
| 127 | assertTrue(Arrays.equals(node1.key, byteAddresses.get("192.168.10.0/24"))); |
| 128 | |
| 129 | log.debug("{} null: {}", "node1", node1 == null ? "true" : "false"); |
| 130 | if (node1 != null) { |
| 131 | log.debug("{} key: {}, keybits: {}", new Object[] {"node1", node1.key, node1.keyBits}); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Ignore |
| 136 | @Test |
| 137 | public void testTraverse() { |
| 138 | |
| 139 | String expected = "[0, 0, 0, 0]/0\n"; |
| 140 | expected += "[8, 0, 0, 0]/6\n"; |
| 141 | expected += "[9, 17, 0, 0]/12\n"; |
| 142 | expected += "[10, 0, 23, -128]/25\n"; |
| 143 | expected += "[-64, 0, 0, 0]/4\n"; |
| 144 | expected += "[-64, -88, 0, 0]/16\n"; |
| 145 | expected += "[-64, -88, 8, 0]/22\n"; |
| 146 | expected += "[-64, -88, 10, 0]/23\n"; |
| 147 | expected += "[-64, -88, 10, 0]/24\n"; |
| 148 | expected += "[-64, -88, 11, 0]/24\n"; |
| 149 | expected += "[-50, 17, -112, 0]/20\n"; |
| 150 | |
| 151 | PtreeNode node; |
| 152 | String result = ""; |
| 153 | |
| 154 | for (node = ptree.begin(); node != null; node = ptree.next(node)) { |
| 155 | result += printByteArray(node.key) + "/" + node.keyBits + "\n"; |
| 156 | } |
| 157 | |
| 158 | assertEquals(expected, result); |
| 159 | } |
| 160 | |
| 161 | @Ignore |
| 162 | @Test |
| 163 | public void testBegin() { |
| 164 | fail("Not yet implemented"); |
| 165 | } |
| 166 | |
| 167 | @Ignore |
| 168 | @Test |
| 169 | public void testNext() { |
| 170 | fail("Not yet implemented"); |
| 171 | } |
| 172 | |
| 173 | @Ignore |
| 174 | @Test |
| 175 | public void testDelReference() { |
| 176 | fail("Not yet implemented"); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void testMisc() { |
| 181 | int bitIndex = -1; |
| 182 | int index = (int)(bitIndex / Byte.SIZE); |
| 183 | int bit = (int)(bitIndex % Byte.SIZE); |
| 184 | |
| 185 | log.debug("index {} bit {}", index, bit); |
| 186 | log.debug("percent {}", 1%8); |
| 187 | |
| 188 | //PtreeNode node1 = new PtreeNode(new byte[] {0x0, 0x0, 0x0, 0x0}, 0, 4); |
| 189 | PtreeNode node1 = new PtreeNode(null, 0, 4); |
| 190 | log.debug("node1: key {}, keybits {}", printByteArray(node1.key), node1.keyBits); |
| 191 | |
| 192 | //PtreeNode node2 = new PtreeNode(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff}, |
| 193 | PtreeNode node2 = new PtreeNode(null, |
| 194 | 32, 4); |
| 195 | log.debug("node2: key {}, keybits {}", printByteArray(node2.key), node2.keyBits); |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testIteration() { |
| 200 | Iterator<IPatriciaTrie.Entry> it = ooptrie.iterator(); |
| 201 | |
| 202 | while (it.hasNext()) { |
| 203 | IPatriciaTrie.Entry entry = it.next(); |
| 204 | log.debug("PatriciaTrie prefix {} \t {}", entry.getPrefix(), entry.getPrefix().printAsBits()); |
| 205 | } |
| 206 | |
| 207 | try { |
| 208 | PtreeNode node; |
| 209 | for (node = ptree.begin(); node != null; node = ptree.next(node)) { |
| 210 | log.debug("Ptree prefix {}/{}", InetAddress.getByAddress(node.key).getHostAddress(), node.keyBits); |
| 211 | } |
| 212 | } catch (UnknownHostException e) { |
| 213 | |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private String printByteArray(byte[] array) { |
| 218 | String result = "["; |
| 219 | for (byte b : array) { |
| 220 | result += b + ", "; |
| 221 | } |
| 222 | result = result.substring(0, result.length() - 2); |
| 223 | result += "]"; |
| 224 | return result; |
| 225 | } |
| 226 | |
| 227 | } |