Skip to content

Commit 5fbe20f

Browse files
committed
Auth tests
1 parent 7d9cb04 commit 5fbe20f

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

tokio-postgres/tests/test.rs

+120
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,128 @@ extern crate tokio_postgres;
44

55
use tokio::prelude::*;
66
use tokio::runtime::current_thread::Runtime;
7+
use tokio_postgres::error::SqlState;
78
use tokio_postgres::types::Type;
89

10+
fn smoke_test(url: &str) {
11+
let _ = env_logger::try_init();
12+
let mut runtime = Runtime::new().unwrap();
13+
14+
let handshake = tokio_postgres::connect(url.parse().unwrap());
15+
let (mut client, connection) = runtime.block_on(handshake).unwrap();
16+
let connection = connection.map_err(|e| panic!("{}", e));
17+
runtime.handle().spawn(connection).unwrap();
18+
19+
let prepare = client.prepare("SELECT 1::INT4");
20+
let statement = runtime.block_on(prepare).unwrap();
21+
let select = client.query(&statement, &[]).collect().map(|rows| {
22+
assert_eq!(rows.len(), 1);
23+
assert_eq!(rows[0].get::<_, i32>(0), 1);
24+
});
25+
runtime.block_on(select).unwrap();
26+
27+
drop(statement);
28+
drop(client);
29+
runtime.run().unwrap();
30+
}
31+
32+
#[test]
33+
fn plain_password_missing() {
34+
let _ = env_logger::try_init();
35+
let mut runtime = Runtime::new().unwrap();
36+
37+
let handshake = tokio_postgres::connect("postgres://pass_user@localhost:5433".parse().unwrap());
38+
match runtime.block_on(handshake) {
39+
Ok(_) => panic!("unexpected success"),
40+
Err(ref e) if e.as_connection().is_some() => {}
41+
Err(e) => panic!("{}", e),
42+
}
43+
}
44+
45+
#[test]
46+
fn plain_password_wrong() {
47+
let _ = env_logger::try_init();
48+
let mut runtime = Runtime::new().unwrap();
49+
50+
let handshake =
51+
tokio_postgres::connect("postgres://pass_user:foo@localhost:5433".parse().unwrap());
52+
match runtime.block_on(handshake) {
53+
Ok(_) => panic!("unexpected success"),
54+
Err(ref e) if e.code() == Some(&SqlState::INVALID_PASSWORD) => {}
55+
Err(e) => panic!("{}", e),
56+
}
57+
}
58+
59+
#[test]
60+
fn plain_password_ok() {
61+
smoke_test("postgres://pass_user:password@localhost:5433/postgres");
62+
}
63+
64+
#[test]
65+
fn md5_password_missing() {
66+
let _ = env_logger::try_init();
67+
let mut runtime = Runtime::new().unwrap();
68+
69+
let handshake = tokio_postgres::connect("postgres://md5_user@localhost:5433".parse().unwrap());
70+
match runtime.block_on(handshake) {
71+
Ok(_) => panic!("unexpected success"),
72+
Err(ref e) if e.as_connection().is_some() => {}
73+
Err(e) => panic!("{}", e),
74+
}
75+
}
76+
77+
#[test]
78+
fn md5_password_wrong() {
79+
let _ = env_logger::try_init();
80+
let mut runtime = Runtime::new().unwrap();
81+
82+
let handshake =
83+
tokio_postgres::connect("postgres://md5_user:foo@localhost:5433".parse().unwrap());
84+
match runtime.block_on(handshake) {
85+
Ok(_) => panic!("unexpected success"),
86+
Err(ref e) if e.code() == Some(&SqlState::INVALID_PASSWORD) => {}
87+
Err(e) => panic!("{}", e),
88+
}
89+
}
90+
91+
#[test]
92+
fn md5_password_ok() {
93+
smoke_test("postgres://md5_user:password@localhost:5433/postgres");
94+
}
95+
96+
#[test]
97+
fn scram_password_missing() {
98+
let _ = env_logger::try_init();
99+
let mut runtime = Runtime::new().unwrap();
100+
101+
let handshake =
102+
tokio_postgres::connect("postgres://scram_user@localhost:5433".parse().unwrap());
103+
match runtime.block_on(handshake) {
104+
Ok(_) => panic!("unexpected success"),
105+
Err(ref e) if e.as_connection().is_some() => {}
106+
Err(e) => panic!("{}", e),
107+
}
108+
}
109+
110+
#[test]
111+
fn scram_password_wrong() {
112+
let _ = env_logger::try_init();
113+
let mut runtime = Runtime::new().unwrap();
114+
115+
let handshake =
116+
tokio_postgres::connect("postgres://scram_user:foo@localhost:5433".parse().unwrap());
117+
match runtime.block_on(handshake) {
118+
Ok(_) => panic!("unexpected success"),
119+
Err(ref e) if e.code() == Some(&SqlState::INVALID_PASSWORD) => {}
120+
Err(e) => panic!("{}", e),
121+
}
122+
}
123+
124+
#[test]
125+
fn scram_password_ok() {
126+
smoke_test("postgres://scram_user:password@localhost:5433/postgres");
127+
}
128+
9129
#[test]
10130
fn pipelined_prepare() {
11131
let _ = env_logger::try_init();

0 commit comments

Comments
 (0)