forked from sfackler/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rs
100 lines (87 loc) · 2.76 KB
/
test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use futures_util::FutureExt;
use native_tls::{self, Certificate};
use tokio::net::TcpStream;
use tokio_postgres::tls::TlsConnect;
#[cfg(feature = "runtime")]
use crate::MakeTlsConnector;
use crate::TlsConnector;
async fn smoke_test<T>(s: &str, tls: T)
where
T: TlsConnect<TcpStream>,
T::Stream: 'static + Send,
{
let stream = TcpStream::connect("127.0.0.1:5433").await.unwrap();
let builder = s.parse::<tokio_postgres::Config>().unwrap();
let (client, connection) = builder.connect_raw(stream, tls).await.unwrap();
let connection = connection.map(|r| r.unwrap());
tokio::spawn(connection);
let stmt = client.prepare("SELECT $1::INT4").await.unwrap();
let rows = client.query(&stmt, &[&1i32]).await.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 1);
}
#[tokio::test]
async fn require() {
let connector = native_tls::TlsConnector::builder()
.add_root_certificate(
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
)
.build()
.unwrap();
smoke_test(
"user=ssl_user dbname=postgres sslmode=require",
TlsConnector::new(connector, "localhost"),
)
.await;
}
#[tokio::test]
async fn prefer() {
let connector = native_tls::TlsConnector::builder()
.add_root_certificate(
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
)
.build()
.unwrap();
smoke_test(
"user=ssl_user dbname=postgres",
TlsConnector::new(connector, "localhost"),
)
.await;
}
#[tokio::test]
async fn scram_user() {
let connector = native_tls::TlsConnector::builder()
.add_root_certificate(
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
)
.build()
.unwrap();
smoke_test(
"user=scram_user password=password dbname=postgres sslmode=require",
TlsConnector::new(connector, "localhost"),
)
.await;
}
#[tokio::test]
#[cfg(feature = "runtime")]
async fn runtime() {
let connector = native_tls::TlsConnector::builder()
.add_root_certificate(
Certificate::from_pem(include_bytes!("../../test/server.crt")).unwrap(),
)
.build()
.unwrap();
let connector = MakeTlsConnector::new(connector);
let (client, connection) = tokio_postgres::connect(
"host=localhost port=5433 user=postgres sslmode=require",
connector,
)
.await
.unwrap();
let connection = connection.map(|r| r.unwrap());
tokio::spawn(connection);
let stmt = client.prepare("SELECT $1::INT4").await.unwrap();
let rows = client.query(&stmt, &[&1i32]).await.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 1);
}