Add unit tests and correct bugs detected by the tests

This commit is contained in:
DanyLE
2023-01-25 17:01:01 +01:00
parent d4bb12c6b5
commit 7711526a7c
29 changed files with 1814 additions and 1024 deletions

View File

@ -208,7 +208,7 @@ static int l_base64_decode(lua_State *L)
// decode data to a byte array
lua_new_slice(L, len);
slice_t *vec = NULL;
vec = lua_check_slice(L, 2);
vec = lua_check_slice(L, -1);
len = Base64decode((char *)vec->data, s);
vec->len = len;
// lua_pushstring(L,dst);

View File

@ -14,15 +14,43 @@
#define SLICE "slice"
typedef struct {
size_t magic;
size_t len;
uint8_t* data;
} slice_t;
#ifndef LUA_SLICE_MAGIC
/**
* @brief Send data to the server via fastCGI protocol
* This function is defined by the luad fcgi server
*
* @param fd the socket fd
* @param id the request id
* @param ptr data pointer
* @param size data size
* @return int
*/
int fcgi_send_slice(int fd, uint16_t id, uint8_t* ptr, size_t size);
/**
* @brief Get the magic number of the slice
* This value is defined by the luad fastCGI server
*
* @return size_t
*/
size_t lua_slice_magic();
#else
#define lua_slice_magic() (LUA_SLICE_MAGIC)
#define fcgi_send_slice(fd,id,ptr,size) (-1)
#endif
void lua_new_slice(lua_State*L, int n)
{
size_t nbytes = sizeof(slice_t) + n * 1U;
slice_t *a = (slice_t *)lua_newuserdata(L, nbytes);
a->data = &((char *)a)[sizeof(slice_t)];
a->magic = lua_slice_magic();
luaL_getmetatable(L, SLICE);
lua_setmetatable(L, -2);

View File

@ -1,4 +1,5 @@
#include "lua/lualib.h"
static int l_slice_send_to(lua_State* L);
void lua_new_light_slice(lua_State *L, int n, char *ptr)
{
@ -6,6 +7,7 @@ void lua_new_light_slice(lua_State *L, int n, char *ptr)
slice_t *a = (slice_t *)lua_newuserdata(L, nbytes);
a->len = n;
a->data = ptr;
a->magic = lua_slice_magic();
luaL_getmetatable(L, SLICE);
lua_setmetatable(L, -2);
}
@ -78,6 +80,14 @@ static int l_slice_write(lua_State *L)
return 1;
}
static int l_slice_ptr(lua_State *L)
{
slice_t *a = lua_check_slice(L, 1);
lua_pushnumber(L, (size_t)a);
return 1;
}
static int l_slice_index(lua_State *L)
{
if(lua_isnumber(L,2))
@ -91,10 +101,18 @@ static int l_slice_index(lua_State *L)
{
lua_pushcfunction(L, l_get_slice_size);
}
else if(strcmp(string, "write") == 0)
else if(strcmp(string, "fileout") == 0)
{
lua_pushcfunction(L, l_slice_write);
}
else if(strcmp(string,"out") == 0)
{
lua_pushcfunction(L, l_slice_send_to);
}
else if(strcmp(string,"ptr") == 0)
{
lua_pushcfunction(L, l_slice_ptr);
}
else
{
lua_pushnil(L);
@ -120,6 +138,16 @@ static int l_slice_to_string(lua_State *L)
return 1;
}
static int l_slice_send_to(lua_State* L)
{
slice_t *a = lua_check_slice(L, 1);
int fd = (int) luaL_checknumber(L, 2);
uint16_t id = (uint16_t) luaL_checknumber(L, 3);
lua_pushboolean(L, fcgi_send_slice(fd, id, a->data, a->len) == 0);
return 1;
}
static const struct luaL_Reg slicemetalib[] = {
{"unew", l_new_lightslice},
{"new", l_new_slice},

View File

@ -83,6 +83,9 @@ static int l_db_query(lua_State *L)
int cols = sqlite3_column_count(statement);
int result = 0;
int cnt = 1;
uint8_t* data = NULL;
size_t len = 0;
slice_t* vec = NULL;
// new table for data
lua_newtable(L);
while ((result = sqlite3_step(statement)) == SQLITE_ROW)
@ -91,10 +94,37 @@ static int l_db_query(lua_State *L)
lua_newtable(L);
for (int col = 0; col < cols; col++)
{
const char *value = (const char *)sqlite3_column_text(statement, col);
const char *name = sqlite3_column_name(statement, col);
lua_pushstring(L, name);
lua_pushstring(L, value);
int type = sqlite3_column_type(statement,col);
switch (type)
{
case SQLITE_INTEGER:
lua_pushnumber(L, sqlite3_column_int64(statement,col));
break;
case SQLITE_FLOAT:
lua_pushnumber(L, sqlite3_column_double(statement,col));
break;
case SQLITE_BLOB:
data = (uint8_t*)sqlite3_column_blob(statement, col);
len = sqlite3_column_bytes(statement, col);
if(len > 0)
{
lua_new_slice(L, len);
vec = lua_check_slice(L, -1);
(void)memcpy(vec->data, data, len);
}
else
{
lua_pushnil(L);
}
break;
default:
lua_pushstring(L, (const char *)sqlite3_column_text(statement, col));
break;
}
lua_settable(L, -3);
}
lua_settable(L, -3);