mirror of
https://github.com/TangentFoxy/lua-sandbox.git
synced 2025-07-28 02:52:22 +00:00
initial version
This commit is contained in:
38
spec/sandbox_spec.lua
Normal file
38
spec/sandbox_spec.lua
Normal file
@@ -0,0 +1,38 @@
|
||||
local sandbox = require 'sandbox'
|
||||
|
||||
describe('sandbox', function()
|
||||
|
||||
it('can run harmless functions', function()
|
||||
local r = sandbox(function() return 'hello' end)
|
||||
assert.equal(r, 'hello')
|
||||
end)
|
||||
|
||||
it('can run harmless strings', function()
|
||||
local r = sandbox("return 'hello'")
|
||||
assert.equal(r, 'hello')
|
||||
end)
|
||||
|
||||
it('has access to safe methods', function()
|
||||
assert.equal(10, sandbox("return tonumber('10')"))
|
||||
assert.equal('HELLO', sandbox("return string.upper('hello')"))
|
||||
assert.equal(1, sandbox("local a = {3,2,1}; table.sort(a); return a[1]"))
|
||||
assert.equal(10, sandbox("return math.max(1,10)"))
|
||||
end)
|
||||
|
||||
it('does not allow access to not-safe stuff', function()
|
||||
assert.has_error(function() sandbox('return setmetatable({}, {})') end)
|
||||
assert.has_error(function() sandbox('return string.rep("hello", 5)') end)
|
||||
end)
|
||||
|
||||
it('does not allow pesky string:rep', function()
|
||||
assert.has_error(function() sandbox('return ("hello"):rep(5)') end)
|
||||
end)
|
||||
|
||||
it('restores the value of string.rep', function()
|
||||
sandbox("")
|
||||
assert.equal('hellohello', string.rep('hello', 2))
|
||||
end)
|
||||
|
||||
|
||||
|
||||
end)
|
Reference in New Issue
Block a user