#949 Created TimedCounter + implement it in TempbanManager

This commit is contained in:
ljacqu
2017-02-18 22:50:30 +01:00
parent ef1d006cdf
commit 152d1dc216
6 changed files with 172 additions and 135 deletions
@@ -2,12 +2,12 @@ package fr.xephi.authme.data;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.TempbanManager.TimedCounter;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.TimedCounter;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -20,8 +20,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;
@@ -195,42 +194,24 @@ public class TempbanManagerTest {
@Test
public void shouldPerformCleanup() {
// given
// `expirationPoint` is the approximate timestamp until which entries should be considered, so subtracting
// from it will create expired entries, and adding a reasonably large number makes it still valid
final long expirationPoint = System.currentTimeMillis() - TEST_EXPIRATION_THRESHOLD;
// 2 current entries with total 6 failed tries
Map<String, TimedCounter> map1 = new HashMap<>();
map1.put("name", newTimedCounter(4, expirationPoint + 20_000));
map1.put("other", newTimedCounter(2, expirationPoint + 40_000));
// 0 current entries
Map<String, TimedCounter> map2 = new HashMap<>();
map2.put("someone", newTimedCounter(10, expirationPoint - 5_000));
map2.put("somebody", newTimedCounter(10, expirationPoint - 8_000));
// 1 current entry with total 4 failed tries
Map<String, TimedCounter> map3 = new HashMap<>();
map3.put("some", newTimedCounter(5, expirationPoint - 12_000));
map3.put("test", newTimedCounter(4, expirationPoint + 8_000));
map3.put("values", newTimedCounter(2, expirationPoint - 80_000));
Map<String, TimedCounter<String>> counts = new HashMap<>();
TimedCounter<String> counter1 = mockCounter();
given(counter1.isEmpty()).willReturn(true);
counts.put("11.11.11.11", counter1);
TimedCounter<String> counter2 = mockCounter();
given(counter2.isEmpty()).willReturn(false);
counts.put("33.33.33.33", counter2);
String[] addresses = {"123.45.67.89", "127.0.0.1", "192.168.0.1"};
Map<String, Map<String, TimedCounter>> counterMap = new HashMap<>();
counterMap.put(addresses[0], map1);
counterMap.put(addresses[1], map2);
counterMap.put(addresses[2], map3);
TempbanManager manager = new TempbanManager(bukkitService, messages, mockSettings(5, 250));
ReflectionTestUtils.setField(TempbanManager.class, manager, "ipLoginFailureCounts", counterMap);
TempbanManager manager = new TempbanManager(bukkitService, messages, mockSettings(3, 10));
ReflectionTestUtils.setField(TempbanManager.class, manager, "ipLoginFailureCounts", counts);
// when
manager.performCleanup();
// then
assertThat(counterMap.get(addresses[0]), aMapWithSize(2));
assertHasCount(manager, addresses[0], "name", 4);
assertHasCount(manager, addresses[0], "other", 2);
assertThat(counterMap.get(addresses[1]), anEmptyMap());
assertThat(counterMap.get(addresses[2]), aMapWithSize(1));
assertHasCount(manager, addresses[2], "test", 4);
verify(counter1).removeExpiredEntries();
verify(counter2).removeExpiredEntries();
assertThat(counts.keySet(), contains("33.33.33.33"));
}
private static Settings mockSettings(int maxTries, int tempbanLength) {
@@ -244,21 +225,20 @@ public class TempbanManagerTest {
}
private static void assertHasNoEntries(TempbanManager manager, String address) {
Map<String, Map<String, TimedCounter>> playerCounts = ReflectionTestUtils
Map<String, TimedCounter<String>> playerCounts = ReflectionTestUtils
.getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts");
Map<String, TimedCounter> map = playerCounts.get(address);
assertThat(map == null || map.isEmpty(), equalTo(true));
TimedCounter<String> counter = playerCounts.get(address);
assertThat(counter == null || counter.isEmpty(), equalTo(true));
}
private static void assertHasCount(TempbanManager manager, String address, String name, int count) {
Map<String, Map<String, TimedCounter>> playerCounts = ReflectionTestUtils
Map<String, TimedCounter<String>> playerCounts = ReflectionTestUtils
.getFieldValue(TempbanManager.class, manager, "ipLoginFailureCounts");
assertThat(playerCounts.get(address).get(name).getCount(TEST_EXPIRATION_THRESHOLD), equalTo(count));
assertThat(playerCounts.get(address).get(name), equalTo(count));
}
private static TimedCounter newTimedCounter(int count, long timestamp) {
TimedCounter counter = new TimedCounter(count);
ReflectionTestUtils.setField(TimedCounter.class, counter, "lastIncrementTimestamp", timestamp);
return counter;
@SuppressWarnings("unchecked")
private static <T> TimedCounter<T> mockCounter() {
return mock(TimedCounter.class);
}
}
@@ -1,6 +1,5 @@
package fr.xephi.authme.util;
import fr.xephi.authme.ReflectionTestUtils;
import org.junit.Test;
import java.util.Map;
@@ -45,14 +44,14 @@ public class ExpiringMapTest {
}
@Test
public void shouldUpdateExpiration() {
public void shouldUpdateExpirationAndSupportNegativeValues() {
// given
ExpiringMap<Integer, Integer> map = new ExpiringMap<>(2, TimeUnit.DAYS);
map.put(2, 4);
map.put(3, 9);
// when
map.setExpiration(0, TimeUnit.SECONDS);
map.setExpiration(-100, TimeUnit.MILLISECONDS);
// then
map.put(5, 25);
@@ -61,20 +60,10 @@ public class ExpiringMapTest {
assertThat(map.get(5), nullValue());
}
@Test
public void shouldAcceptNegativeExpiration() {
// given / when
ExpiringMap<Integer, String> map = new ExpiringMap<>(-3, TimeUnit.MINUTES);
map.put(3, "trois");
// then
assertThat(map.get(3), nullValue());
}
@Test
public void shouldCleanUpExpiredEntries() throws InterruptedException {
// given
ExpiringMap<Integer, Integer> map = new ExpiringMap<>(400, TimeUnit.MILLISECONDS);
ExpiringMap<Integer, Integer> map = new ExpiringMap<>(200, TimeUnit.MILLISECONDS);
map.put(144, 12);
map.put(121, 11);
map.put(81, 9);
@@ -83,12 +72,24 @@ public class ExpiringMapTest {
map.put(25, 5);
// when
Thread.sleep(400);
Thread.sleep(300);
map.removeExpiredEntries();
// then
Map<Integer, ?> internalMap = ReflectionTestUtils.getFieldValue(ExpiringMap.class, map, "entries");
Map<Integer, ?> internalMap = map.entries;
assertThat(internalMap.keySet(), containsInAnyOrder(64, 25));
}
@Test
public void shouldReturnIfIsEmpty() {
// given
ExpiringMap<String, String> map = new ExpiringMap<>(-8, TimeUnit.SECONDS);
// when / then
assertThat(map.isEmpty(), equalTo(true));
map.put("hoi", "Welt");
assertThat(map.isEmpty(), equalTo(false));
map.removeExpiredEntries();
assertThat(map.isEmpty(), equalTo(true));
}
}
@@ -0,0 +1,55 @@
package fr.xephi.authme.util;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link TimedCounter}.
*/
public class TimedCounterTest {
@Test
public void shouldReturnZeroForAnyKey() {
// given
TimedCounter<Double> counter = new TimedCounter<>(1, TimeUnit.DAYS);
// when / then
assertThat(counter.get(2.0), equalTo(0));
assertThat(counter.get(-3.14159), equalTo(0));
}
@Test
public void shouldIncrementCount() {
// given
TimedCounter<String> counter = new TimedCounter<>(10, TimeUnit.MINUTES);
counter.put("moto", 12);
// when
counter.increment("hello");
counter.increment("moto");
// then
assertThat(counter.get("hello"), equalTo(1));
assertThat(counter.get("moto"), equalTo(13));
}
@Test
public void shouldSumUpEntries() {
// given
TimedCounter<String> counter = new TimedCounter<>(90, TimeUnit.SECONDS);
counter.entries.put("expired", new ExpiringMap.ExpiringEntry<>(800, 0));
counter.entries.put("expired2", new ExpiringMap.ExpiringEntry<>(24, System.currentTimeMillis() - 100));
counter.put("other", 10);
counter.put("Another", 4);
// when
int totals = counter.total();
// then
assertThat(totals, equalTo(14));
}
}