Skip to content

Commit 4caa27a

Browse files
committed
tests/net_inet/test_tls_sites.py: Integration test for SSL connections.
This attempts to bootstrap network tests for MicroPython. This commits sets test/net_inet/ as place for tests which require access to wide Internet. They aren't intended to be run as part of the main testsuite, instead to be run manually on demand. test_tls_sites.py in particular check that it's possible to establish SSL/TLS connection to select sites on the Internet: few references ones, plus those for which problems were reported, and resolved.
1 parent c06aa5b commit 4caa27a

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

tests/net_inet/README

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This direcctory contains network tests which require Internet connection.
2+
Note that these tests are not run as part of the main testsuite and need
3+
to be run seperately (from the main test/ directory):
4+
5+
./run-tests net_inet/*.py

tests/net_inet/test_tls_sites.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
try:
2+
import usocket as _socket
3+
except:
4+
import _socket
5+
try:
6+
import ussl as ssl
7+
except:
8+
import ssl
9+
10+
11+
def test_one(site, opts):
12+
ai = _socket.getaddrinfo(site, 443)
13+
addr = ai[0][-1]
14+
15+
s = _socket.socket()
16+
17+
try:
18+
s.connect(addr)
19+
20+
if "sni" in opts:
21+
s = ssl.wrap_socket(s, server_hostname=opts["host"])
22+
else:
23+
s = ssl.wrap_socket(s)
24+
25+
s.write(b"GET / HTTP/1.0\r\n\r\n")
26+
resp = s.read(4096)
27+
# print(resp)
28+
29+
finally:
30+
s.close()
31+
32+
33+
SITES = [
34+
"google.com",
35+
"www.google.com",
36+
"api.telegram.org",
37+
# "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com",
38+
{"host": "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com", "sni": True},
39+
]
40+
41+
42+
def main():
43+
for site in SITES:
44+
opts = {}
45+
if isinstance(site, dict):
46+
opts = site
47+
site = opts["host"]
48+
49+
try:
50+
test_one(site, opts)
51+
print(site, "ok")
52+
except Exception as e:
53+
print(site, repr(e))
54+
55+
56+
main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
google.com ok
2+
www.google.com ok
3+
api.telegram.org ok
4+
w9rybpfril.execute-api.ap-southeast-2.amazonaws.com ok

0 commit comments

Comments
 (0)