]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_frrscript.c
Merge pull request #9330 from idryzhov/vtysh-srv6
[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
66 /* check consecutive call + get_result without re-loading */
67 n = 4;
68 result = frrscript_call(fs, "fact", ("n", &n));
69 assert(result == 0);
70 ansptr = frrscript_get_result(fs, "fact", "ans", lua_tointegerp);
71 assert(*ansptr == 24);
72
73 XFREE(MTYPE_SCRIPT_RES, ansptr);
74
75 /* Negative testing */
76
77 /* Function does not exist in script file*/
78 result = frrscript_load(fs, "does_not_exist", NULL);
79 assert(result == 1);
80
81 /* Function was not (successfully) loaded */
82 result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b));
83 assert(result == 1);
84
85 /* Get result from a function that was not loaded */
86 long long *llptr =
87 frrscript_get_result(fs, "does_not_exist", "c", lua_tointegerp);
88 assert(llptr == NULL);
89
90 /* Function returns void */
91 result = frrscript_call(fs, "bad_return1");
92 assert(result == 1);
93
94 /* Function returns number */
95 result = frrscript_call(fs, "bad_return2");
96 assert(result == 1);
97
98 /* Get non-existent result from a function */
99 result = frrscript_call(fs, "bad_return3");
100 assert(result == 1);
101 long long *cllptr =
102 frrscript_get_result(fs, "bad_return3", "c", lua_tointegerp);
103 assert(cllptr == NULL);
104
105 /* Function throws exception */
106 result = frrscript_call(fs, "bad_return4");
107 assert(result == 1);
108
109 frrscript_delete(fs);
110
111 return 0;
112 }