Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle escape sequence \_ correctly #89

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/89-quoting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "quoting and unquoting filter plugins, api module - handle the escape sequence ``\\_`` correctly as escaping a space and not an underscore (https://github.com/ansible-collections/community.routeros/pull/89)."
2 changes: 1 addition & 1 deletion plugins/module_utils/quoting.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ParseError(Exception):
b'\\': b'\\',
b'?': b'?',
b'$': b'$',
b'_': b'_',
b'_': b' ',
b'a': b'\a',
b'b': b'\b',
b'f': b'\xFF',
Expand Down
6 changes: 3 additions & 3 deletions tests/ee/roles/filter_quoting/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
- >
'a=b' | community.routeros.quote_argument == 'a=b'
- >
'a=b c' | community.routeros.quote_argument == 'a="b c"'
'a=b c' | community.routeros.quote_argument == 'a="b\\_c"'
- >
'a=""' | community.routeros.quote_argument == 'a="\\"\\""'

Expand All @@ -40,13 +40,13 @@
- >
'foo' | community.routeros.quote_argument_value == 'foo'
- >
'"foo bar"' | community.routeros.quote_argument_value == '"\\"foo bar\\""'
'"foo bar"' | community.routeros.quote_argument_value == '"\\"foo\\_bar\\""'

- name: "Test join filter"
assert:
that:
- >
['a=', 'b=c d'] | community.routeros.join == 'a="" b="c d"'
['a=', 'b=c d'] | community.routeros.join == 'a="" b="c\\_d"'

- name: "Test list_to_dict filter"
assert:
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/targets/filter_quoting/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
- >
'a=b' | community.routeros.quote_argument == 'a=b'
- >
'a=b c' | community.routeros.quote_argument == 'a="b c"'
'a=b c' | community.routeros.quote_argument == 'a="b\\_c"'
- >
'a=""' | community.routeros.quote_argument == 'a="\\"\\""'

Expand All @@ -40,13 +40,13 @@
- >
'foo' | community.routeros.quote_argument_value == 'foo'
- >
'"foo bar"' | community.routeros.quote_argument_value == '"\\"foo bar\\""'
'"foo bar"' | community.routeros.quote_argument_value == '"\\"foo\\_bar\\""'

- name: "Test join filter"
assert:
that:
- >
['a=', 'b=c d'] | community.routeros.join == 'a="" b="c d"'
['a=', 'b=c d'] | community.routeros.join == 'a="" b="c\\_d"'

- name: "Test list_to_dict filter"
assert:
Expand Down
17 changes: 9 additions & 8 deletions tests/unit/plugins/module_utils/test_quoting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
(r'"\\"', {}, ('\\', 4)),
(r'"\?"', {}, ('?', 4)),
(r'"\$"', {}, ('$', 4)),
(r'"\_"', {}, ('_', 4)),
(r'"\_"', {}, (' ', 4)),
(r'"\a"', {}, ('\a', 4)),
(r'"\b"', {}, ('\b', 4)),
(r'"\f"', {}, (to_native(b'\xff'), 4)),
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_convert_list_to_dictionary_errors(list, kwargs, message):


TEST_JOIN_ROUTEROS_COMMAND = [
(['a=b', 'c=d=e', 'e=', 'f', 'g=h i j', 'h="h"'], r'a=b c="d=e" e="" f g="h i j" h="\"h\""'),
(['a=b', 'c=d=e', 'e=', 'f', 'g=h i j', 'h="h"'], r'a=b c="d=e" e="" f g="h\_i\_j" h="\"h\""'),
]


Expand All @@ -180,8 +180,8 @@ def test_join_routeros_command(list, expected):
(r'', r''),
(r'a', r'a'),
(r'a=b', r'a=b'),
(r'a=b c', r'a="b c"'),
(r'a="b c"', r'a="\"b c\""'),
(r'a=b c', r'a="b\_c"'),
(r'a="b c"', r'a="\"b\_c\""'),
(r"a='b", "a=\"'b\""),
(r"a=b'", "a=\"b'\""),
(r'a=""', r'a="\"\""'),
Expand Down Expand Up @@ -212,19 +212,20 @@ def test_quote_routeros_argument_errors(argument, message):
TEST_QUOTE_ROUTEROS_ARGUMENT_VALUE = [
(r'', r'""'),
(r";", r'";"'),
(r" ", r'" "'),
(r" ", r'"\_"'),
(r"=", r'"="'),
(r'a', r'a'),
(r'a=b', r'"a=b"'),
(r'b c', r'"b c"'),
(r'"b c"', r'"\"b c\""'),
(r'b c', r'"b\_c"'),
(r'"b c"', r'"\"b\_c\""'),
("'b", "\"'b\""),
("b'", "\"b'\""),
('"', r'"\""'),
('\\', r'"\\"'),
('?', r'"\?"'),
('$', r'"\$"'),
('_', r'"\_"'),
('_', r'_'),
(' ', r'"\_"'),
('\a', r'"\a"'),
('\b', r'"\b"'),
# (to_native(b'\xff'), r'"\f"'),
Expand Down