summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/tests/internal/lua.c
blob: 122748fbacdbdb720db050b2ee10c3f6ac4609f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <fluent-bit/flb_lua.h>
#include <lauxlib.h>
#include <lua.h>

#include "flb_tests_internal.h"
#include "mpack/mpack.h"
#include "msgpack/object.h"
#include "msgpack/pack.h"
#include "msgpack/sbuffer.h"

/* Helper lua function which returns a string representation of lua objects.
 * Tables are stringified in key order (lua table iteration order is not deterministic. */
const char lua_stringify_object_helper[] = (""
"function stringify(o)\n"
"   if type(o) == 'table' then\n"
"      local keys = {}\n"
"      for k in pairs(o) do table.insert(keys, k) end\n"
"      table.sort(keys)\n"
"      local s = '{ '\n"
"      for _,k in ipairs(keys) do\n"
"         local v = o[k]\n"
"         s = s .. '['..k..'] = ' .. stringify(v) .. ' '\n"
"      end\n"
"      return s .. '}'\n"
"   else\n"
"      return tostring(o)\n"
"   end\n"
"end\n");

static lua_State *lua_setup(const char *script)
{
    lua_State *ret = luaL_newstate();
    if (!ret) {
        flb_error("[lua] error creating new context");
        return NULL;
    }
    luaL_openlibs(ret);
    luaL_loadstring(ret, lua_stringify_object_helper);
    if (lua_pcall(ret, 0, 0, 0)) {
        flb_error("[lua] error executing stringify helper script");
        lua_close(ret);
        return NULL;
    }
    if (script) {
        luaL_loadstring(ret, script);
        if (lua_pcall(ret, 0, 0, 0)) {
            flb_error("[lua] error executing test script");
            lua_close(ret);
            return NULL;
        }
    }
    return ret;
}

static void check_equals(lua_State *l, const char *expected)
{
    /* push the stringify function on the stack */
    lua_getglobal(l, "stringify");
    /* swap the top two elements of the stack, so that the function is below the arg */ 
    lua_insert(l, -2);
    /* call the function */
    if (lua_pcall(l, 1, 1, 0)) {
        flb_error("[lua] error calling stringify helper function");
        return;
    }
    const char *result = lua_tostring(l, -1);
    TEST_CHECK(strcmp(result, expected) == 0);
    TEST_MSG("Expected: %s", expected);
    TEST_MSG("Actual:   %s", result);
    /* remove the result */
    lua_pop(l, 1);
}

static void test_is_valid_func()
{
    lua_State *l = lua_setup(NULL);
    TEST_CHECK(flb_lua_is_valid_func(l, "invalid_function") == false);
    TEST_CHECK(flb_lua_is_valid_func(l, "stringify") == true);
    lua_close(l);
}

static void test_pushtimetable()
{
    lua_State *l = lua_setup(NULL);
    struct flb_time t = {{ 5, 6 }};
    flb_lua_pushtimetable(l, &t);
    check_equals(l, "{ [nsec] = 6 [sec] = 5 }");
    t.tm.tv_nsec = 7;
    t.tm.tv_sec = 8;
    flb_lua_pushtimetable(l, &t);
    check_equals(l, "{ [nsec] = 7 [sec] = 8 }");
    lua_close(l);
}

static void test_pushmsgpack()
{
    msgpack_packer pck;
    msgpack_sbuffer sbuf;
    msgpack_unpacked msg;
    lua_State *l = lua_setup(NULL);

    msgpack_sbuffer_init(&sbuf);
    msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);
    msgpack_pack_array(&pck, 3);
    msgpack_pack_map(&pck, 1);
    msgpack_pack_str_with_body(&pck, "key", 3);
    msgpack_pack_str_with_body(&pck, "value", 5);
    msgpack_pack_str_with_body(&pck, "msgpack-str", 11);
    msgpack_pack_int(&pck, 4);

    msgpack_unpacked_init(&msg);
    msgpack_unpack_next(&msg, sbuf.data, sbuf.size, NULL);
    flb_lua_pushmsgpack(l, &msg.data);
    check_equals(l, "{ [1] = { [key] = value } [2] = msgpack-str [3] = 4 }");

    msgpack_unpacked_destroy(&msg);
    msgpack_sbuffer_destroy(&sbuf);
    lua_close(l);
}

static void test_pushmpack()
{
    msgpack_packer pck;
    msgpack_sbuffer sbuf;
    mpack_reader_t reader;
    lua_State *l = lua_setup(NULL);

    msgpack_sbuffer_init(&sbuf);
    msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);
    msgpack_pack_array(&pck, 3);
    msgpack_pack_map(&pck, 1);
    msgpack_pack_str_with_body(&pck, "key", 3);
    msgpack_pack_str_with_body(&pck, "value", 5);
    msgpack_pack_str_with_body(&pck, "msgpack-str", 11);
    msgpack_pack_int(&pck, 4);

    mpack_reader_init_data(&reader, sbuf.data, sbuf.size);
    flb_lua_pushmpack(l, &reader);
    check_equals(l, "{ [1] = { [key] = value } [2] = msgpack-str [3] = 4 }");

    msgpack_sbuffer_destroy(&sbuf);
    lua_close(l);
}

