-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathDNSCacheManagerTest.java
More file actions
124 lines (96 loc) · 3.9 KB
/
Copy pathDNSCacheManagerTest.java
File metadata and controls
124 lines (96 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package com.amazonaws.services.lambda.crac;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.JniHelper;
import java.util.Map;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.lang.reflect.Field;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
@DisabledOnOs(OS.MAC)
public class DNSCacheManagerTest {
static String CACHE_FIELD_NAME = "cache";
// this should have no effect, as the DNS cache is cleared explicitly in these tests
static {
java.security.Security.setProperty("networkaddress.cache.ttl" , "10000");
java.security.Security.setProperty("networkaddress.cache.negative.ttl" , "10000");
}
@BeforeAll
public static void jniLoad() {
JniHelper.load();
}
@BeforeEach
public void setup() {
Core.resetGlobalContext();
DNSManager.clearCache();
}
static class StatefulResource implements Resource {
int state = 0;
@Override
public void afterRestore(Context<? extends Resource> context) {
state += 1;
}
@Override
public void beforeCheckpoint(Context<? extends Resource> context) {
state += 2;
}
int getValue() {
return state;
}
}
@Test
public void positiveDnsCacheShouldBeEmpty() throws CheckpointException, RestoreException, UnknownHostException, ReflectiveOperationException {
int baselineDNSEntryCount = getDNSEntryCount();
StatefulResource resource = new StatefulResource();
Core.getGlobalContext().register(resource);
String[] hosts = {"github.com", "amazonaws.com"};
for(String singleHost : hosts) {
InetAddress address = InetAddress.getByName(singleHost);
}
// n hosts -> n DNS entries
assertEquals(hosts.length, getDNSEntryCount() - baselineDNSEntryCount);
// this should call the native static method clearDNSCache
Core.getGlobalContext().beforeCheckpoint(null);
// cache should be cleared
assertEquals(0, getDNSEntryCount());
}
@Test
public void negativeDnsCacheShouldBeEmpty() throws CheckpointException, RestoreException, UnknownHostException, ReflectiveOperationException {
int baselineDNSEntryCount = getDNSEntryCount();
StatefulResource resource = new StatefulResource();
Core.getGlobalContext().register(resource);
String invalidHost = "not.a.valid.host";
try {
InetAddress address = InetAddress.getByName(invalidHost);
fail();
} catch(UnknownHostException uhe) {
// this is actually fine
}
// 1 host -> 1 DNS entry
assertEquals(1, getDNSEntryCount() - baselineDNSEntryCount);
// this should the native static method clearDNSCache
Core.getGlobalContext().beforeCheckpoint(null);
// cache should be cleared
assertEquals(0, getDNSEntryCount());
}
// helper functions to access the cache via reflection (see maven-surefire-plugin command args)
protected static Map<?, ?> getDNSCache() throws ReflectiveOperationException {
Class<InetAddress> klass = InetAddress.class;
Field acf = klass.getDeclaredField(CACHE_FIELD_NAME);
acf.setAccessible(true);
Object addressCache = acf.get(null);
return (Map<?, ?>) acf.get(addressCache);
}
protected static int getDNSEntryCount() throws ReflectiveOperationException {
Map<?, ?> cache = getDNSCache();
return cache.size();
}
}