Debian Codegraph @

ansible_collections/community/routeros/tests/unit/plugins/module_utils/test_quoting.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2021, Felix Fontein (@felixfontein) <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 import pytest
11
12 from ansible.module_utils.common.text.converters import to_native
13
14 from ansible_collections.community.routeros.plugins.module_utils.quoting import (
15 ParseError,
16 convert_list_to_dictionary,
17 join_routeros_command,
18 parse_argument_value,
19 quote_routeros_argument,
20 quote_routeros_argument_value,
21 split_routeros_command,
22 )
23
24
25 TEST_PARSE_ARGUMENT_VALUE = [
26 ('a', {}, ('a', 1)),
27 ('a ', {'must_match_everything': False}, ('a', 1)),
28 (r'"a b"', {}, ('a b', 5)),
29 (r'"b\"f"', {}, ('b"f', 6)),
30 (r'"\01"', {}, ('\x01', 5)),
31 (r'"\1F"', {}, ('\x1f', 5)),
32 (r'"\FF"', {}, (to_native(b'\xff'), 5)),
33 (r'"\"e"', {}, ('"e', 5)),
34 (r'"\""', {}, ('"', 4)),
35 (r'"\\"', {}, ('\\', 4)),
36 (r'"\?"', {}, ('?', 4)),
37 (r'"\$"', {}, ('$', 4)),
38 (r'"\_"', {}, (' ', 4)),
39 (r'"\a"', {}, ('\a', 4)),
40 (r'"\b"', {}, ('\b', 4)),
41 (r'"\f"', {}, (to_native(b'\xff'), 4)),
42 (r'"\n"', {}, ('\n', 4)),
43 (r'"\r"', {}, ('\r', 4)),
44 (r'"\t"', {}, ('\t', 4)),
45 (r'"\v"', {}, ('\v', 4)),
46 (r'"b=c"', {}, ('b=c', 5)),
47 (r'""', {}, ('', 2)),
48 (r'"" ', {'must_match_everything': False}, ('', 2)),
49 ("'e", {'start_index': 1}, ('e', 2)),
50 ]
51
52
53 @pytest.mark.parametrize("command, kwargs, result", TEST_PARSE_ARGUMENT_VALUE)
54 def test_parse_argument_value(command, kwargs, result):
55 result_ = parse_argument_value(command, **kwargs)
56 print(result_, result)
57 assert result_ == result
58
59
60 TEST_PARSE_ARGUMENT_VALUE_ERRORS = [
61 (r'"e', {}, 'Unexpected end of string during escaped parameter'),
62 ("'e", {}, '"\'" can only be used inside double quotes'),
63 (r'\FF', {}, 'Escape sequences can only be used inside double quotes'),
64 (r'\"e', {}, 'Escape sequences can only be used inside double quotes'),
65 ('e=f', {}, '"=" can only be used inside double quotes'),
66 ('e$', {}, '"$" can only be used inside double quotes'),
67 ('e(', {}, '"(" can only be used inside double quotes'),
68 ('e)', {}, '")" can only be used inside double quotes'),
69 ('e[', {}, '"[" can only be used inside double quotes'),
70 ('e{', {}, '"{" can only be used inside double quotes'),
71 ('e`', {}, '"`" can only be used inside double quotes'),
72 ('?', {}, '"?" can only be used in escaped form'),
73 (r'b"', {}, '\'"\' must not appear in an unquoted value'),
74 (r'""a', {}, "Ending '\"' must be followed by space or end of string"),
75 (r'"" ', {}, "Unexpected data at end of value"),
76 ('"\\', {}, r"'\' must not be at the end of the line"),
77 (r'"\A', {}, r'Hex escape sequence cut off at end of line'),
78 (r'"\Z"', {}, r"Invalid escape sequence '\Z'"),
79 (r'"\Aa"', {}, r"Invalid hex escape sequence '\Aa'"),
80 ]
81
82
83 @pytest.mark.parametrize("command, kwargs, message", TEST_PARSE_ARGUMENT_VALUE_ERRORS)
84 def test_parse_argument_value_errors(command, kwargs, message):
85 with pytest.raises(ParseError) as exc:
86 parse_argument_value(command, **kwargs)
87 print(exc.value.args[0], message)
88 assert exc.value.args[0] == message
89
90
91 TEST_SPLIT_ROUTEROS_COMMAND = [
92 ('', []),
93 (' ', []),
94 (r'a b c', ['a', 'b', 'c']),
95 (r'a=b c d=e', ['a=b', 'c', 'd=e']),
96 (r'a="b f" c d=e', ['a=b f', 'c', 'd=e']),
97 (r'a="b\"f" c="\FF" d="\"e"', ['a=b"f', to_native(b'c=\xff'), 'd="e']),
98 (r'a="b=c"', ['a=b=c']),
99 (r'a=b ', ['a=b']),
100 ]
101
102
103 @pytest.mark.parametrize("command, result", TEST_SPLIT_ROUTEROS_COMMAND)
104 def test_split_routeros_command(command, result):
105 result_ = split_routeros_command(command)
106 print(result_, result)
107 assert result_ == result
108
109
110 TEST_SPLIT_ROUTEROS_COMMAND_ERRORS = [
111 (r'a=', 'Expected value, but found end of string'),
112 (r'a="b\"f" d="e', 'Unexpected end of string during escaped parameter'),
113 ('d=\'e', '"\'" can only be used inside double quotes'),
114 (r'c\FF', r'Found unexpected "\"'),
115 (r'd=\"e', 'Escape sequences can only be used inside double quotes'),
116 ('d=e=f', '"=" can only be used inside double quotes'),
117 ('d=e$', '"$" can only be used inside double quotes'),
118 ('d=e(', '"(" can only be used inside double quotes'),
119 ('d=e)', '")" can only be used inside double quotes'),
120 ('d=e[', '"[" can only be used inside double quotes'),
121 ('d=e{', '"{" can only be used inside double quotes'),
122 ('d=e`', '"`" can only be used inside double quotes'),
123 ('d=?', '"?" can only be used in escaped form'),
124 (r'a=b"', '\'"\' must not appear in an unquoted value'),
125 (r'a=""a', "Ending '\"' must be followed by space or end of string"),
126 ('a="\\', r"'\' must not be at the end of the line"),
127 (r'a="\Z', r"Invalid escape sequence '\Z'"),
128 (r'a="\Aa', r"Invalid hex escape sequence '\Aa'"),
129 ]
130
131
132 @pytest.mark.parametrize("command, message", TEST_SPLIT_ROUTEROS_COMMAND_ERRORS)
133 def test_split_routeros_command_errors(command, message):
134 with pytest.raises(ParseError) as exc:
135 split_routeros_command(command)
136 print(exc.value.args[0], message)
137 assert exc.value.args[0] == message
138
139
140 TEST_CONVERT_LIST_TO_DICTIONARY = [
141 (['a=b', 'c=d=e', 'e='], {}, {'a': 'b', 'c': 'd=e', 'e': ''}),
142 (['a=b', 'c=d=e', 'e='], {'skip_empty_values': False}, {'a': 'b', 'c': 'd=e', 'e': ''}),
143 (['a=b', 'c=d=e', 'e='], {'skip_empty_values': True}, {'a': 'b', 'c': 'd=e'}),
144 (['a=b', 'c=d=e', 'e=', 'f'], {'require_assignment': False}, {'a': 'b', 'c': 'd=e', 'e': '', 'f': None}),
145 ]
146
147
148 @pytest.mark.parametrize("list, kwargs, expected_dict", TEST_CONVERT_LIST_TO_DICTIONARY)
149 def test_convert_list_to_dictionary(list, kwargs, expected_dict):
150 result = convert_list_to_dictionary(list, **kwargs)
151 print(result, expected_dict)
152 assert result == expected_dict
153
154
155 TEST_CONVERT_LIST_TO_DICTIONARY_ERRORS = [
156 (['a=b', 'c=d=e', 'e=', 'f'], {}, "missing '=' after 'f'"),
157 ]
158
159
160 @pytest.mark.parametrize("list, kwargs, message", TEST_CONVERT_LIST_TO_DICTIONARY_ERRORS)
161 def test_convert_list_to_dictionary_errors(list, kwargs, message):
162 with pytest.raises(ParseError) as exc:
163 result = convert_list_to_dictionary(list, **kwargs)
164 print(exc.value.args[0], message)
165 assert exc.value.args[0] == message
166
167
168 TEST_JOIN_ROUTEROS_COMMAND = [
169 (['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\""'),
170 ]
171
172
173 @pytest.mark.parametrize("list, expected", TEST_JOIN_ROUTEROS_COMMAND)
174 def test_join_routeros_command(list, expected):
175 result = join_routeros_command(list)
176 print(result, expected)
177 assert result == expected
178
179
180 TEST_QUOTE_ROUTEROS_ARGUMENT = [
181 (r'', r''),
182 (r'a', r'a'),
183 (r'a=b', r'a=b'),
184 (r'a=b c', r'a="b\_c"'),
185 (r'a="b c"', r'a="\"b\_c\""'),
186 (r"a='b", "a=\"'b\""),
187 (r"a=b'", "a=\"b'\""),
188 (r'a=""', r'a="\"\""'),
189 ]
190
191
192 @pytest.mark.parametrize("argument, expected", TEST_QUOTE_ROUTEROS_ARGUMENT)
193 def test_quote_routeros_argument(argument, expected):
194 result = quote_routeros_argument(argument)
195 print(result, expected)
196 assert result == expected
197
198
199 TEST_QUOTE_ROUTEROS_ARGUMENT_ERRORS = [
200 ('a b', 'Attribute names must not contain spaces'),
201 ('a b=c', 'Attribute names must not contain spaces'),
202 ]
203
204
205 @pytest.mark.parametrize("argument, message", TEST_QUOTE_ROUTEROS_ARGUMENT_ERRORS)
206 def test_quote_routeros_argument_errors(argument, message):
207 with pytest.raises(ParseError) as exc:
208 result = quote_routeros_argument(argument)
209 print(exc.value.args[0], message)
210 assert exc.value.args[0] == message
211
212
213 TEST_QUOTE_ROUTEROS_ARGUMENT_VALUE = [
214 (r'', r'""'),
215 (r";", r'";"'),
216 (r" ", r'"\_"'),
217 (r"=", r'"="'),
218 (r'a', r'a'),
219 (r'a=b', r'"a=b"'),
220 (r'b c', r'"b\_c"'),
221 (r'"b c"', r'"\"b\_c\""'),
222 ("'b", "\"'b\""),
223 ("b'", "\"b'\""),
224 ('"', r'"\""'),
225 ('\\', r'"\\"'),
226 ('?', r'"\?"'),
227 ('$', r'"\$"'),
228 ('_', r'_'),
229 (' ', r'"\_"'),
230 ('\a', r'"\a"'),
231 ('\b', r'"\b"'),
232 # (to_native(b'\xff'), r'"\f"'),
233 ('\n', r'"\n"'),
234 ('\r', r'"\r"'),
235 ('\t', r'"\t"'),
236 ('\v', r'"\v"'),
237 ('\x01', r'"\01"'),
238 ('\x1f', r'"\1F"'),
239 ]
240
241
242 @pytest.mark.parametrize("argument, expected", TEST_QUOTE_ROUTEROS_ARGUMENT_VALUE)
243 def test_quote_routeros_argument_value(argument, expected):
244 result = quote_routeros_argument_value(argument)
245 print(result, expected)
246 assert result == expected
247
248
249 TEST_ROUNDTRIP = [
250 {'a': 'b', 'c': 'd'},
251 {'script': ''':local host value=[/system identity get name];
252 :local date value=[/system clock get date];
253 :local day [ :pick $date 4 6 ];
254 :local month [ :pick $date 0 3 ];
255 :local year [ :pick $date 7 11 ];
256 :local name value=($host."-".$day."-".$month."-".$year);
257 /system backup save name=$name;
258 /export file=$name;
259 /tool fetch address="192.168.1.1" user=ros password="PASSWORD" mode=ftp dst-path=("/mikrotik/rsc/".$name.".rsc") src-path=($name.".rsc") upload=yes;
260 /tool fetch address="192.168.1.1" user=ros password="PASSWORD" mode=ftp dst-path=("/mikrotik/backup/".$name.".backup") src-path=($name.".backup") upload=yes;
261 '''},
262 ]
263
264
265 @pytest.mark.parametrize("dictionary", TEST_ROUNDTRIP)
266 def test_roundtrip(dictionary):
267 argument_list = ['%s=%s' % (k, v) for k, v in dictionary.items()]
268 command = join_routeros_command(argument_list)
269 resplit_list = split_routeros_command(command)
270 print(resplit_list, argument_list)
271 assert resplit_list == argument_list
272 re_dictionary = convert_list_to_dictionary(resplit_list)
273 print(re_dictionary, dictionary)
274 assert re_dictionary == dictionary