blob: b6e5e86f1cab71fa92a4027f78f96cf980ee6448 [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 {
Yuta HIGUCHI1a1b44f2014-06-18 10:56:42 -070020
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
TeruU3c049c42014-04-15 10:13:25 -070026 ArpCache arpCache;
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070027 InetAddress ip1, ip2;
TeruU3c049c42014-04-15 10:13:25 -070028 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 HIGUCHI44a0b352014-05-14 21:32:48 -070036 ip1 = InetAddress.getByAddress(new byte[]{10, 0, 0, 1});
TeruU3c049c42014-04-15 10:13:25 -070037 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 HIGUCHI44a0b352014-05-14 21:32:48 -070060 arpCache.update(ip1, mac);
61 map.put(ip1, mac);
TeruU3c049c42014-04-15 10:13:25 -070062 arpCache.update(ip2, mac2);
63 map.put(ip2, mac2);
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070064 assertEquals(mac, arpCache.lookup(ip1));
TeruU3c049c42014-04-15 10:13:25 -070065 }
66
67 @Test
68 public void testRemove() {
69 testUpdate();
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070070 arpCache.remove(ip1);
71 assertNull(arpCache.lookup(ip1));
TeruU3c049c42014-04-15 10:13:25 -070072 }
73
74 @Test
75 public void testGetMappings() {
76 testUpdate();
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070077 for (String macStr :arpCache.getMappings()) {
TeruU3c049c42014-04-15 10:13:25 -070078 assertNotNull(macStr);
79 }
80 }
81
82 @Test
83 public void testGetExpiredArpCacheIps() {
84 testUpdate();
TeruU8b2d1672014-04-25 17:02:56 -070085
TeruU3c049c42014-04-15 10:13:25 -070086 try {
87 Thread.sleep(3000);
88 } catch (InterruptedException e) {
89 fail();
90 }
TeruU8b2d1672014-04-25 17:02:56 -070091
TeruU3c049c42014-04-15 10:13:25 -070092 assertNotNull(arpCache.getExpiredArpCacheIps());
93 assertEquals(map.size(), arpCache.getExpiredArpCacheIps().size());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070094 for (InetAddress ip : arpCache.getExpiredArpCacheIps()) {
TeruU3c049c42014-04-15 10:13:25 -070095 assertTrue(map.containsKey(ip));
96 }
97 }
98
TeruU8b2d1672014-04-25 17:02:56 -070099 @Test
100 public void testSetArpEntryTimeoutConfig() {
101 long arpEntryTimeout = 10000;
102 arpCache.setArpEntryTimeoutConfig(arpEntryTimeout);
103 assertEquals(arpEntryTimeout, arpCache.getArpEntryTimeout());
104 }
TeruU3c049c42014-04-15 10:13:25 -0700105}