Test WIP
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
NADAL Jean-Baptiste
2020-01-22 19:07:30 +01:00
parent 75eb680bd8
commit 09d7cce817
9 changed files with 166 additions and 72 deletions

View File

@@ -3,6 +3,7 @@ cmake_minimum_required (VERSION 3.0)
project (librestd) project (librestd)
add_definitions (-g -DBUILD_DEBUG) add_definitions (-g -DBUILD_DEBUG)
add_definitions("-fprofile-arcs -ftest-coverage")
include_directories (${CMAKE_SOURCE_DIR}/../libevent/include) include_directories (${CMAKE_SOURCE_DIR}/../libevent/include)
include_directories (${CMAKE_BINARY_DIR}/libevent/include) include_directories (${CMAKE_BINARY_DIR}/libevent/include)

View File

@@ -681,15 +681,18 @@ static int parse_requestline(restd_http_t *http, char *line)
char *uri = strtok_r(NULL, " ", &saveptr); char *uri = strtok_r(NULL, " ", &saveptr);
char *httpver = strtok_r(NULL, " ", &saveptr); char *httpver = strtok_r(NULL, " ", &saveptr);
char *tmp = strtok_r(NULL, " ", &saveptr); char *tmp = strtok_r(NULL, " ", &saveptr);
printf("=============parse_requestline\n");
if (method == NULL || uri == NULL || httpver == NULL || tmp != NULL) if (method == NULL || uri == NULL || httpver == NULL || tmp != NULL)
{ {
DEBUG("Invalid request line. %s", line); DEBUG("Invalid request line. %s", line);
printf("=============pInvalid request line. %s\n", line);
return RESTD_HTTP_ERROR; return RESTD_HTTP_ERROR;
} }
// Set request method // Set request method
http->request.method = qstrupper(strdup(method)); http->request.method = qstrupper(strdup(method));
printf("============= SAVE METHOD HTTP: %s\n", http->request.method);
// Set HTTP version // Set HTTP version
http->request.httpver = qstrupper(strdup(httpver)); http->request.httpver = qstrupper(strdup(httpver));

View File

@@ -240,11 +240,15 @@ int restd_rest_handler(short event, restd_conn_t *conn)
conn->id = atoi(buffer); conn->id = atoi(buffer);
return hook->cb(event, conn, hook->userdata); return hook->cb(event, conn, hook->userdata);
} }
else if (strcmp(hook->path, http->request.path) == 0) else
{
int rett = strcmp(hook->path, http->request.path);
if (rett == 0)
return hook->cb(event, conn, hook->userdata); return hook->cb(event, conn, hook->userdata);
} }
} }
} }
}
// No Hook Found check if it's a real file into document root. // No Hook Found check if it's a real file into document root.
root_path = restd_server_get_option(conn->server, "server.root_path"); root_path = restd_server_get_option(conn->server, "server.root_path");
if ((root_path != NULL) && (strlen(root_path) != 0)) if ((root_path != NULL) && (strlen(root_path) != 0))
@@ -270,7 +274,9 @@ int restd_rest_handler(short event, restd_conn_t *conn)
} }
} }
if (conn->server->error_handler != NULL)
return conn->server->error_handler(reason, conn, NULL); return conn->server->error_handler(reason, conn, NULL);
return RESTD_CLOSE;
} }
BUG_EXIT(); BUG_EXIT();

View File

@@ -23,9 +23,6 @@
* *
*/ */
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
void device_create_set_state_free(const char *capability, const char *test_name_true, const char *test_name_false) void device_create_set_state_free(const char *capability, const char *test_name_true, const char *test_name_false)

View File

@@ -23,9 +23,6 @@
* *
*/ */
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#include <fcntl.h> #include <fcntl.h>
#include <qlibc/qlibc.h> #include <qlibc/qlibc.h>

View File

