]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_frrscript.c
lib: Add new MTYPE for script results
[mirror_frr.git] / tests / lib / test_frrscript.c
1 /*
2 * frrscript unit tests
3 * Copyright (C) 2021 Donald Lee
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <zebra.h>
21
22 #include "lib/frrscript.h"
23 #include "lib/frrlua.h"
24
25 int main(int argc, char **argv)
26 {
27 frrscript_init("./lib");
28 struct frrscript *fs = frrscript_new("script1");
29 int result;
30
31 /* Positive testing */
32
33 long long a = 100, b = 200;
34
35 result = frrscript_load(fs, "foo", NULL);
36 assert(result == 0);
37 result = frrscript_call(fs, "foo", ("a", &a), ("b", &b));
38 assert(result == 0);
39 assert(a == 101);
40 assert(b == 201);
41
42 a = 100, b = 200;
43
44 result = frrscript_load(fs, "bar", NULL);
45 assert(result == 0);
46 result = frrscript_call(fs, "bar", ("a", &a), ("b", &b));
47 assert(result == 0);
48 long long *cptr = frrscript_get_result(fs, "bar", "c", lua_tointegerp);
49
50 /* a should not occur in the returned table in script */
51 assert(a == 100);
52 assert(b == 201);
53 assert(*cptr == 303);
54 XFREE(MTYPE_SCRIPT_RES, cptr);
55
56 long long n = 5;
57
58 result = frrscript_load(fs, "fact", NULL);
59 assert(result == 0);
60 result = frrscript_call(fs, "fact", ("n", &n));
61 assert(result == 0);
62 long long *ansptr =
63 frrscript_get_result(fs, "fact", "ans", lua_tointegerp);
64 assert(*ansptr == 120);
65 XFREE(MTYPE_SCRIPT_RES, ansptr);
66
67 /* Negative testing */
68
69 /* Function does not exist in script file*/
70 result = frrscript_load(fs, "does_not_exist", NULL);
71 assert(result == 1);
72
73 /* Function was not (successfully) loaded */
74 result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b));
75 assert(result == 1);
76
77 /* Get result from a function that was not loaded */
78 long long *llptr =
79 frrscript_get_result(fs, "does_not_exist", "c", lua_tointegerp);
80 assert(llptr == NULL);
81
82 /* Function returns void */
83 result = frrscript_call(fs, "bad_return1");
84 assert(result == 1);
85
86 /* Function returns number */
87 result = frrscript_call(fs, "bad_return2");
88 assert(result == 1);
89
90 /* Get non-existent result from a function */
91 result = frrscript_call(fs, "bad_return3");
92 assert(result == 1);
93 long long *cllptr =
94 frrscript_get_result(fs, "bad_return3", "c", lua_tointegerp);
95 assert(cllptr == NULL);
96
97 /* Function throws exception */
98 result = frrscript_call(fs, "bad_return4");
99 assert(result == 1);
100
101 frrscript_delete(fs);
102
103 return 0;
104 }