From 4535b6113cf105287e060042b956251d4cbf4198 Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Sun, 4 Jul 2021 23:13:32 +0800 Subject: tests: Add test for calling Lua function Signed-off-by: Donald Lee --- tests/lib/test_frrscript.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'tests/lib/test_frrscript.c') diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index bd75cc5552..4d34b58832 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -24,14 +24,30 @@ int main(int argc, char **argv) { frrscript_init("./lib"); + struct frrscript *fs = frrscript_new("script1"); + int result; - struct frrscript *fs = frrscript_load("script1", NULL); long long a = 100, b = 200; - int result = frrscript_call(fs, ("a", &a), ("b", &b)); + result = frrscript_load(fs, "foo", NULL); + assert(result == 0); + result = frrscript_call(fs, "foo", ("a", &a), ("b", &b)); assert(result == 0); assert(a == 300); assert(b == 200); + result = frrscript_load(fs, "does_not_exist", NULL); + assert(result == 1); + + result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b)); + assert(result == 1); + + frrscript_load(fs, "fact", NULL); + long long n = 5; + + result = frrscript_call(fs, "fact", ("n", &n)); + assert(result == 0); + assert(n == 120); + return 0; } -- cgit v1.2.3 From 7948c5d27ac5313841a0f8e0b88ecc090932dffe Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Mon, 5 Jul 2021 05:25:49 +0800 Subject: tests: Add errorneous test cases Signed-off-by: Donald Lee --- tests/lib/script1.lua | 12 ++++++++++++ tests/lib/test_frrscript.c | 28 +++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) (limited to 'tests/lib/test_frrscript.c') diff --git a/tests/lib/script1.lua b/tests/lib/script1.lua index f7cd8a90b9..e911a1c941 100644 --- a/tests/lib/script1.lua +++ b/tests/lib/script1.lua @@ -18,3 +18,15 @@ function fact(n) n = helper(n) } end + +function bad_return1() +end + +function bad_return2() + return 1 +end + +function bad_return3() + error("Something bad!") +end + diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index 4d34b58832..3bb45a2651 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -36,18 +36,36 @@ int main(int argc, char **argv) assert(a == 300); assert(b == 200); + frrscript_load(fs, "fact", NULL); + long long n = 5; + + result = frrscript_call(fs, "fact", ("n", &n)); + assert(result == 0); + assert(n == 120); + + /* Function does not exist in script file*/ + result = frrscript_load(fs, "does_not_exist", NULL); + assert(result == 1); + + /* Function does not exist in script file*/ result = frrscript_load(fs, "does_not_exist", NULL); assert(result == 1); + /* Function was not (successfully) loaded */ result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b)); assert(result == 1); - frrscript_load(fs, "fact", NULL); - long long n = 5; + /* Function returns void */ + result = frrscript_call(fs, "bad_return1"); + assert(result == 1); - result = frrscript_call(fs, "fact", ("n", &n)); - assert(result == 0); - assert(n == 120); + /* Function returns number */ + result = frrscript_call(fs, "bad_return2"); + assert(result == 1); + + /* Function throws exception */ + result = frrscript_call(fs, "bad_return3"); + assert(result == 1); return 0; } -- cgit v1.2.3 From 5090d7249fd4b4fd65945a6f3282855e8b21bcfb Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Wed, 7 Jul 2021 21:54:00 +0800 Subject: tests: Add test for frrscript_get_result Signed-off-by: Donald Lee --- tests/lib/script1.lua | 2 +- tests/lib/test_frrscript.c | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'tests/lib/test_frrscript.c') diff --git a/tests/lib/script1.lua b/tests/lib/script1.lua index e911a1c941..5029a921bc 100644 --- a/tests/lib/script1.lua +++ b/tests/lib/script1.lua @@ -15,7 +15,7 @@ function fact(n) end end return { - n = helper(n) + ans = helper(n) } end diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index 3bb45a2651..e8b7927e15 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -20,6 +20,7 @@ #include #include "lib/frrscript.h" +#include "lib/frrlua.h" int main(int argc, char **argv) { @@ -36,12 +37,16 @@ int main(int argc, char **argv) assert(a == 300); assert(b == 200); - frrscript_load(fs, "fact", NULL); long long n = 5; + result = frrscript_load(fs, "fact", NULL); + assert(result == 0); result = frrscript_call(fs, "fact", ("n", &n)); assert(result == 0); - assert(n == 120); + long long *ansptr = + frrscript_get_result(fs, "fact", "ans", lua_tointegerp); + assert(*ansptr == 120); + XFREE(MTYPE_TMP, ansptr); /* Function does not exist in script file*/ result = frrscript_load(fs, "does_not_exist", NULL); -- cgit v1.2.3 From ad6e9b854d64aa91eef2d965b7778188d2d7811a Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Thu, 8 Jul 2021 01:32:30 +0800 Subject: test: Use frrscript_unload Signed-off-by: Donald Lee --- lib/command.c | 2 ++ tests/lib/test_frrscript.c | 2 ++ 2 files changed, 4 insertions(+) (limited to 'tests/lib/test_frrscript.c') diff --git a/lib/command.c b/lib/command.c index b2b80daddd..7808f57594 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2443,6 +2443,8 @@ DEFUN(script, vty_out(vty, "p: %s\n", buf); vty_out(vty, "Script result: %d\n", ret); + frrscript_unload(fs); + return CMD_SUCCESS; } #endif diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index e8b7927e15..399b950799 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -72,5 +72,7 @@ int main(int argc, char **argv) result = frrscript_call(fs, "bad_return3"); assert(result == 1); + frrscript_unload(fs); + return 0; } -- cgit v1.2.3 From 8a04c1e74e0a21d34a626c378a80c3d5b6302971 Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Thu, 8 Jul 2021 03:07:06 +0800 Subject: tests: Add more examples to get_result Signed-off-by: Donald Lee --- tests/lib/script1.lua | 26 ++++++++++++++++++++++++-- tests/lib/test_frrscript.c | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 8 deletions(-) (limited to 'tests/lib/test_frrscript.c') diff --git a/tests/lib/script1.lua b/tests/lib/script1.lua index 5029a921bc..6361c960a7 100644 --- a/tests/lib/script1.lua +++ b/tests/lib/script1.lua @@ -1,12 +1,28 @@ + +-- Positive testing + function foo(a, b) - a = a + b + a = a + 1 + b = b + 1 return { a = a, b = b, } end +function bar(a, b) + a = a + 1 + b = b + 1 + c = 303 + return { + b = b, + c = c, + } +end + function fact(n) + -- outer function must return a table + -- inner functions can be used to recurse or as helpers function helper(m) if m == 0 then return 1 @@ -19,14 +35,20 @@ function fact(n) } end +-- Negative testing + function bad_return1() end function bad_return2() - return 1 + return 123 end function bad_return3() + return {} +end + +function bad_return4() error("Something bad!") end diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index 399b950799..e01807f193 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -28,14 +28,30 @@ int main(int argc, char **argv) struct frrscript *fs = frrscript_new("script1"); int result; + /* Positive testing */ + long long a = 100, b = 200; result = frrscript_load(fs, "foo", NULL); assert(result == 0); result = frrscript_call(fs, "foo", ("a", &a), ("b", &b)); assert(result == 0); - assert(a == 300); - assert(b == 200); + assert(a == 101); + assert(b == 202); + + a = 100, b = 200; + + result = frrscript_load(fs, "bar", NULL); + assert(result == 0); + result = frrscript_call(fs, "bar", ("a", &a), ("b", &b)); + assert(result == 0); + long long *cptr = frrscript_get_result(fs, "bar", "c", lua_tointegerp); + + /* a should not occur in the returned table in script */ + assert(a == 100); + assert(b == 202); + assert(*cptr == 303); + XFREE(MTYPE_TMP, cptr); long long n = 5; @@ -48,9 +64,7 @@ int main(int argc, char **argv) assert(*ansptr == 120); XFREE(MTYPE_TMP, ansptr); - /* Function does not exist in script file*/ - result = frrscript_load(fs, "does_not_exist", NULL); - assert(result == 1); + /* Negative testing */ /* Function does not exist in script file*/ result = frrscript_load(fs, "does_not_exist", NULL); @@ -60,6 +74,11 @@ int main(int argc, char **argv) result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b)); assert(result == 1); + /* Get result from a function that was not loaded */ + long long *llptr = + frrscript_get_result(fs, "does_not_exist", "c", lua_tointegerp); + assert(llptr == NULL); + /* Function returns void */ result = frrscript_call(fs, "bad_return1"); assert(result == 1); @@ -68,9 +87,16 @@ int main(int argc, char **argv) result = frrscript_call(fs, "bad_return2"); assert(result == 1); - /* Function throws exception */ + /* Get non-existent result from a function */ result = frrscript_call(fs, "bad_return3"); assert(result == 1); + long long *cllptr = + frrscript_get_result(fs, "bad_return3", "c", lua_tointegerp); + assert(cllptr == NULL); + + /* Function throws exception */ + result = frrscript_call(fs, "bad_return4"); + assert(result == 1); frrscript_unload(fs); -- cgit v1.2.3 From 64d457d7ac22d643afb5638bacb0b9713274689e Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Thu, 8 Jul 2021 17:51:14 +0800 Subject: lib: Rename frrscript_unload to delete frrscript_load now loads a function instead of a file, so frrscript_unload should be renamed since it does not unload a function. Signed-off-by: Donald Lee --- bgpd/bgp_routemap.c | 2 +- lib/command.c | 2 +- lib/frrscript.c | 2 +- lib/frrscript.h | 2 +- tests/lib/test_frrscript.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) (limited to 'tests/lib/test_frrscript.c') diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 0b22caaee7..c2062a5f60 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -434,7 +434,7 @@ route_match_script(void *rule, const struct prefix *prefix, void *object) break; } - frrscript_unload(fs); + frrscript_delete(fs); return status; } diff --git a/lib/command.c b/lib/command.c index 7808f57594..ceea186a9d 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2443,7 +2443,7 @@ DEFUN(script, vty_out(vty, "p: %s\n", buf); vty_out(vty, "Script result: %d\n", ret); - frrscript_unload(fs); + frrscript_delete(fs); return CMD_SUCCESS; } diff --git a/lib/frrscript.c b/lib/frrscript.c index 8252455fd9..b3f8a9f6f5 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -319,7 +319,7 @@ fail: return 1; } -void frrscript_unload(struct frrscript *fs) +void frrscript_delete(struct frrscript *fs) { hash_iterate(fs->lua_function_hash, lua_function_free, NULL); XFREE(MTYPE_SCRIPT, fs->name); diff --git a/lib/frrscript.h b/lib/frrscript.h index 1f234a38a9..14a922a741 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -89,7 +89,7 @@ int frrscript_load(struct frrscript *fs, const char *function_name, /* * Destroy FRR script. */ -void frrscript_unload(struct frrscript *fs); +void frrscript_delete(struct frrscript *fs); /* * Register a Lua codec for a type. diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index e01807f193..2d5746b587 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -98,7 +98,7 @@ int main(int argc, char **argv) result = frrscript_call(fs, "bad_return4"); assert(result == 1); - frrscript_unload(fs); + frrscript_delete(fs); return 0; } -- cgit v1.2.3 From 26693b3afce6abfaf77ac4833ce9f594ef7118b1 Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Sat, 24 Jul 2021 03:09:49 +0800 Subject: lib: typo in tests Signed-off-by: Donald Lee --- tests/lib/test_frrscript.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/lib/test_frrscript.c') diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index 2d5746b587..44e644c52a 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -37,7 +37,7 @@ int main(int argc, char **argv) result = frrscript_call(fs, "foo", ("a", &a), ("b", &b)); assert(result == 0); assert(a == 101); - assert(b == 202); + assert(b == 201); a = 100, b = 200; @@ -49,7 +49,7 @@ int main(int argc, char **argv) /* a should not occur in the returned table in script */ assert(a == 100); - assert(b == 202); + assert(b == 201); assert(*cptr == 303); XFREE(MTYPE_TMP, cptr); -- cgit v1.2.3 From 78f1ac25744f3db6d64570037066380f9b347abe Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Mon, 26 Jul 2021 23:27:43 +0800 Subject: lib: Add new MTYPE for script results Signed-off-by: Donald Lee --- bgpd/bgp_routemap.c | 2 +- doc/developer/scripting.rst | 6 +++--- lib/frrlua.c | 22 ++++++++++++++-------- lib/frrlua.h | 2 ++ tests/lib/test_frrscript.c | 4 ++-- 5 files changed, 22 insertions(+), 14 deletions(-) (limited to 'tests/lib/test_frrscript.c') diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index b01186e657..4b71ba30ce 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -426,7 +426,7 @@ route_match_script(void *rule, const struct prefix *prefix, void *object) break; } - XFREE(MTYPE_TMP, action); + XFREE(MTYPE_SCRIPT_RES, action); frrscript_delete(fs); diff --git a/doc/developer/scripting.rst b/doc/developer/scripting.rst index e40029c2b4..fa1c521fc6 100644 --- a/doc/developer/scripting.rst +++ b/doc/developer/scripting.rst @@ -274,7 +274,7 @@ So, a typical execution call, with error checking, looks something like this: goto DONE; // "d" might not have been in returned table assert(*d == 800); - XFREE(MTYPE_TMP, d); // caller responsible for free + XFREE(MTYPE_SCRIPT_RES, d); // caller responsible for free DONE: frrscript_delete(fs); @@ -430,7 +430,7 @@ This function can and should be implemented using ``lua_decode_*``: void *lua_toprefix(lua_State *L, int idx) { - struct prefix *p = XCALLOC(MTYPE_TMP, sizeof(struct prefix)); + struct prefix *p = XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct prefix)); lua_decode_prefix(L, idx, p); return p; @@ -438,7 +438,7 @@ This function can and should be implemented using ``lua_decode_*``: The returned data must always be copied off the stack and the copy must be -allocated with ``MTYPE_TMP``. This way it is possible to unload the script +allocated with ``MTYPE_SCRIPT_RES``. This way it is possible to unload the script (destroy the state) without invalidating any references to values stored in it. Note that it is the caller's responsibility to free the data. diff --git a/lib/frrlua.c b/lib/frrlua.c index 5d0732eac0..96d7269440 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -29,6 +29,8 @@ #include "log.h" #include "buffer.h" +DEFINE_MTYPE(LIB, SCRIPT_RES, "Scripting results"); + /* Lua stuff */ /* @@ -81,7 +83,7 @@ void lua_decode_prefix(lua_State *L, int idx, struct prefix *prefix) void *lua_toprefix(lua_State *L, int idx) { - struct prefix *p = XCALLOC(MTYPE_TMP, sizeof(struct prefix)); + struct prefix *p = XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct prefix)); lua_decode_prefix(L, idx, p); return p; } @@ -153,7 +155,8 @@ void lua_decode_interface(lua_State *L, int idx, struct interface *ifp) } void *lua_tointerface(lua_State *L, int idx) { - struct interface *ifp = XCALLOC(MTYPE_TMP, sizeof(struct interface)); + struct interface *ifp = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct interface)); lua_decode_interface(L, idx, ifp); return ifp; @@ -183,7 +186,8 @@ void lua_decode_inaddr(lua_State *L, int idx, struct in_addr *inaddr) void *lua_toinaddr(lua_State *L, int idx) { - struct in_addr *inaddr = XCALLOC(MTYPE_TMP, sizeof(struct in_addr)); + struct in_addr *inaddr = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct in_addr)); lua_decode_inaddr(L, idx, inaddr); return inaddr; } @@ -213,7 +217,8 @@ void lua_decode_in6addr(lua_State *L, int idx, struct in6_addr *in6addr) void *lua_toin6addr(lua_State *L, int idx) { - struct in6_addr *in6addr = XCALLOC(MTYPE_TMP, sizeof(struct in6_addr)); + struct in6_addr *in6addr = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct in6_addr)); lua_decode_in6addr(L, idx, in6addr); return in6addr; } @@ -243,7 +248,8 @@ void lua_decode_sockunion(lua_State *L, int idx, union sockunion *su) void *lua_tosockunion(lua_State *L, int idx) { - union sockunion *su = XCALLOC(MTYPE_TMP, sizeof(union sockunion)); + union sockunion *su = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(union sockunion)); lua_decode_sockunion(L, idx, su); return su; @@ -262,7 +268,7 @@ void lua_decode_timet(lua_State *L, int idx, time_t *t) void *lua_totimet(lua_State *L, int idx) { - time_t *t = XCALLOC(MTYPE_TMP, sizeof(time_t)); + time_t *t = XCALLOC(MTYPE_SCRIPT_RES, sizeof(time_t)); lua_decode_timet(L, idx, t); return t; @@ -283,7 +289,7 @@ void lua_decode_integerp(lua_State *L, int idx, long long *num) void *lua_tointegerp(lua_State *L, int idx) { - long long *num = XCALLOC(MTYPE_TMP, sizeof(long long)); + long long *num = XCALLOC(MTYPE_SCRIPT_RES, sizeof(long long)); lua_decode_integerp(L, idx, num); return num; @@ -297,7 +303,7 @@ void lua_decode_stringp(lua_State *L, int idx, char *str) void *lua_tostringp(lua_State *L, int idx) { - char *string = XSTRDUP(MTYPE_TMP, lua_tostring(L, idx)); + char *string = XSTRDUP(MTYPE_SCRIPT_RES, lua_tostring(L, idx)); return string; } diff --git a/lib/frrlua.h b/lib/frrlua.h index 98bff00fd6..3e16c82e22 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -34,6 +34,8 @@ extern "C" { #endif +DECLARE_MTYPE(SCRIPT_RES); + /* * gcc-10 is complaining about the wrapper function * not being compatible with lua_pushstring returning diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index 44e644c52a..7b23045978 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -51,7 +51,7 @@ int main(int argc, char **argv) assert(a == 100); assert(b == 201); assert(*cptr == 303); - XFREE(MTYPE_TMP, cptr); + XFREE(MTYPE_SCRIPT_RES, cptr); long long n = 5; @@ -62,7 +62,7 @@ int main(int argc, char **argv) long long *ansptr = frrscript_get_result(fs, "fact", "ans", lua_tointegerp); assert(*ansptr == 120); - XFREE(MTYPE_TMP, ansptr); + XFREE(MTYPE_SCRIPT_RES, ansptr); /* Negative testing */ -- cgit v1.2.3