]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_frrlua.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2021 Donald Lee
10 #include "lib/frrlua.h"
12 static void test_encode_decode(void)
14 lua_State
*L
= luaL_newstate();
19 lua_pushintegerp(L
, &a
);
20 lua_decode_integerp(L
, -1, &a
);
22 assert(lua_gettop(L
) == 0);
25 time_t time_b
= time_a
;
27 lua_pushtimet(L
, &time_a
);
28 lua_decode_timet(L
, -1, &time_a
);
29 assert(time_a
== time_b
);
30 assert(lua_gettop(L
) == 0);
32 char str_b
[] = "Hello", str_a
[6];
34 strlcpy(str_a
, str_b
, sizeof(str_b
));
35 lua_pushstring_wrapper(L
, str_a
);
36 lua_decode_stringp(L
, -1, str_a
);
37 assert(strncmp(str_a
, str_b
, sizeof(str_b
)) == 0);
38 assert(lua_gettop(L
) == 0);
40 char p_b_str
[] = "10.0.0.0/24", p_a_str
[12];
43 strlcpy(p_a_str
, p_b_str
, sizeof(p_b_str
));
44 str2prefix(p_a_str
, &p_a
);
45 lua_pushprefix(L
, &p_a
);
46 lua_decode_prefix(L
, -1, &p_a
);
47 prefix2str(&p_a
, p_a_str
, sizeof(p_b_str
));
48 assert(strncmp(p_a_str
, p_b_str
, sizeof(p_b_str
)) == 0);
49 assert(lua_gettop(L
) == 0);
51 struct interface ifp_a
= {};
52 struct interface ifp_b
= ifp_a
;
54 lua_pushinterface(L
, &ifp_a
);
55 lua_decode_interface(L
, -1, &ifp_a
);
56 assert(strncmp(ifp_a
.name
, ifp_b
.name
, sizeof(ifp_b
.name
)) == 0);
57 assert(ifp_a
.ifindex
== ifp_b
.ifindex
);
58 assert(ifp_a
.status
== ifp_b
.status
);
59 assert(ifp_a
.flags
== ifp_b
.flags
);
60 assert(ifp_a
.metric
== ifp_b
.metric
);
61 assert(ifp_a
.speed
== ifp_b
.speed
);
62 assert(ifp_a
.mtu
== ifp_b
.mtu
);
63 assert(ifp_a
.mtu6
== ifp_b
.mtu6
);
64 assert(ifp_a
.bandwidth
== ifp_b
.bandwidth
);
65 assert(ifp_a
.link_ifindex
== ifp_b
.link_ifindex
);
66 assert(ifp_a
.ll_type
== ifp_b
.ll_type
);
67 assert(lua_gettop(L
) == 0);
69 struct in_addr addr_a
= {};
70 struct in_addr addr_b
= addr_a
;
72 lua_pushinaddr(L
, &addr_a
);
73 lua_decode_inaddr(L
, -1, &addr_a
);
74 assert(addr_a
.s_addr
== addr_b
.s_addr
);
75 assert(lua_gettop(L
) == 0);
77 struct in6_addr in6addr_a
= {};
78 struct in6_addr in6addr_b
= in6addr_a
;
80 lua_pushin6addr(L
, &in6addr_a
);
81 lua_decode_in6addr(L
, -1, &in6addr_a
);
82 assert(in6addr_cmp(&in6addr_a
, &in6addr_b
) == 0);
83 assert(lua_gettop(L
) == 0);
85 union sockunion su_a
, su_b
;
87 memset(&su_a
, 0, sizeof(union sockunion
));
88 memset(&su_b
, 0, sizeof(union sockunion
));
89 lua_pushsockunion(L
, &su_a
);
90 lua_decode_sockunion(L
, -1, &su_a
);
91 assert(sockunion_cmp(&su_a
, &su_b
) == 0);
92 assert(lua_gettop(L
) == 0);
95 int main(int argc
, char **argv
)