File: test_commandline.py

package info (click to toggle)
aiocoap 0.4.17-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,040 kB
  • sloc: python: 17,241; makefile: 23; sh: 9
file content (451 lines) | stat: -rw-r--r-- 16,445 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors
#
# SPDX-License-Identifier: MIT

"""This tests launch the command line utility aiocoap-client in a sub-process.

The aiocoap-proxy utility is tested in test_proxy inside this process as
orchestration of success reporting is not that easy with a daemon process;
aiocoap-rd might need to get tested in a similar way to -proxy."""

import asyncio
import subprocess
import unittest
import os

import aiocoap.defaults

from .test_server import WithTestServer, no_warnings
from .common import PYTHON_PREFIX, using_simple6, in_woodpecker

linkheader_modules = aiocoap.defaults.linkheader_missing_modules()
prettyprinting_modules = aiocoap.defaults.prettyprint_missing_modules()

AIOCOAP_CLIENT = PYTHON_PREFIX + ['/usr/bin/aiocoap-client']
AIOCOAP_RD = PYTHON_PREFIX + ['/usr/bin/aiocoap-rd']


async def check_output(*args, **kwargs):
    return (await check_both(*args, **kwargs))[0]


async def check_stderr(*args, **kwargs):
    return (await check_both(*args, **kwargs))[1]


async def check_both(
    args, *, stdin_buffer=None, stderr=subprocess.PIPE, env=None, expected_returncode=0
):
    proc = await asyncio.create_subprocess_exec(
        *args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=stderr, env=env
    )
    (stdout, stderr) = await proc.communicate(stdin_buffer)
    if proc.returncode != expected_returncode:
        raise subprocess.CalledProcessError(
            cmd=args, returncode=proc.returncode, output=stdout, stderr=stderr
        )
    return (stdout, stderr)


