2023-02-08 13:17:09 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-06-20 00:41:28 +02:00
|
|
|
/*
|
|
|
|
* frrscript unit tests
|
|
|
|
* Copyright (C) 2021 Donald Lee
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zebra.h>
|
|
|
|
|
|
|
|
#include "lib/frrscript.h"
|
2021-07-07 15:54:00 +02:00
|
|
|
#include "lib/frrlua.h"
|
2021-06-20 00:41:28 +02:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
frrscript_init("./lib");
|
2021-07-04 17:13:32 +02:00
|
|
|
struct frrscript *fs = frrscript_new("script1");
|
|
|
|
int result;
|
2021-06-20 00:41:28 +02:00
|
|
|
|
2021-07-07 21:07:06 +02:00
|
|
|
/* Positive testing */
|
|
|
|
|
2021-06-20 00:41:28 +02:00
|
|
|
long long a = 100, b = 200;
|
|
|
|
|
2021-07-04 17:13:32 +02:00
|
|
|
result = frrscript_load(fs, "foo", NULL);
|
|
|
|
assert(result == 0);
|
|
|
|
result = frrscript_call(fs, "foo", ("a", &a), ("b", &b));
|
2021-06-20 00:41:28 +02:00
|
|
|
assert(result == 0);
|
2021-07-07 21:07:06 +02:00
|
|
|
assert(a == 101);
|
2021-07-23 21:09:49 +02:00
|
|
|
assert(b == 201);
|
2021-07-07 21:07:06 +02:00
|
|
|
|
|
|
|
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);
|
2021-08-09 22:54:50 +02:00
|
|
|
long long *cptr = frrscript_get_result(fs, "bar", "c", lua_tolonglongp);
|
2021-07-07 21:07:06 +02:00
|
|
|
|
|
|
|
/* a should not occur in the returned table in script */
|
|
|
|
assert(a == 100);
|
2021-07-23 21:09:49 +02:00
|
|
|
assert(b == 201);
|
2021-07-07 21:07:06 +02:00
|
|
|
assert(*cptr == 303);
|
2021-07-26 17:27:43 +02:00
|
|
|
XFREE(MTYPE_SCRIPT_RES, cptr);
|
2021-06-20 00:41:28 +02:00
|
|
|
|
2021-07-04 23:25:49 +02:00
|
|
|
long long n = 5;
|
|
|
|
|
2021-07-07 15:54:00 +02:00
|
|
|
result = frrscript_load(fs, "fact", NULL);
|
|
|
|
assert(result == 0);
|
2021-07-04 23:25:49 +02:00
|
|
|
result = frrscript_call(fs, "fact", ("n", &n));
|
|
|
|
assert(result == 0);
|
2021-07-07 15:54:00 +02:00
|
|
|
long long *ansptr =
|
2021-08-09 22:54:50 +02:00
|
|
|
frrscript_get_result(fs, "fact", "ans", lua_tolonglongp);
|
2021-07-07 15:54:00 +02:00
|
|
|
assert(*ansptr == 120);
|
2024-11-18 10:10:05 +01:00
|
|
|
XFREE(MTYPE_SCRIPT_RES, ansptr);
|
2021-08-10 00:29:19 +02:00
|
|
|
|
|
|
|
/* check consecutive call + get_result without re-loading */
|
|
|
|
n = 4;
|
|
|
|
result = frrscript_call(fs, "fact", ("n", &n));
|
|
|
|
assert(result == 0);
|
2024-11-18 10:10:05 +01:00
|
|
|
int *ansptr_c = frrscript_get_result(fs, "fact", "ans", lua_tointegerp);
|
2021-08-10 00:29:19 +02:00
|
|
|
|
2024-11-18 10:10:05 +01:00
|
|
|
assert(*ansptr_c == 24);
|
|
|
|
XFREE(MTYPE_SCRIPT_RES, ansptr_c);
|
2021-07-04 23:25:49 +02:00
|
|
|
|
2021-07-07 21:07:06 +02:00
|
|
|
/* Negative testing */
|
2021-07-04 23:25:49 +02:00
|
|
|
|
|
|
|
/* Function does not exist in script file*/
|
2021-07-04 17:13:32 +02:00
|
|
|
result = frrscript_load(fs, "does_not_exist", NULL);
|
|
|
|
assert(result == 1);
|
|
|
|
|
2021-07-04 23:25:49 +02:00
|
|
|
/* Function was not (successfully) loaded */
|
2021-07-04 17:13:32 +02:00
|
|
|
result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b));
|
|
|
|
assert(result == 1);
|
|
|
|
|
2021-07-07 21:07:06 +02:00
|
|
|
/* Get result from a function that was not loaded */
|
2024-11-18 10:10:05 +01:00
|
|
|
int *intptr = frrscript_get_result(fs, "does_not_exist", "c", lua_tointegerp);
|
|
|
|
|
|
|
|
assert(intptr == NULL);
|
2021-07-07 21:07:06 +02:00
|
|
|
|
2021-07-04 23:25:49 +02:00
|
|
|
/* Function returns void */
|
|
|
|
result = frrscript_call(fs, "bad_return1");
|
|
|
|
assert(result == 1);
|
2021-07-04 17:13:32 +02:00
|
|
|
|
2021-07-04 23:25:49 +02:00
|
|
|
/* Function returns number */
|
|
|
|
result = frrscript_call(fs, "bad_return2");
|
|
|
|
assert(result == 1);
|
|
|
|
|
2021-07-07 21:07:06 +02:00
|
|
|
/* Get non-existent result from a function */
|
2021-07-04 23:25:49 +02:00
|
|
|
result = frrscript_call(fs, "bad_return3");
|
|
|
|
assert(result == 1);
|
2024-11-18 10:10:05 +01:00
|
|
|
intptr = frrscript_get_result(fs, "bad_return3", "c", lua_tointegerp);
|
|
|
|
assert(intptr == NULL);
|
|
|
|
XFREE(MTYPE_SCRIPT_RES, intptr);
|
2021-07-07 21:07:06 +02:00
|
|
|
|
|
|
|
/* Function throws exception */
|
|
|
|
result = frrscript_call(fs, "bad_return4");
|
|
|
|
assert(result == 1);
|
2021-07-04 17:13:32 +02:00
|
|
|
|
2021-07-08 11:51:14 +02:00
|
|
|
frrscript_delete(fs);
|
2021-07-07 19:32:30 +02:00
|
|
|
|
2021-06-20 00:41:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|