TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.proxyarp; |
| 2 | |
TeruU | 8b2d167 | 2014-04-25 17:02:56 -0700 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertNotNull; |
| 5 | import static org.junit.Assert.assertNull; |
| 6 | import static org.junit.Assert.assertTrue; |
| 7 | import static org.junit.Assert.fail; |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 8 | |
| 9 | import java.net.InetAddress; |
| 10 | import java.util.HashMap; |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 11 | import java.util.Map; |
| 12 | |
| 13 | import net.floodlightcontroller.util.MACAddress; |
| 14 | |
| 15 | import org.junit.After; |
| 16 | import org.junit.Before; |
| 17 | import org.junit.Test; |
| 18 | |
| 19 | public class ArpCacheTest { |
Yuta HIGUCHI | 1a1b44f | 2014-06-18 10:56:42 -0700 | [diff] [blame^] | 20 | |
| 21 | static { |
| 22 | // configuration to quickly fall back to instance mode for faster test run |
| 23 | System.setProperty("net.onrc.onos.core.datastore.hazelcast.client.attemptLimit", "0"); |
| 24 | } |
| 25 | |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 26 | ArpCache arpCache; |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 27 | InetAddress ip1, ip2; |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 28 | MACAddress mac, mac2; |
| 29 | Map<InetAddress, MACAddress> map; |
| 30 | |
| 31 | @Before |
| 32 | public void setUp() throws Exception { |
| 33 | arpCache = new ArpCache(); |
| 34 | arpCache.setArpEntryTimeoutConfig(1000); |
| 35 | mac = MACAddress.valueOf("00:01:02:03:04:05"); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 36 | ip1 = InetAddress.getByAddress(new byte[]{10, 0, 0, 1}); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 37 | mac2 = MACAddress.valueOf("00:01:02:03:04:06"); |
| 38 | ip2 = InetAddress.getByAddress(new byte[]{10, 0, 0, 2}); |
| 39 | } |
| 40 | |
| 41 | @After |
| 42 | public void tearDown() throws Exception { |
| 43 | arpCache = null; |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void testArpCache() { |
| 48 | assertNotNull(new ArpCache()); |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testLookup() { |
| 53 | testUpdate(); |
| 54 | assertEquals(mac2, arpCache.lookup(ip2)); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testUpdate() { |
| 59 | map = new HashMap<InetAddress, MACAddress>(); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 60 | arpCache.update(ip1, mac); |
| 61 | map.put(ip1, mac); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 62 | arpCache.update(ip2, mac2); |
| 63 | map.put(ip2, mac2); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 64 | assertEquals(mac, arpCache.lookup(ip1)); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testRemove() { |
| 69 | testUpdate(); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 70 | arpCache.remove(ip1); |
| 71 | assertNull(arpCache.lookup(ip1)); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testGetMappings() { |
| 76 | testUpdate(); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 77 | for (String macStr :arpCache.getMappings()) { |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 78 | assertNotNull(macStr); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testGetExpiredArpCacheIps() { |
| 84 | testUpdate(); |
TeruU | 8b2d167 | 2014-04-25 17:02:56 -0700 | [diff] [blame] | 85 | |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 86 | try { |
| 87 | Thread.sleep(3000); |
| 88 | } catch (InterruptedException e) { |
| 89 | fail(); |
| 90 | } |
TeruU | 8b2d167 | 2014-04-25 17:02:56 -0700 | [diff] [blame] | 91 | |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 92 | assertNotNull(arpCache.getExpiredArpCacheIps()); |
| 93 | assertEquals(map.size(), arpCache.getExpiredArpCacheIps().size()); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 94 | for (InetAddress ip : arpCache.getExpiredArpCacheIps()) { |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 95 | assertTrue(map.containsKey(ip)); |
| 96 | } |
| 97 | } |
| 98 | |
TeruU | 8b2d167 | 2014-04-25 17:02:56 -0700 | [diff] [blame] | 99 | @Test |
| 100 | public void testSetArpEntryTimeoutConfig() { |
| 101 | long arpEntryTimeout = 10000; |
| 102 | arpCache.setArpEntryTimeoutConfig(arpEntryTimeout); |
| 103 | assertEquals(arpEntryTimeout, arpCache.getArpEntryTimeout()); |
| 104 | } |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 105 | } |