class TestCommandlineClient(WithTestServer):
    @no_warnings
    async def test_help(self):
        # CI environments often don't have any locale set. That's not
        # representative of the interactive environments that run --help
        # (though it may be for others). Setting C.UTF-8 because at least pypy3
        # 7.3 doesn't default to a UTF-8 enabled mode, and the help output is
        # not just ASCII.
        helptext = await check_output(
            AIOCOAP_CLIENT + ["--help"], env={"LANG": "C.UTF-8"}
        )
        # We can't test for "usage: aiocoap-client" because starting 3.14,
        # output is colored.
        self.assertTrue(
            b"usage:" in helptext
            and b"Content format of the --payload data." in helptext
        )

    @no_warnings
    async def test_get(self):
        empty_default = await check_output(
            AIOCOAP_CLIENT + ["coap://" + self.servernetloc + "/empty"]
        )
        self.assertEqual(empty_default, b"")

        empty_json = await check_output(
            AIOCOAP_CLIENT
            + [
                "coap://" + self.servernetloc + "/empty",
                "--accept",
                "application/json",
                "--quiet",
            ]
        )
        self.assertEqual(empty_json, b"{}")

        verbose = await check_output(
            AIOCOAP_CLIENT + ["coap://" + self.servernetloc + "/empty", "-vv"],
            stderr=subprocess.STDOUT,
        )
        verbose = verbose.decode("utf-8").strip().split("\n")
        # Filtering out regular `-v` output, which is intended for users.
        info_from_cli = [l for l in verbose if ":coap.aiocoap-client:" in l]
        info_from_library = [l for l in verbose if l not in info_from_cli]
        # It'd not be actually wrong to have info level messages in here, but
        # they should at least not start appearing unnoticed.
        self.assertEqual(
            info_from_library, [], "Unexpected info-level messages in simple request"
        )
        # Precise format may vary
        self.assertTrue(
            any("Uri-Path (11): 'empty'" in l for l in info_from_cli),
            f"-v should include human-redable form of request (but is just {info_from_cli})",
        )

        debug = await check_output(
            AIOCOAP_CLIENT
            + [
                "coap://" + self.servernetloc + "/empty",
                "-v",
                "-v",
                "-v",
                "--no-color",
            ],
            stderr=subprocess.STDOUT,
        )
        self.assertTrue(
            b"DEBUG:coap:Incoming message" in debug,
            "Not even some (or unexpected) output in aiocoap-client -vvv",
        )

        quiet = await check_output(
            AIOCOAP_CLIENT + ["coap://" + self.servernetloc + "/empty", "--quiet"],
            stderr=subprocess.STDOUT,
        )
        self.assertEqual(quiet, b"")

        explicit_code = await check_output(
            AIOCOAP_CLIENT + ["coap://" + self.servernetloc + "/empty", "-m1"]
        )
        self.assertEqual(explicit_code, b"")

        if not prettyprinting_modules:
            json_formatted = await check_output(
                AIOCOAP_CLIENT
                + [
                    "coap://" + self.servernetloc + "/answer",
                    "--accept",
                    "application/json",
                    "--pretty-print",
                ]
            )
            # Concrete formatting may vary, but it should be indented
            self.assertEqual(
                json_formatted.replace(b"\r", b""), b'{\n    "answer": 42\n}'
            )

            json_colorformatted = await check_output(
                AIOCOAP_CLIENT
                + [
                    "coap://" + self.servernetloc + "/answer",
                    "--accept",
                    "application/json",
                    "--pretty-print",
                    "--color",
                ]
            )
            self.assertTrue(
                b"\x1b[" in json_colorformatted,
                "No color indication in pretty-printed JSON",
            )
            self.assertTrue(
                b"    " in json_colorformatted,
                "No indentation in color-pretty-printed JSON",
            )

            json_coloronly = await check_output(
                AIOCOAP_CLIENT
                + [
                    "coap://" + self.servernetloc + "/answer",
                    "--accept",
                    "application/json",
                    "--color",
                ]
            )
            self.assertTrue(
                b"\x1b[" in json_coloronly, "No color indication in color-printed JSON"
            )
            self.assertTrue(
                b"    " not in json_coloronly, "Indentation in color-printed JSON"
            )

            cbor_formatted = await check_output(
                AIOCOAP_CLIENT
                + [
                    "coap://" + self.servernetloc + "/answer",
                    "--accept",
                    "application/cbor",
                    "--pretty-print",
                ]
            )
            # Concrete formatting depends on cbor-diag package
            self.assertEqual(cbor_formatted, b'{"answer": 42}')

    @no_warnings
    async def test_post(self):
        replace_foo = await check_output(
            AIOCOAP_CLIENT
            + [
                "coap://" + self.servernetloc + "/replacing/one",
                "-m",
                "post",
                "--payload",
                "f00",
            ]
        )
        self.assertEqual(replace_foo, b"fOO")

        replace_file = await check_output(
            AIOCOAP_CLIENT
            + [
                "coap://" + self.servernetloc + "/replacing/one",
                "-m",
                "post",
                "--payload",
                "@" + os.devnull,
            ]
        )
        self.assertEqual(replace_file, b"")

    @unittest.skipIf(
        prettyprinting_modules,
        "Modules missing for running pretty-printing tests: %s"
        % (prettyprinting_modules,),
    )
    @no_warnings
    async def test_pretty_post(self):
        # POSTing CBOR is very risky, but it works for this value and allows
        # testing, in one go, the parsing of payload based on content-format,
        # and the rendering of output based on the Accept
        replace_cbor = await check_output(
            AIOCOAP_CLIENT
            + [
                "coap://" + self.servernetloc + "/replacing/one",
                "-m",
                "post",
                "--content-format",
                "application/cbor",
                "--accept",
                "application/octet-stream",
                "--pretty-print",
                "--payload",
                '["f00"]',
            ]
        )
        self.assertEqual(
            replace_cbor,
            b"00000000  81 63 66 4f 4f                                    |.cfOO|\n00000005\n",
        )

    @no_warnings
    async def test_location(self):
        diagnostic_post = await check_output(
            AIOCOAP_CLIENT
            + [
                "coap://" + self.servernetloc + "/create/",
                "-m",
                "post",
            ],
            stderr=subprocess.STDOUT,
        )
        # Or similar; what matters is that the URI is properly recomposed
        self.assertEqual(
            b"Location options indicate new resource: /create/here/?this=this&that=that\n",
            diagnostic_post,
        )

    @no_warnings
    async def test_interactive(self):
        interactive_out = await check_output(
            AIOCOAP_CLIENT
            + [
                "--interactive",
            ],
            stdin_buffer=f"coap://{self.servernetloc}/create/ -m post\ncoap://{self.servernetloc}/empty\n".encode(
                "utf8"
            ),
            stderr=subprocess.STDOUT,
        )
        # Or similar, point is it should be plausible
        self.assertEqual(
            b"aiocoap> Location options indicate new resource: /create/here/?this=this&that=that\naiocoap> aiocoap> ",
            interactive_out,
        )

    @no_warnings
    async def test_erroneous(self):
        with self.assertRaises(subprocess.CalledProcessError):
            # non-existent method
            await check_output(
                AIOCOAP_CLIENT + ["coap://" + self.servernetloc + "/empty", "-mSPAM"],
                stderr=subprocess.STDOUT,
            )

        with self.assertRaises(subprocess.CalledProcessError):
            # not a URI
            await check_output(
                AIOCOAP_CLIENT + ["coap::://" + self.servernetloc + "/empty"],
                stderr=subprocess.STDOUT,
            )

        with self.assertRaises(subprocess.CalledProcessError):
            # relative URI
            await check_output(AIOCOAP_CLIENT + ["/empty"], stderr=subprocess.STDOUT)

        with self.assertRaises(subprocess.CalledProcessError):
            # non-existent mime type
            await check_output(
                AIOCOAP_CLIENT
                + ["coap://" + self.servernetloc + "/empty", "--accept", "spam/eggs"],
                stderr=subprocess.STDOUT,
            )

        try:
            # No full URI given
            await check_output(
                AIOCOAP_CLIENT + [self.servernetloc + "/empty"],
                stderr=subprocess.STDOUT,
            )
        except subprocess.CalledProcessError as e:
            self.assertTrue(
                "URL incomplete: Must start with a scheme." in e.output.decode("utf8")
            )
            # It must also show the extra_help
            self.assertTrue(
                "Most URLs in aiocoap need to be given with a scheme"
                in e.output.decode("utf8")
            )
        else:
            raise AssertionError(
                "Calling aiocoap-client without a full URI should fail."
            )

        try:
            await check_output(
                AIOCOAP_CLIENT + ["http://" + self.servernetloc + "/empty"],
                stderr=subprocess.STDOUT,
            )
        except subprocess.CalledProcessError as e:
            self.assertTrue(
                "No remote endpoint set for request" in e.output.decode("utf8")
            )
            # Extra help even gives concrete output
            self.assertTrue(
                f"The message is set up for use with a proxy (because the scheme of 'http://{self.servernetloc}/empty' is not supported)"
                in e.output.decode("utf8")
            )
        else:
            raise AssertionError(
                "Calling aiocoap-client without a HTTP URI should fail."
            )

    @no_warnings
    async def test_noproxy(self):
        # Having this successful and just return text is a bespoke weirdness of
        # the /empty resource (and MultiRepresentationResource in general).
        # Once https://github.com/chrysn/aiocoap/issues/268 is resolved, their
        # workarounds that make this not just ignore the critical proxy options
        # in the first place will go away, and this will need to process
        # aiocoap-client failing regularly.
        stdout = await check_output(
            AIOCOAP_CLIENT
            + [
                "coap://0.0.0.0/empty",
                "--proxy",
                "coap://" + self.servernetloc,
            ],
            stderr=subprocess.STDOUT,
        )

        self.assertEqual(stdout, b"This is no proxy")

    @no_warnings
    async def test_blame_broadcast(self):
        # None of the errors have to be verbatim, but the gist should be there.

        stderr = await check_stderr(
            AIOCOAP_CLIENT + ["coap://255.255.255.255"], expected_returncode=1
        )
        self.assertTrue(
            b"For example, this can occur when attempting to send broadcast requests instead of multicast requests."
            in stderr,
            f"Expected error about broadcast traffic but got {stderr!r}",
        )

    @no_warnings
    @unittest.skipIf(
        using_simple6 or in_woodpecker,
        "Error reporting does not work in this situation for unknown reasons",
        # but different reasons for simple6 and woodpecker
    )
    async def test_blame_connectivity_ipv6(self):
        # Conveniently, on Linux this behaves indistinguishable from not having
        # v6 connectivity, and we don't have to create a v4-only test setup.
        stderr = await check_stderr(
            AIOCOAP_CLIENT + ["coap://[64:ff9b::]"], expected_returncode=1
        )
        self.assertTrue(
            b"This may be due to lack of IPv6 connectivity" in stderr,
            f"Expected error about IPv6 connectivity but got {stderr!r}",
        )

    @no_warnings
    @unittest.skipIf(
        using_simple6 or in_woodpecker,
        "Error reporting does not work in this situation for unknown reasons",
        # but different reasons for simple6 and woodpecker
    )
    async def test_blame_connectivity_ipv4(self):
        # Conveniently, on Linux this behaves indistinguishable from not having
        # v4 connectivity, and we don't have to create a v6-only test setup.
        stderr = await check_stderr(
            AIOCOAP_CLIENT + ["coap://100.64.0.0"], expected_returncode=1
        )
        self.assertTrue(
            b"This may be due to lack of IPv4 connectivity" in stderr,
            f"Expected error about IPv4 connectivity but got {stderr!r}",
        )

    @no_warnings
    @unittest.skipIf(
        using_simple6,
        """Needs decision on whether simple6 should be really unicast-only (and reject multicast addresses outright, leading to a different error) or should retain its current behavior of "multicast may work in some ways but we don't have a good understanding of when it does".""",
    )
    async def test_blame_multicast_con(self):
        stderr = await check_stderr(
            AIOCOAP_CLIENT + ["coap://[ff02::1]"], expected_returncode=1
        )
        self.assertTrue(
            b"can only be sent to unicast addresses" in stderr,
            f"Expected error about multicast CON but got {stderr!r}",
        )

        stderr = await check_stderr(
            AIOCOAP_CLIENT + ["coap://224.0.0.0"], expected_returncode=1
        )
        self.assertTrue(
            b"can only be sent to unicast addresses" in stderr,
            f"Expected error about multicast CON but got {stderr!r}",
        )


class TestCommandlineRD(unittest.TestCase):
    @unittest.skipIf(
        linkheader_modules,
        "Modules missing for running RD tests: %s" % (linkheader_modules,),
    )
    def test_help(self):
        helptext = subprocess.check_output(AIOCOAP_RD + ["--help"])
        self.assertTrue(
            b"usage:" in helptext and b"Compatibility mode for LwM2M" in helptext
        )