#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);
}
}