blob: 34d4bc43b3bff357a7d65b9c3a9a30c224d1b60d [file] [log] [blame]
TeruU3c049c42014-04-15 10:13:25 -07001package net.onrc.onos.apps.proxyarp;
2
TeruU8b2d1672014-04-25 17:02:56 -07003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertNull;
6import static org.junit.Assert.assertTrue;
7import static org.junit.Assert.fail;
TeruU3c049c42014-04-15 10:13:25 -07008
9import java.net.InetAddress;
10import java.util.HashMap;
TeruU3c049c42014-04-15 10:13:25 -070011import java.util.Map;
12
13import net.floodlightcontroller.util.MACAddress;
14
15import org.junit.After;
16import org.junit.Before;
17import org.junit.Test;
18
19public 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();
TeruU8b2d1672014-04-25 17:02:56 -070079
TeruU3c049c42014-04-15 10:13:25 -070080 try {
81 Thread.sleep(3000);
82 } catch (InterruptedException e) {
83 fail();
84 }
TeruU8b2d1672014-04-25 17:02:56 -070085
TeruU3c049c42014-04-15 10:13:25 -070086 assertNotNull(arpCache.getExpiredArpCacheIps());
87 assertEquals(map.size(), arpCache.getExpiredArpCacheIps().size());
88 for(InetAddress ip : arpCache.getExpiredArpCacheIps()) {
89 assertTrue(map.containsKey(ip));
90 }
91 }
92
TeruU8b2d1672014-04-25 17:02:56 -070093 @Test
94 public void testSetArpEntryTimeoutConfig() {
95 long arpEntryTimeout = 10000;
96 arpCache.setArpEntryTimeoutConfig(arpEntryTimeout);
97 assertEquals(arpEntryTimeout, arpCache.getArpEntryTimeout());
98 }
TeruU3c049c42014-04-15 10:13:25 -070099}