@@ -23,9 +23,6 @@
* *
*/ */
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#define kOutletTestNameTrue "{ \"id\": 1, \"name\": \"test_name\", \"state\": true, \"zone\": \"\", \"sender\": 0, \"switch\": 0 }" #define kOutletTestNameTrue "{ \"id\": 1, \"name\": \"test_name\", \"state\": true, \"zone\": \"\", \"sender\": 0, \"switch\": 0 }"
#define kOutletTestNameFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false, \"zone\": \"\", \"sender\": 0, \"switch\": 0 }" #define kOutletTestNameFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false, \"zone\": \"\", \"sender\": 0, \"switch\": 0 }"
#define kOutletTestNameFullFalse "{ \"id\": 1, \"name\": \"test_name\", \"speech_name\": \"speech_name\", \"state\": false, \"zone\": \"zone\", \"sender\": 123456, \"switch\": 77 }" #define kOutletTestNameFullFalse "{ \"id\": 1, \"name\": \"test_name\", \"speech_name\": \"speech_name\", \"state\": false, \"zone\": \"zone\", \"sender\": 123456, \"switch\": 77 }"

View File

@@ -23,11 +23,18 @@
* *
*/ */
// This is an independent project of an individual developer. Dear PVS-Studio, please check it. /*--------------------------------------------------------------------------*/
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#define ksuccesGet_body "{\"status\":\"ok\"}"
#define ksuccesDelete_body "{\"status\":\"delete\"}"
#define ksuccess_body "{\"status\":\"ok\"}" #define kServerPort "7777"
#define kApiTestGet "/api/v1/test_get"
#define kPostMethod "POST"
#define kGetMethod "GET"
#define kPutMethod "PUT"
#define kDeleteMethod "DELETE"
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -39,11 +46,11 @@ int my_error_handler(short event, restd_conn_t *conn, void *userdata)
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
int my_success_handler(short event, restd_conn_t *conn, void *userdata) int my_success_http_handler(short event, restd_conn_t *conn, void *userdata)
{ {
if (event & RESTD_EVENT_READ) if (event & RESTD_EVENT_READ)
{ {
restd_http_response(conn, 200, "application/json", ksuccess_body, 15); restd_http_response(conn, 200, "application/json", ksuccesGet_body, strlen(ksuccesGet_body));
return RESTD_CLOSE; // Close connection. return RESTD_CLOSE; // Close connection.
} }
return RESTD_OK; return RESTD_OK;
@@ -51,6 +58,22 @@ int my_success_handler(short event, restd_conn_t *conn, void *userdata)
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
int my_success_rest_handler(short event, restd_conn_t *conn, void *userdata)
{
restd_http_response(conn, 200, "application/json", ksuccesGet_body, strlen(ksuccesDelete_body));
return RESTD_CLOSE; // Close connection.
}
/*--------------------------------------------------------------------------*/
int my_success_rest_delete_handler(short event, restd_conn_t *conn, void *userdata)
{
restd_http_response(conn, 200, "application/json", ksuccesDelete_body, strlen(ksuccesDelete_body));
return RESTD_CLOSE; // Close connection.
}
/*--------------------------------------------------------------------------*/
size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream) size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{ {
size_t body_size = 0; size_t body_size = 0;
@@ -81,6 +104,76 @@ size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
int exec_request(const char *request, const char *path, int expected_code, const char *expected_body)
{
int ret;
CURL *curl_handle;
CURLcode res;
char *body = NULL;
long http_result_code;
printf ("\n\nexec_request: %s\n", request);
/* init libcurl */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL and method */
curl_easy_setopt(curl_handle, CURLOPT_URL, path);
if (strcmp(request, kGetMethod) == 0)
{
printf("method GET\n");
curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);
}
else if (strcmp(request, kDeleteMethod) == 0)
{
printf("method Delete\n");
curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 0);
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, kDeleteMethod);
}
else if (strcmp(request, kPutMethod) == 0)
{
printf("method PUT\n");
curl_easy_setopt(curl_handle, CURLOPT_PUT, 0);
}
else if (strcmp(request, kPostMethod) == 0)
{
printf("method POST\n");
curl_easy_setopt(curl_handle, CURLOPT_POST, 0);
}
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &body);
curl_easy_setopt(curl_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
/* get it! */
res = curl_easy_perform(curl_handle);
ASSERT_EQUAL_INT(res, CURLE_OK);
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_result_code);
ASSERT_EQUAL_INT(http_result_code, expected_code);
res = strcmp(expected_body, body);
ASSERT_EQUAL_INT(res, 0);
free(body);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return 0;
}
/*--------------------------------------------------------------------------*/
bool found_route(restd_server_t *server, const char *method, const char *path) bool found_route(restd_server_t *server, const char *method, const char *path)
{ {
qlist_t *hooks = server->hooks; qlist_t *hooks = server->hooks;
@@ -194,20 +287,20 @@ TEST("Rest - create access regular route free\t")
restd_server_set_option(rest_server, "server.port", "7777"); restd_server_set_option(rest_server, "server.port", "7777");
restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_success_http_handler, NULL);
ASSERT_TRUE(found_route(rest_server, "POST", "/api/v1/test")); ASSERT_TRUE(found_route(rest_server, "POST", "/api/v1/test"));
ASSERT_FALSE(found_route(rest_server, "POST", "/api/v1/notfound")); ASSERT_FALSE(found_route(rest_server, "POST", "/api/v1/notfound"));
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_http_handler, NULL);
ASSERT_TRUE(found_route(rest_server, "GET", "/api/v1/test")); ASSERT_TRUE(found_route(rest_server, "GET", "/api/v1/test"));
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_http_handler, NULL);
ASSERT_TRUE(found_route(rest_server, "GET", "/api/v1/test")); ASSERT_TRUE(found_route(rest_server, "GET", "/api/v1/test"));
restd_server_register_hook_on_path(rest_server, "PUT", "/api/v1/test", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "PUT", "/api/v1/test", my_success_http_handler, NULL);
ASSERT_TRUE(found_route(rest_server, "PUT", "/api/v1/test")); ASSERT_TRUE(found_route(rest_server, "PUT", "/api/v1/test"));
restd_server_register_hook_on_path(rest_server, "DELETE", "/api/v1/test", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "DELETE", "/api/v1/test", my_success_http_handler, NULL);
ASSERT_TRUE(found_route(rest_server, "DELETE", "/api/v1/test")); ASSERT_TRUE(found_route(rest_server, "DELETE", "/api/v1/test"));
restd_server_free(rest_server); restd_server_free(rest_server);
@@ -222,9 +315,9 @@ TEST("Rest - create access route with param free\t")
rest_server = restd_server_new(); rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server); ASSERT_NOT_NULL(rest_server);
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/klong/:id", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/klong/:id", my_success_http_handler, NULL);
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/donkey", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/donkey", my_success_http_handler, NULL);
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_success_http_handler, NULL);
ASSERT_FALSE(found_special_route(rest_server, "GET", "/api/v1/notfound/77", &id)); ASSERT_FALSE(found_special_route(rest_server, "GET", "/api/v1/notfound/77", &id));
@@ -247,7 +340,7 @@ TEST("Rest - create start free\t")
restd_server_set_option(rest_server, "server.port", "7777"); restd_server_set_option(rest_server, "server.port", "7777");
restd_server_set_option(rest_server, "server.thread", "1"); restd_server_set_option(rest_server, "server.thread", "1");
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_success_http_handler, NULL);
ret = restd_server_start(rest_server); ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0); ASSERT_EQUAL_INT(ret, 0);
@@ -257,58 +350,65 @@ TEST("Rest - create start free\t")
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
TEST("Rest - create start access route free\t") TEST("Rest - create start access http hook free\t")
{ {
restd_server_t *rest_server;
int ret; int ret;
CURL *curl_handle; restd_server_t *rest_server;
CURLcode res;
char *body = NULL;
long http_result_code;
rest_server = restd_server_new(); rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server); ASSERT_NOT_NULL(rest_server);
restd_server_set_option(rest_server, "server.port", "7777"); restd_server_set_option(rest_server, "server.port", kServerPort);
restd_server_set_option(rest_server, "server.thread", "1"); restd_server_set_option(rest_server, "server.thread", "1");
restd_server_register_hook(rest_server, restd_http_handler, NULL); restd_server_register_hook(rest_server, restd_http_handler, NULL);
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/", my_success_handler, NULL); restd_server_register_hook_on_path(rest_server, "GET", kApiTestGet, my_success_http_handler, NULL);
ret = restd_server_start(rest_server); ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0); ASSERT_EQUAL_INT(ret, 0);
/* init libcurl */ ret = exec_request(kGetMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccesGet_body);
curl_global_init(CURL_GLOBAL_ALL); ASSERT_EQUAL_INT(ret, 0);
/* init the curl session */ restd_server_free(rest_server);
curl_handle = curl_easy_init(); }
/* specify URL to get */ /*--------------------------------------------------------------------------*/
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://localhost:7777/api/v1/test/");
TEST("Rest - create start access rest hook free\t")
/* send all data to this function */ {
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback); int ret;
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &body); restd_server_t *rest_server;
curl_easy_setopt(curl_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
rest_server = restd_server_new();
/* get it! */ ASSERT_NOT_NULL(rest_server);
res = curl_easy_perform(curl_handle);
ASSERT_EQUAL_INT(res, CURLE_OK); restd_log_level(RESTD_LOG_DEBUG);
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_result_code); restd_server_set_option(rest_server, "server.port", kServerPort);
ASSERT_EQUAL_INT(http_result_code, 200); restd_server_set_option(rest_server, "server.thread", "1");
res = strcmp(ksuccess_body, body); restd_server_register_call_hooks_handler(rest_server, restd_rest_handler);
ASSERT_EQUAL_INT(res, 0);
restd_server_register_hook_on_path(rest_server, "GET", kApiTestGet, my_success_rest_handler, NULL);
/* cleanup curl stuff */ //restd_server_register_hook_on_path(rest_server, "PUT", kApiTestGet, my_success_rest_handler, NULL);
curl_easy_cleanup(curl_handle); restd_server_register_hook_on_path(rest_server, "DELETE", kApiTestGet, my_success_rest_delete_handler, NULL);
free(body); ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
ret = exec_request(kGetMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccesGet_body);
ASSERT_EQUAL_INT(ret, 0);
printf("======================\n");
ret = exec_request(kDeleteMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccesDelete_body);
ASSERT_EQUAL_INT(ret, 0);
// ret = exec_request(kPutMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccess_body);
// ASSERT_EQUAL_INT(ret, 0);
restd_server_free(rest_server); restd_server_free(rest_server);
} }

