]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_frrscript.c
3 * Copyright (C) 2021 Donald Lee
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)
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
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
22 #include "lib/frrscript.h"
23 #include "lib/frrlua.h"
25 int main(int argc
, char **argv
)
27 frrscript_init("./lib");
28 struct frrscript
*fs
= frrscript_new("script1");
31 /* Positive testing */
33 long long a
= 100, b
= 200;
35 result
= frrscript_load(fs
, "foo", NULL
);
37 result
= frrscript_call(fs
, "foo", ("a", &a
), ("b", &b
));
44 result
= frrscript_load(fs
, "bar", NULL
);
46 result
= frrscript_call(fs
, "bar", ("a", &a
), ("b", &b
));
48 long long *cptr
= frrscript_get_result(fs
, "bar", "c", lua_tointegerp
);
50 /* a should not occur in the returned table in script */
54 XFREE(MTYPE_SCRIPT_RES
, cptr
);
58 result
= frrscript_load(fs
, "fact", NULL
);
60 result
= frrscript_call(fs
, "fact", ("n", &n
));
63 frrscript_get_result(fs
, "fact", "ans", lua_tointegerp
);
64 assert(*ansptr
== 120);
65 XFREE(MTYPE_SCRIPT_RES
, ansptr
);
67 /* Negative testing */
69 /* Function does not exist in script file*/
70 result
= frrscript_load(fs
, "does_not_exist", NULL
);
73 /* Function was not (successfully) loaded */
74 result
= frrscript_call(fs
, "does_not_exist", ("a", &a
), ("b", &b
));
77 /* Get result from a function that was not loaded */
79 frrscript_get_result(fs
, "does_not_exist", "c", lua_tointegerp
);
80 assert(llptr
== NULL
);
82 /* Function returns void */
83 result
= frrscript_call(fs
, "bad_return1");
86 /* Function returns number */
87 result
= frrscript_call(fs
, "bad_return2");
90 /* Get non-existent result from a function */
91 result
= frrscript_call(fs
, "bad_return3");
94 frrscript_get_result(fs
, "bad_return3", "c", lua_tointegerp
);
95 assert(cllptr
== NULL
);
97 /* Function throws exception */
98 result
= frrscript_call(fs
, "bad_return4");
101 frrscript_delete(fs
);