File: test_server.py

package info (click to toggle)
aiocoap 0.4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,756 kB
  • sloc: python: 16,846; makefile: 23; sh: 9
file content (615 lines) | stat: -rw-r--r-- 21,865 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors
#
# SPDX-License-Identifier: MIT

import asyncio
import contextlib
import re
import aiocoap
import aiocoap.resource
from aiocoap.numbers import ContentFormat
import unittest
import os
import json

from . import common
from .fixtures import no_warnings, WithAsyncLoop, Destructing, CLEANUPTIME, asynctest


class slow_empty_ack(contextlib.ContextDecorator):
    def __enter__(self):
        # FIXME: Pushing the times here so that even on a loaded system
        # with pypy and coverage, this can complete realistically.
        #
        # This will break as soon as transport tuning becomes more
        # per-transport or even per-remote; the proper fix would be to use
        # mock time anyway.
        self.original_empty_ack_delay = aiocoap.numbers.TransportTuning.EMPTY_ACK_DELAY
        aiocoap.numbers.TransportTuning.EMPTY_ACK_DELAY = (
            0.9 * aiocoap.numbers.TransportTuning.ACK_TIMEOUT
        )
        return self

    def __exit__(self, *exc):
        aiocoap.numbers.TransportTuning.EMPTY_ACK_DELAY = self.original_empty_ack_delay
        return False


class MultiRepresentationResource(aiocoap.resource.Resource):
    def __init__(self, representations):
        self._representations = representations
        super().__init__()

    async def render_get(self, request):
        if request.opt.proxy_scheme is not None:
            # For CLI test_noproxy -- but otherwise a symptom of https://github.com/chrysn/aiocoap/issues/268
            return aiocoap.Message(code=aiocoap.CONTENT, payload=b"This is no proxy")

        m = request.opt.accept or ContentFormat.TEXT

        if m in self._representations:
            response = self._representations[m]
        else:
            return aiocoap.Message(code=aiocoap.NOT_ACCEPTABLE)

        return aiocoap.Message(payload=response, content_format=request.opt.accept or 0)


class SlowResource(aiocoap.resource.Resource):
    async def render_get(self, request):
        await asyncio.sleep(0.2)
        # Marking the response as confirmable overrides the default behavior of
        # sending NON responses to NON requests.
        #
        # This is done to aid the test_freeoncancel test, and should revert to
        # the default behavior once that has better control over the environment.
        return aiocoap.Message(mtype=aiocoap.CON)


class BigResource(aiocoap.resource.Resource):
    async def render_get(self, request):
        # 10kb
        payload = b"0123456789----------" * 512
        response = aiocoap.Message(payload=payload)

        aiocoap.resource.hashing_etag(request, response)
        return response


class SlowBigResource(aiocoap.resource.Resource):
    async def render_get(self, request):
        # Should be 0.2s usually, but while running in slow_empty_ack mode, we
        # have to slow it down. Adds 2s to the tests, but that makes the test
        # more reliable.
        await asyncio.sleep(aiocoap.numbers.TransportTuning.EMPTY_ACK_DELAY * 1.1)
        # 1.6kb
        payload = b"0123456789----------" * 80
        return aiocoap.Message(payload=payload)


class ManualBigResource(aiocoap.resource.Resource):
    async def needs_blockwise_assembly(self, request):
        return False

    async def render_get(self, request):
        BlockwiseTuple = aiocoap.optiontypes.BlockOption.BlockwiseTuple
        block2 = request.opt.block2 or BlockwiseTuple(0, 0, 6)
        # as above
        body = b"0123456789----------" * 80
        # in a more realistic example, we wouldn't build this in memory but eg.
        # seek and read limited length
        slice = body[block2.start :]
        more = len(slice) > block2.size
        slice = slice[: block2.size]
        block2 = BlockwiseTuple(block2.block_number, more, block2.size_exponent)
        return aiocoap.Message(payload=slice, block2=block2)


class ReplacingResource(aiocoap.resource.Resource):
    async def render_get(self, request):
        return aiocoap.Message(payload=self.value)

    async def render_put(self, request):
        self.value = request.payload.replace(b"0", b"O")
        return aiocoap.Message()

    async def render_post(self, request):
        response = request.payload.replace(b"0", b"O")
        return aiocoap.Message(code=aiocoap.CONTENT, payload=response)


class RootResource(aiocoap.resource.Resource):
    async def render_get(self, request):
        return aiocoap.Message(
            code=aiocoap.CONTENT, payload=b"Welcome to the test server"
        )


class GenericErrorResource(aiocoap.resource.Resource):
    async def render_get(self, request):
        raise RuntimeError()


class PrettyErrorResource(aiocoap.resource.Resource):
    async def render_get(self, request):
        raise self.MyError()

    class MyError(aiocoap.error.ConstructionRenderableError):
        code = aiocoap.NOT_FOUND
        message = "I'm sorry nothing is here"


