mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-08 21:48:08 +02:00
libnm/tests: test that nm_vpn_service_plugin_read_vpn_details() does not consume "QUIT" command
(cherry picked from commit b1b1ee8cc4)
This commit is contained in:
parent
cb70a15fd5
commit
74cbcbbb59
1 changed files with 88 additions and 7 deletions
|
|
@ -2272,6 +2272,12 @@ _memfd_create(const char *name)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
READ_VPN_DETAIL_TYPE_GOOD,
|
||||||
|
READ_VPN_DETAIL_TYPE_NO_DONE,
|
||||||
|
READ_VPN_DETAIL_TYPE_BROKEN,
|
||||||
|
} ReadVpnDetailType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char *key;
|
const char *key;
|
||||||
const char *val;
|
const char *val;
|
||||||
|
|
@ -2285,6 +2291,7 @@ _do_read_vpn_details_impl1(const char *file,
|
||||||
int memfd,
|
int memfd,
|
||||||
char *mem,
|
char *mem,
|
||||||
gsize len,
|
gsize len,
|
||||||
|
ReadVpnDetailType detail_type,
|
||||||
const ReadVpnDetailData *expected_data,
|
const ReadVpnDetailData *expected_data,
|
||||||
guint expected_data_len,
|
guint expected_data_len,
|
||||||
const ReadVpnDetailData *expected_secrets,
|
const ReadVpnDetailData *expected_secrets,
|
||||||
|
|
@ -2294,10 +2301,27 @@ _do_read_vpn_details_impl1(const char *file,
|
||||||
off_t lseeked;
|
off_t lseeked;
|
||||||
gs_unref_hashtable GHashTable *data = NULL;
|
gs_unref_hashtable GHashTable *data = NULL;
|
||||||
gs_unref_hashtable GHashTable *secrets = NULL;
|
gs_unref_hashtable GHashTable *secrets = NULL;
|
||||||
|
char ch;
|
||||||
|
gboolean append_quit;
|
||||||
|
char read_buf[1024];
|
||||||
|
gssize n_read;
|
||||||
|
gssize i;
|
||||||
|
|
||||||
written = write(memfd, mem, len);
|
written = write(memfd, mem, len);
|
||||||
g_assert_cmpint(written, ==, (gssize) len);
|
g_assert_cmpint(written, ==, (gssize) len);
|
||||||
|
|
||||||
|
append_quit = nmtst_get_rand_bool();
|
||||||
|
|
||||||
|
if (append_quit) {
|
||||||
|
if (len > 0 && mem[len - 1] != '\n') {
|
||||||
|
ch = '\n';
|
||||||
|
written = write(memfd, &ch, 1);
|
||||||
|
g_assert_cmpint(written, ==, 1);
|
||||||
|
}
|
||||||
|
written = write(memfd, "QUIT", 4);
|
||||||
|
g_assert_cmpint(written, ==, 4);
|
||||||
|
}
|
||||||
|
|
||||||
lseeked = lseek(memfd, 0, SEEK_SET);
|
lseeked = lseek(memfd, 0, SEEK_SET);
|
||||||
g_assert_cmpint(lseeked, ==, 0);
|
g_assert_cmpint(lseeked, ==, 0);
|
||||||
|
|
||||||
|
|
@ -2361,10 +2385,58 @@ _do_read_vpn_details_impl1(const char *file,
|
||||||
_assert_hash(secrets, expected_secrets, expected_secrets_len);
|
_assert_hash(secrets, expected_secrets, expected_secrets_len);
|
||||||
|
|
||||||
#undef _assert_hash
|
#undef _assert_hash
|
||||||
|
|
||||||
|
n_read = read(memfd, read_buf, sizeof(read_buf));
|
||||||
|
|
||||||
|
if (0) {
|
||||||
|
gs_free char *ss = NULL;
|
||||||
|
|
||||||
|
g_print(">>>> n_read=%zd; \"%s\"",
|
||||||
|
n_read,
|
||||||
|
n_read > 0 ? (
|
||||||
|
ss = nm_utils_buf_utf8safe_escape_cp(read_buf,
|
||||||
|
n_read,
|
||||||
|
NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL))
|
||||||
|
: "");
|
||||||
|
}
|
||||||
|
|
||||||
|
g_assert_cmpint(n_read, >=, 0);
|
||||||
|
g_assert_cmpint(n_read, <, sizeof(read_buf));
|
||||||
|
|
||||||
|
i = read(memfd, &ch, 1);
|
||||||
|
g_assert_cmpint(i, ==, 0);
|
||||||
|
|
||||||
|
switch (detail_type) {
|
||||||
|
case READ_VPN_DETAIL_TYPE_GOOD:
|
||||||
|
g_assert_cmpint(n_read, >=, 0);
|
||||||
|
i = 0;
|
||||||
|
while (i < n_read && NM_IN_SET(read_buf[i], '\n'))
|
||||||
|
i++;
|
||||||
|
if (append_quit)
|
||||||
|
g_assert_cmpmem("QUIT", 4, &read_buf[i], n_read - i);
|
||||||
|
else
|
||||||
|
g_assert_cmpint(n_read - i, ==, 0);
|
||||||
|
break;
|
||||||
|
case READ_VPN_DETAIL_TYPE_NO_DONE:
|
||||||
|
g_assert_cmpint(n_read, ==, 0);
|
||||||
|
break;
|
||||||
|
case READ_VPN_DETAIL_TYPE_BROKEN:
|
||||||
|
g_assert_cmpint(n_read, >, 0);
|
||||||
|
if (append_quit) {
|
||||||
|
g_assert_cmpint(n_read, >, 4);
|
||||||
|
g_assert(memmem(&read_buf[i], n_read + i, "QUIT", 4));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
g_assert_not_reached();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define _do_read_vpn_details_impl0(str, \
|
#define _do_read_vpn_details_impl0(str, \
|
||||||
|
detail_type, \
|
||||||
expected_data, \
|
expected_data, \
|
||||||
expected_data_len, \
|
expected_data_len, \
|
||||||
expected_secrets, \
|
expected_secrets, \
|
||||||
|
|
@ -2385,6 +2457,7 @@ _do_read_vpn_details_impl1(const char *file,
|
||||||
_memfd, \
|
_memfd, \
|
||||||
"" str "", \
|
"" str "", \
|
||||||
NM_STRLEN(str), \
|
NM_STRLEN(str), \
|
||||||
|
(detail_type), \
|
||||||
expected_data, \
|
expected_data, \
|
||||||
expected_data_len, \
|
expected_data_len, \
|
||||||
expected_secrets, \
|
expected_secrets, \
|
||||||
|
|
@ -2393,14 +2466,16 @@ _do_read_vpn_details_impl1(const char *file,
|
||||||
} \
|
} \
|
||||||
G_STMT_END
|
G_STMT_END
|
||||||
|
|
||||||
#define _do_read_vpn_details_empty(str) _do_read_vpn_details_impl0(str, NULL, 0, NULL, 0, {})
|
#define _do_read_vpn_details_empty(str) \
|
||||||
|
_do_read_vpn_details_impl0(str, READ_VPN_DETAIL_TYPE_GOOD, NULL, 0, NULL, 0, {})
|
||||||
|
|
||||||
#define _do_read_vpn_details(str, expected_data, expected_secrets, pre_setup_cmd) \
|
#define _do_read_vpn_details(str, detail_type, expected_data, expected_secrets, pre_setup_cmd) \
|
||||||
_do_read_vpn_details_impl0(str, \
|
_do_read_vpn_details_impl0(str, \
|
||||||
expected_data, \
|
detail_type, \
|
||||||
G_N_ELEMENTS(expected_data), \
|
expected_data, \
|
||||||
expected_secrets, \
|
G_N_ELEMENTS(expected_data), \
|
||||||
G_N_ELEMENTS(expected_secrets), \
|
expected_secrets, \
|
||||||
|
G_N_ELEMENTS(expected_secrets), \
|
||||||
pre_setup_cmd)
|
pre_setup_cmd)
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
@ -2426,6 +2501,7 @@ test_nm_vpn_service_plugin_read_vpn_details(void)
|
||||||
"DONE\n"
|
"DONE\n"
|
||||||
"\n"
|
"\n"
|
||||||
"",
|
"",
|
||||||
|
READ_VPN_DETAIL_TYPE_GOOD,
|
||||||
READ_VPN_DETAIL_DATA({"some-key", "string"}, {"some-other-key", "val2"}, ),
|
READ_VPN_DETAIL_DATA({"some-key", "string"}, {"some-other-key", "val2"}, ),
|
||||||
READ_VPN_DETAIL_DATA({"some-secret", "val3"}, ), );
|
READ_VPN_DETAIL_DATA({"some-secret", "val3"}, ), );
|
||||||
|
|
||||||
|
|
@ -2433,6 +2509,7 @@ test_nm_vpn_service_plugin_read_vpn_details(void)
|
||||||
"DATA_KEY=some-key\n"
|
"DATA_KEY=some-key\n"
|
||||||
"DATA_VAL=string\n"
|
"DATA_VAL=string\n"
|
||||||
"DONE\n",
|
"DONE\n",
|
||||||
|
READ_VPN_DETAIL_TYPE_GOOD,
|
||||||
READ_VPN_DETAIL_DATA({"some-key", "string"}, ),
|
READ_VPN_DETAIL_DATA({"some-key", "string"}, ),
|
||||||
READ_VPN_DETAIL_DATA(), );
|
READ_VPN_DETAIL_DATA(), );
|
||||||
|
|
||||||
|
|
@ -2461,6 +2538,7 @@ test_nm_vpn_service_plugin_read_vpn_details(void)
|
||||||
"\n"
|
"\n"
|
||||||
"DONE\n"
|
"DONE\n"
|
||||||
"",
|
"",
|
||||||
|
READ_VPN_DETAIL_TYPE_BROKEN,
|
||||||
READ_VPN_DETAIL_DATA({"some-key", "string\ncontinued after a line break"}, ),
|
READ_VPN_DETAIL_DATA({"some-key", "string\ncontinued after a line break"}, ),
|
||||||
READ_VPN_DETAIL_DATA({"key names\ncan have\ncontinuations too", "value"}, ), );
|
READ_VPN_DETAIL_DATA({"key names\ncan have\ncontinuations too", "value"}, ), );
|
||||||
|
|
||||||
|
|
@ -2474,6 +2552,7 @@ test_nm_vpn_service_plugin_read_vpn_details(void)
|
||||||
"=continuations too\n"
|
"=continuations too\n"
|
||||||
"SECRET_VAL=value\n"
|
"SECRET_VAL=value\n"
|
||||||
"",
|
"",
|
||||||
|
READ_VPN_DETAIL_TYPE_NO_DONE,
|
||||||
READ_VPN_DETAIL_DATA({"some-key", "string\ncontinued after a line break"}, ),
|
READ_VPN_DETAIL_DATA({"some-key", "string\ncontinued after a line break"}, ),
|
||||||
READ_VPN_DETAIL_DATA({"key names\ncan have\ncontinuations too", "value"}, ), );
|
READ_VPN_DETAIL_DATA({"key names\ncan have\ncontinuations too", "value"}, ), );
|
||||||
|
|
||||||
|
|
@ -2507,6 +2586,7 @@ test_nm_vpn_service_plugin_read_vpn_details(void)
|
||||||
"DONE\n"
|
"DONE\n"
|
||||||
"\n"
|
"\n"
|
||||||
"",
|
"",
|
||||||
|
READ_VPN_DETAIL_TYPE_GOOD,
|
||||||
READ_VPN_DETAIL_DATA({"some\nkey-2", "val2"},
|
READ_VPN_DETAIL_DATA({"some\nkey-2", "val2"},
|
||||||
{"some-key", "string"},
|
{"some-key", "string"},
|
||||||
{"key3\nkey-2", "val3"}, ),
|
{"key3\nkey-2", "val3"}, ),
|
||||||
|
|
@ -2546,6 +2626,7 @@ test_nm_vpn_service_plugin_read_vpn_details(void)
|
||||||
"DONE\n"
|
"DONE\n"
|
||||||
"\n"
|
"\n"
|
||||||
"",
|
"",
|
||||||
|
READ_VPN_DETAIL_TYPE_GOOD,
|
||||||
READ_VPN_DETAIL_DATA({"some\nkey-2", "val2"},
|
READ_VPN_DETAIL_DATA({"some\nkey-2", "val2"},
|
||||||
{"some-key", "string"},
|
{"some-key", "string"},
|
||||||
{"key3\nkey-2", "val3"}, ),
|
{"key3\nkey-2", "val3"}, ),
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue