Skip to content

Commit

Permalink
docs: zencode-exec more documentation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaromil committed Aug 9, 2023
1 parent f91c5b5 commit 4316ddd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
6 changes: 5 additions & 1 deletion bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ The `zencode-data-keys-conf` is a file or stream with a newline separated list o
2. zencode script (string -> base64) *newline*
3. keys (json -> base64) *newline*
4. data (json -> base64) *newline*
5. extra (json -> base64) *newline*
6. context (json -> base64) *newline*

Each line should start directly with the base64 string without any prefix and should end with a newline. Anything else will likely be rejected.
Each line should start directly with the base64 string without any prefix and should end with a newline.

Empty lines can be a newline (or CRLF) and will be skipped, but should never be omitted: the zencode-exec expects 6 input lines in total, not less, including empty ones, all newline terminated.

This executes and returns two streams:
1. `stdout` with the `json` formatted results of the execution
Expand Down
7 changes: 6 additions & 1 deletion src/zen_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ static int lua_unserialize_json(lua_State* L) {
return 0;
}

// removed because of unexplained segfault when used inside pcall to
// parse zencode: set_rule and set_scenario will explode, also seems
// to perform worse than pure Lua (see PR #709)

#if 0
char* strtok_single(char* str, char const* delims)
{
static char* src = NULL;
Expand Down Expand Up @@ -249,6 +254,7 @@ static int lua_strtok(lua_State* L) {
}
return 1;
}
#endif

void zen_add_parse(lua_State *L) {
// override print() and io.write()
Expand All @@ -258,7 +264,6 @@ void zen_add_parse(lua_State *L) {
{"trim", lua_trim_spaces},
{"trimq", lua_trim_quotes},
{"jsontok", lua_unserialize_json},
// {"strtok", lua_strtok},
{NULL, NULL} };
lua_getglobal(L, "_G");
luaL_setfuncs(L, custom_parser, 0); // for Lua versions 5.2 or greater
Expand Down
79 changes: 77 additions & 2 deletions test/zencode/zencode_exec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ EOF
echo >> zencode_exec_stdin # data
echo >> zencode_exec_stdin # extra
echo >> zencode_exec_stdin # context
echo >> zencode_exec_stdin

cat zencode_exec_stdin | ${TR}/zencode-exec > $TMP/out
save_output empty.json
}
@test "Execute zencode-exec with all stdin inputs" {
@test "Execute zencode-exec with keys and data stdin inputs" {

# empty conf
echo > zencode_exec_stdin
Expand Down Expand Up @@ -120,3 +119,79 @@ EOF
save_output trace.json
assert_output '["Given nothing","When I create the random object of '"'"'256'"'"' bits","and debug"]'
}

@test "Execute zencode-exec with all stdin inputs including extra" {

# empty conf
echo > zencode_exec_stdin

# zencode
cat <<EOF | base64 -w0 >> zencode_exec_stdin
rule check version 3.0.0
Scenario 'ecdh': Bob verifies the signature from Alice
# Here we load the pubkey we'll verify the signature against
Given I have a 'public key' from 'Alice'
# Here we load the objects to be verified
Given I have a 'string' named 'myMessage'
Given I have a 'string array' named 'myStringArray'
# Here we load the objects's signatures
Given I have a 'signature' named 'myStringArray.signature'
Given I have a 'signature' named 'myMessage.signature'
# Here we perform the verifications
When I verify the 'myMessage' has a ecdh signature in 'myMessage.signature' by 'Alice'
When I verify the 'myStringArray' has a ecdh signature in 'myStringArray.signature' by 'Alice'
# Here we print out the result: if the verifications succeeded, a string will be printed out
# if the verifications failed, Zenroom will throw an error
Then print the string 'Zenroom certifies that signatures are all correct!'
Then print the 'myMessage'
EOF
echo >> zencode_exec_stdin

# keys
cat <<EOF | base64 -w0 >> zencode_exec_stdin
{
"Alice": {
"public_key": "BBCQg21VcjsmfTmNsg+I+8m1Cm0neaYONTqRnXUjsJLPa8075IYH+a9w2wRO7rFM1cKmv19Igd7ntDZcUvLq3xI="
}
}
EOF
echo >> zencode_exec_stdin

# data
cat <<EOF | base64 -w0 >> zencode_exec_stdin
{
"myMessage": "Dear Bob, your name is too short, goodbye - Alice.",
"myMessage.signature": {
"r": "vWerszPubruWexUib69c7IU8Dxy1iisUmMGC7h7arDw=",
"s": "nSjxT+JAP56HMRJjrLwwB6kP+mluYySeZcG8JPBGcpY="
}
}
EOF

# extra
cat <<EOF | base64 -w0 >> zencode_exec_stdin
{
"myStringArray": [
"Hello World! This is my string array, element [0]",
"Hello World! This is my string array, element [1]",
"Hello World! This is my string array, element [2]"
],
"myStringArray.signature": {
"r": "B8qrQqYSWaTf5Q16mBCjY1tfsD4Cf6ZSMJTHCCV8Chg=",
"s": "S1/Syca6+XozVr5P9fQ6/AkQ+fJTMfwc063sbKmZ5B4="
}
}
EOF
echo >> zencode_exec_stdin

# empty context
echo >> zencode_exec_stdin

cat zencode_exec_stdin | ${TR}/zencode-exec > $TMP/out
save_output verified.json
assert_output '{"myMessage":"Dear Bob, your name is too short, goodbye - Alice.","output":["Zenroom_certifies_that_signatures_are_all_correct!"]}'
}

0 comments on commit 4316ddd

Please sign in to comment.