View File

@@ -23,9 +23,6 @@
* *
*/ */
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#define kShutterTestNameTrue "{ \"id\": 1, \"name\": \"test_name\", \"state\": true, \"zone\": \"\", \"sender\": 0, \"switch\": 0, \"speed_up\": 0, \"speed_down\": 0 }" #define kShutterTestNameTrue "{ \"id\": 1, \"name\": \"test_name\", \"state\": true, \"zone\": \"\", \"sender\": 0, \"switch\": 0, \"speed_up\": 0, \"speed_down\": 0 }"
#define kShutterTestNameFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false, \"zone\": \"\", \"sender\": 0, \"switch\": 0, \"speed_up\": 0, \"speed_down\": 0 }" #define kShutterTestNameFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false, \"zone\": \"\", \"sender\": 0, \"switch\": 0, \"speed_up\": 0, \"speed_down\": 0 }"
#define kShutterTestNameFullFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false, \"zone\": \"zone\", \"sender\": 123456, \"switch\": 77, \"speed_up\": 50, \"speed_down\": 60 }" #define kShutterTestNameFullFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false, \"zone\": \"zone\", \"sender\": 123456, \"switch\": 77, \"speed_up\": 50, \"speed_down\": 60 }"

View File

@@ -23,10 +23,6 @@
* *
*/ */
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#define kSprinklerTestNameTrue "{ \"id\": 1, \"name\": \"test_name\", \"state\": true }" #define kSprinklerTestNameTrue "{ \"id\": 1, \"name\": \"test_name\", \"state\": true }"
#define kSprinklerTestNameFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false }" #define kSprinklerTestNameFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false }"
#define kSprinklerTestNameFull "[ { \"id\": 1, \"name\": \"test1\", \"state\": false }, { \"id\": 2, \"name\": \"test2\", \"state\": false }, { \"id\": 3, \"name\": \"test3\", \"state\": false } ]" #define kSprinklerTestNameFull "[ { \"id\": 1, \"name\": \"test1\", \"state\": false }, { \"id\": 2, \"name\": \"test2\", \"state\": false }, { \"id\": 3, \"name\": \"test3\", \"state\": false } ]"