mdns: add some txtdata unit tests

This commit is contained in:
2026-01-02 00:09:35 -07:00
parent 4599f551de
commit c01c655c5f

View File

@@ -35,6 +35,7 @@
#include "lwip/pbuf.h"
#include "lwip/apps/mdns.h"
#include "lwip/apps/mdns_domain.h"
#include "lwip/apps/mdns_out.h"
#include "lwip/apps/mdns_priv.h"
START_TEST(readname_basic)
@@ -876,6 +877,90 @@ START_TEST(compress_long_match)
}
END_TEST
#define TXT_STRING_1 "path=/"
#define TXT_LENGTH_1 6
#define TXT_LENSTR_1 "\006"
#define TXT_STRING_2 ""
#define TXT_LENGTH_2 0
#define TXT_LENSTR_2 "\000"
#define TXT_STRING_3 "This sentence is sixty-three bytes long, including punctuation."
#define TXT_LENGTH_3 63
#define TXT_LENSTR_3 "\077"
START_TEST(txt_short_item)
{
const char *expected_txtdata = TXT_LENSTR_1 TXT_STRING_1;
const size_t expected_txtdata_length = 1 + TXT_LENGTH_1;
struct mdns_service service;
err_t res;
memset(&service, 0, sizeof(struct mdns_service));
mdns_prepare_txtdata(&service);
res = mdns_resp_add_service_txtitem(&service, TXT_STRING_1, TXT_LENGTH_1);
fail_unless(res == ERR_OK);
fail_unless(service.txtdata.length == expected_txtdata_length);
fail_if(memcmp(service.txtdata.name, expected_txtdata, expected_txtdata_length));
}
END_TEST
START_TEST(txt_empty_item)
{
const char *expected_txtdata = TXT_LENSTR_2 TXT_STRING_2;
const size_t expected_txtdata_length = 1 + TXT_LENGTH_2;
struct mdns_service service;
err_t res;
memset(&service, 0, sizeof(struct mdns_service));
mdns_prepare_txtdata(&service);
res = mdns_resp_add_service_txtitem(&service, TXT_STRING_2, TXT_LENGTH_2);
fail_unless(res == ERR_OK);
fail_unless(service.txtdata.length == expected_txtdata_length);
fail_if(memcmp(service.txtdata.name, expected_txtdata, expected_txtdata_length));
}
END_TEST
START_TEST(txt_multiple_items)
{
const char *expected_txtdata = (
TXT_LENSTR_1
TXT_STRING_1
TXT_LENSTR_2
TXT_STRING_2
TXT_LENSTR_3
TXT_STRING_3
);
const size_t expected_txtdata_length = (
1 + TXT_LENGTH_1
+ 1 + TXT_LENGTH_2
+ 1 + TXT_LENGTH_3
);
struct mdns_service service;
err_t res;
memset(&service, 0, sizeof(struct mdns_service));
mdns_prepare_txtdata(&service);
res = mdns_resp_add_service_txtitem(&service, TXT_STRING_1, TXT_LENGTH_1);
fail_unless(res == ERR_OK); /* TXT_STRING_1 */
res = mdns_resp_add_service_txtitem(&service, TXT_STRING_2, TXT_LENGTH_2);
fail_unless(res == ERR_OK); /* TXT_STRING_1 */
res = mdns_resp_add_service_txtitem(&service, TXT_STRING_3, TXT_LENGTH_3);
fail_unless(res == ERR_OK); /* TXT_STRING_3 */
fail_unless(service.txtdata.length == expected_txtdata_length);
fail_if(memcmp(service.txtdata.name, expected_txtdata, expected_txtdata_length));
}
END_TEST
Suite* mdns_suite(void)
{
testfunc tests[] = {
@@ -911,6 +996,10 @@ Suite* mdns_suite(void)
TESTFUNC(compress_2nd_label_short),
TESTFUNC(compress_jump_to_jump),
TESTFUNC(compress_long_match),
TESTFUNC(txt_short_item),
TESTFUNC(txt_empty_item),
TESTFUNC(txt_multiple_items),
};
return create_suite("MDNS", tests, sizeof(tests)/sizeof(testfunc), NULL, NULL);
}