1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-24 22:13:52 +01:00

tests/lua_loader: test the RIOT-customized lua loader.

For the RIOT port of Lua, the module loader has been more or less
rewritten to allow for easily integrating source modules defined in static
arrays and C modules embedded in the application.

So  far the loader had not been tested (and a bug was found). This test
should give a bit more certainty that the RIOT integration works as it
should.
This commit is contained in:
Juan Carrano 2019-05-29 14:56:33 +02:00
parent 56eb7d9d92
commit 913f74b90b
5 changed files with 220 additions and 0 deletions

16
tests/lua_loader/Makefile Normal file
View File

@ -0,0 +1,16 @@
include ../Makefile.tests_common
USEPKG += lua
BOARD_WHITELIST += native samr21-xpro
ifneq ($(BOARD),native)
# This stack size is large enough to run Lua print() functions of
# various lengths. Other functions untested.
CFLAGS += -DTHREAD_STACKSIZE_MAIN='(THREAD_STACKSIZE_DEFAULT+2048)'
endif
include $(RIOTBASE)/Makefile.include
test:
tests/01-run.py

8
tests/lua_loader/README Normal file
View File

@ -0,0 +1,8 @@
Test modified LUA module loader
===============================
This test defines a bunch of dummy modules, both in
pure lua and in C. The way modules are declares is not "nice"
in the sense that it is not the way it would be done in a big
application. The goal is to test the module loader code, and not
the build tooling.

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2019 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
* @brief Define a couple of dummy lua extension modules
*
* @author Juan Carrano <j.carrano@fu-berlin.de>
*
* Normally one would not define more than one module in a single file, but
* these modules are exacly the same: a single table with an integer value.
*
* @}
*/
#define LUA_LIB
#include "lprefix.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static const luaL_Reg funcs[] = {
/* placeholder */
{ "X", NULL },
{ NULL, NULL }
};
static int _luaopen_X(lua_State *L, const char *xstring)
{
luaL_newlib(L, funcs);
lua_pushstring(L, xstring);
lua_setfield(L, -2, "X");
return 1;
}
int _luaopen_hello(lua_State *L)
{
return _luaopen_X(L, "E se deixa no céu,");
}
int _luaopen_world(lua_State *L)
{
return _luaopen_X(L, "como esquecida");
}

105
tests/lua_loader/main.c Normal file
View File

@ -0,0 +1,105 @@
/*
* Copyright (C) 2019 Freie Universität Berlin.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
* @brief Test custom lua loader ("require" module)
*
* @author Juan Carrano <j.carrano@fu-berlin.de>
*
* This application defines some pure-lua and some C-extension modules.
* First it tests running a module as a script (lua_riot_do_module).
*
* Then it goes into a loop reading single lines of input from stdin and
* executing them as lua source. The test script can use that to test module
* loading.
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "lua_run.h"
#include "lua_builtin.h"
static const uint8_t pure_module_1[] = "\n\
return {a='Quando uma lua'}\n\
";
static const uint8_t pure_module_2[] = "\n\
return {a='chega de repente'}\n\
";
static const uint8_t test_script[] = "\n\
print'I am a module, hi!'\n\
";
/* The -1 is because the final '\0' is not part of the source code that should
* be read by lua.
*/
const struct lua_riot_builtin_lua _lua_riot_builtin_lua_table[] = {
{ "m1", pure_module_1, sizeof(pure_module_1) - 1 },
{ "m2", pure_module_2, sizeof(pure_module_2) - 1 },
{ "test", test_script, sizeof(test_script) - 1 }
};
extern int _luaopen_hello(lua_State *L);
extern int _luaopen_world(lua_State *L);
const struct lua_riot_builtin_c _lua_riot_builtin_c_table[] = {
{ "c1", _luaopen_hello },
{ "c2", _luaopen_world }
};
const struct lua_riot_builtin_lua *const lua_riot_builtin_lua_table = _lua_riot_builtin_lua_table;
const struct lua_riot_builtin_c *const lua_riot_builtin_c_table = _lua_riot_builtin_c_table;
const size_t lua_riot_builtin_lua_table_len =
sizeof(_lua_riot_builtin_lua_table) / sizeof(*_lua_riot_builtin_lua_table);
const size_t lua_riot_builtin_c_table_len =
sizeof(_lua_riot_builtin_c_table) / sizeof(*_lua_riot_builtin_c_table);
#define LUA_MEM_SIZE (11000)
static char lua_mem[LUA_MEM_SIZE] __attribute__ ((aligned(__BIGGEST_ALIGNMENT__)));
#define LINEBUF_SZ (32)
static char linebuf[LINEBUF_SZ];
int main(void)
{
int status;
status = lua_riot_do_module("test", lua_mem, LUA_MEM_SIZE,
LUAR_LOAD_BASE, NULL);
assert(status == LUAR_EXIT);
while (fgets(linebuf, LINEBUF_SZ, stdin) != NULL) {
int status;
size_t linelen = strlen(linebuf);
if (!linelen) {
continue;
}
else if (linebuf[linelen - 1] != '\n') {
puts("[ERROR] could not read a complete line");
break;
}
status = lua_riot_do_buffer((unsigned char *)linebuf, linelen,
lua_mem, LUA_MEM_SIZE,
LUAR_LOAD_BASE | LUAR_LOAD_PACKAGE, NULL);
assert(status == LUAR_EXIT);
}
return 0;
}

View File

@ -0,0 +1,36 @@
#!/usr/bin/env python3
# Copyright (C) 2019 Freie Universität Berlin
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
# Tell the lua interpreter running in riot to load some modules and print
# the value of a variable inside that module.
import os
import sys
MODULE_QUERIES = [
("m1", "a", "Quando uma lua"),
("m2", "a", "chega de repente"),
("c1", "X", "E se deixa no céu,"),
("c2", "X", "como esquecida"),
]
def test(child):
# check startup message
child.expect_exact('I am a module, hi!')
# loop other defined commands and expected output
for mod, attr, val in MODULE_QUERIES:
child.sendline('print((require"{}").{})'.format(mod, attr))
child.expect_exact(val)
if __name__ == "__main__":
sys.path.append(os.path.join(os.environ['RIOTTOOLS'], 'testrunner'))
from testrunner import run
sys.exit(run(test))