File: tests.rs

package info (click to toggle)
rust-hyper-rustls 0.27.6-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 312 kB
  • sloc: sh: 48; makefile: 2
file content (105 lines) | stat: -rw-r--r-- 2,555 bytes parent folder | download | duplicates (2)
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
101
102
103
104
105
use std::env;
use std::net::TcpStream;
use std::path::PathBuf;
use std::process::Command;
use std::thread;
use std::time;

fn examples_dir() -> PathBuf {
    let target_dir: PathBuf = env::var("CARGO_TARGET_DIR")
        .unwrap_or_else(|_| "target".to_string())
        .into();
    target_dir
        .join("debug")
        .join("examples")
}

fn server_command() -> Command {
    Command::new(examples_dir().join("server"))
}

fn client_command() -> Command {
    Command::new(examples_dir().join("client"))
}

fn wait_for_server(addr: &str) {
    for i in 0..10 {
        if TcpStream::connect(addr).is_ok() {
            return;
        }
        thread::sleep(time::Duration::from_millis(i * 100));
    }
    panic!("failed to connect to {:?} after 10 tries", addr);
}

#[test]
#[cfg(with_network)]
fn client() {
    let rc = client_command()
        .arg("https://google.com")
        .output()
        .expect("cannot run client example");

    assert!(rc.status.success());
}

#[test]
#[cfg(with_network)]
fn server() {
    let mut srv = server_command()
        .arg("1337")
        .spawn()
        .expect("cannot run server example");

    let addr = "localhost:1337";
    wait_for_server(addr);

    let output = Command::new("curl")
        .arg("--insecure")
        .arg("--http1.0")
        .arg(format!("https://{}", addr))
        .output()
        .expect("cannot run curl");

    srv.kill().unwrap();
    srv.wait()
        .expect("failed to wait on server process");

    if !output.status.success() {
        let version_stdout = Command::new("curl")
            .arg("--version")
            .output()
            .expect("cannot run curl to collect --version")
            .stdout;
        println!("curl version: {}", String::from_utf8_lossy(&version_stdout));
        println!("curl stderr:\n{}", String::from_utf8_lossy(&output.stderr));
    }

    assert_eq!(String::from_utf8_lossy(&output.stdout), "Try POST /echo\n");
}

#[test]
#[cfg(with_network)]
fn custom_ca_store() {
    let mut srv = server_command()
        .arg("1338")
        .spawn()
        .expect("cannot run server example");

    let addr = "localhost:1338";
    wait_for_server(addr);

    let rc = client_command()
        .arg(format!("https://{}", addr))
        .arg("examples/sample.pem")
        .output()
        .expect("cannot run client example");

    srv.kill().unwrap();
    srv.wait()
        .expect("failed to wait on server process");

    if !rc.status.success() {
        assert_eq!(String::from_utf8_lossy(&rc.stdout), "");
    }
}