diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2012-05-13 19:34:39 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2012-05-13 19:34:39 +0100 |
commit | bc57f6a438ab6c4666b6c6bea27148027e7156c2 (patch) | |
tree | 18c02b410583df4bdfab72f7cb269e427b995f47 | |
parent | 3f6627a44db021f00d44009e6776ef7b44a4bf16 (diff) | |
download | lace-bc57f6a438ab6c4666b6c6bea27148027e7156c2.tar.gz |
Initial result builtin (allow/deny)
-rw-r--r-- | lib/lace.lua | 2 | ||||
-rw-r--r-- | lib/lace/builtin.lua | 45 | ||||
-rw-r--r-- | test/test-lace.builtin.lua | 78 |
3 files changed, 125 insertions, 0 deletions
diff --git a/lib/lace.lua b/lib/lace.lua index f6818f1..3822b38 100644 --- a/lib/lace.lua +++ b/lib/lace.lua @@ -9,8 +9,10 @@ local lex = require "lace.lex" local compiler = require "lace.compiler" +local builtin = require "lace.builtin" return { lex = lex, compiler = compiler, + builtin = builtin, } diff --git a/lib/lace/builtin.lua b/lib/lace/builtin.lua new file mode 100644 index 0000000..209916d --- /dev/null +++ b/lib/lace/builtin.lua @@ -0,0 +1,45 @@ +-- lib/lace/builtin.lua +-- +-- Lua Access Control Engine -- Builtin commands for Lace +-- +-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org> +-- +-- For Licence terms, see COPYING +-- + +local builtin = {} + +local function compiler() + return require "lace.compiler" +end + +local function _do_return(exec_context, result, reason, cond) + if #cond > 0 then + -- Run the conditions + end + return result, reason +end + +local function _return(compcontext, result, reason, ...) + if result ~= "allow" and result ~= "deny" then + return compiler().error("Unknown result: " .. result, {1}) + end + if type(reason) ~= "string" then + return compiler().error("Expected reason, got nothing") + end + return { + fn = _do_return, + args = { + result, + reason, + {...} + } + } +end + +builtin.allow = _return +builtin.deny = _return + +return { + commands = builtin, +} diff --git a/test/test-lace.builtin.lua b/test/test-lace.builtin.lua new file mode 100644 index 0000000..7fd76fc --- /dev/null +++ b/test/test-lace.builtin.lua @@ -0,0 +1,78 @@ +-- test/test-lace.builtin.lua +-- +-- Lua Access Control Engine -- Tests for the builtins for Lace +-- +-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org> +-- +-- For Licence terms, see COPYING +-- + +-- Step one, start coverage + +local luacov = require 'luacov' + +local builtin = require 'lace.builtin' + +local testnames = {} + +local function add_test(suite, name, value) + rawset(suite, name, value) + testnames[#testnames+1] = name +end + +local suite = setmetatable({}, {__newindex = add_test}) + +function suite.compile_builtin_allow_deny_badname() + local cmdtab, msg = builtin.commands.allow({}, "badname") + assert(cmdtab == false, "Internal errors should return false") + assert(type(msg) == "table", "Internal errors should return tables") + assert(type(msg.msg) == "string", "Internal errors should have string messages") + assert(msg.msg:match("badname"), "Expected error should contain badname") + assert(type(msg.words) == "table", "Internal error should contain a words table") + assert(msg.words[1] == 1, "Internal error should reference word 1, the bad command name") +end + +function suite.compile_builtin_allow_deny_noreason() + local cmdtab, msg = builtin.commands.allow({}, "allow") + assert(cmdtab == false, "Internal errors should return false") + assert(type(msg) == "table", "Internal errors should return tables") + assert(type(msg.msg) == "string", "Internal errors should have string messages") + assert(msg.msg:match("Expected reason"), "Expected error should mention a lack of reason") +end + +function suite.compile_builtin_allow_deny_novariables() + local cmdtab, msg = builtin.commands.allow({}, "allow", "because") + assert(type(cmdtab) == "table", "Result should be a table") + assert(type(cmdtab.fn) == "function", "Result should contain a function") + assert(type(cmdtab.args) == "table", "Result table should contain an args table") + assert(cmdtab.args[1] == "allow", "Result args table should contain the given result 'allow'") + assert(cmdtab.args[2] == "because", "Result args table should contain te given reason 'because'") + assert(type(cmdtab.args[3]) == "table", "The third argument should be a table") + assert(#cmdtab.args[3] == 0, "There should be no conditions") +end + +function suite.run_builtin_allow_deny_novariables() + local cmdtab, msg = builtin.commands.allow({}, "allow", "because") + assert(type(cmdtab) == "table", "Result should be a table") + assert(type(cmdtab.fn) == "function", "Result should contain a function") + assert(type(cmdtab.args) == "table", "Result table should contain an args table") + local result, msg = cmdtab.fn({}, unpack(cmdtab.args)) + assert(result == "allow", "Expected result should be 'allow'") + assert(msg == "because", "Expected reason should be 'because'") +end + +local count_ok = 0 +for _, testname in ipairs(testnames) do + print("Run: " .. testname) + local ok, err = xpcall(suite[testname], debug.traceback) + if not ok then + print(err) + print() + else + count_ok = count_ok + 1 + end +end + +print("Lace: " .. tostring(count_ok) .. "/" .. tostring(#testnames) .. " OK") + +os.exit(count_ok == #testnames and 0 or 1) |