Skip to content

Commit 32b245a

Browse files
UjOwtucjbg
authored andcommitted
unit test only: trust any certificate
1 parent 4139951 commit 32b245a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ tokio-rustls = "0.23"
1919
[dev-dependencies]
2020
env_logger = { version = "0.8", default-features = false }
2121
tokio = { version = "1", features = ["macros", "rt"] }
22+
rustls = { version = "0.20", features = ["dangerous_configuration"] }
23+

src/lib.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,37 @@ where
128128

129129
#[cfg(test)]
130130
mod tests {
131+
use super::*;
131132
use futures::future::TryFutureExt;
133+
use rustls::{client::ServerCertVerified, client::ServerCertVerifier, Certificate, Error};
134+
use std::time::SystemTime;
135+
136+
struct AcceptAllVerifier {}
137+
impl ServerCertVerifier for AcceptAllVerifier {
138+
fn verify_server_cert(
139+
&self,
140+
_end_entity: &Certificate,
141+
_intermediates: &[Certificate],
142+
_server_name: &ServerName,
143+
_scts: &mut dyn Iterator<Item = &[u8]>,
144+
_ocsp_response: &[u8],
145+
_now: SystemTime,
146+
) -> Result<ServerCertVerified, Error> {
147+
Ok(ServerCertVerified::assertion())
148+
}
149+
}
132150

133151
#[tokio::test]
134152
async fn it_works() {
135153
env_logger::builder().is_test(true).try_init().unwrap();
136154

137-
let config = rustls::ClientConfig::builder()
155+
let mut config = rustls::ClientConfig::builder()
138156
.with_safe_defaults()
139157
.with_root_certificates(rustls::RootCertStore::empty())
140158
.with_no_client_auth();
159+
config
160+
.dangerous()
161+
.set_certificate_verifier(Arc::new(AcceptAllVerifier {}));
141162
let tls = super::MakeRustlsConnect::new(config);
142163
let (client, conn) = tokio_postgres::connect(
143164
"sslmode=require host=localhost port=5432 user=postgres",

0 commit comments

Comments
 (0)