@@ -11,8 +11,8 @@ fn tls_config_from_source() {
11
11
use rocket:: config:: { Config , TlsConfig } ;
12
12
use rocket:: figment:: Figment ;
13
13
14
- let cert_path = relative ! ( "examples/tls/private/cert.pem" ) ;
15
- let key_path = relative ! ( "examples/tls/private/key.pem" ) ;
14
+ let cert_path = relative ! ( "../../ examples/tls/private/cert.pem" ) ;
15
+ let key_path = relative ! ( "../../ examples/tls/private/key.pem" ) ;
16
16
17
17
let rocket_config = Config {
18
18
tls : Some ( TlsConfig :: from_paths ( cert_path, key_path) ) ,
@@ -24,3 +24,71 @@ fn tls_config_from_source() {
24
24
assert_eq ! ( tls. certs( ) . unwrap_left( ) , cert_path) ;
25
25
assert_eq ! ( tls. key( ) . unwrap_left( ) , key_path) ;
26
26
}
27
+
28
+ #[ test]
29
+ fn tls_server_operation ( ) {
30
+ use std:: io:: Read ;
31
+
32
+ use rocket:: { get, routes} ;
33
+ use rocket:: config:: { Config , TlsConfig } ;
34
+ use rocket:: figment:: Figment ;
35
+
36
+ let cert_path = relative ! ( "../../examples/tls/private/rsa_sha256_cert.pem" ) ;
37
+ let key_path = relative ! ( "../../examples/tls/private/rsa_sha256_key.pem" ) ;
38
+ let ca_cert_path = relative ! ( "../../examples/tls/private/ca_cert.pem" ) ;
39
+
40
+ println ! ( "{cert_path:?}" ) ;
41
+
42
+ let port = {
43
+ let listener = std:: net:: TcpListener :: bind ( ( "127.0.0.1" , 0 ) ) . expect ( "creating listener" ) ;
44
+ listener. local_addr ( ) . expect ( "getting listener's port" ) . port ( )
45
+ } ;
46
+
47
+ let rocket_config = Config {
48
+ port,
49
+ tls : Some ( TlsConfig :: from_paths ( cert_path, key_path) ) ,
50
+ ..Default :: default ( )
51
+ } ;
52
+ let config: Config = Figment :: from ( rocket_config) . extract ( ) . expect ( "creating config" ) ;
53
+ let ( shutdown_signal_sender, mut shutdown_signal_receiver) = tokio:: sync:: mpsc:: channel :: < ( ) > ( 1 ) ;
54
+
55
+ // Create a runtime in a separate thread for the server being tested
56
+ let join_handle = std:: thread:: spawn ( move || {
57
+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
58
+
59
+ #[ get( "/hello" ) ]
60
+ fn tls_test_get ( ) -> & ' static str {
61
+ "world"
62
+ }
63
+
64
+ rt. block_on ( async {
65
+ let task_handle = tokio:: spawn ( async {
66
+ rocket:: custom ( config)
67
+ . mount ( "/" , routes ! [ tls_test_get] )
68
+ . launch ( ) . await . unwrap ( ) ;
69
+ } ) ;
70
+ shutdown_signal_receiver. recv ( ) . await ;
71
+ task_handle. abort ( ) ;
72
+ } ) ;
73
+ } ) ;
74
+
75
+ let request_url = format ! ( "https://localhost:{}/hello" , port) ;
76
+
77
+ // CA certificate is not loaded, so request should fail
78
+ assert ! ( reqwest:: blocking:: get( & request_url) . is_err( ) ) ;
79
+
80
+ // Load the CA certicate for use with test client
81
+ let cert = {
82
+ let mut buf = Vec :: new ( ) ;
83
+ std:: fs:: File :: open ( ca_cert_path) . expect ( "open ca_certs" )
84
+ . read_to_end ( & mut buf) . expect ( "read ca_certs" ) ;
85
+ reqwest:: Certificate :: from_pem ( & buf) . expect ( "create certificate" )
86
+ } ;
87
+ let client = reqwest:: blocking:: Client :: builder ( ) . add_root_certificate ( cert) . build ( ) . expect ( "build client" ) ;
88
+
89
+ let response = client. get ( & request_url) . send ( ) . expect ( "https request" ) ;
90
+ assert_eq ! ( & response. text( ) . unwrap( ) , "world" ) ;
91
+
92
+ shutdown_signal_sender. blocking_send ( ( ) ) . expect ( "signal shutdown" ) ;
93
+ join_handle. join ( ) . expect ( "join thread" ) ;
94
+ }
0 commit comments