fix: renforcer la robustesse du module et étendre les tests/CI
Co-authored-by: aider (openrouter/openai/gpt-5.3-codex) <aider@aider.chat>
This commit is contained in:
@ -8,34 +8,88 @@
|
||||
#include <cmocka.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <apr_pools.h>
|
||||
#include <apr_strings.h>
|
||||
#include <apr_time.h>
|
||||
#include <apr_lib.h>
|
||||
|
||||
/* Mock JSON string escaping function for testing */
|
||||
static void append_json_string(apr_pool_t *pool, apr_strbuf_t *buf, const char *str)
|
||||
typedef struct {
|
||||
char *data;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
apr_pool_t *pool;
|
||||
} testbuf_t;
|
||||
|
||||
static void testbuf_init(testbuf_t *buf, apr_pool_t *pool, size_t initial_capacity)
|
||||
{
|
||||
buf->pool = pool;
|
||||
buf->cap = initial_capacity;
|
||||
buf->len = 0;
|
||||
buf->data = apr_palloc(pool, initial_capacity);
|
||||
buf->data[0] = '\0';
|
||||
}
|
||||
|
||||
static void testbuf_append(testbuf_t *buf, const char *str, size_t len)
|
||||
{
|
||||
if (str == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (len == (size_t)-1) {
|
||||
len = strlen(str);
|
||||
}
|
||||
|
||||
if (buf->len + len + 1 > buf->cap) {
|
||||
size_t new_cap = (buf->len + len + 1) * 2;
|
||||
char *new_data = apr_palloc(buf->pool, new_cap);
|
||||
memcpy(new_data, buf->data, buf->len + 1);
|
||||
buf->data = new_data;
|
||||
buf->cap = new_cap;
|
||||
}
|
||||
|
||||
memcpy(buf->data + buf->len, str, len);
|
||||
buf->len += len;
|
||||
buf->data[buf->len] = '\0';
|
||||
}
|
||||
|
||||
static void testbuf_append_char(testbuf_t *buf, char c)
|
||||
{
|
||||
if (buf->len + 2 > buf->cap) {
|
||||
size_t new_cap = (buf->cap * 2);
|
||||
char *new_data = apr_palloc(buf->pool, new_cap);
|
||||
memcpy(new_data, buf->data, buf->len + 1);
|
||||
buf->data = new_data;
|
||||
buf->cap = new_cap;
|
||||
}
|
||||
|
||||
buf->data[buf->len++] = c;
|
||||
buf->data[buf->len] = '\0';
|
||||
}
|
||||
|
||||
/* Mock JSON string escaping function for testing */
|
||||
static void append_json_string(testbuf_t *buf, const char *str)
|
||||
{
|
||||
if (str == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const char *p = str; *p; p++) {
|
||||
char c = *p;
|
||||
switch (c) {
|
||||
case '"': apr_strbuf_append(buf, "\\\"", 2); break;
|
||||
case '\\': apr_strbuf_append(buf, "\\\\", 2); break;
|
||||
case '\b': apr_strbuf_append(buf, "\\b", 2); break;
|
||||
case '\f': apr_strbuf_append(buf, "\\f", 2); break;
|
||||
case '\n': apr_strbuf_append(buf, "\\n", 2); break;
|
||||
case '\r': apr_strbuf_append(buf, "\\r", 2); break;
|
||||
case '\t': apr_strbuf_append(buf, "\\t", 2); break;
|
||||
case '"': testbuf_append(buf, "\\\"", 2); break;
|
||||
case '\\': testbuf_append(buf, "\\\\", 2); break;
|
||||
case '\b': testbuf_append(buf, "\\b", 2); break;
|
||||
case '\f': testbuf_append(buf, "\\f", 2); break;
|
||||
case '\n': testbuf_append(buf, "\\n", 2); break;
|
||||
case '\r': testbuf_append(buf, "\\r", 2); break;
|
||||
case '\t': testbuf_append(buf, "\\t", 2); break;
|
||||
default:
|
||||
if ((unsigned char)c < 0x20) {
|
||||
char unicode[8];
|
||||
apr_snprintf(unicode, sizeof(unicode), "\\u%04x", (unsigned char)c);
|
||||
apr_strbuf_append(buf, unicode, -1);
|
||||
testbuf_append(buf, unicode, (size_t)-1);
|
||||
} else {
|
||||
apr_strbuf_append_char(buf, c);
|
||||
testbuf_append_char(buf, c);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -46,16 +100,16 @@ static void append_json_string(apr_pool_t *pool, apr_strbuf_t *buf, const char *
|
||||
static void test_json_escape_empty_string(void **state)
|
||||
{
|
||||
apr_pool_t *pool;
|
||||
testbuf_t buf;
|
||||
(void)state;
|
||||
|
||||
apr_pool_create(&pool, NULL);
|
||||
|
||||
apr_strbuf_t buf;
|
||||
char *initial = apr_palloc(pool, 256);
|
||||
apr_strbuf_init(pool, &buf, initial, 256);
|
||||
|
||||
append_json_string(pool, &buf, "");
|
||||
|
||||
assert_string_equal(buf.buf, "");
|
||||
|
||||
testbuf_init(&buf, pool, 256);
|
||||
|
||||
append_json_string(&buf, "");
|
||||
|
||||
assert_string_equal(buf.data, "");
|
||||
|
||||
apr_pool_destroy(pool);
|
||||
}
|
||||
|
||||
@ -63,16 +117,16 @@ static void test_json_escape_empty_string(void **state)
|
||||
static void test_json_escape_simple_string(void **state)
|
||||
{
|
||||
apr_pool_t *pool;
|
||||
testbuf_t buf;
|
||||
(void)state;
|
||||
|
||||
apr_pool_create(&pool, NULL);
|
||||
|
||||
apr_strbuf_t buf;
|
||||
char *initial = apr_palloc(pool, 256);
|
||||
apr_strbuf_init(pool, &buf, initial, 256);
|
||||
|
||||
append_json_string(pool, &buf, "hello world");
|
||||
|
||||
assert_string_equal(buf.buf, "hello world");
|
||||
|
||||
testbuf_init(&buf, pool, 256);
|
||||
|
||||
append_json_string(&buf, "hello world");
|
||||
|
||||
assert_string_equal(buf.data, "hello world");
|
||||
|
||||
apr_pool_destroy(pool);
|
||||
}
|
||||
|
||||
@ -80,16 +134,16 @@ static void test_json_escape_simple_string(void **state)
|
||||
static void test_json_escape_quotes(void **state)
|
||||
{
|
||||
apr_pool_t *pool;
|
||||
testbuf_t buf;
|
||||
(void)state;
|
||||
|
||||
apr_pool_create(&pool, NULL);
|
||||
|
||||
apr_strbuf_t buf;
|
||||
char *initial = apr_palloc(pool, 256);
|
||||
apr_strbuf_init(pool, &buf, initial, 256);
|
||||
|
||||
append_json_string(pool, &buf, "hello \"world\"");
|
||||
|
||||
assert_string_equal(buf.buf, "hello \\\"world\\\"");
|
||||
|
||||
testbuf_init(&buf, pool, 256);
|
||||
|
||||
append_json_string(&buf, "hello \"world\"");
|
||||
|
||||
assert_string_equal(buf.data, "hello \\\"world\\\"");
|
||||
|
||||
apr_pool_destroy(pool);
|
||||
}
|
||||
|
||||
@ -97,16 +151,16 @@ static void test_json_escape_quotes(void **state)
|
||||
static void test_json_escape_backslashes(void **state)
|
||||
{
|
||||
apr_pool_t *pool;
|
||||
testbuf_t buf;
|
||||
(void)state;
|
||||
|
||||
apr_pool_create(&pool, NULL);
|
||||
|
||||
apr_strbuf_t buf;
|
||||
char *initial = apr_palloc(pool, 256);
|
||||
apr_strbuf_init(pool, &buf, initial, 256);
|
||||
|
||||
append_json_string(pool, &buf, "path\\to\\file");
|
||||
|
||||
assert_string_equal(buf.buf, "path\\\\to\\\\file");
|
||||
|
||||
testbuf_init(&buf, pool, 256);
|
||||
|
||||
append_json_string(&buf, "path\\to\\file");
|
||||
|
||||
assert_string_equal(buf.data, "path\\\\to\\\\file");
|
||||
|
||||
apr_pool_destroy(pool);
|
||||
}
|
||||
|
||||
@ -114,16 +168,16 @@ static void test_json_escape_backslashes(void **state)
|
||||
static void test_json_escape_newlines_tabs(void **state)
|
||||
{
|
||||
apr_pool_t *pool;
|
||||
testbuf_t buf;
|
||||
(void)state;
|
||||
|
||||
apr_pool_create(&pool, NULL);
|
||||
|
||||
apr_strbuf_t buf;
|
||||
char *initial = apr_palloc(pool, 256);
|
||||
apr_strbuf_init(pool, &buf, initial, 256);
|
||||
|
||||
append_json_string(pool, &buf, "line1\nline2\ttab");
|
||||
|
||||
assert_string_equal(buf.buf, "line1\\nline2\\ttab");
|
||||
|
||||
testbuf_init(&buf, pool, 256);
|
||||
|
||||
append_json_string(&buf, "line1\nline2\ttab");
|
||||
|
||||
assert_string_equal(buf.data, "line1\\nline2\\ttab");
|
||||
|
||||
apr_pool_destroy(pool);
|
||||
}
|
||||
|
||||
@ -131,18 +185,18 @@ static void test_json_escape_newlines_tabs(void **state)
|
||||
static void test_json_escape_control_chars(void **state)
|
||||
{
|
||||
apr_pool_t *pool;
|
||||
testbuf_t buf;
|
||||
(void)state;
|
||||
|
||||
apr_pool_create(&pool, NULL);
|
||||
|
||||
apr_strbuf_t buf;
|
||||
char *initial = apr_palloc(pool, 256);
|
||||
apr_strbuf_init(pool, &buf, initial, 256);
|
||||
|
||||
testbuf_init(&buf, pool, 256);
|
||||
|
||||
/* Test with bell character (0x07) */
|
||||
append_json_string(pool, &buf, "test\bell");
|
||||
|
||||
append_json_string(&buf, "test\bell");
|
||||
|
||||
/* Should contain unicode escape */
|
||||
assert_true(strstr(buf.buf, "\\u0007") != NULL);
|
||||
|
||||
assert_true(strstr(buf.data, "\\u0007") != NULL);
|
||||
|
||||
apr_pool_destroy(pool);
|
||||
}
|
||||
|
||||
@ -150,16 +204,16 @@ static void test_json_escape_control_chars(void **state)
|
||||
static void test_json_escape_null_string(void **state)
|
||||
{
|
||||
apr_pool_t *pool;
|
||||
testbuf_t buf;
|
||||
(void)state;
|
||||
|
||||
apr_pool_create(&pool, NULL);
|
||||
|
||||
apr_strbuf_t buf;
|
||||
char *initial = apr_palloc(pool, 256);
|
||||
apr_strbuf_init(pool, &buf, initial, 256);
|
||||
|
||||
append_json_string(pool, &buf, NULL);
|
||||
|
||||
assert_string_equal(buf.buf, "");
|
||||
|
||||
testbuf_init(&buf, pool, 256);
|
||||
|
||||
append_json_string(&buf, NULL);
|
||||
|
||||
assert_string_equal(buf.data, "");
|
||||
|
||||
apr_pool_destroy(pool);
|
||||
}
|
||||
|
||||
@ -167,17 +221,17 @@ static void test_json_escape_null_string(void **state)
|
||||
static void test_json_escape_user_agent(void **state)
|
||||
{
|
||||
apr_pool_t *pool;
|
||||
apr_pool_create(&pool, NULL);
|
||||
|
||||
apr_strbuf_t buf;
|
||||
char *initial = apr_palloc(pool, 512);
|
||||
apr_strbuf_init(pool, &buf, initial, 512);
|
||||
|
||||
testbuf_t buf;
|
||||
const char *ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) \"Test\"";
|
||||
append_json_string(pool, &buf, ua);
|
||||
|
||||
assert_true(strstr(buf.buf, "\\\"Test\\\"") != NULL);
|
||||
|
||||
(void)state;
|
||||
|
||||
apr_pool_create(&pool, NULL);
|
||||
testbuf_init(&buf, pool, 512);
|
||||
|
||||
append_json_string(&buf, ua);
|
||||
|
||||
assert_true(strstr(buf.data, "\\\"Test\\\"") != NULL);
|
||||
|
||||
apr_pool_destroy(pool);
|
||||
}
|
||||
|
||||
@ -193,6 +247,6 @@ int main(void)
|
||||
cmocka_unit_test(test_json_escape_null_string),
|
||||
cmocka_unit_test(test_json_escape_user_agent),
|
||||
};
|
||||
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user