Skip to content

Commit

Permalink
feat(mdns): Console test: add/remove TXT recs for delegated srvs
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Aug 15, 2024
1 parent 6b9a6ce commit c9a58d7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 19 deletions.
52 changes: 45 additions & 7 deletions components/mdns/mdns_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,8 @@ static void register_mdns_service_port_set(void)
static struct {
struct arg_str *service;
struct arg_str *proto;
struct arg_str *instance;
struct arg_str *host;
struct arg_str *txt;
struct arg_end *end;
} mdns_txt_replace_args;
Expand All @@ -918,7 +920,16 @@ static int cmd_mdns_service_txt_replace(int argc, char **argv)
printf("ERROR: Bad arguments!\n");
return 1;
}

const char *instance = NULL;
if (mdns_txt_replace_args.instance->count && mdns_txt_replace_args.instance->sval[0]) {
instance = mdns_txt_replace_args.instance->sval[0];
printf("MDNS: Service Instance: %s\n", instance);
}
const char *host = NULL;
if (mdns_txt_replace_args.host->count && mdns_txt_replace_args.host->sval[0]) {
host = mdns_txt_replace_args.host->sval[0];
printf("MDNS: Service for delegated host: %s\n", host);
}
if (mdns_txt_replace_args.txt->count) {
items = _convert_items(mdns_txt_replace_args.txt->sval, mdns_txt_replace_args.txt->count);
if (!items) {
Expand All @@ -927,7 +938,7 @@ static int cmd_mdns_service_txt_replace(int argc, char **argv)

}
}
ESP_ERROR_CHECK( mdns_service_txt_set(mdns_txt_replace_args.service->sval[0], mdns_txt_replace_args.proto->sval[0], items, mdns_txt_replace_args.txt->count) );
ESP_ERROR_CHECK( mdns_service_txt_set_for_host(instance, mdns_txt_replace_args.service->sval[0], mdns_txt_replace_args.proto->sval[0], host, items, mdns_txt_replace_args.txt->count) );
free(items);
return 0;
}
Expand All @@ -936,8 +947,10 @@ static void register_mdns_service_txt_replace(void)
{
mdns_txt_replace_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
mdns_txt_replace_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
mdns_txt_replace_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
mdns_txt_replace_args.host = arg_str0("h", "host", "<hostname>", "Service for this (delegated) host");
mdns_txt_replace_args.txt = arg_strn(NULL, NULL, "item", 0, 30, "TXT Items (name=value)");
mdns_txt_replace_args.end = arg_end(2);
mdns_txt_replace_args.end = arg_end(5);

const esp_console_cmd_t cmd_txt_set = {
.command = "mdns_service_txt_replace",
Expand All @@ -953,6 +966,8 @@ static void register_mdns_service_txt_replace(void)
static struct {
struct arg_str *service;
struct arg_str *proto;
struct arg_str *instance;
struct arg_str *host;
struct arg_str *var;
struct arg_str *value;
struct arg_end *end;
Expand All @@ -970,8 +985,18 @@ static int cmd_mdns_service_txt_set(int argc, char **argv)
printf("ERROR: Bad arguments!\n");
return 1;
}
const char *instance = NULL;
if (mdns_txt_set_args.instance->count && mdns_txt_set_args.instance->sval[0]) {
instance = mdns_txt_set_args.instance->sval[0];
printf("MDNS: Service Instance: %s\n", instance);
}
const char *host = NULL;
if (mdns_txt_set_args.host->count && mdns_txt_set_args.host->sval[0]) {
host = mdns_txt_set_args.host->sval[0];
printf("MDNS: Service for delegated host: %s\n", host);
}

ESP_ERROR_CHECK( mdns_service_txt_item_set(mdns_txt_set_args.service->sval[0], mdns_txt_set_args.proto->sval[0], mdns_txt_set_args.var->sval[0], mdns_txt_set_args.value->sval[0]) );
ESP_ERROR_CHECK( mdns_service_txt_item_set_for_host(instance, mdns_txt_set_args.service->sval[0], mdns_txt_set_args.proto->sval[0], host, mdns_txt_set_args.var->sval[0], mdns_txt_set_args.value->sval[0]) );
return 0;
}

Expand All @@ -981,7 +1006,9 @@ static void register_mdns_service_txt_set(void)
mdns_txt_set_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
mdns_txt_set_args.var = arg_str1(NULL, NULL, "<var>", "Item Name");
mdns_txt_set_args.value = arg_str1(NULL, NULL, "<value>", "Item Value");
mdns_txt_set_args.end = arg_end(2);
mdns_txt_set_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
mdns_txt_set_args.host = arg_str0("h", "host", "<hostname>", "Service for this (delegated) host");
mdns_txt_set_args.end = arg_end(6);

const esp_console_cmd_t cmd_txt_set = {
.command = "mdns_service_txt_set",
Expand All @@ -998,6 +1025,8 @@ static struct {
struct arg_str *service;
struct arg_str *proto;
struct arg_str *var;
struct arg_str *instance;
struct arg_str *host;
struct arg_end *end;
} mdns_txt_remove_args;

Expand All @@ -1013,8 +1042,15 @@ static int cmd_mdns_service_txt_remove(int argc, char **argv)
printf("ERROR: Bad arguments!\n");
return 1;
}

ESP_ERROR_CHECK( mdns_service_txt_item_remove(mdns_txt_remove_args.service->sval[0], mdns_txt_remove_args.proto->sval[0], mdns_txt_remove_args.var->sval[0]) );
const char *instance = NULL;
if (mdns_txt_remove_args.instance->count && mdns_txt_remove_args.instance->sval[0]) {
instance = mdns_txt_remove_args.instance->sval[0];
}
const char *host = NULL;
if (mdns_txt_remove_args.host->count && mdns_txt_remove_args.host->sval[0]) {
host = mdns_txt_remove_args.host->sval[0];
}
ESP_ERROR_CHECK( mdns_service_txt_item_remove_for_host(instance, mdns_txt_remove_args.service->sval[0], mdns_txt_remove_args.proto->sval[0], host, mdns_txt_remove_args.var->sval[0]) );
return 0;
}

