-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/test/java/com/github/antiad/AntiAd/AdfinderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |