Commit b846b350 authored by Alberto Lerda's avatar Alberto Lerda
Browse files

fix: go include

fix: test to fix go get

fix: ZenResult not exported in golang

fix: update example in golang README
parent b0740083
......@@ -23,30 +23,26 @@ $ go get github.com/dyne/Zenroom/bindings/golang/zenroom
package main
import (
"fmt"
"log"
"github.com/dyne/Zenroom/bindings/golang/zenroom"
zenroom "github.com/dyne/Zenroom/bindings/golang/zenroom"
"fmt"
)
genKeysScript := []byte(`
keyring = ecdh.new('ec25519')
keyring:keygen()
output = JSON.encode({
public = keyring:public():base64(),
private = keyring:private():base64()
})
print(output)
`)
keys, err := zenroom.Exec(genKeysScript)
if err != nil {
log.Fatal(err)
}
log.Printf("Keys: %s", keys)
```
func main() {
genKeysScript :=
`Scenario ecdh
Given nothing
hen I create the ecdh key
Then print 'keyring'
`
result, success := zenroom.ZencodeExec(genKeysScript, "", "", "")
if ! success {
fmt.Println(result.Logs)
}
fmt.Println(result.Output)
}
```
## More Documentation
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
......@@ -24,8 +24,8 @@ import (
const BUFSIZE = 2 * 1024 * 1024
type ZenResult struct {
output string;
logs string;
Output string;
Logs string;
}
// ZenroomExec is our primary public API method, and it is here that we call Zenroom's
......@@ -95,8 +95,8 @@ func wrapper(fun C.fun, script string, conf string, keys string, data string) (Z
)
zen_res := ZenResult{
output: C.GoString(stdout),
logs: C.GoString(stderr),
Output: C.GoString(stdout),
Logs: C.GoString(stderr),
}
return zen_res, res == 0
......
/* This file is part of Zenroom (https://zenroom.dyne.org)
*
* Copyright (C) 2017-2020 Dyne.org foundation
* designed, written and maintained by Denis Roio <jaromil@dyne.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef __ZENROOM_H__
#define __ZENROOM_H__
/////////////////////////////////////////
// high level api: one simple call
int zenroom_exec(char *script, char *conf, char *keys, char *data);
int zencode_exec(char *script, char *conf, char *keys, char *data);
// in case buffers should be used instead of stdout/err file
// descriptors, this call defines where to print out the output and
// the maximum sizes allowed for it. Output is NULL terminated.
int zenroom_exec_tobuf(char *script, char *conf, char *keys, char *data,
char *stdout_buf, size_t stdout_len,
char *stderr_buf, size_t stderr_len);
int zencode_exec_tobuf(char *script, char *conf, char *keys, char *data,
char *stdout_buf, size_t stdout_len,
char *stderr_buf, size_t stderr_len);
////////////////////////////////////////
// lower level api: init (exec_line*) teardown
// heap initialised by the memory manager
typedef struct {
void* (*malloc)(size_t size);
void* (*realloc)(void *ptr, size_t size);
void (*free)(void *ptr);
void* (*sys_malloc)(size_t size);
void* (*sys_realloc)(void *ptr, size_t size);
void (*sys_free)(void *ptr);
} zen_mem_t;
#define RANDOM_SEED_LEN 64
#include <stdarg.h>
typedef int (*sprintf_t)( char * buf, char const * fmt, ... );
typedef int (*snprintf_t)( char * buf, size_t count, char const * fmt, ... );
typedef int (*vsprintf_t)( char * buf, char const * fmt, va_list va );
typedef int (*vsnprintf_t)( char * buf, size_t count, char const * fmt, va_list va );
// zenroom context, also available as "_Z" global in lua space
// contents are opaque in lua and available only as lightuserdata
typedef struct {
void *lua; // (lua_State*)
void *zstd_c; // ZSTD context
void *zstd_d;
char *stdout_buf;
size_t stdout_len;
size_t stdout_pos;
size_t stdout_full;
char *stderr_buf;
size_t stderr_len;
size_t stderr_pos;
size_t stderr_full;
void *random_generator; // cast to RNG
char random_seed[RANDOM_SEED_LEN];
int random_external; // signal when rngseed is external
int debuglevel;
int errorlevel;
void *userdata; // anything passed at init (reserved for caller)
sprintf_t sprintf;
snprintf_t snprintf;
vsprintf_t vsprintf;
vsnprintf_t vsnprintf;
} zenroom_t;
zenroom_t *zen_init(const char *conf, char *keys, char *data);
int zen_exec_script(zenroom_t *Z, const char *script);
int zen_exec_zencode(zenroom_t *Z, const char *script);
void zen_teardown(zenroom_t *zenroom);
#define MAX_LINE 1024 // 1KiB maximum length for a newline terminated line (Zencode)
#ifndef MAX_ZENCODE_LINE
#define MAX_ZENCODE_LINE 512
#endif
#ifndef MAX_CONFIG // for the configuration parser
#define MAX_CONFIG 512
#endif
#ifndef MAX_ZENCODE // maximum size of a zencode script
#define MAX_ZENCODE 16384
#endif
#ifndef MAX_FILE // for cli.c
#define MAX_FILE 2048000 // load max 2MiB files
#endif
#ifndef MAX_STRING // mostly for cli.c
#define MAX_STRING 20480 // max 20KiB strings
#endif
#ifndef MAX_OCTET
#define MAX_OCTET 4096000 // max 4MiB for octets
#endif
#define LUA_BASELIBNAME "_G"
#define ZEN_BITS 32
#ifndef SIZE_MAX
#if ZEN_BITS == 32
#define SIZE_MAX 4294967296
#elif ZEN_BITS == 8
#define SIZE_MAX 65536
#endif
#endif
#endif
......@@ -8,8 +8,8 @@ import (
func TestBasicCall(t *testing.T) {
script := "print (1)"
res, _ := ZenroomExec(script, "", "", "")
if res.output == "1" {
t.Errorf("calling print (1), got:%s", res.output)
if res.Output == "1" {
t.Errorf("calling print (1), got:%s", res.Output)
}
}
......@@ -17,7 +17,7 @@ func TestVersion(t *testing.T) {
script := "print(VERSION)"
res, _ := ZenroomExec(script, "", "", "")
fmt.Printf("Zenroom version: %s\n", res.output)
fmt.Printf("Zenroom version: %s\n", res.Output)
}
func TestCallStrings(t *testing.T) {
......@@ -40,8 +40,8 @@ func TestCallStrings(t *testing.T) {
for _, testcase := range testcases {
t.Run(testcase.label, func(t *testing.T) {
res, _ := ZenroomExec(testcase.script, "", "", "")
if res.output != testcase.resp {
t.Errorf("calling [%s] got %s", testcase.script, res.output)
if res.Output != testcase.resp {
t.Errorf("calling [%s] got %s", testcase.script, res.Output)
}
})
}
......@@ -54,7 +54,7 @@ When I create the ecdh key
Then print my data`
res, success := ZencodeExec(script, "", "", "")
if !success {
t.Error(res.logs)
t.Error(res.Logs)
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment