micropython: add micropython component

This commit is contained in:
KY-zhang-X
2022-09-29 12:10:37 +08:00
parent 1514f1cb9b
commit dd76146324
2679 changed files with 354110 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
This directory contains network tests which require just "peer to peer"
network connection between test host and device under test, instead of
full Internet connection.
Note that setup for these tests and tests themselves are WIP, and may
not yet fully correspond to the functional specification above.
So far, these tests are not run as part of the main testsuite and need
to be run seperately (from the main test/ directory):
./run-tests.py net_hosted/*.py

View File

@@ -0,0 +1,16 @@
# test that socket.accept() on a non-blocking socket raises EAGAIN
try:
import usocket as socket
except:
import socket
s = socket.socket()
s.bind(socket.getaddrinfo("127.0.0.1", 8123)[0][-1])
s.setblocking(False)
s.listen(1)
try:
s.accept()
except OSError as er:
print(er.errno == 11) # 11 is EAGAIN
s.close()

View File

@@ -0,0 +1,22 @@
# test that socket.accept() on a socket with timeout raises ETIMEDOUT
try:
import uerrno as errno, usocket as socket
except:
import errno, socket
try:
socket.socket.settimeout
except AttributeError:
print("SKIP")
raise SystemExit
s = socket.socket()
s.bind(socket.getaddrinfo("127.0.0.1", 8123)[0][-1])
s.settimeout(1)
s.listen(1)
try:
s.accept()
except OSError as er:
print(er.errno in (errno.ETIMEDOUT, "timed out")) # CPython uses a string instead of errno
s.close()

View File

@@ -0,0 +1,21 @@
# test that socket.connect() on a non-blocking socket raises EINPROGRESS
try:
import usocket as socket
import uerrno as errno
except:
import socket, errno
def test(peer_addr):
s = socket.socket()
s.setblocking(False)
try:
s.connect(peer_addr)
except OSError as er:
print(er.errno == errno.EINPROGRESS)
s.close()
if __name__ == "__main__":
test(socket.getaddrinfo("micropython.org", 80)[0][-1])

View File

@@ -0,0 +1,147 @@
# test that socket.connect() on a non-blocking socket raises EINPROGRESS
# and that an immediate write/send/read/recv does the right thing
try:
import sys, time
import uerrno as errno, usocket as socket, ussl as ssl
except:
import socket, errno, ssl
isMP = sys.implementation.name == "micropython"
def dp(e):
# uncomment next line for development and testing, to print the actual exceptions
# print(repr(e))
pass
# do_connect establishes the socket and wraps it if tls is True.
# If handshake is true, the initial connect (and TLS handshake) is
# allowed to be performed before returning.
def do_connect(peer_addr, tls, handshake):
s = socket.socket()
s.setblocking(False)
try:
# print("Connecting to", peer_addr)
s.connect(peer_addr)
except OSError as er:
print("connect:", er.errno == errno.EINPROGRESS)
if er.errno != errno.EINPROGRESS:
print(" got", er.errno)
# wrap with ssl/tls if desired
if tls:
try:
if sys.implementation.name == "micropython":
s = ssl.wrap_socket(s, do_handshake=handshake)
else:
s = ssl.wrap_socket(s, do_handshake_on_connect=handshake)
print("wrap: True")
except Exception as e:
dp(e)
print("wrap:", e)
elif handshake:
# just sleep a little bit, this allows any connect() errors to happen
time.sleep(0.2)
return s
# test runs the test against a specific peer address.
def test(peer_addr, tls=False, handshake=False):
# MicroPython plain sockets have read/write, but CPython's don't
# MicroPython TLS sockets and CPython's have read/write
# hasRW captures this wonderful state of affairs
hasRW = isMP or tls
# MicroPython plain sockets and CPython's have send/recv
# MicroPython TLS sockets don't have send/recv, but CPython's do
# hasSR captures this wonderful state of affairs
hasSR = not (isMP and tls)
# connect + send
if hasSR:
s = do_connect(peer_addr, tls, handshake)
# send -> 4 or EAGAIN
try:
ret = s.send(b"1234")
print("send:", handshake and ret == 4)
except OSError as er:
#
dp(er)
print("send:", er.errno in (errno.EAGAIN, errno.EINPROGRESS))
s.close()
else: # fake it...
print("connect:", True)
if tls:
print("wrap:", True)
print("send:", True)
# connect + write
if hasRW:
s = do_connect(peer_addr, tls, handshake)
# write -> None
try:
ret = s.write(b"1234")
print("write:", ret in (4, None)) # SSL may accept 4 into buffer
except OSError as er:
dp(er)
print("write:", False) # should not raise
except ValueError as er: # CPython
dp(er)
print("write:", er.args[0] == "Write on closed or unwrapped SSL socket.")
s.close()
else: # fake it...
print("connect:", True)
if tls:
print("wrap:", True)
print("write:", True)
if hasSR:
# connect + recv
s = do_connect(peer_addr, tls, handshake)
# recv -> EAGAIN
try:
print("recv:", s.recv(10))
except OSError as er:
dp(er)
print("recv:", er.errno == errno.EAGAIN)
s.close()
else: # fake it...
print("connect:", True)
if tls:
print("wrap:", True)
print("recv:", True)
# connect + read
if hasRW:
s = do_connect(peer_addr, tls, handshake)
# read -> None
try:
ret = s.read(10)
print("read:", ret is None)
except OSError as er:
dp(er)
print("read:", False) # should not raise
except ValueError as er: # CPython
dp(er)
print("read:", er.args[0] == "Read on closed or unwrapped SSL socket.")
s.close()
else: # fake it...
print("connect:", True)
if tls:
print("wrap:", True)
print("read:", True)
if __name__ == "__main__":
# these tests use a non-existent test IP address, this way the connect takes forever and
# we can see EAGAIN/None (https://tools.ietf.org/html/rfc5737)
print("--- Plain sockets to nowhere ---")
test(socket.getaddrinfo("192.0.2.1", 80)[0][-1], False, False)
print("--- SSL sockets to nowhere ---")
# this test fails with AXTLS because do_handshake=False blocks on first read/write and
# there it times out until the connect is aborted
test(socket.getaddrinfo("192.0.2.1", 443)[0][-1], True, False)
print("--- Plain sockets ---")
test(socket.getaddrinfo("micropython.org", 80)[0][-1], False, True)
print("--- SSL sockets ---")
test(socket.getaddrinfo("micropython.org", 443)[0][-1], True, True)

View File

@@ -0,0 +1,31 @@
# test that socket.connect() has correct polling behaviour before, during and after
try:
import usocket as socket, uselect as select
except:
import socket, select
def test(peer_addr):
s = socket.socket()
poller = select.poll()
poller.register(s)
# test poll before connect
p = poller.poll(0)
print(len(p), p[0][-1])
s.connect(peer_addr)
# test poll during connection
print(len(poller.poll(0)))
# test poll after connection is established
p = poller.poll(1000)
print(len(p), p[0][-1])
s.close()
if __name__ == "__main__":
test(socket.getaddrinfo("micropython.org", 80)[0][-1])

View File

@@ -0,0 +1,3 @@
1 20
1
1 4

View File

@@ -0,0 +1,21 @@
# test ssl.getpeercert() method
try:
import usocket as socket
import ussl as ssl
except:
import socket
import ssl
def test(peer_addr):
s = socket.socket()
s.connect(peer_addr)
s = ssl.wrap_socket(s)
cert = s.getpeercert(True)
print(type(cert), len(cert) > 100)
s.close()
if __name__ == "__main__":
test(socket.getaddrinfo("micropython.org", 443)[0][-1])

View File

@@ -0,0 +1 @@
<class 'bytes'> True

View File

@@ -0,0 +1,31 @@
# Test basic behaviour of uasyncio.start_server()
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def test():
# Test creating 2 servers using the same address
print("create server1")
server1 = await asyncio.start_server(None, "0.0.0.0", 8000)
try:
print("create server2")
await asyncio.start_server(None, "0.0.0.0", 8000)
except OSError as er:
print("OSError")
# Wait for server to close.
async with server1:
print("sleep")
await asyncio.sleep(0)
print("done")
asyncio.run(test())

View File

@@ -0,0 +1,5 @@
create server1
create server2
OSError
sleep
done