class DoubleErrorResource(aiocoap.resource.Resource):
    async def render_get(self, request):
        raise self.MyError()

    class MyError(aiocoap.error.RenderableError):
        def to_message(self):
            raise RuntimeError()


class WhoAmI(aiocoap.resource.Resource):
    async def render_get(self, request):
        p = dict(
            repr=repr(request.remote),
            hostinfo=request.remote.hostinfo,
            hostinfo_local=request.remote.hostinfo_local,
            scheme=request.remote.scheme,
            urihost_option=request.opt.uri_host,
            # FIXME: The whole get_request_uri is a mess
            requested_uri=request._original_request_uri,
            claims=request.remote.authenticated_claims,
        )
        return aiocoap.Message(
            code=aiocoap.CONTENT,
            content_format=ContentFormat.JSON,
            payload=json.dumps(p).encode("utf8"),
        )


class BasicTestingSite(aiocoap.resource.Site):
    def __init__(self):
        super(BasicTestingSite, self).__init__()

        # Not part of the test suite, but handy when running standalone
        self.add_resource(
            [".well-known", "core"],
            aiocoap.resource.WKCResource(self.get_resources_as_linkheader),
        )

        self.add_resource(
            ["empty"],
            MultiRepresentationResource(
                {
                    ContentFormat.JSON: b"{}",
                    ContentFormat.LINKFORMAT: b"<>",
                    ContentFormat.TEXT: b"",
                }
            ),
        )
        self.add_resource(
            ["answer"],
            MultiRepresentationResource(
                {
                    ContentFormat.JSON: b'{"answer": 42}',
                    ContentFormat.CBOR: b"\xa1\x66\x61\x6e\x73\x77\x65\x72\x18\x2a",
                    ContentFormat.LINKFORMAT: b'<data:text/plain;42>;rel="answer";anchor="https://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life,_the_Universe,_and_Everything_(42)"',
                    ContentFormat.TEXT: b"The answer to life, the universe, and everything is 42.",
                }
            ),
        )
        self.add_resource(["slow"], SlowResource())
        self.add_resource(["big"], BigResource())
        self.add_resource(["slowbig"], SlowBigResource())
        self.add_resource(["manualbig"], ManualBigResource())
        self.add_resource(["replacing"], self.Subsite())
        self.add_resource(["error", "generic"], GenericErrorResource())
        self.add_resource(["error", "pretty"], PrettyErrorResource())
        self.add_resource(["error", "double"], DoubleErrorResource())
        self.add_resource(["whoami"], WhoAmI())
        self.add_resource([], RootResource())

    class Subsite(aiocoap.resource.Site):
        def __init__(self):
            super().__init__()
            self.add_resource(["one"], ReplacingResource())


class WithTestServer(WithAsyncLoop, Destructing):
    # to allow overriding the factory class
    TestingSite = BasicTestingSite

    def create_testing_site(self):
        return self.TestingSite()

    def get_server_ssl_context(self):
        """Override hook for subclasses that want to populate _ssl_context at construction"""
        return None

    def setUp(self):
        super(WithTestServer, self).setUp()

        multicastif = (
            os.environ["AIOCOAP_TEST_MCIF"].split(":")
            if "AIOCOAP_TEST_MCIF" in os.environ
            else []
        )

        self.server = self.loop.run_until_complete(
            aiocoap.Context.create_server_context(
                self.create_testing_site(),
                bind=(self.serveraddress, None),
                multicast=multicastif,
                _ssl_context=self.get_server_ssl_context(),
            )
        )

    def tearDown(self):
        # let the server receive the acks we just sent
        self.loop.run_until_complete(asyncio.sleep(CLEANUPTIME))
        self.loop.run_until_complete(self.server.shutdown())
        # Nothing in the context should keep the request interfaces alive;
        # delete them first to see *which* of them is the one causing the
        # trouble
        while self.server.request_interfaces:
            self._del_to_be_sure(
                {
                    "get": (lambda self: self.server.request_interfaces[0]),
                    "del": (lambda self: self.server.request_interfaces.__delitem__(0)),
                    "label": "request_interfaces[%s]"
                    % self.server.request_interfaces[0],
                }
            )
        self._del_to_be_sure("server")

        super(WithTestServer, self).tearDown()

    serveraddress = "::1"
    servernetloc = "[%s]" % serveraddress
    servernamealias = common.loopbackname_v6 or common.loopbackname_v46


