summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBeniamino Galvani <bgalvani@redhat.com>2018-05-25 18:00:51 +0200
committerBeniamino Galvani <bgalvani@redhat.com>2018-05-28 11:33:40 +0200
commit1f7780cba9a7446e04c32a765fa72585829c0eec (patch)
tree18e972f386ac19b190189796e3bfacc4fd8fcab7
parentaf946871614db6fdf2b46ae2c909aa63fdaf1337 (diff)
downloadNetworkManager-1f7780cba9a7446e04c32a765fa72585829c0eec.tar.gz
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: 3fd9bf9d7d9fc0290fd25f709b60a3a8f5c7e334 https://github.com/NetworkManager/NetworkManager/pull/123
-rw-r--r--clients/common/nm-client-utils.c41
-rw-r--r--clients/tests/test-client.check-on-disk/Makefile.am6
-rw-r--r--clients/tests/test-client.check-on-disk/test_004-001.expected13
-rw-r--r--clients/tests/test-client.check-on-disk/test_004-002.expected13
-rw-r--r--clients/tests/test-client.check-on-disk/test_004-003.expected12
-rw-r--r--clients/tests/test-client.check-on-disk/test_004-004.expected12
-rw-r--r--clients/tests/test-client.check-on-disk/test_004-005.expected13
-rw-r--r--clients/tests/test-client.check-on-disk/test_004-006.expected12
-rwxr-xr-xclients/tests/test-client.py15
9 files changed, 120 insertions, 17 deletions
diff --git a/clients/common/nm-client-utils.c b/clients/common/nm-client-utils.c
index 257053093e..fec6e33b8f 100644
--- a/clients/common/nm-client-utils.c
+++ b/clients/common/nm-client-utils.c
@@ -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
diff --git a/clients/tests/test-client.check-on-disk/Makefile.am b/clients/tests/test-client.check-on-disk/Makefile.am
index 50897f8e1f..aa49eda643 100644
--- a/clients/tests/test-client.check-on-disk/Makefile.am
+++ b/clients/tests/test-client.check-on-disk/Makefile.am
@@ -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)
diff --git a/clients/tests/test-client.check-on-disk/test_004-001.expected b/clients/tests/test-client.check-on-disk/test_004-001.expected
new file mode 100644
index 0000000000..2999fa2f5f
--- /dev/null
+++ b/clients/tests/test-client.check-on-disk/test_004-001.expected
@@ -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
+>>>
+
+<<<
diff --git a/clients/tests/test-client.check-on-disk/test_004-002.expected b/clients/tests/test-client.check-on-disk/test_004-002.expected
new file mode 100644
index 0000000000..1eb463d439
--- /dev/null
+++ b/clients/tests/test-client.check-on-disk/test_004-002.expected
@@ -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.
+
+<<<
diff --git a/clients/tests/test-client.check-on-disk/test_004-003.expected b/clients/tests/test-client.check-on-disk/test_004-003.expected
new file mode 100644
index 0000000000..61d22b9bb5
--- /dev/null
+++ b/clients/tests/test-client.check-on-disk/test_004-003.expected
@@ -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
+>>>
+
+<<<
diff --git a/clients/tests/test-client.check-on-disk/test_004-004.expected b/clients/tests/test-client.check-on-disk/test_004-004.expected
new file mode 100644
index 0000000000..6969c2340a
--- /dev/null
+++ b/clients/tests/test-client.check-on-disk/test_004-004.expected
@@ -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
+>>>
+
+<<<
diff --git a/clients/tests/test-client.check-on-disk/test_004-005.expected b/clients/tests/test-client.check-on-disk/test_004-005.expected
new file mode 100644
index 0000000000..f340fd089a
--- /dev/null
+++ b/clients/tests/test-client.check-on-disk/test_004-005.expected
@@ -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.
+
+<<<
diff --git a/clients/tests/test-client.check-on-disk/test_004-006.expected b/clients/tests/test-client.check-on-disk/test_004-006.expected
new file mode 100644
index 0000000000..51c4d89735
--- /dev/null
+++ b/clients/tests/test-client.check-on-disk/test_004-006.expected
@@ -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
+>>>
+
+<<<
diff --git a/clients/tests/test-client.py b/clients/tests/test-client.py
index 7414bb51bb..e9642ef4f3 100755
--- a/clients/tests/test-client.py
+++ b/clients/tests/test-client.py
@@ -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'])
###############################################################################