Skip to content

Commit 20a86ef

Browse files
committed
tests/net_inet: Add simpler tls sites test, and skip existing on axtls.
Ports that use axtls cannot run the `test_tls_sites.py` test because the sites it connects to use advanced ciphers. So skip this test on such ports, and add a new, simpler test that doesn't require certificate verification and works with axtls. Signed-off-by: Damien George <damien@micropython.org>
1 parent c3e37d1 commit 20a86ef

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

tests/net_inet/test_tls_sites.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
# Test making HTTPS requests to sites that may require advanced ciphers.
2+
13
import sys
24
import select
35
import socket
46
import ssl
57

8+
# Don't run if ssl doesn't support required certificates (eg axtls).
9+
if not hasattr(ssl, "CERT_REQUIRED"):
10+
print("SKIP")
11+
raise SystemExit
12+
613

714
def test_one(site, opts):
815
ai = socket.getaddrinfo(site, 443, socket.AF_INET)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Test making HTTPS requests to sites that allow simple ciphers.
2+
3+
import sys
4+
import socket
5+
import ssl
6+
7+
SITES = (
8+
("micropython.org", "/ks/test.html"),
9+
("pypi.org", "/"),
10+
)
11+
12+
13+
def test_one(site, path):
14+
ai = socket.getaddrinfo(site, 443, socket.AF_INET)
15+
addr = ai[0][-1]
16+
17+
# Create SSLContext.
18+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
19+
if sys.implementation.name != "micropython":
20+
# CPython compatibility: disable check_hostname
21+
ssl_context.check_hostname = False
22+
ssl_context.verify_mode = ssl.CERT_NONE
23+
24+
s = socket.socket(socket.AF_INET)
25+
s.connect(addr)
26+
s = ssl_context.wrap_socket(s)
27+
28+
s.write(b"GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (bytes(path, "ascii"), bytes(site, "ascii")))
29+
resp = s.read(4096)
30+
s.close()
31+
32+
if resp.startswith(b"HTTP/1."):
33+
print(site, "ok")
34+
else:
35+
print(site, "response doesn't start with HTTP/1.")
36+
37+
38+
def main():
39+
for site, path in SITES:
40+
test_one(site, path)
41+
42+
43+
main()

0 commit comments

Comments
 (0)