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 { |
| 20 | ArpCache arpCache; |
| 21 | InetAddress ip, ip2; |
| 22 | MACAddress mac, mac2; |
| 23 | Map<InetAddress, MACAddress> map; |
| 24 | |
| 25 | @Before |
| 26 | public void setUp() throws Exception { |
| 27 | arpCache = new ArpCache(); |
| 28 | arpCache.setArpEntryTimeoutConfig(1000); |
| 29 | mac = MACAddress.valueOf("00:01:02:03:04:05"); |
| 30 | ip = InetAddress.getByAddress(new byte[]{10, 0, 0, 1}); |
| 31 | mac2 = MACAddress.valueOf("00:01:02:03:04:06"); |
| 32 | ip2 = InetAddress.getByAddress(new byte[]{10, 0, 0, 2}); |
| 33 | } |
| 34 | |
| 35 | @After |
| 36 | public void tearDown() throws Exception { |
| 37 | arpCache = null; |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | public void testArpCache() { |
| 42 | assertNotNull(new ArpCache()); |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | public void testLookup() { |
| 47 | testUpdate(); |
| 48 | assertEquals(mac2, arpCache.lookup(ip2)); |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testUpdate() { |
| 53 | map = new HashMap<InetAddress, MACAddress>(); |
| 54 | arpCache.update(ip, mac); |
| 55 | map.put(ip, mac); |
| 56 | arpCache.update(ip2, mac2); |
| 57 | map.put(ip2, mac2); |
| 58 | assertEquals(mac, arpCache.lookup(ip)); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testRemove() { |
| 63 | testUpdate(); |
| 64 | arpCache.remove(ip); |
| 65 | assertNull(arpCache.lookup(ip)); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testGetMappings() { |
| 70 | testUpdate(); |
| 71 | for(String macStr :arpCache.getMappings()) { |
| 72 | assertNotNull(macStr); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testGetExpiredArpCacheIps() { |
| 78 | testUpdate(); |
TeruU | 8b2d167 | 2014-04-25 17:02:56 -0700 | [diff] [blame^] | 79 | |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 80 | try { |
| 81 | Thread.sleep(3000); |
| 82 | } catch (InterruptedException e) { |
| 83 | fail(); |
| 84 | } |
TeruU | 8b2d167 | 2014-04-25 17:02:56 -0700 | [diff] [blame^] | 85 | |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 86 | assertNotNull(arpCache.getExpiredArpCacheIps()); |
| 87 | assertEquals(map.size(), arpCache.getExpiredArpCacheIps().size()); |
| 88 | for(InetAddress ip : arpCache.getExpiredArpCacheIps()) { |
| 89 | assertTrue(map.containsKey(ip)); |
| 90 | } |
| 91 | } |
| 92 | |
TeruU | 8b2d167 | 2014-04-25 17:02:56 -0700 | [diff] [blame^] | 93 | @Test |
| 94 | public void testSetArpEntryTimeoutConfig() { |
| 95 | long arpEntryTimeout = 10000; |
| 96 | arpCache.setArpEntryTimeoutConfig(arpEntryTimeout); |
| 97 | assertEquals(arpEntryTimeout, arpCache.getArpEntryTimeout()); |
| 98 | } |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 99 | } |