class WithClient(WithAsyncLoop, Destructing):
    def setUp(self):
        super(WithClient, self).setUp()

        self.test_did_shut_down_client = False

        self.client = self.loop.run_until_complete(
            aiocoap.Context.create_client_context()
        )

    def tearDown(self):
        if not self.test_did_shut_down_client:
            self.test_did_shut_down_client = False
            self.loop.run_until_complete(asyncio.sleep(CLEANUPTIME))
            self.loop.run_until_complete(self.client.shutdown())

        # Nothing in the context should keep the request interfaces alive;
        # delete them first to see *which* of them is the one causing the
        # trouble
        while self.client.request_interfaces:
            self._del_to_be_sure(
                {
                    "get": (lambda self: self.client.request_interfaces[0]),
                    "del": (lambda self: self.client.request_interfaces.__delitem__(0)),
                    "label": "request_interfaces[%s]"
                    % self.client.request_interfaces[0],
                }
            )

        self._del_to_be_sure("client")

        super(WithClient, self).tearDown()


# test cases


class TestServerBase(WithTestServer, WithClient):
    """All the tools for building requests, but no actual tests; use this when
    working off the test server with no intention to to the full set of
    tests."""

    @no_warnings
    def build_request(self):
        request = aiocoap.Message(code=aiocoap.GET)
        request.unresolved_remote = self.servernetloc
        return request

    @asynctest
    async def fetch_response(self, request):
        return await self.client.request(request).response