Expand All @@ -1023,6 +1059,8 @@ static void register_mdns_service_txt_remove(void)
mdns_txt_remove_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
mdns_txt_remove_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
mdns_txt_remove_args.var = arg_str1(NULL, NULL, "<var>", "Item Name");
mdns_txt_remove_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
mdns_txt_remove_args.host = arg_str0("h", "host", "<hostname>", "Service for this (delegated) host");
mdns_txt_remove_args.end = arg_end(2);

const esp_console_cmd_t cmd_txt_remove = {
Expand Down
36 changes: 24 additions & 12 deletions components/mdns/tests/host_test/pytest_mdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,33 @@ def test_remove_delegated_service(mdns_console, dig_app):
mdns_console.send_input('mdns_service_lookup _test2 _tcp -d')
mdns_console.get_output('No results found!')
dig_app.check_record('_test2._tcp.local', query_type='PTR', expected=False)
# add the delegated service again, would be used in the TXT test
mdns_console.send_input('mdns_service_add _test2 _tcp 80 -i extern -h delegated')
mdns_console.get_output('MDNS: Service Instance: extern')


def check_txt_for_service(instance, service, proto, mdns_console, dig_app, host=None):
for_host_arg = f'-h {host}' if host is not None else ''
mdns_console.send_input(f'mdns_service_txt_set {service} {proto} {for_host_arg} key1 value1')
dig_app.check_record(f'{instance}.{service}.{proto}.local', query_type='SRV', expected=True)
dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key1=value1')
mdns_console.send_input(f'mdns_service_txt_set {service} {proto} {for_host_arg} key2 value2')
dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key2=value2')
mdns_console.send_input(f'mdns_service_txt_remove {service} {proto} {for_host_arg} key2')
dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=False, expect='key2=value2')
dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key1=value1')
mdns_console.send_input(f'mdns_service_txt_replace {service} {proto} {for_host_arg} key3=value3 key4=value4')
dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=False, expect='key1=value1')
dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key3=value3')
dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key4=value4')


def test_update_txt(mdns_console, dig_app):
mdns_console.send_input('mdns_service_txt_set _test _tcp key1 value1')
dig_app.check_record('local._test._tcp.local', query_type='SRV', expected=True)
dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key1=value1')
mdns_console.send_input('mdns_service_txt_set _test _tcp key2 value2')
dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key2=value2')
mdns_console.send_input('mdns_service_txt_remove _test _tcp key2')
dig_app.check_record('_test._tcp.local', query_type='TXT', expected=False, expect='key2=value2')
dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key1=value1')
mdns_console.send_input('mdns_service_txt_replace _test _tcp key3=value3 key4=value4')
dig_app.check_record('_test._tcp.local', query_type='TXT', expected=False, expect='key1=value1')
dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key3=value3')
dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key4=value4')
check_txt_for_service('local', '_test', '_tcp', mdns_console=mdns_console, dig_app=dig_app)


def test_update_delegated_txt(mdns_console, dig_app):
check_txt_for_service('extern', '_test2', '_tcp', mdns_console=mdns_console, dig_app=dig_app, host='delegated')


if __name__ == '__main__':
Expand Down

0 comments on commit c9a58d7

Please sign in to comment.