static void test_tomsgpack()
{
    const char expected[] = "[{\"key\"=>\"value\"}, \"msgpack-str\", 4]";
    char buf[256];
    msgpack_packer pck;
    msgpack_sbuffer sbuf;
    msgpack_unpacked msg;
    struct flb_lua_l2c_config l2cc;
    lua_State *l = lua_setup("obj = {{['key']='value'},'msgpack-str',4}");

    msgpack_sbuffer_init(&sbuf);
    msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);
    mk_list_init(&l2cc.l2c_types);
    l2cc.l2c_types_num = 0;

    lua_getglobal(l, "obj");
    flb_lua_tomsgpack(l, &pck, 0, &l2cc);

    msgpack_unpacked_init(&msg);
    msgpack_unpack_next(&msg, sbuf.data, sbuf.size, NULL);
    msgpack_object_print_buffer(buf, sizeof(buf), msg.data);

    TEST_CHECK(strcmp(buf, expected) == 0);
    TEST_MSG("Expected: %s", expected);
    TEST_MSG("Actual:   %s", buf);

    msgpack_unpacked_destroy(&msg);
    msgpack_sbuffer_destroy(&sbuf);
    lua_close(l);
}

static void test_tompack()
{
    const char expected[] = "[{\"key\"=>\"value\"}, \"msgpack-str\", 4]";
    char buf[256];
    char printbuf[256];
    mpack_writer_t writer;
    msgpack_unpacked msg;
    struct flb_lua_l2c_config l2cc;
    lua_State *l = lua_setup("obj = {{['key']='value'},'msgpack-str',4}");

    mpack_writer_init(&writer, buf, sizeof(buf));
    mk_list_init(&l2cc.l2c_types);
    l2cc.l2c_types_num = 0;

    lua_getglobal(l, "obj");
    flb_lua_tompack(l, &writer, 0, &l2cc);

    msgpack_unpacked_init(&msg);
    msgpack_unpack_next(&msg, writer.buffer, writer.position - writer.buffer, NULL);
    msgpack_object_print_buffer(printbuf, sizeof(printbuf), msg.data);

    TEST_CHECK(strcmp(printbuf, expected) == 0);
    TEST_MSG("Expected: %s", expected);
    TEST_MSG("Actual:   %s", buf);

    msgpack_unpacked_destroy(&msg);
    lua_close(l);
}


static void test_lua_arraylength()
{
    lua_State *l;
    int i;
    int len;
    int size = 10;

    l = luaL_newstate();
    if (!TEST_CHECK(l != NULL)) {
        TEST_MSG("luaL_newstate faild");
        return;
    }
    luaL_openlibs(l);

    /* create array. */
    lua_createtable(l, size, 0);

    for (i=1; i<=size; i++) {
        lua_pushinteger(l, i); /* set an index of array */
        lua_pushinteger(l, 3+i); /* set a value */
        lua_settable(l, -3); /* points created table */
    }

    len = flb_lua_arraylength(l, -1);
    if (!TEST_CHECK(len == size)) {
        TEST_MSG("size error. got=%d expect=%d", len, size);
    }
    lua_pop(l, 1);

    lua_close(l);
}

static void test_lua_arraylength_with_index()
{
    lua_State *l;
    int i;
    int len;
    int size = 10;

    l = luaL_newstate();
    if (!TEST_CHECK(l != NULL)) {
        TEST_MSG("luaL_newstate faild");
        return;
    }
    luaL_openlibs(l);

    /* create array. */
    lua_createtable(l, size, 0);

    for (i=1; i<=size; i++) {
        lua_pushinteger(l, i); /* set an index of array */
        lua_pushinteger(l, 3+i); /* set a value */
        lua_settable(l, -3); /* points created table */
    }

    /* push 2 values */
    lua_pushinteger(l, 100);
    lua_pushinteger(l, 101);

    len = flb_lua_arraylength(l, -3); /* points array. -1 points 101, -2 points 100 */
    if (!TEST_CHECK(len == size)) {
        TEST_MSG("size error. got=%d expect=%d", len, size);
    }
    lua_pop(l, 1);

    lua_close(l);
}

static void test_lua_arraylength_for_array_contains_nil()
{
    lua_State *l;
    int i;
    int len;
    int size = 10;

    l = luaL_newstate();
    if (!TEST_CHECK(l != NULL)) {
        TEST_MSG("luaL_newstate faild");
        return;
    }
    luaL_openlibs(l);

    /* create array. */
    lua_createtable(l, size, 0);

    for (i=1; i<=size; i++) {
        lua_pushinteger(l, i); /* set an index of array */
        if (i == 7) {
            lua_pushnil(l);
        }
        else {
            lua_pushinteger(l, 3+i); /* set a value */
        }
        lua_settable(l, -3); /* points created table */
    }

    len = flb_lua_arraylength(l, -1);
    if (!TEST_CHECK(len == size)) {
        TEST_MSG("size error. got=%d expect=%d", len, size);
    }
    lua_pop(l, 1);

    lua_close(l);
}


TEST_LIST = {
    { "lua_is_valid_func" , test_is_valid_func},
    { "lua_pushtimetable" , test_pushtimetable},
    { "lua_pushmsgpack" , test_pushmsgpack },
    { "lua_pushmpack" , test_pushmpack },
    { "lua_tomsgpack" , test_tomsgpack },
    { "lua_tompack" , test_tompack },
    { "lua_arraylength" , test_lua_arraylength },
    { "lua_arraylength_with_index" , test_lua_arraylength_with_index },
    { "lua_arraylength_for_array_contains_nil", test_lua_arraylength_for_array_contains_nil},
    { 0 }
};