]> git.proxmox.com Git - mirror_frr.git/commitdiff
tests: Add unit test for lua encoders/decoders
authorDonald Lee <dlqs@gmx.com>
Sat, 19 Jun 2021 22:42:12 +0000 (06:42 +0800)
committerDonald Lee <dlqs@gmx.com>
Tue, 22 Jun 2021 16:58:39 +0000 (00:58 +0800)
Signed-off-by: Donald Lee <dlqs@gmx.com>
tests/.gitignore
tests/lib/test_frrlua.c [new file with mode: 0644]
tests/lib/test_frrlua.py [new file with mode: 0644]
tests/subdir.am

index c5c3bb87ba5456b0a526b2c5a01edcc6dbef7603..3fad1b081320e3f96e87394a7d87a4783496c6b3 100644 (file)
@@ -26,6 +26,7 @@
 /lib/test_buffer
 /lib/test_checksum
 /lib/test_frrscript
+/lib/test_frrlua
 /lib/test_graph
 /lib/test_heavy
 /lib/test_heavy_thread
diff --git a/tests/lib/test_frrlua.c b/tests/lib/test_frrlua.c
new file mode 100644 (file)
index 0000000..a81446f
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * frrlua unit tests
+ * Copyright (C) 2021  Donald Lee
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <zebra.h>
+#include "string.h"
+#include "stdio.h"
+#include "lib/frrlua.h"
+
+static void test_encode_decode(void)
+{
+       lua_State *L = luaL_newstate();
+
+       long long a = 123;
+       long long b = a;
+
+       lua_pushintegerp(L, &a);
+       lua_decode_integerp(L, -1, &a);
+       assert(a == b);
+       assert(lua_gettop(L) == 0);
+
+       time_t time_a = 100;
+       time_t time_b = time_a;
+
+       lua_pushtimet(L, &time_a);
+       lua_decode_timet(L, -1, &time_a);
+       assert(time_a == time_b);
+       assert(lua_gettop(L) == 0);
+
+       char str_b[] = "Hello", str_a[6];
+
+       strlcpy(str_a, str_b, sizeof(str_b));
+       lua_pushstring_wrapper(L, str_a);
+       lua_decode_stringp(L, -1, str_a);
+       assert(strncmp(str_a, str_b, sizeof(str_b)) == 0);
+       assert(lua_gettop(L) == 0);
+
+       char p_b_str[] = "10.0.0.0/24", p_a_str[12];
+       struct prefix p_a;
+
+       strlcpy(p_a_str, p_b_str, sizeof(p_b_str));
+       str2prefix(p_a_str, &p_a);
+       lua_pushprefix(L, &p_a);
+       lua_decode_prefix(L, -1, &p_a);
+       prefix2str(&p_a, p_a_str, sizeof(p_b_str));
+       assert(strncmp(p_a_str, p_b_str, sizeof(p_b_str)) == 0);
+       assert(lua_gettop(L) == 0);
+
+       struct interface ifp_a;
+       struct interface ifp_b = ifp_a;
+
+       lua_pushinterface(L, &ifp_a);
+       lua_decode_interface(L, -1, &ifp_a);
+       assert(strncmp(ifp_a.name, ifp_b.name, sizeof(ifp_b.name)) == 0);
+       assert(ifp_a.ifindex == ifp_b.ifindex);
+       assert(ifp_a.status == ifp_b.status);
+       assert(ifp_a.flags == ifp_b.flags);
+       assert(ifp_a.metric == ifp_b.metric);
+       assert(ifp_a.speed == ifp_b.speed);
+       assert(ifp_a.mtu == ifp_b.mtu);
+       assert(ifp_a.mtu6 == ifp_b.mtu6);
+       assert(ifp_a.bandwidth == ifp_b.bandwidth);
+       assert(ifp_a.link_ifindex == ifp_b.link_ifindex);
+       assert(ifp_a.ll_type == ifp_b.ll_type);
+       assert(lua_gettop(L) == 0);
+
+       struct in_addr addr_a;
+       struct in_addr addr_b = addr_a;
+
+       lua_pushinaddr(L, &addr_a);
+       lua_decode_inaddr(L, -1, &addr_a);
+       assert(addr_a.s_addr == addr_b.s_addr);
+       assert(lua_gettop(L) == 0);
+
+       struct in6_addr in6addr_a;
+       struct in6_addr in6addr_b = in6addr_a;
+
+       lua_pushin6addr(L, &in6addr_a);
+       lua_decode_in6addr(L, -1, &in6addr_a);
+       assert(in6addr_cmp(&in6addr_a, &in6addr_b) == 0);
+       assert(lua_gettop(L) == 0);
+
+       union sockunion su_a, su_b;
+
+       memset(&su_a, 0, sizeof(union sockunion));
+       memset(&su_b, 0, sizeof(union sockunion));
+       lua_pushsockunion(L, &su_a);
+       lua_decode_sockunion(L, -1, &su_a);
+       assert(sockunion_cmp(&su_a, &su_b) == 0);
+       assert(lua_gettop(L) == 0);
+}
+
+int main(int argc, char **argv)
+{
+       test_encode_decode();
+}
diff --git a/tests/lib/test_frrlua.py b/tests/lib/test_frrlua.py
new file mode 100644 (file)
index 0000000..e4943f9
--- /dev/null
@@ -0,0 +1,8 @@
+import frrtest
+
+
+class TestFrrlua(frrtest.TestMultiOut):
+    program = "./test_frrlua"
+
+
+TestFrrlua.exit_cleanly()
index 0c083debce044444b9ae0f933b6ad7721996a447..5cf78dd3a1fa5c863494103e3e3b5dc2b4e5e35e 100644 (file)
@@ -70,6 +70,7 @@ check_PROGRAMS = \
        tests/lib/test_atomlist \
        tests/lib/test_buffer \
        tests/lib/test_checksum \
+       tests/lib/test_frrlua \
        tests/lib/test_frrscript \
        tests/lib/test_heavy_thread \
        tests/lib/test_heavy_wq \
@@ -290,6 +291,10 @@ tests_lib_test_checksum_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_checksum_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_checksum_LDADD = $(ALL_TESTS_LDADD)
 tests_lib_test_checksum_SOURCES = tests/lib/test_checksum.c
+tests_lib_test_frrlua_CFLAGS = $(TESTS_CFLAGS)
+tests_lib_test_frrlua_CPPFLAGS = $(TESTS_CPPFLAGS)
+tests_lib_test_frrlua_LDADD = $(ALL_TESTS_LDADD)
+tests_lib_test_frrlua_SOURCES = tests/lib/test_frrlua.c
 tests_lib_test_frrscript_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_frrscript_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_frrscript_LDADD = $(ALL_TESTS_LDADD)