mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-03 10:10:28 +01:00
cli: fix property matching
@ret was not initialized when there was only one partial match.
Also, refactor the code to return all matching values.
Fixes: 3fd9bf9d7d
https://github.com/NetworkManager/NetworkManager/pull/123
This commit is contained in:
parent
af94687161
commit
1f7780cba9
9 changed files with 120 additions and 17 deletions
|
|
@ -175,11 +175,10 @@ nmc_string_is_valid (const char *input, const char **allowed, GError **error)
|
|||
{
|
||||
const char **p;
|
||||
size_t input_ln, p_len;
|
||||
const char *partial_match = NULL;
|
||||
gboolean ambiguous = FALSE;
|
||||
const char *prev_match = NULL;
|
||||
const char *ret = NULL;
|
||||
|
||||
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
||||
g_return_val_if_fail (!error || !*error, NULL);
|
||||
|
||||
if (!input || !*input)
|
||||
goto finish;
|
||||
|
|
@ -188,27 +187,34 @@ nmc_string_is_valid (const char *input, const char **allowed, GError **error)
|
|||
for (p = allowed; p && *p; p++) {
|
||||
p_len = strlen (*p);
|
||||
if (g_ascii_strncasecmp (input, *p, input_ln) == 0) {
|
||||
if (input_ln == p_len) {
|
||||
ret = *p;
|
||||
ambiguous = FALSE;
|
||||
break;
|
||||
}
|
||||
if (!prev_match) {
|
||||
prev_match = *p;
|
||||
} else {
|
||||
ret = *p;
|
||||
if (input_ln == p_len)
|
||||
return *p;
|
||||
if (!partial_match)
|
||||
partial_match = *p;
|
||||
else
|
||||
ambiguous = TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ambiguous) {
|
||||
g_set_error (error, 1, 1, _("'%s' is ambiguous (%s x %s)"),
|
||||
input, prev_match, ret);
|
||||
GString *candidates = g_string_new ("");
|
||||
|
||||
for (p = allowed; *p; p++) {
|
||||
if (g_ascii_strncasecmp (input, *p, input_ln) == 0) {
|
||||
if (candidates->len > 0)
|
||||
g_string_append (candidates, ", ");
|
||||
g_string_append (candidates, *p);
|
||||
}
|
||||
}
|
||||
g_set_error (error, 1, 1, _("'%s' is ambiguous: %s"),
|
||||
input, candidates->str);
|
||||
g_string_free (candidates, TRUE);
|
||||
return NULL;
|
||||
}
|
||||
finish:
|
||||
if (ret == NULL) {
|
||||
if (!partial_match) {
|
||||
char *valid_vals = g_strjoinv (", ", (char **) allowed);
|
||||
|
||||
if (!input || !*input)
|
||||
g_set_error (error, 1, 0, _("missing name, try one of [%s]"), valid_vals);
|
||||
else
|
||||
|
|
@ -216,7 +222,8 @@ finish:
|
|||
|
||||
g_free (valid_vals);
|
||||
}
|
||||
return ret;
|
||||
|
||||
return partial_match;
|
||||
}
|
||||
|
||||
gboolean
|
||||
|
|
|
|||
|
|
@ -105,4 +105,10 @@ clients_tests_expected_files = \
|
|||
clients/tests/test-client.check-on-disk/test_003-052.expected \
|
||||
clients/tests/test-client.check-on-disk/test_003-053.expected \
|
||||
clients/tests/test-client.check-on-disk/test_003-054.expected \
|
||||
clients/tests/test-client.check-on-disk/test_004-001.expected \
|
||||
clients/tests/test-client.check-on-disk/test_004-002.expected \
|
||||
clients/tests/test-client.check-on-disk/test_004-003.expected \
|
||||
clients/tests/test-client.check-on-disk/test_004-004.expected \
|
||||
clients/tests/test-client.check-on-disk/test_004-005.expected \
|
||||
clients/tests/test-client.check-on-disk/test_004-006.expected \
|
||||
$(NULL)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
location: clients/tests/test-client.py:722:test_004()/1
|
||||
cmd: $NMCLI c add type wifi ifname '*' ssid foobar con-name con-xx1
|
||||
lang: C
|
||||
returncode: 0
|
||||
stdout: 80 bytes
|
||||
>>>
|
||||
Connection 'con-xx1' (UUID-con-xx1-REPLACED-REPLACED-REPLA) successfully added.
|
||||
|
||||
<<<
|
||||
stderr: 0 bytes
|
||||
>>>
|
||||
|
||||
<<<
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
location: clients/tests/test-client.py:724:test_004()/2
|
||||
cmd: $NMCLI connection mod con-xx1 ip.gateway ''
|
||||
lang: C
|
||||
returncode: 2
|
||||
stdout: 0 bytes
|
||||
>>>
|
||||
|
||||
<<<
|
||||
stderr: 75 bytes
|
||||
>>>
|
||||
Error: invalid or not allowed setting 'ip': 'ip' is ambiguous: ipv4, ipv6.
|
||||
|
||||
<<<
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
location: clients/tests/test-client.py:725:test_004()/3
|
||||
cmd: $NMCLI connection mod con-xx1 ipv4.gateway 172.16.0.1
|
||||
lang: C
|
||||
returncode: 0
|
||||
stdout: 0 bytes
|
||||
>>>
|
||||
|
||||
<<<
|
||||
stderr: 0 bytes
|
||||
>>>
|
||||
|
||||
<<<
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
location: clients/tests/test-client.py:726:test_004()/4
|
||||
cmd: $NMCLI connection mod con-xx1 ipv6.gateway ::99
|
||||
lang: C
|
||||
returncode: 0
|
||||
stdout: 0 bytes
|
||||
>>>
|
||||
|
||||
<<<
|
||||
stderr: 0 bytes
|
||||
>>>
|
||||
|
||||
<<<
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
location: clients/tests/test-client.py:727:test_004()/5
|
||||
cmd: $NMCLI connection mod con-xx1 802.abc ''
|
||||
lang: C
|
||||
returncode: 2
|
||||
stdout: 0 bytes
|
||||
>>>
|
||||
|
||||
<<<
|
||||
stderr: 116 bytes
|
||||
>>>
|
||||
Error: invalid or not allowed setting '802': '802' is ambiguous: 802-11-wireless, 802-11-wireless-security, 802-1x.
|
||||
|
||||
<<<
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
location: clients/tests/test-client.py:728:test_004()/6
|
||||
cmd: $NMCLI connection mod con-xx1 802-11-wireless.band a
|
||||
lang: C
|
||||
returncode: 0
|
||||
stdout: 0 bytes
|
||||
>>>
|
||||
|
||||
<<<
|
||||
stderr: 0 bytes
|
||||
>>>
|
||||
|
||||
<<<
|
||||
|
|
@ -711,6 +711,21 @@ class TestNmcli(NmTestBase):
|
|||
self.call_nmcli_l(['con', 's', 'ethernet'],
|
||||
replace_stdout = replace_stdout)
|
||||
|
||||
def test_004(self):
|
||||
self.init_001()
|
||||
|
||||
replace_stdout = []
|
||||
|
||||
replace_stdout.append((lambda: self.srv.findConnectionUuid('con-xx1'), 'UUID-con-xx1-REPLACED-REPLACED-REPLA'))
|
||||
|
||||
self.call_nmcli(['c', 'add', 'type', 'wifi', 'ifname', '*', 'ssid', 'foobar', 'con-name', 'con-xx1'],
|
||||
replace_stdout = replace_stdout)
|
||||
|
||||
self.call_nmcli(['connection', 'mod', 'con-xx1', 'ip.gateway', ''])
|
||||
self.call_nmcli(['connection', 'mod', 'con-xx1', 'ipv4.gateway', '172.16.0.1'])
|
||||
self.call_nmcli(['connection', 'mod', 'con-xx1', 'ipv6.gateway', '::99'])
|
||||
self.call_nmcli(['connection', 'mod', 'con-xx1', '802.abc', ''])
|
||||
self.call_nmcli(['connection', 'mod', 'con-xx1', '802-11-wireless.band', 'a'])
|
||||
|
||||
###############################################################################
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue