]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_frrlua.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
23 #include "lib/frrlua.h"
25 static void test_encode_decode(void)
27 lua_State
*L
= luaL_newstate();
32 lua_pushintegerp(L
, &a
);
33 lua_decode_integerp(L
, -1, &a
);
35 assert(lua_gettop(L
) == 0);
38 time_t time_b
= time_a
;
40 lua_pushtimet(L
, &time_a
);
41 lua_decode_timet(L
, -1, &time_a
);
42 assert(time_a
== time_b
);
43 assert(lua_gettop(L
) == 0);
45 char str_b
[] = "Hello", str_a
[6];
47 strlcpy(str_a
, str_b
, sizeof(str_b
));
48 lua_pushstring_wrapper(L
, str_a
);
49 lua_decode_stringp(L
, -1, str_a
);
50 assert(strncmp(str_a
, str_b
, sizeof(str_b
)) == 0);
51 assert(lua_gettop(L
) == 0);
53 char p_b_str
[] = "10.0.0.0/24", p_a_str
[12];
56 strlcpy(p_a_str
, p_b_str
, sizeof(p_b_str
));
57 str2prefix(p_a_str
, &p_a
);
58 lua_pushprefix(L
, &p_a
);
59 lua_decode_prefix(L
, -1, &p_a
);
60 prefix2str(&p_a
, p_a_str
, sizeof(p_b_str
));
61 assert(strncmp(p_a_str
, p_b_str
, sizeof(p_b_str
)) == 0);
62 assert(lua_gettop(L
) == 0);
64 struct interface ifp_a
;
65 struct interface ifp_b
= ifp_a
;
67 lua_pushinterface(L
, &ifp_a
);
68 lua_decode_interface(L
, -1, &ifp_a
);
69 assert(strncmp(ifp_a
.name
, ifp_b
.name
, sizeof(ifp_b
.name
)) == 0);
70 assert(ifp_a
.ifindex
== ifp_b
.ifindex
);
71 assert(ifp_a
.status
== ifp_b
.status
);
72 assert(ifp_a
.flags
== ifp_b
.flags
);
73 assert(ifp_a
.metric
== ifp_b
.metric
);
74 assert(ifp_a
.speed
== ifp_b
.speed
);
75 assert(ifp_a
.mtu
== ifp_b
.mtu
);
76 assert(ifp_a
.mtu6
== ifp_b
.mtu6
);
77 assert(ifp_a
.bandwidth
== ifp_b
.bandwidth
);
78 assert(ifp_a
.link_ifindex
== ifp_b
.link_ifindex
);
79 assert(ifp_a
.ll_type
== ifp_b
.ll_type
);
80 assert(lua_gettop(L
) == 0);
82 struct in_addr addr_a
;
83 struct in_addr addr_b
= addr_a
;
85 lua_pushinaddr(L
, &addr_a
);
86 lua_decode_inaddr(L
, -1, &addr_a
);
87 assert(addr_a
.s_addr
== addr_b
.s_addr
);
88 assert(lua_gettop(L
) == 0);
90 struct in6_addr in6addr_a
;
91 struct in6_addr in6addr_b
= in6addr_a
;
93 lua_pushin6addr(L
, &in6addr_a
);
94 lua_decode_in6addr(L
, -1, &in6addr_a
);
95 assert(in6addr_cmp(&in6addr_a
, &in6addr_b
) == 0);
96 assert(lua_gettop(L
) == 0);
98 union sockunion su_a
, su_b
;
100 memset(&su_a
, 0, sizeof(union sockunion
));
101 memset(&su_b
, 0, sizeof(union sockunion
));
102 lua_pushsockunion(L
, &su_a
);
103 lua_decode_sockunion(L
, -1, &su_a
);
104 assert(sockunion_cmp(&su_a
, &su_b
) == 0);
105 assert(lua_gettop(L
) == 0);
108 int main(int argc
, char **argv
)
110 test_encode_decode();