Skip to content

Commit

Permalink
emitter: allow unicode characters in the emitter
Browse files Browse the repository at this point in the history
The YAML emitter from libyaml will convert non-ascii characters to
latin1 by default.

See LP: #2071652
  • Loading branch information
daniloegea committed Jul 2, 2024
1 parent 1879133 commit 7d6844a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/yaml-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
#define YAML_OUT_START(event_ptr, emitter_ptr, file) \
{ \
yaml_emitter_initialize(emitter_ptr); \
yaml_emitter_set_unicode(emitter_ptr, 1); \
yaml_emitter_set_output_file(emitter_ptr, file); \
yaml_stream_start_event_initialize(event_ptr, YAML_UTF8_ENCODING); \
if (!yaml_emitter_emit(emitter_ptr, event_ptr)) goto err_path; \
Expand Down
66 changes: 66 additions & 0 deletions tests/ctests/test_netplan_keyfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,71 @@ test_load_keyfile_route_options_without_route(__unused void** state)
netplan_state_clear(&np_state);
}

void
test_load_keyfile_utf8_password(__unused void** state)
{
NetplanState *np_state = NULL;
int fd;
int size;
int res;
char* yaml;

const char* keyfile =
"[connection]\n"
"id=mywifi\n"
"uuid=03c8f2a7-268d-4765-b626-efcc02dd686c\n"
"type=wifi\n"
"interface-name=wlp2s0\n"
"[wifi]\n"
"mode=infrastructure\n"
"ssid=mywifi\n"
"[wifi-security]\n"
"key-mgmt=wpa-psk\n"
"password=áÁéÉíÍóÓúÚ\n"
"[ipv4]\n"
"method=auto\n";

const char* expected =
"network:\n"
" version: 2\n"
" wifis:\n"
" NM-03c8f2a7-268d-4765-b626-efcc02dd686c:\n"
" renderer: NetworkManager\n"
" match:\n"
" name: \"wlp2s0\"\n"
" dhcp4: true\n"
" access-points:\n"
" \"mywifi\":\n"
" auth:\n"
" key-management: \"psk\"\n"
" networkmanager:\n"
" uuid: \"03c8f2a7-268d-4765-b626-efcc02dd686c\"\n"
" name: \"mywifi\"\n"
" passthrough:\n"
" wifi-security.password: \"áÁéÉíÍóÓúÚ\"\n"
" networkmanager:\n"
" uuid: \"03c8f2a7-268d-4765-b626-efcc02dd686c\"\n"
" name: \"mywifi\"\n";

np_state = load_keyfile_string_to_netplan_state(keyfile);

fd = memfd_create("netplan-tests", 0);

netplan_state_dump_yaml(np_state, fd, NULL);

size = lseek(fd, 0, SEEK_CUR) + 1;
yaml = malloc(size);
memset(yaml, 0, size);
lseek(fd, 0, SEEK_SET);
res = read(fd, yaml, size - 1);
assert_true(res > 0);

assert_string_equal(yaml, expected);

netplan_state_clear(&np_state);
close(fd);
free(yaml);
}

int
setup(__unused void** state)
Expand All @@ -280,6 +345,7 @@ main()
cmocka_unit_test(test_load_keyfile_vxlan),
cmocka_unit_test(test_load_keyfile_multiple_addresses_and_routes),
cmocka_unit_test(test_load_keyfile_route_options_without_route),
cmocka_unit_test(test_load_keyfile_utf8_password),
};

return cmocka_run_group_tests(tests, setup, tear_down);
Expand Down

0 comments on commit 7d6844a

Please sign in to comment.