Skip to content

Commit

Permalink
Implement basic unit testing (#75)
Browse files Browse the repository at this point in the history
This helps with issue #69

This also adds another dependency, mockito, for the easy creation of mocks,
this helps with the testing, because you don't need to start a bukkit server

This also fixes a bug in the Adfinder that allowed you to bypass the filter when
using uppercase for the url
  • Loading branch information
ferrybig authored and kasper Franz committed Oct 31, 2016
1 parent 87f6395 commit e2fcce7
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<version>R8-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.name}-${project.version}</finalName>
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/github/antiad/AntiAd/Adfinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public class Adfinder {
// Simple pattern http://regexr.com/3cu3l
private final String webpatternSimple = "[-a-zA-Z0-9@:%_\\+.~#?&//=]{2,256}\\.(com|ru|net|org|de|jp|uk|br|pl|in|it|fr|au|info|nl|cn|ir|es|cz|biz|ca|kr|eu|ua|za|co|gr|ro|se|tw|vn|mx|ch|tr|at|be|hu|dk|tv|me|ar|us|no|sk|fi|id|cl|nz|by|pt)\\b(\\/[-a-zA-Z0-9@:%_\\+.~#?&//=]*)?";
private HashMap<Player, Integer> warn;
public boolean urlDetection, spamDetection, IPDetection, checkWordLenght;
public boolean urlDetection;
private boolean spamDetection;
public boolean IPDetection, checkWordLenght;
public int numbers, procentCapital;
private ArrayList<String> whitelistLine, whitelistWildCardList;

Expand Down Expand Up @@ -125,6 +127,14 @@ public int checkForAdvertising(Check check) {
return advertising;
}

/**
* Does this adfinder protect against spam and caps?
* @return return true if it protects against spam and caps
*/
public boolean isSpamDetection() {
return spamDetection;
}

/**
*
* Save the message to the log if the server have that in the config, by default it does this.
Expand Down Expand Up @@ -433,7 +443,7 @@ private int checkForIPPattern(Check check) {
*/
private int checkForWebPattern(Check check) {
int advertising = 0;
String message = check.getMessage();
String message = check.getMessage().toLowerCase();
Matcher regexMatcherurl = webpattern.matcher(message);
plugin.debug("Message: " + message);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/antiad/AntiAd/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public boolean check(String message, int type, boolean checkForSpam) {
rtnbool = true;
} else if (ad == 0) {
//if it's not advertising then check for spam
if (adfinder.spamDetection && checkForSpam && !player.hasPermission("antiad.bypass.spam")) {
if (adfinder.isSpamDetection() && checkForSpam && !player.hasPermission("antiad.bypass.spam")) {
spam = adfinder.checkForSpam(this);
if (spam) {
adfinder.sendWarning(player, message, 2, type);
Expand Down
135 changes: 135 additions & 0 deletions src/test/java/com/github/antiad/AntiAd/AdfinderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.github.antiad.AntiAd;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.logging.Logger;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginLogger;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/**
*
* @author Fernando
*/
@RunWith(Parameterized.class)
public class AdfinderTest {

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
// isSpam, isAdvertisement, isCaps, message
{false, false, false, "Hello world"}, // Should be ok
{false, true, false, "Join my server 127.0.0.1"}, // advertisement
{true, false, false, "12121212121212121212121212121212121212"}, // spam
{false, false, true, "TROLLOLOOLOOLOLLOL"}, // caps
{true, false, true, "T1R1O1L1L1O1L1O1O1L1O1O1L1O1L1L1O1L"}, // spam, caps
{false, true, false, "Join my server play.minecraft.net"}, // advertisement
{false, true, true, "JOIN MY SERVER PLAY.MINECRAFT.NET"}, // caps, advertisement
{false, false, false, "Join my server github.com"}, // Whitelisted ip
{false, false, false, "gg"}, // Should be ok
{false, false, false, "I like how you build the floor from grass"}, // Should be ok
});
}

@Rule
public TestName name = new TestName();

private final Boolean isSpam;

private final Boolean isAdvertisement;

private final Boolean isCaps;

private final String message;

@Mock
private Check check;

@Mock
private AntiAd plugin;

@Mock
private PluginLogger logger;

@Mock
private FileConfiguration config;

private Adfinder finder;

public AdfinderTest(Boolean isSpam, Boolean isAdvertisement, Boolean isCaps, String message) {
this.isSpam = isSpam;
this.isAdvertisement = isAdvertisement;
this.isCaps = isCaps;
this.message = message;
}

@Before
public void before() throws Exception {
MockitoAnnotations.initMocks(this);

Mockito.when(plugin.getConfig()).thenReturn(config);
Field f1 = JavaPlugin.class.getDeclaredField("logger");
f1.setAccessible(true);
f1.set(plugin, logger);
Mockito.when(check.getMessage()).thenReturn(message);
Mockito.when(config.getBoolean("Spam-Detection")).thenReturn(true);
Mockito.when(config.getBoolean("URL-Detection")).thenReturn(true);
Mockito.when(config.getBoolean("IP-Detection")).thenReturn(true);
Mockito.when(config.getInt("Spam-Number-Letters")).thenReturn(20);
Mockito.when(config.getInt("Spam-Procent-Capital-Words")).thenReturn(80);
Mockito.when(config.getBoolean("Spam-Number-Letters-check")).thenReturn(true);

finder = new Adfinder(plugin);

// Required because plguin fails loding properly when in mocked context (whitelist file missing)
Field f2 = Adfinder.class.getDeclaredField("whitelistLine");
f2.setAccessible(true);
f2.set(finder, new ArrayList<>());
Field f3 = Adfinder.class.getDeclaredField("whitelistWildCardList");
f3.setAccessible(true);
f3.set(finder, new ArrayList<>());

finder.whitelistAdd("github.com");
}

@Test
public void checkSpam() {
Assume.assumeNotNull(isSpam);
Assert.assertEquals(name.getMethodName() + " failed for " + message,
isSpam, finder.checkForSpam(check));
}

@Test
public void checkAdvertisement() {
Assume.assumeNotNull(isAdvertisement);
Assert.assertEquals(name.getMethodName() + " failed for " + message,
isAdvertisement, finder.checkForAdvertising(check) == 1);
}

@Test
public void checkCaps() {
Assume.assumeNotNull(isCaps);
Assert.assertEquals(name.getMethodName() + " failed for " + message,
isCaps, finder.checkForCaps(check));
}

}
112 changes: 112 additions & 0 deletions src/test/java/com/github/antiad/AntiAd/CheckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.github.antiad.AntiAd;

import org.bukkit.entity.Player;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Ignore;
import org.mockito.Mockito;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
*
* @author Fernando
*/
public class CheckTest {

public CheckTest() {
}

@Test
public void testAllAdfinderMethodsAreCalled() {
AntiAd pl = Mockito.mock(AntiAd.class);
Player player = Mockito.mock(Player.class);
Adfinder finder = Mockito.mock(Adfinder.class);

Mockito.when(pl.getAdfinder()).thenReturn(finder);
Mockito.when(finder.isSpamDetection()).thenReturn(true);

Check check = new Check(pl, player);

check.check("Hello World", 1, true);

Mockito.verify(finder).checkForAdvertising(check);
Mockito.verify(finder).checkForCaps(check);
Mockito.verify(finder).checkForSpam(check);
Assert.assertEquals(false, check.isAdvertisement());
Assert.assertEquals(false, check.isCaps());
Assert.assertEquals(false, check.isSpam());
}

@Test
public void testOnlyAdvertisementAdfinderMethodsAreCalled() {
AntiAd pl = Mockito.mock(AntiAd.class);
Player player = Mockito.mock(Player.class);
Adfinder finder = Mockito.mock(Adfinder.class);

Mockito.when(pl.getAdfinder()).thenReturn(finder);
Mockito.when(finder.isSpamDetection()).thenReturn(false);

Check check = new Check(pl, player);

check.check("Hello World", 1, true);

Mockito.verify(finder).checkForAdvertising(check);
Mockito.verify(finder, Mockito.never()).checkForCaps(check);
Mockito.verify(finder, Mockito.never()).checkForSpam(check);
Assert.assertEquals(false, check.isAdvertisement());
Assert.assertEquals(false, check.isCaps());
Assert.assertEquals(false, check.isSpam());
}

@Test
public void testOtherChecksAreSkippedWhenAdvertisementsAreDetected() {
AntiAd pl = Mockito.mock(AntiAd.class);
Player player = Mockito.mock(Player.class);
Adfinder finder = Mockito.mock(Adfinder.class);

Mockito.when(pl.getAdfinder()).thenReturn(finder);
Mockito.when(finder.isSpamDetection()).thenReturn(true);
Mockito.when(finder.checkForAdvertising(Mockito.any(Check.class))).thenReturn(1);

Check check = new Check(pl, player);

check.check("Hello World", 1, true);

Mockito.verify(finder).checkForAdvertising(check);
Mockito.verify(finder, Mockito.never()).checkForCaps(check);
Mockito.verify(finder, Mockito.never()).checkForSpam(check);
Assert.assertEquals(true, check.isAdvertisement());
Assert.assertEquals(false, check.isCaps());
Assert.assertEquals(false, check.isSpam());
}

@Ignore
@Test
public void testOtherChecksAreStillRunWhenWhitelistedAdvertisementsAreDetected() {
AntiAd pl = Mockito.mock(AntiAd.class);
Player player = Mockito.mock(Player.class);
Adfinder finder = Mockito.mock(Adfinder.class);

Mockito.when(pl.getAdfinder()).thenReturn(finder);
Mockito.when(finder.isSpamDetection()).thenReturn(true);
Mockito.when(finder.checkForAdvertising(Mockito.any(Check.class))).thenReturn(2);

Check check = new Check(pl, player);

check.check("Hello World", 1, true);

Mockito.verify(finder).checkForAdvertising(check);
Mockito.verify(finder).checkForCaps(check);
Mockito.verify(finder).checkForSpam(check);
Assert.assertEquals(false, check.isAdvertisement());
Assert.assertEquals(false, check.isCaps());
Assert.assertEquals(false, check.isSpam());
}

}

0 comments on commit e2fcce7

Please sign in to comment.