Skip to content

Commit

Permalink
tests: Add unit test for lua encoders/decoders
Browse files Browse the repository at this point in the history
Signed-off-by: Donald Lee <dlqs@gmx.com>
  • Loading branch information
Donald Lee committed Jun 22, 2021
1 parent 62510c0 commit e06feaf
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
111 changes: 111 additions & 0 deletions tests/lib/test_frrlua.c
Original file line number Diff line number Diff line change
@@ -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();
}
8 changes: 8 additions & 0 deletions tests/lib/test_frrlua.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import frrtest


class TestFrrlua(frrtest.TestMultiOut):
program = "./test_frrlua"


TestFrrlua.exit_cleanly()
5 changes: 5 additions & 0 deletions tests/subdir.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e06feaf

Please sign in to comment.