diff --git a/src/test/java/org/squiddev/cobalt/ProtectionTest.java b/src/test/java/org/squiddev/cobalt/ProtectionTest.java index e284b462..17373540 100644 --- a/src/test/java/org/squiddev/cobalt/ProtectionTest.java +++ b/src/test/java/org/squiddev/cobalt/ProtectionTest.java @@ -2,31 +2,64 @@ import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import org.squiddev.cobalt.debug.DebugHandler; +import org.squiddev.cobalt.debug.DebugInfo; +import org.squiddev.cobalt.debug.DebugState; import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; /** * Tests protection */ +@RunWith(Parameterized.class) public class ProtectionTest { - private final ScriptDrivenHelpers helpers = new ScriptDrivenHelpers("/"); + private final String name; + private ScriptDrivenHelpers helpers; + + public ProtectionTest(String name) { + this.name = name; + this.helpers = new ScriptDrivenHelpers("/protection/"); + } + + @Parameterized.Parameters(name = "{0}") + public static Collection getTests() { + Object[][] tests = { + {"string"}, + {"loop"}, + }; + + return Arrays.asList(tests); + } @Before public void setup() { helpers.setup(); - long time = System.currentTimeMillis(); helpers.state.debug = new DebugHandler(helpers.state) { + private long time = System.currentTimeMillis(); + @Override public void poll() { - if (System.currentTimeMillis() > time + 500) throw new LuaError("Timed out"); + if (System.currentTimeMillis() > time + 500) { + time = System.currentTimeMillis(); + throw new LuaError("Timed out"); + } + } + + @Override + public void onInstruction(DebugState ds, DebugInfo di, int pc, Varargs extras, int top) { + poll(); + super.onInstruction(ds, di, pc, extras, top); } }; } - @Test(timeout = 2000) + @Test(timeout = 3000) public void run() throws IOException { - helpers.loadScript("protection").call(helpers.state); + helpers.loadScript(name).call(helpers.state); } } diff --git a/src/test/resources/protection.lua b/src/test/resources/protection.lua deleted file mode 100644 index 10d966ba..00000000 --- a/src/test/resources/protection.lua +++ /dev/null @@ -1,14 +0,0 @@ ---- Horrible strings in Lua -local function check(func) - local success, message = pcall(function() - func(("a"):rep(1e4), ".-.-.-.-b$") - end) - - assert(not success, "Expected abort") - assert(message:find("Timed out"), "Got " .. message) -end - -check(string.find) -check(string.match) -check(function(...) string.gmatch(...)() end) -check(function(a, b) string.gsub(a, b, "") end) diff --git a/src/test/resources/protection/loop.lua b/src/test/resources/protection/loop.lua new file mode 100644 index 00000000..0ff54b0c --- /dev/null +++ b/src/test/resources/protection/loop.lua @@ -0,0 +1,12 @@ +-- Test infinite loops + +local function check(...) + local success, message = pcall(...) + + assert(not success, "Expected abort") + assert(message:find("Timed out"), "Got " .. message) +end + +check(function() + while true do end +end) diff --git a/src/test/resources/protection/string.lua b/src/test/resources/protection/string.lua new file mode 100644 index 00000000..873ba06b --- /dev/null +++ b/src/test/resources/protection/string.lua @@ -0,0 +1,17 @@ +-- Test string matching +local function check(...) + local success, message = pcall(...) + + assert(not success, "Expected abort") + assert(message:find("Timed out"), "Got " .. message) +end + +local function checkString(func) + check(func, ("a"):rep(1e4), ".-.-.-.-b$") +end + +checkString(string.find) +checkString(string.match) +checkString(function(...) string.gmatch(...)() end) +checkString(function(a, b) string.gsub(a, b, "") end) +