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