-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Add unit test for lua encoders/decoders
Signed-off-by: Donald Lee <dlqs@gmx.com>
- Loading branch information
Donald Lee
committed
Jun 22, 2021
1 parent
62510c0
commit e06feaf
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters