fix unit tests

This commit is contained in:
Sergio R. Caprile committed 2026-04-01 10:30:59 -03:00
1 parent 1620110f50
commit 9210a762de
4 files changed
+18 -17

No files matched your search

+3 -3
View File
@@ -2748,7 +2748,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
const char *buf = (char *) c->recv.buf + ofs; const char *buf = (char *) c->recv.buf + ofs;
int n = mg_http_parse(buf, c->recv.len - ofs, &hm); int n = mg_http_parse(buf, c->recv.len - ofs, &hm);
struct mg_str *te; // Transfer - encoding header struct mg_str *te; // Transfer - encoding header
bool is_chunked = false; bool is_chunked = false, is_http_1_0 = false;
size_t old_len = c->recv.len; size_t old_len = c->recv.len;
if (n < 0) { if (n < 0) {
// We don't use mg_error() here, to avoid closing pipelined requests // We don't use mg_error() here, to avoid closing pipelined requests
@@ -2771,7 +2771,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf); hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
} }
bool is_http_1_0 = is_http_1_0 =
hm.proto.len > 8 && mg_ncasecmp(hm.proto.buf, "HTTP/1.0", 8) == 0; hm.proto.len > 8 && mg_ncasecmp(hm.proto.buf, "HTTP/1.0", 8) == 0;
// HTTP/1.0 does not use "Transfer-Encoding: chunked" // HTTP/1.0 does not use "Transfer-Encoding: chunked"
if (!is_http_1_0 && if (!is_http_1_0 &&
@@ -14254,7 +14254,7 @@ static int mg_tls_verify_cert_signature(const struct mg_tls_cert *cert,
(unsigned) cert->tbshashsz, sig, (unsigned) cert->tbshashsz, sig,
mg_uecc_secp256r1()); mg_uecc_secp256r1());
} else if (issuer->pubkey.len == 96) { } else if (issuer->pubkey.len == 96) {
MG_VERBOSE(("ignore secp386 for now")); MG_ERROR(("reject secp386 for now"));
return 0; return 0;
} else { } else {
MG_ERROR(("unsupported public key length: %d", issuer->pubkey.len)); MG_ERROR(("unsupported public key length: %d", issuer->pubkey.len));
+2 -2
View File
@@ -1023,7 +1023,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
const char *buf = (char *) c->recv.buf + ofs; const char *buf = (char *) c->recv.buf + ofs;
int n = mg_http_parse(buf, c->recv.len - ofs, &hm); int n = mg_http_parse(buf, c->recv.len - ofs, &hm);
struct mg_str *te; // Transfer - encoding header struct mg_str *te; // Transfer - encoding header
bool is_chunked = false; bool is_chunked = false, is_http_1_0 = false;
size_t old_len = c->recv.len; size_t old_len = c->recv.len;
if (n < 0) { if (n < 0) {
// We don't use mg_error() here, to avoid closing pipelined requests // We don't use mg_error() here, to avoid closing pipelined requests
@@ -1046,7 +1046,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf); hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
} }
bool is_http_1_0 = is_http_1_0 =
hm.proto.len > 8 && mg_ncasecmp(hm.proto.buf, "HTTP/1.0", 8) == 0; hm.proto.len > 8 && mg_ncasecmp(hm.proto.buf, "HTTP/1.0", 8) == 0;
// HTTP/1.0 does not use "Transfer-Encoding: chunked" // HTTP/1.0 does not use "Transfer-Encoding: chunked"
if (!is_http_1_0 && if (!is_http_1_0 &&
+1 -1
View File
@@ -1539,7 +1539,7 @@ static int mg_tls_verify_cert_signature(const struct mg_tls_cert *cert,
(unsigned) cert->tbshashsz, sig, (unsigned) cert->tbshashsz, sig,
mg_uecc_secp256r1()); mg_uecc_secp256r1());
} else if (issuer->pubkey.len == 96) { } else if (issuer->pubkey.len == 96) {
MG_VERBOSE(("ignore secp386 for now")); MG_ERROR(("reject secp386 for now"));
return 0; return 0;
} else { } else {
MG_ERROR(("unsupported public key length: %d", issuer->pubkey.len)); MG_ERROR(("unsupported public key length: %d", issuer->pubkey.len));
+12 -11
View File
@@ -73,10 +73,9 @@ static void test_match(void) {
ASSERT(mg_match(mg_str_n("a__b_c", 6), mg_str_n("a*b*c", 5), NULL) == 1); ASSERT(mg_match(mg_str_n("a__b_c", 6), mg_str_n("a*b*c", 5), NULL) == 1);
{ {
char *ae_value = malloc(1); // Exact size for ASAN char *ae_value = (char *) malloc(1); // Exact size for ASAN
ae_value[0] = 'g'; ae_value[0] = 'g';
struct mg_str input = {ae_value, 1}; mg_match(mg_str_n(ae_value, 1), mg_str("*gzip*"), NULL); // Check for OOB
mg_match(input, mg_str("*gzip*"), NULL); // Check for OOB
free(ae_value); free(ae_value);
} }
@@ -1558,7 +1557,7 @@ static void test_http_client(void) {
ASSERT(ok == 301 || ok == 200); ASSERT(ok == 301 || ok == 200);
mg_mgr_poll(&mgr, 0); mg_mgr_poll(&mgr, 0);
ok = 0; ok = 0;
#if MG_TLS #if MG_TLS && MG_TLS != MG_TLS_BUILTIN
url = "https://cesanta.com"; url = "https://cesanta.com";
opts.name = mg_url_host(url); opts.name = mg_url_host(url);
#if MG_TLS == MG_TLS_BUILTIN #if MG_TLS == MG_TLS_BUILTIN
@@ -1889,8 +1888,7 @@ static void test_http_parse(void) {
} }
{ {
static const char *s = const char *s = "a b HTTP/1.0\na:1\nb:2\nc:3\nd:4\ne:5\nf:6\ng:7\nh:8\n\n";
"a b HTTP/1.0\na:1\nb:2\nc:3\nd:4\ne:5\nf:6\ng:7\nh:8\n\n";
ASSERT(mg_http_parse(s, strlen(s), &req) == (int) strlen(s)); ASSERT(mg_http_parse(s, strlen(s), &req) == (int) strlen(s));
ASSERT((v = mg_http_get_header(&req, "e")) != NULL); ASSERT((v = mg_http_get_header(&req, "e")) != NULL);
ASSERT(vcmp(*v, "5")); ASSERT(vcmp(*v, "5"));
@@ -1901,16 +1899,16 @@ static void test_http_parse(void) {
{ {
// no HTTP version // no HTTP version
static char *s = "a b\na:1\nb:2\nc:2\n\n"; const char *s = "a b\na:1\nb:2\nc:2\n\n";
ASSERT(mg_http_parse(s, strlen(s), &req) == -1); ASSERT(mg_http_parse(s, strlen(s), &req) == -1);
// HTTP version // HTTP version
s = "a b HTTP/1.0\na:1\nb:2\nc:2\n\n"; s = "a b HTTP/1.0\na:1\nb:2\nc:2\n\n";
ASSERT(mg_http_parse(s, strlen(s), &req) == strlen(s)); ASSERT(mg_http_parse(s, strlen(s), &req) == (int) strlen(s));
// Content-Length // Content-Length
s = "a b HTTP/1.0\nContent-Length:10\nb:2\nc:2\n\n"; s = "a b HTTP/1.0\nContent-Length:10\nb:2\nc:2\n\n";
ASSERT(mg_http_parse(s, strlen(s), &req) == strlen(s)); ASSERT(mg_http_parse(s, strlen(s), &req) == (int) strlen(s));
// duplicated Content-Length // duplicated Content-Length
s = "a b HTTP/1.0\nContent-Length:10\nb:2\nContent-Length:20\n\n"; s = "a b HTTP/1.0\nContent-Length:10\nb:2\nContent-Length:20\n\n";
@@ -3654,7 +3652,7 @@ static void test_json(void) {
// mg_json_get_num: parsing exponential // mg_json_get_num: parsing exponential
{ {
double d = 0.0; double d = 0.0, tolerance = 1e-12;
json = mg_str( json = mg_str(
"{" "{"
"\"i\":1e3," "\"i\":1e3,"
@@ -3666,7 +3664,6 @@ static void test_json(void) {
"\"a\":[6.123456789e3,-1e-9]," "\"a\":[6.123456789e3,-1e-9],"
"\"bad\":\"1e3\"" "\"bad\":\"1e3\""
"}"); "}");
double tolerance = 1e-12;
ASSERT(mg_json_get_num(json, "$.i", &d) == true); ASSERT(mg_json_get_num(json, "$.i", &d) == true);
ASSERT(fabs(d - 1000.0) < tolerance); ASSERT(fabs(d - 1000.0) < tolerance);
ASSERT(mg_json_get_num(json, "$.n", &d) == true); ASSERT(mg_json_get_num(json, "$.n", &d) == true);
@@ -4196,9 +4193,13 @@ int main(void) {
test_sntp(); test_sntp();
DASHBOARD("sntp"); DASHBOARD("sntp");
#if MG_TLS != MG_TLS_BUILTIN
s_error = false; s_error = false;
test_mqtt(); // sorry, MQTT_LOCALHOST is also skipped test_mqtt(); // sorry, MQTT_LOCALHOST is also skipped
DASHBOARD("mqtt"); DASHBOARD("mqtt");
#else
(void) test_mqtt;
#endif
s_error = false; s_error = false;
test_http_client(); test_http_client();