-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MaxmindDataAdapterTest.java
183 lines (156 loc) · 7.02 KB
/
MaxmindDataAdapterTest.java
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package org.graylog.plugins.map.geoip;
import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableMap;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.model.CountryResponse;
import org.graylog.plugins.map.ConditionalRunner;
import org.graylog.plugins.map.ResourceExistsCondition;
import org.graylog.plugins.map.config.DatabaseType;
import org.graylog2.plugin.lookup.LookupResult;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(Suite.class)
@Suite.SuiteClasses({
MaxmindDataAdapterTest.CityDatabaseTest.class,
MaxmindDataAdapterTest.CountryDatabaseTest.class
})
public class MaxmindDataAdapterTest {
private static final String GEO_LITE2_CITY_MMDB = "/GeoLite2-City.mmdb";
private static final String GEO_LITE2_COUNTRY_MMDB = "/GeoLite2-Country.mmdb";
private static final Map<DatabaseType, String> DB_PATH = ImmutableMap.of(
DatabaseType.MAXMIND_CITY, GEO_LITE2_CITY_MMDB,
DatabaseType.MAXMIND_COUNTRY, GEO_LITE2_COUNTRY_MMDB
);
abstract static class Base {
MaxmindDataAdapter adapter;
private final DatabaseType databaseType;
Base(DatabaseType databaseType) {
this.databaseType = databaseType;
}
@Before
public void setUp() throws Exception {
final MaxmindDataAdapter.Config config = MaxmindDataAdapter.Config.builder()
.checkInterval(1L)
.checkIntervalUnit(TimeUnit.HOURS)
.dbType(databaseType)
.path(getTestDatabasePath(DB_PATH.get(databaseType)))
.type("test")
.build();
adapter = new MaxmindDataAdapter("test", "test", config, new MetricRegistry());
adapter.doStart();
}
@After
public void tearDown() throws Exception {
adapter.doStop();
}
private String getTestDatabasePath(String name) throws URISyntaxException {
return this.getClass().getResource(name).toURI().getPath();
}
@Test
public void doGetSuccessfullyResolvesLoopBackToEmptyResult() {
final LookupResult lookupResult = adapter.doGet("127.0.0.1");
assertThat(lookupResult.isEmpty()).isTrue();
}
@Test
public void doGetSuccessfullyResolvesRFC1918AddressToEmptyResult() {
final LookupResult lookupResult = adapter.doGet("192.168.23.42");
assertThat(lookupResult.isEmpty()).isTrue();
}
@Test
public void doGetReturnsEmptyResultIfDatabaseReaderReturnsNull() {
final DatabaseReader mockDatabaseReader = mock(DatabaseReader.class);
final DatabaseReader oldDatabaseReader = adapter.getDatabaseReader();
try {
adapter.setDatabaseReader(mockDatabaseReader);
final LookupResult lookupResult = adapter.doGet("127.0.0.1");
assertThat(lookupResult.isEmpty()).isTrue();
} finally {
adapter.setDatabaseReader(oldDatabaseReader);
}
}
@Test
public void doGetReturnsEmptyResultForInvalidIPAddress() {
final LookupResult lookupResult = adapter.doGet("Foobar");
assertThat(lookupResult.isEmpty()).isTrue();
}
}
@RunWith(ConditionalRunner.class)
@ResourceExistsCondition(GEO_LITE2_CITY_MMDB)
public static class CityDatabaseTest extends Base {
public CityDatabaseTest() {
super(DatabaseType.MAXMIND_CITY);
}
@Test
public void doGetSuccessfullyResolvesGooglePublicDNSAddress() {
// This test will possibly get flaky when the entry for 8.8.8.8 changes!
final LookupResult lookupResult = adapter.doGet("8.8.8.8");
assertThat(lookupResult.isEmpty()).isFalse();
assertThat(lookupResult.multiValue())
.extracting("city")
.extracting("geoNameId")
.containsExactly(5375480);
assertThat(lookupResult.multiValue())
.extracting("location")
.extracting("metroCode")
.containsExactly(807);
}
@Test
public void doGetReturnsResultIfCityResponseFieldsAreNull() throws Exception {
final CityResponse cityResponse = new CityResponse(null, null, null, null, null, null, null, null, null, null);
final DatabaseReader mockDatabaseReader = mock(DatabaseReader.class);
when(mockDatabaseReader.city(any())).thenReturn(cityResponse);
final DatabaseReader oldDatabaseReader = adapter.getDatabaseReader();
try {
adapter.setDatabaseReader(mockDatabaseReader);
final LookupResult lookupResult = adapter.doGet("127.0.0.1");
assertThat(lookupResult.isEmpty()).isFalse();
assertThat(lookupResult.singleValue()).isNull();
} finally {
adapter.setDatabaseReader(oldDatabaseReader);
}
}
}
@RunWith(ConditionalRunner.class)
@ResourceExistsCondition(GEO_LITE2_COUNTRY_MMDB)
public static class CountryDatabaseTest extends Base {
public CountryDatabaseTest() {
super(DatabaseType.MAXMIND_COUNTRY);
}
@Test
public void doGetSuccessfullyResolvesGooglePublicDNSAddress() {
// This test will possibly get flaky when the entry for 8.8.8.8 changes!
final LookupResult lookupResult = adapter.doGet("8.8.8.8");
assertThat(lookupResult.isEmpty()).isFalse();
assertThat(lookupResult.multiValue())
.extracting("country")
.extracting("geoNameId")
.containsExactly(6252001);
}
@Test
public void doGetReturnsResultIfCountryResponseFieldsAreNull() throws Exception {
final CountryResponse countryResponse = new CountryResponse(null, null, null, null, null, null);
final DatabaseReader mockDatabaseReader = mock(DatabaseReader.class);
when(mockDatabaseReader.country(any())).thenReturn(countryResponse);
final DatabaseReader oldDatabaseReader = adapter.getDatabaseReader();
try {
adapter.setDatabaseReader(mockDatabaseReader);
final LookupResult lookupResult = adapter.doGet("127.0.0.1");
assertThat(lookupResult.isEmpty()).isFalse();
assertThat(lookupResult.singleValue()).isNull();
} finally {
adapter.setDatabaseReader(oldDatabaseReader);
}
}
}
}