feat: cJSON, Gerätenamen und HTTP-Parser

This commit is contained in:
Michael Sauer
2026-09-11 22:18:14 +02:00
parent 0648b3a252
commit 4a23df1b8d
12 changed files with 3829 additions and 2 deletions
+7
View File
@@ -11,6 +11,11 @@ FetchContent_Declare(unity
FetchContent_MakeAvailable(unity)
enable_testing()
# cJSON is third-party code: build it without -Werror.
add_library(nl_cjson STATIC ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/cJSON/cJSON.c)
target_include_directories(nl_cjson PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/cJSON)
set(NL_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../src)
function(nl_test name)
@@ -35,3 +40,5 @@ nl_test(test_state_log test_state_log.c fake_flash.c ${NL_SRC}/config/nl_state_l
${NL_SRC}/light/nl_boot_state.c ${NL_SRC}/util/nl_crc.c)
nl_test(test_reset_counter test_reset_counter.c fake_flash.c ${NL_SRC}/config/nl_reset_counter.c)
nl_test(test_console_cmd test_console_cmd.c ${NL_SRC}/sys/nl_console_cmd.c)
nl_test(test_names test_names.c ${NL_SRC}/net/nl_names.c)
nl_test(test_http test_http.c ${NL_SRC}/net/nl_http.c)
+118
View File
@@ -0,0 +1,118 @@
#include <string.h>
#include "unity.h"
#include "net/nl_http.h"
void setUp(void) {}
void tearDown(void) {}
static nl_http_parse_t parse(const char *s, nl_http_request_t *r)
{
return nl_http_parse(s, strlen(s), r);
}
static void test_simple_get(void)
{
nl_http_request_t r;
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE, parse("GET /api/v1/device/state HTTP/1.1\r\nHost: x\r\n\r\n", &r));
TEST_ASSERT_EQUAL(NL_HTTP_GET, r.method);
TEST_ASSERT_EQUAL_STRING("/api/v1/device/state", r.path);
TEST_ASSERT_EQUAL_STRING("", r.auth);
TEST_ASSERT_EQUAL_UINT(0, r.body_len);
}
static void test_post_body_arrives_in_parts(void)
{
const char *full = "POST /api/v1/device/state HTTP/1.1\r\nContent-Length: 11\r\n\r\n{\"on\":true}";
nl_http_request_t r;
TEST_ASSERT_EQUAL(NL_HTTP_INCOMPLETE, nl_http_parse(full, 20, &r));
TEST_ASSERT_EQUAL(NL_HTTP_INCOMPLETE, nl_http_parse(full, strlen(full) - 1, &r));
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE, nl_http_parse(full, strlen(full), &r));
TEST_ASSERT_EQUAL(NL_HTTP_POST, r.method);
TEST_ASSERT_EQUAL_UINT(11, r.body_len);
TEST_ASSERT_EQUAL_MEMORY("{\"on\":true}", r.body, 11);
}
static void test_headers_case_insensitive_and_bearer(void)
{
nl_http_request_t r;
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE,
parse("PUT /api/v1/system/command HTTP/1.1\r\nauthorization: Bearer abc123\r\n"
"CONTENT-LENGTH: 2\r\n\r\n{}", &r));
TEST_ASSERT_EQUAL(NL_HTTP_PUT, r.method);
TEST_ASSERT_EQUAL_STRING("abc123", r.auth);
TEST_ASSERT_EQUAL_UINT(2, r.body_len);
}
static void test_plain_token_and_query_string(void)
{
nl_http_request_t r;
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE,
parse("GET /api/v1/system/info?x=1 HTTP/1.0\r\nAuthorization: aabbccddeeff0011223344\r\n\r\n", &r));
TEST_ASSERT_EQUAL_STRING("/api/v1/system/info", r.path);
TEST_ASSERT_EQUAL_STRING("aabbccddeeff0011223344", r.auth);
}
static void test_other_method(void)
{
nl_http_request_t r;
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE, parse("DELETE /api/v1/device/state HTTP/1.1\r\n\r\n", &r));
TEST_ASSERT_EQUAL(NL_HTTP_OTHER, r.method);
}
static void test_body_too_large(void)
{
nl_http_request_t r;
TEST_ASSERT_EQUAL(NL_HTTP_TOO_LARGE, parse("POST / HTTP/1.1\r\nContent-Length: 1025\r\n\r\n", &r));
}
static void test_headers_never_end(void)
{
static char buf[NL_HTTP_MAX_REQUEST];
nl_http_request_t r;
memset(buf, 'a', sizeof buf);
memcpy(buf, "GET / HTTP/1.1\r\n", 16);
TEST_ASSERT_EQUAL(NL_HTTP_TOO_LARGE, nl_http_parse(buf, sizeof buf, &r));
TEST_ASSERT_EQUAL(NL_HTTP_INCOMPLETE, nl_http_parse(buf, 100, &r));
}
static void test_bad_requests(void)
{
nl_http_request_t r;
char longpath[200];
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse("GET /x\r\n\r\n", &r));
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse("GET /x FTP/1.1\r\n\r\n", &r));
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse("\r\n\r\n", &r));
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse("POST / HTTP/1.1\r\nContent-Length: abc\r\n\r\n", &r));
strcpy(longpath, "GET /");
memset(longpath + 5, 'p', 100);
strcpy(longpath + 105, " HTTP/1.1\r\n\r\n");
TEST_ASSERT_EQUAL(NL_HTTP_BAD_REQUEST, parse(longpath, &r));
}
static void test_response(void)
{
char out[256];
size_t n = nl_http_response(out, sizeof out, 404, "{\"error\":\"not found\"}");
TEST_ASSERT_EQUAL_STRING("HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\n"
"Content-Length: 21\r\nConnection: close\r\n\r\n{\"error\":\"not found\"}", out);
TEST_ASSERT_EQUAL_UINT(strlen(out), n);
TEST_ASSERT_EQUAL_UINT(0, nl_http_response(out, 20, 200, "{}"));
TEST_ASSERT_EQUAL_STRING("Accepted", nl_http_reason(202));
TEST_ASSERT_EQUAL_STRING("Payload Too Large", nl_http_reason(413));
TEST_ASSERT_EQUAL_STRING("Not Implemented", nl_http_reason(501));
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_simple_get);
RUN_TEST(test_post_body_arrives_in_parts);
RUN_TEST(test_headers_case_insensitive_and_bearer);
RUN_TEST(test_plain_token_and_query_string);
RUN_TEST(test_other_method);
RUN_TEST(test_body_too_large);
RUN_TEST(test_headers_never_end);
RUN_TEST(test_bad_requests);
RUN_TEST(test_response);
return UNITY_END();
}
+45
View File
@@ -0,0 +1,45 @@
#include "unity.h"
#include "net/nl_names.h"
void setUp(void) {}
void tearDown(void) {}
static const uint8_t MAC[6] = {0x28, 0x6D, 0xCD, 0x5C, 0x68, 0x1F};
static void test_suffix(void)
{
char s[5];
nl_names_mac_suffix(MAC, s);
TEST_ASSERT_EQUAL_STRING("681f", s);
}
static void test_hostname(void)
{
char h[24];
nl_names_hostname(MAC, h, sizeof h);
TEST_ASSERT_EQUAL_STRING("ninlight-681f", h);
}
static void test_hostname_truncates_safely(void)
{
char h[8];
nl_names_hostname(MAC, h, sizeof h);
TEST_ASSERT_EQUAL_STRING("ninligh", h);
}
static void test_mac_str(void)
{
char m[18];
nl_names_mac_str(MAC, m);
TEST_ASSERT_EQUAL_STRING("28:6d:cd:5c:68:1f", m);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_suffix);
RUN_TEST(test_hostname);
RUN_TEST(test_hostname_truncates_safely);
RUN_TEST(test_mac_str);
return UNITY_END();
}