Skip to content

Commit

Permalink
Add discover-root-test.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Nov 12, 2024
1 parent 43fcbed commit f03f2b6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
46 changes: 27 additions & 19 deletions examples/discover_root.toit
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ import http
import tls
import certificate_roots

HOST ::= "ecc256.badssl.com" // Replace with the host you want to connect to.
PATH ::= "/" // Replace with the path part after the domain.
network_interface ::= net.open
found_one_that_worked := false

main:

main args:
if args.size != 1:
print "Usage: discover_root.toit <uri>"
return

uri := args[0]

discover-root --uri=uri

discover-root --uri/string -> string?:
names := []
certs := []

Expand All @@ -34,34 +41,35 @@ main:
// This will not work on small devices since it parses all certificates
// at once. Once parsed, the memory is not freed, so there's no easy
// way around this.
binary_split names certs
result := binary_split names certs --uri=uri

if not found_one_that_worked:
print "None of the certificate roots was suitable for connecting to $HOST"
if not result:
print "None of the certificate roots was suitable for connecting to $uri"
return result

binary_split names/List certs/List -> none:
binary_split names/List certs/List --uri/string -> string?:

print "."

exception := catch:
client := http.Client.tls network_interface --root_certificates=certs
response := client.get HOST PATH
// TODO(florian): Don't reach into private variables of response.
response.connection_.close
try:
response := client.get --uri=uri
finally:
client.close

if exception:
if exception.to_string.starts_with "Site relies on unknown root":
return
return null
if exception.to_string.starts_with "X509 - Certificate verification failed":
return
return null
if exception.to_string.starts_with "Unknown root certificate":
return
return null
throw exception

if names.size == 1:
print "Successful connection to https://$HOST$PATH with $names[0]"
found_one_that_worked = true
return
print "Successful connection to $uri with $names[0]"
return names[0]

else:
// names.size >= 2.
Expand All @@ -70,5 +78,5 @@ binary_split names/List certs/List -> none:
l_certs := certs[..certs.size / 2]
r_certs := certs[certs.size / 2..]

binary_split l_names l_certs
binary_split r_names r_certs
return binary_split l_names l_certs --uri=uri or
binary_split r_names r_certs --uri=uri
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Use of this source code is governed by a Zero-Clause BSD license that can
# be found in the tests/TESTS_LICENSE file.

file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*_test.toit")
file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*_test.toit" "*-test.toit")

set(TOIT_EXEC "toit.run" CACHE FILEPATH "The executable used to run the tests")
set(TPKG_EXEC "toit.pkg" CACHE FILEPATH "The executable used to install the packages")
Expand Down
10 changes: 10 additions & 0 deletions tests/discover-root-test.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2024 Toitware ApS.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the tests/TESTS_LICENSE file.
import expect show *
import ..examples.discover-root as discover-root

main:
result := discover-root.discover-root --uri="https://toitlang.org"
expect-not-null result
7 changes: 7 additions & 0 deletions tests/package.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
sdk: ^2.0.0-alpha.145
prefixes:
certificate_roots: ..
http: pkg-http
packages:
..:
path: ..
pkg-http:
url: github.com/toitlang/pkg-http
name: http
version: 2.8.0
hash: 81754d64fe466cd00cc494ec515da8749bf04975
3 changes: 3 additions & 0 deletions tests/package.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
dependencies:
certificate_roots:
path: ..
http:
url: github.com/toitlang/pkg-http
version: ^2.8.0

0 comments on commit f03f2b6

Please sign in to comment.