Skip to content

Commit

Permalink
Add protection in pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Mar 11, 2016
1 parent 2c35eac commit 7804ebd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
43 changes: 38 additions & 5 deletions src/test/java/org/squiddev/cobalt/ProtectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object[]> 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);
}
}
14 changes: 0 additions & 14 deletions src/test/resources/protection.lua

This file was deleted.

12 changes: 12 additions & 0 deletions src/test/resources/protection/loop.lua
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions src/test/resources/protection/string.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7804ebd

Please sign in to comment.