class TestServer(TestServerBase):
    @no_warnings
    def test_empty_accept(self):
        request = self.build_request()
        request.opt.uri_path = ["empty"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code, aiocoap.CONTENT, "Simple request did not succede"
        )
        self.assertEqual(response.payload, b"", "Simple request gave unexpected result")

    @no_warnings
    def test_unacceptable_accept(self):
        request = self.build_request()
        request.opt.uri_path = ["empty"]
        request.opt.accept = 9999
        response = self.fetch_response(request)
        self.assertEqual(
            response.code,
            aiocoap.NOT_ACCEPTABLE,
            "Inacceptable request was not not accepted",
        )

    @no_warnings
    def test_js_accept(self):
        request = self.build_request()
        request.opt.uri_path = ["empty"]
        request.opt.accept = ContentFormat.JSON
        response = self.fetch_response(request)
        self.assertEqual(response.code, aiocoap.CONTENT, "JSON request did not succede")
        self.assertEqual(response.payload, b"{}", "JSON request gave unexpected result")

    @no_warnings
    def test_nonexisting_resource(self):
        request = self.build_request()
        request.opt.uri_path = ["nonexisting"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code, aiocoap.NOT_FOUND, "Nonexisting resource was not not found"
        )

    @no_warnings
    def test_spurious_resource(self):
        request = self.build_request()
        request.opt.uri_path = ["..", "empty"]
        response = self.fetch_response(request)
        # different behavior would be ok-ish, as the .. in the request is forbidden, but returning 4.04 is sane here
        self.assertEqual(
            response.code,
            aiocoap.NOT_FOUND,
            "'..' component in path did not get ignored the way it was expected",
        )

    @no_warnings
    @slow_empty_ack()
    def test_fast_resource(self):
        request = self.build_request()
        request.opt.uri_path = ["empty"]

        response = self.fetch_response(request)

        self.assertEqual(response.code, aiocoap.CONTENT, "Fast request did not succede")
        self.assertEqual(self._count_empty_acks(), 0, "Fast resource had an empty ack")

    @no_warnings
    def test_slow_resource(self):
        request = self.build_request()
        request.opt.uri_path = ["slow"]

        response = self.fetch_response(request)

        self.assertEqual(response.code, aiocoap.CONTENT, "Slow request did not succede")
        if response.requested_scheme in (None, "coap"):
            self.assertEqual(
                self._count_empty_acks(),
                1,
                "Slow resource was not handled in two exchanges",
            )

    @no_warnings
    def test_big_resource(self):
        request = self.build_request()
        request.opt.uri_path = ["big"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code, aiocoap.CONTENT, "Big resource request did not succede"
        )
        self.assertEqual(
            len(response.payload), 10240, "Big resource is not as big as expected"
        )

        self.assertTrue(
            response.opt.etag != None, "Big resource does not provide an ETag"
        )

        request = self.build_request()
        request.opt.uri_path = ["big"]
        request.opt.etags = [response.opt.etag]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code,
            aiocoap.VALID,
            "Big resource does not support ETag validation",
        )
        self.assertTrue(
            response.opt.etag != None, "Big resource does not send ETag for validation"
        )

    @no_warnings
    @slow_empty_ack()
    def test_slowbig_resource(self):
        request = self.build_request()
        request.opt.uri_path = ["slowbig"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code, aiocoap.CONTENT, "SlowBig resource request did not succede"
        )
        self.assertEqual(
            len(response.payload), 1600, "SlowBig resource is not as big as expected"
        )
        if response.requested_scheme in (None, "coap"):
            self.assertEqual(
                self._count_empty_acks(),
                1,
                "SlowBig resource was not handled in two exchanges",
            )

    @no_warnings
    def test_manualbig_resource(self):
        request = self.build_request()
        request.opt.uri_path = ["manualbig"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code, aiocoap.CONTENT, "ManualBig resource request did not succede"
        )
        self.assertEqual(
            len(response.payload), 1600, "ManualBig resource is not as big as expected"
        )

    @no_warnings
    def test_replacing_resource(self):
        testpattern = b"01" * 1024

        request = self.build_request()
        request.code = aiocoap.PUT
        request.payload = testpattern
        request.opt.uri_path = ["replacing", "one"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code, aiocoap.CHANGED, "PUT did not result in CHANGED"
        )
        self.assertEqual(response.payload, b"", "PUT has unexpected payload")

        request = self.build_request()
        request.code = aiocoap.GET
        request.opt.uri_path = ["replacing", "one"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code,
            aiocoap.CONTENT,
            "Replacing resource could not be GOT (GET'd?) successfully",
        )
        self.assertEqual(
            response.payload,
            testpattern.replace(b"0", b"O"),
            "Replacing resource did not replace as expected between PUT and GET",
        )

        request = self.build_request()
        request.code = aiocoap.POST
        request.payload = testpattern
        request.opt.uri_path = ["replacing", "one"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code,
            aiocoap.CONTENT,
            "Replacing resource could not be POSTed to successfully",
        )
        self.assertEqual(
            response.payload,
            testpattern.replace(b"0", b"O"),
            "Replacing resource did not replace as expected when POSTed",
        )

    def test_error_resources(self):
        request = self.build_request()
        request.opt.uri_path = ["error", "generic"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code,
            aiocoap.INTERNAL_SERVER_ERROR,
            "Runtime error possibly leaking information",
        )
        self.assertEqual(
            response.payload, b"", "Runtime error possibly leaking information"
        )

        request = self.build_request()
        request.opt.uri_path = ["error", "pretty"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code,
            aiocoap.NOT_FOUND,
            "Runtime error possibly leaking information",
        )
        self.assertTrue(
            response.payload.decode("ascii").startswith("I'm sorry"),
            "Runtime error possibly leaking information",
        )

        request = self.build_request()
        request.opt.uri_path = ["error", "double"]
        response = self.fetch_response(request)
        self.assertEqual(
            response.code,
            aiocoap.INTERNAL_SERVER_ERROR,
            "Runtime double error possibly leaking information",
        )
        self.assertEqual(
            response.payload, b"", "Runtime double error possibly leaking information"
        )

    @no_warnings
    def test_root_resource(self):
        request = self.build_request()
        request.opt.uri_path = []
        response = self.fetch_response(request)
        self.assertEqual(response.code, aiocoap.CONTENT, "Root resource was not found")

    _empty_ack_logmsg = re.compile(
        "^Incoming message <aiocoap.Message at 0x[0-9a-f]+: ACK EMPTY ([^)]+)"
    )

    def _count_empty_acks(self):
        # only client-side received empty-acks are counted; they typically
        # generate an empty ack back when the separate response is ack'd.
        return sum(
            self._empty_ack_logmsg.match(x.msg) is not None
            for x in self.handler
            if x.name != "coap-server"
        )

    @no_warnings
    def test_clean_shutdown(self):
        self.loop.run_until_complete(self.client.shutdown())
        self.test_did_shut_down_client = True

        request = self.build_request()
        # This is clearly misuse of the API, but nonetheless, only a
        # LibraryShutdown should fly out of it.
        #
        # Ideally, we'd do that "during" shutdown", but that's terribly racy
        # business, both wins are acceptable (what if the shutdown hasn't
        # really begun yet), and it's highly unlikely that we hit the juicy
        # cases.
        with self.assertRaises(aiocoap.error.LibraryShutdown):
            self.loop.run_until_complete(self.fetch_response(request))


@unittest.skipIf(common.tcp_disabled, "TCP disabled in environment")
class TestServerTCP(TestServer):
    # no modification in server setup necessary, as by default, all transports
    # are enabled on servers.

    def build_request(self):
        request = super().build_request()
        request.requested_scheme = "coap+tcp"
        return request


ws_modules = aiocoap.defaults.ws_missing_modules()


@unittest.skipIf(
    ws_modules or common.ws_disabled,
    "WS missing modules (%s) or disabled in this environment" % (ws_modules,),
)
class TestServerWS(TestServer):
    # as with TestServerWS

    def build_request(self):
        request = super().build_request()
        # odd default port
        request.unresolved_remote += ":8683"
        request.requested_scheme = "coap+ws"
        return request


if __name__ == "__main__":
    # due to the imports, you'll need to run this as `python3 -m tests.test_server`
    common.run_fixture_as_standalone_server(WithTestServer)