forked from chrysn/aiocoap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_noncoap_client.py
61 lines (48 loc) · 2.23 KB
/
test_noncoap_client.py
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
# This file is part of the Python aiocoap library project.
#
# Copyright (c) 2012-2014 Maciej Wasilak <http://sixpinetrees.blogspot.com/>,
# 2013-2014 Christian Amsüss <[email protected]>
#
# aiocoap is free software, this file is published under the MIT license as
# described in the accompanying LICENSE file.
"""Confront an aiocoap server with a client that speaks so bad protocol it is
easier to mock with sending byte sequences than with aiocoap"""
import socket
import asyncio
import aiocoap
from .test_server import WithTestServer, precise_warnings, no_warnings
class TestNoncoapClient(WithTestServer):
def setUp(self):
super(TestNoncoapClient, self).setUp()
self.mocksock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
self.mocksock.connect((self.serveraddress, aiocoap.COAP_PORT))
def tearDown(self):
self.mocksock.close()
super(TestNoncoapClient, self).tearDown()
@precise_warnings(["Ignoring unparsable message from ..."])
def test_veryshort(self):
self.mocksock.send(b'\x40')
self.loop.run_until_complete(asyncio.sleep(0.1))
@precise_warnings(["Ignoring unparsable message from ..."])
def test_short_mid(self):
self.mocksock.send(b'\x40\x01\x97')
self.loop.run_until_complete(asyncio.sleep(0.1))
@precise_warnings(["Ignoring unparsable message from ..."])
def test_version2(self):
self.mocksock.send(b'\x80\x01\x99\x98')
self.loop.run_until_complete(asyncio.sleep(0.1))
@no_warnings
def test_duplicate(self):
self.mocksock.send(b'\x40\x01\x99\x99') # that's a GET /
self.loop.run_until_complete(asyncio.sleep(0.1))
self.mocksock.send(b'\x40\x01\x99\x99') # that's a GET /
self.loop.run_until_complete(asyncio.sleep(0.1))
r1 = self.mocksock.recv(1024)
r2 = self.mocksock.recv(1024)
self.assertEqual(r1, r2, "Duplicate GETs gave different responses")
@no_warnings
def test_ping(self):
self.mocksock.send(b'\x40\x00\x99\x9a') # CoAP ping -- should this test be doable in aiocoap?
self.loop.run_until_complete(asyncio.sleep(0.1))
response = self.mocksock.recv(1024)
assert response == b'\x70\x00\x99\x9a'