Skip to content

Commit

Permalink
fix: zencode error reporting cleanup
Browse files Browse the repository at this point in the history
also using error inside main ZEN:parse function to block execution

fix tests for new error codes

improve partial line parsing in many tests

python and js bindings update error log test
  • Loading branch information
jaromil committed Oct 12, 2023
1 parent a42d52e commit 3b10b98
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 126 deletions.
5 changes: 1 addition & 4 deletions bindings/javascript/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ test("error format contains newlines", async t => {
await zencode_exec(`a`);
} catch (e) {
const lines = e.logs.split('\n');

t.true(lines.includes('[W] Zencode text too short to parse'));
t.true(lines.includes('[W] Zencode is missing version check, please add: rule check version N.N.N'));
t.true(lines.includes('[!] Execution aborted with errors.'));
t.true(lines.includes('[!] Zencode parser error'));
}
})

Expand Down
2 changes: 1 addition & 1 deletion bindings/python3/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_zencode_failure():
Then print the data
"""
res = zencode_exec(contract)
assert("ERROR" in res.logs)
assert("fatal error" in res.logs)

# def test_lua_call_hello_world():
# lua_res = zenroom_exec(
Expand Down
94 changes: 47 additions & 47 deletions src/lua/zencode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -414,57 +414,57 @@ end

function ZEN:parse(text)
self:crumb()
if #text < 9 then -- strlen("and debug") == 9
warn("Zencode text too short to parse")
return false
end
local branching = false
local looping = false
local prefixes = {}
local parse_prefix <const> = parse_prefix -- optimization
self.linenum = 0
if #text < 9 then -- strlen("and debug") == 9
error("Zencode text too short to parse")
return false
end
local branching = false
local looping = false
local prefixes = {}
local parse_prefix <const> = parse_prefix -- optimization
self.linenum = 0
for line in zencode_newline_iter(text) do
self.linenum = self.linenum + 1
local tline = trim(line) -- saves trims in isempty / iscomment
xxx(tline,3)

if not zencode_isempty(tline) and not zencode_iscomment(tline) then
-- xxx('Line: '.. text, 3)
-- max length for single zencode line is #define MAX_LINE
-- hard-coded inside zenroom.h
local prefix = parse_prefix(line) -- trim is included
assert(prefix, "Invalid Zencode line "..self.linenum..": "..line)
self.OK = true
exitcode(0)
if not branching and prefix == 'if' then
branching = true
table.insert(prefixes, 1, 'if')
elseif not looping and prefix == 'foreach' then
looping = true
table.insert(prefixes, 1, 'foreach')
elseif prefix == 'endif' then
branching = false
table.remove(prefixes, 1)
elseif prefix == 'endforeach' then
looping = false
table.remove(prefixes, 1)
end
if prefix == 'if' or prefix == 'foreach' then
prefix = table.concat(prefixes,'')
elseif prefix == 'when' or prefix == 'then'
or prefix == 'endif' or prefix == 'endforeach' then
prefix = prefix .. table.concat(prefixes,'')
end
self.linenum = self.linenum + 1
local tline = trim(line) -- saves trims in isempty / iscomment
xxx(tline,3)

if not zencode_isempty(tline) and not zencode_iscomment(tline) then
-- xxx('Line: '.. text, 3)
-- max length for single zencode line is #define MAX_LINE
-- hard-coded inside zenroom.h
local prefix = parse_prefix(line) -- trim is included
assert(prefix, "Invalid Zencode line "..self.linenum..": "..line)
self.OK = true
exitcode(0)
if not branching and prefix == 'if' then
branching = true
table.insert(prefixes, 1, 'if')
elseif not looping and prefix == 'foreach' then
looping = true
table.insert(prefixes, 1, 'foreach')
elseif prefix == 'endif' then
branching = false
table.remove(prefixes, 1)
elseif prefix == 'endforeach' then
looping = false
table.remove(prefixes, 1)
end
if prefix == 'if' or prefix == 'foreach' then
prefix = table.concat(prefixes,'')
elseif prefix == 'when' or prefix == 'then'
or prefix == 'endif' or prefix == 'endforeach' then
prefix = prefix .. table.concat(prefixes,'')
end

-- try to enter the machine state named in prefix
-- xxx("Zencode machine enter_"..prefix..": "..text, 3)
local fm <const> = self.machine["enter_"..prefix]
assert(fm, "Invalid Zencode prefix "..self.linenum..": '"..line.."'")
assert(fm(self.machine, { msg = tline, Z = self }),
-- try to enter the machine state named in prefix
-- xxx("Zencode machine enter_"..prefix..": "..text, 3)
local fm <const> = self.machine["enter_"..prefix]
assert(fm, "Invalid Zencode prefix "..self.linenum..": '"..line.."'")
assert(fm(self.machine, { msg = tline, Z = self }),
line.."\n "..
"Invalid transition from: "..self.machine.current)
end
-- continue
end
-- continue
end
collectgarbage'collect'
return true
Expand Down
85 changes: 40 additions & 45 deletions src/zenroom.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,39 +350,35 @@ void zen_teardown(zenroom_t *ZZ) {
int zen_exec_zencode(zenroom_t *ZZ, const char *script) {
SAFE_EXEC;
int ret;
lua_State* L = (lua_State*)ZZ->lua;
// introspection on code being executed
zen_setenv(L,"CODE",(char*)script);
ret = luaL_dostring(L,
"local _res, _err <const> = pcall( function() ZEN:begin() end)\n"
"if not _res then exitcode(4) ZEN.OK = false error(_err,2) end\n");
if(ret != SUCCESS) {
zerror(L, "Zencode init error");
zerror(L, "%s", lua_tostring(L, -1));
ZZ->exitcode = ZZ->exitcode==SUCCESS ? ERR_GENERIC : ZZ->exitcode;
return ZZ->exitcode;
}
ret = luaL_dostring(L,
"local _res, _err <const> = pcall( function() ZEN:parse(CONF.code.encoding.fun(CODE)) end)\n"
"if not _res then exitcode(3) ZEN.OK = false error(_err,2) end\n");
if(ret != SUCCESS) {
zerror(L, "Zencode parser error");
zerror(L, "%s", lua_tostring(L, -1));
ZZ->exitcode = ZZ->exitcode==SUCCESS ? ERR_GENERIC : ZZ->exitcode;
return ZZ->exitcode;
}
ret = luaL_dostring(L,
"local _res, _err <const> = pcall( function() ZEN:run() end)\n"
"if not _res then exitcode(2) ZEN.OK = false error(_err,2) end\n");
if(ret != SUCCESS) {
zerror(L, "Zencode runtime error");
zerror(L, "%s", lua_tostring(L, -1));
ZZ->exitcode = ZZ->exitcode==SUCCESS ? ERR_GENERIC : ZZ->exitcode;
return ZZ->exitcode;
}
func(L, "Zencode successfully executed");
ZZ->exitcode = SUCCESS;
return SUCCESS;
lua_State* L = (lua_State*)ZZ->lua;
// introspection on code being executed
zen_setenv(L,"CODE",(char*)script);
ZZ->exitcode = luaL_dostring
(L,"local _res, _err <const> = pcall( function() ZEN:begin() end)\n"
"if not _res then exitcode(4) ZEN.OK = false error(_err,2) end\n");
if(ZZ->exitcode != SUCCESS) {
zerror(L, "Zencode init error");
zerror(L, "%s", lua_tostring(L, -1));
return ZZ->exitcode;
}
ZZ->exitcode = luaL_dostring
(L,"local _res, _err <const> = pcall( function() ZEN:parse(CONF.code.encoding.fun(CODE)) end)\n"
"if not _res then exitcode(3) ZEN.OK = false error(_err,2) end\n");
if(ZZ->exitcode != SUCCESS) {
zerror(L, "Zencode parser error");
zerror(L, "%s", lua_tostring(L, -1));
return ZZ->exitcode;
}
ZZ->exitcode = luaL_dostring
(L,"local _res, _err <const> = pcall( function() ZEN:run() end)\n"
"if not _res then exitcode(2) ZEN.OK = false error(_err,2) end\n");
if(ZZ->exitcode != SUCCESS) {
zerror(L, "Zencode runtime error");
zerror(L, "%s", lua_tostring(L, -1));
return ZZ->exitcode;
}
if(ZZ->exitcode == SUCCESS) func(L, "Zencode successfully executed");
return ZZ->exitcode;
}

int zen_exec_script(zenroom_t *ZZ, const char *script) {
Expand Down Expand Up @@ -449,10 +445,9 @@ int _check_zenroom_init(zenroom_t *zz) {
return SUCCESS;
}

int _check_zenroom_result(zenroom_t *zz, int res) {
int exitcode = res;
zz->exitcode = res;
if(res != SUCCESS) {
int _check_zenroom_result(zenroom_t *zz) {
int exitcode = zz->exitcode;
if(exitcode != SUCCESS) {
zerror(zz->lua, "Execution aborted with errors.");
} else {
act(zz->lua, "Zenroom execution completed.");
Expand Down Expand Up @@ -480,8 +475,8 @@ int zencode_exec(const char *script, const char *conf, const char *keys, const c
zenroom_t *Z = zen_init(c, k, d);

if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;

return( _check_zenroom_result(Z, zen_exec_zencode(Z, script) ));
zen_exec_zencode(Z, script);
return( _check_zenroom_result(Z) );
}

int zenroom_exec(const char *script, const char *conf, const char *keys, const char *data) {
Expand All @@ -495,8 +490,8 @@ int zenroom_exec(const char *script, const char *conf, const char *keys, const c

zenroom_t *Z = zen_init(c, k, d);
if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;

return( _check_zenroom_result(Z, zen_exec_script(Z, script) ));
zen_exec_script(Z, script);
return( _check_zenroom_result(Z));
}

int zencode_exec_tobuf(const char *script, const char *conf, const char *keys, const char *data,
Expand All @@ -518,8 +513,8 @@ int zencode_exec_tobuf(const char *script, const char *conf, const char *keys, c
Z->stdout_len = stdout_len;
Z->stderr_buf = stderr_buf;
Z->stderr_len = stderr_len;

return( _check_zenroom_result(Z, zen_exec_zencode(Z, script) ));
zen_exec_zencode(Z, script);
return( _check_zenroom_result(Z));
}


Expand All @@ -542,7 +537,7 @@ int zenroom_exec_tobuf(const char *script, const char *conf, const char *keys, c
Z->stdout_len = stdout_len;
Z->stderr_buf = stderr_buf;
Z->stderr_len = stderr_len;

return( _check_zenroom_result(Z, zen_exec_script(Z, script) ));
zen_exec_script(Z, script);
return( _check_zenroom_result(Z));
}

4 changes: 2 additions & 2 deletions test/testgen_sign_verify_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run \$ZENROOM_EXECUTABLE -z -a sign_pubkey.json wrong_message.zen
assert_line '[W] The %%ALGOSIGN%% signature by Alice is not authentic'
assert_line --partial 'The %%ALGOSIGN%% signature by Alice is not authentic'
}
@test "Fail verification on a different public key" {
Expand All @@ -132,7 +132,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run \$ZENROOM_EXECUTABLE -z -a wrong_pubkey.json wrong_pubkey.zen
assert_line '[W] The %%ALGOSIGN%% signature by Alice is not authentic'
assert_line --partial 'The %%ALGOSIGN%% signature by Alice is not authentic'
}
@test "Alice signs a big file" {
Expand Down
4 changes: 2 additions & 2 deletions test/zencode/bbs_sha.bats
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ When I verify the 'myStringArray' has a bbs signature in 'bbs signature' by 'Ali
Then print the string 'Signature is valid'
EOF
run $ZENROOM_EXECUTABLE -z -a '3messages_sha.json' -k sign_pubkey_sha.json wrong_message_sha.zen
assert_line '[W] The bbs signature by Alice is not authentic'
assert_line --partial 'The bbs signature by Alice is not authentic'
}

@test "Fail verification on a different public key" {
Expand All @@ -210,7 +210,7 @@ When I verify the 'myStringArray' has a bbs signature in 'bbs signature' by 'Ali
Then print the string 'Signature is valid'
EOF
run $ZENROOM_EXECUTABLE -z -a multi_msg_data_sha.json -k sign_pubkey_sha.json verify_from_wrong_pk_sha.zen
assert_line '[W] The bbs signature by Alice is not authentic'
assert_line --partial 'The bbs signature by Alice is not authentic'
}

@test "DOCS: Generate keys for Alice" {
Expand Down
4 changes: 2 additions & 2 deletions test/zencode/bbs_shake.bats
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ When I verify the 'myStringArray' has a bbs signature in 'bbs signature' by 'Ali
Then print the string 'Signature is valid'
EOF
run $ZENROOM_EXECUTABLE -z -a '3messages_shake.json' -k sign_pubkey_shake.json wrong_message_shake.zen
assert_line '[W] The bbs signature by Alice is not authentic'
assert_line --partial 'The bbs signature by Alice is not authentic'
}

@test "Fail verification on a different public key" {
Expand All @@ -210,5 +210,5 @@ When I verify the 'myStringArray' has a bbs signature in 'bbs signature' by 'Ali
Then print the string 'Signature is valid'
EOF
run $ZENROOM_EXECUTABLE -z -a multi_msg_data_shake.json -k sign_pubkey_shake.json verify_from_wrong_pk_shake.zen
assert_line '[W] The bbs signature by Alice is not authentic'
assert_line --partial 'The bbs signature by Alice is not authentic'
}
6 changes: 3 additions & 3 deletions test/zencode/ethereum.bats
Original file line number Diff line number Diff line change
Expand Up @@ -758,8 +758,8 @@ When I verify the ethereum address string 'string address' is valid
Then print the string 'This fails'
EOF
run $ZENROOM_EXECUTABLE -z -a checksum_fail.json checksum_fail.zen
assert_line '[W] "Invalid encoding for ethereum address. Expected encoding: 0x1e30e53E87869aaD8dC5A1A9dAc31a8dD3559460"'
assert_line '[W] [!] The address has a wrong encoding'
assert_line --partial 'Invalid encoding for ethereum address. Expected encoding: 0x1e30e53E87869aaD8dC5A1A9dAc31a8dD3559460'
assert_line --partial 'The address has a wrong encoding'
}

@test "Call generic smart contract" {
Expand Down Expand Up @@ -976,7 +976,7 @@ EOF
Then print the 'result_array'
EOF
run $ZENROOM_EXECUTABLE -z -a fail_array_verification.data fail_array_verification.zen
assert_line '[W] The ethereum signature by 0xe1C2F1ACb2758c4D88EDb84e0306A0a96682E62a is not authentic'
assert_line --partial 'The ethereum signature by 0xe1C2F1ACb2758c4D88EDb84e0306A0a96682E62a is not authentic'
}

@test "create the verification result of array of signature" {
Expand Down
4 changes: 2 additions & 2 deletions test/zencode/generic_bbs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run $ZENROOM_EXECUTABLE -z -a sign_pubkey.json wrong_message.zen
assert_line '[W] The bbs signature by Alice is not authentic'
assert_line --partial 'The bbs signature by Alice is not authentic'
}

@test "Fail verification on a different public key" {
Expand All @@ -126,7 +126,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run $ZENROOM_EXECUTABLE -z -a wrong_pubkey.json wrong_pubkey.zen
assert_line '[W] The bbs signature by Alice is not authentic'
assert_line --partial 'The bbs signature by Alice is not authentic'
}

@test "Alice signs a big file" {
Expand Down
4 changes: 2 additions & 2 deletions test/zencode/generic_dilithium.bats
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run $ZENROOM_EXECUTABLE -z -a sign_pubkey.json wrong_message.zen
assert_line '[W] The dilithium signature by Alice is not authentic'
assert_line --partial 'The dilithium signature by Alice is not authentic'
}

@test "Fail verification on a different public key" {
Expand All @@ -126,7 +126,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run $ZENROOM_EXECUTABLE -z -a wrong_pubkey.json wrong_pubkey.zen
assert_line '[W] The dilithium signature by Alice is not authentic'
assert_line --partial 'The dilithium signature by Alice is not authentic'
}

@test "Alice signs a big file" {
Expand Down
4 changes: 2 additions & 2 deletions test/zencode/generic_ecdh.bats
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run $ZENROOM_EXECUTABLE -z -a sign_pubkey.json wrong_message.zen
assert_line '[W] The ecdh signature by Alice is not authentic'
assert_line --partial 'The ecdh signature by Alice is not authentic'
}

@test "Fail verification on a different public key" {
Expand All @@ -126,7 +126,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run $ZENROOM_EXECUTABLE -z -a wrong_pubkey.json wrong_pubkey.zen
assert_line '[W] The ecdh signature by Alice is not authentic'
assert_line --partial 'The ecdh signature by Alice is not authentic'
}

@test "Alice signs a big file" {
Expand Down
4 changes: 2 additions & 2 deletions test/zencode/generic_eddsa.bats
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run $ZENROOM_EXECUTABLE -z -a sign_pubkey.json wrong_message.zen
assert_line '[W] The eddsa signature by Alice is not authentic'
assert_line --partial 'The eddsa signature by Alice is not authentic'
}

@test "Fail verification on a different public key" {
Expand All @@ -126,7 +126,7 @@ Then print the string 'Signature is valid'
and print the 'message'
EOF
run $ZENROOM_EXECUTABLE -z -a wrong_pubkey.json wrong_pubkey.zen
assert_line '[W] The eddsa signature by Alice is not authentic'
assert_line --partial 'The eddsa signature by Alice is not authentic'
}

@test "Alice signs a big file" {
Expand Down
Loading

0 comments on commit 3b10b98

Please sign in to comment.