zpinger 0.3.0

zonda pinger library
Documentation
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use std::net::TcpListener;
use std::sync::Arc;
use std::time::Duration;

use zpinger::Pinger;

fn closed_tcp_addr() -> String {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();
    drop(listener);
    addr.to_string()
}

#[test]
fn resolve_returns_at_least_one_address() {
    let addr = testserver::start_tcp_echo("127.0.0.1:0").unwrap();
    let resolved = zpinger::resolve(&addr.to_string());
    assert!(!resolved.is_empty());
}

#[test]
fn resolve_returns_empty_on_unresolvable_input() {
    // No DNS, no panic — empty Vec lets the CLI keep going so the
    // real pinger can surface the actual error.
    let resolved = zpinger::resolve("definitely-not-a-real-host.invalid:1");
    assert!(resolved.is_empty());
}

#[test]
fn tcp_pinger_struct_succeeds() {
    let addr = testserver::start_tcp_echo("127.0.0.1:0").unwrap();
    zpinger::TcpPinger::new(addr.to_string()).ping().unwrap();
}

#[test]
fn tcp_pinger_via_timed_helper() {
    let addr = testserver::start_tcp_echo("127.0.0.1:0").unwrap();
    let p = zpinger::TcpPinger::new(addr.to_string());
    let elapsed = zpinger::timed(&p).unwrap();
    assert!(elapsed > Duration::from_nanos(0));
}

#[test]
fn tcp_pinger_with_custom_timeout() {
    let addr = testserver::start_tcp_echo("127.0.0.1:0").unwrap();
    let p = zpinger::TcpPinger::new(addr.to_string()).with_timeout(Duration::from_secs(1));
    p.ping().unwrap();
}

#[test]
fn udp_pinger_struct_succeeds() {
    let addr = testserver::start_udp_echo("127.0.0.1:0").unwrap();
    zpinger::UdpPinger::new(addr.to_string()).ping().unwrap();
}

#[test]
fn level4_pingers_usable_as_trait_objects() {
    let tcp_addr = testserver::start_tcp_echo("127.0.0.1:0").unwrap();
    let udp_addr = testserver::start_udp_echo("127.0.0.1:0").unwrap();
    let pingers: Vec<Box<dyn Pinger>> = vec![
        Box::new(zpinger::TcpPinger::new(tcp_addr.to_string())),
        Box::new(zpinger::UdpPinger::new(udp_addr.to_string())),
    ];
    for p in &pingers {
        p.ping().unwrap();
    }
}

#[test]
fn tcp_pinger_struct_fails_on_closed_port() {
    let p = zpinger::TcpPinger::new(closed_tcp_addr());
    assert!(p.ping().is_err());
}

#[test]
fn http_pinger_struct_succeeds() {
    let addr = testserver::start_http_ok("127.0.0.1:0").unwrap();
    let p = zpinger::HttpPinger::new(zpinger::HttpMethod::Get, format!("{}/x", addr));
    p.ping().unwrap();
}

#[test]
fn http_pinger_all_methods_via_struct() {
    let addr = testserver::start_http_ok("127.0.0.1:0").unwrap();
    let target = format!("{}/x", addr);
    for method in [
        zpinger::HttpMethod::Connect,
        zpinger::HttpMethod::Get,
        zpinger::HttpMethod::Post,
        zpinger::HttpMethod::Put,
        zpinger::HttpMethod::Delete,
        zpinger::HttpMethod::Patch,
    ] {
        zpinger::HttpPinger::new(method, target.clone())
            .ping()
            .unwrap_or_else(|e| panic!("{:?} failed: {}", method, e));
    }
}

#[test]
fn http_pinger_via_timed_helper() {
    let addr = testserver::start_http_ok("127.0.0.1:0").unwrap();
    let p = zpinger::HttpPinger::new(zpinger::HttpMethod::Get, format!("{}/x", addr));
    let elapsed = zpinger::timed(&p).unwrap();
    assert!(elapsed > Duration::from_nanos(0));
}

#[test]
fn http_pinger_rejects_unknown_scheme() {
    // Anything that isn't http or https should be refused up front.
    let p = zpinger::HttpPinger::new(zpinger::HttpMethod::Get, "ftp://example.com:21/foo");
    let err = p.ping().expect_err("non-http scheme must be rejected");
    let msg = err.to_string();
    assert!(msg.contains("ftp"), "unexpected error message: {msg}");
}

#[test]
fn http_pinger_https_succeeds_with_trusted_cert() {
    let server = testserver::start_https_ok("127.0.0.1:0").unwrap();
    let target = format!("https://localhost:{}/anything", server.addr.port());
    let p = zpinger::HttpPinger::new(zpinger::HttpMethod::Get, target)
        .with_tls_config(server.client_config);
    p.ping().unwrap();
}

#[test]
fn http_pinger_https_all_methods_via_struct() {
    let server = testserver::start_https_ok("127.0.0.1:0").unwrap();
    let target = format!("https://localhost:{}/x", server.addr.port());
    for method in [
        zpinger::HttpMethod::Connect,
        zpinger::HttpMethod::Get,
        zpinger::HttpMethod::Post,
        zpinger::HttpMethod::Put,
        zpinger::HttpMethod::Delete,
        zpinger::HttpMethod::Patch,
    ] {
        zpinger::HttpPinger::new(method, target.clone())
            .with_tls_config(Arc::clone(&server.client_config))
            .ping()
            .unwrap_or_else(|e| panic!("{method:?} failed: {e}"));
    }
}

#[test]
fn http_pinger_succeeds_without_explicit_port_on_localhost_default() {
    // Run an HTTP server on the platform's HTTP default port (80) is
    // not portable in tests, so instead we verify the URI parser +
    // pinger handle the implicit-port path: when the URL has no
    // ":port" segment, the pinger must apply the scheme default and
    // not crash. Use a closed default port — the test asserts that
    // we get a connect-level error (port refused) rather than a
    // "missing host" or parser error.
    let target = "http://127.0.0.1/anything";
    let p = zpinger::HttpPinger::new(zpinger::HttpMethod::Get, target);
    let err = p
        .ping()
        .expect_err("port 80 should be refused on this host");
    let msg = err.to_string().to_lowercase();
    assert!(
        !msg.contains("missing host") && !msg.contains("invalid"),
        "unexpected error type for implicit-port path: {msg}"
    );
}

#[test]
fn http_pinger_https_fails_without_trust_anchor() {
    // Without injecting the test server's cert the default trust
    // store (webpki-roots, public CAs only) cannot verify the
    // self-signed cert, so the handshake must fail rather than
    // silently succeed.
    let server = testserver::start_https_ok("127.0.0.1:0").unwrap();
    let target = format!("https://localhost:{}/anything", server.addr.port());
    let p = zpinger::HttpPinger::new(zpinger::HttpMethod::Get, target);
    assert!(p.ping().is_err());
}

#[test]
fn http_pinger_rejects_non_http_response() {
    // Point the HTTP pinger at the TCP echo server. The "response" is
    // an echo of the GET request, which starts with "GET", not
    // "HTTP/", so the response validator should reject it. The old
    // implementation would have returned Ok here because the 404/501
    // substring check happens to miss "GET".
    let addr = testserver::start_tcp_echo("127.0.0.1:0").unwrap();
    let p = zpinger::HttpPinger::new(zpinger::HttpMethod::Get, format!("{}/x", addr));
    assert!(p.ping().is_err());
}

#[test]
fn ws_pinger_succeeds_on_ws_server() {
    let addr = testserver::start_ws_ok("127.0.0.1:0").unwrap();
    let target = format!("ws://{addr}/");
    zpinger::WebSocketPinger::new(target).ping().unwrap();
}

#[test]
fn ws_pinger_via_timed_helper() {
    let addr = testserver::start_ws_ok("127.0.0.1:0").unwrap();
    let target = format!("ws://{addr}/");
    let p = zpinger::WebSocketPinger::new(target);
    let elapsed = zpinger::timed(&p).unwrap();
    assert!(elapsed > Duration::from_nanos(0));
}

#[test]
fn ws_pinger_fails_on_closed_port() {
    let target = format!("ws://{}/", closed_tcp_addr());
    let p = zpinger::WebSocketPinger::new(target);
    assert!(p.ping().is_err());
}

#[test]
fn ws_pinger_rejects_non_ws_scheme() {
    let p = zpinger::WebSocketPinger::new("http://example.com:80/");
    let err = p.ping().expect_err("non-ws scheme must be rejected");
    let msg = err.to_string();
    assert!(msg.contains("http"), "unexpected error message: {msg}");
}

#[test]
fn wss_pinger_succeeds_with_trusted_cert() {
    let server = testserver::start_wss_ok("127.0.0.1:0").unwrap();
    let target = format!("wss://localhost:{}/", server.addr.port());
    let p = zpinger::WebSocketPinger::new(target).with_tls_config(server.client_config);
    p.ping().unwrap();
}

#[test]
fn wss_pinger_fails_without_trust_anchor() {
    // Without the test server's cert installed as a trust anchor, the
    // TLS handshake must fail rather than silently succeed.
    let server = testserver::start_wss_ok("127.0.0.1:0").unwrap();
    let target = format!("wss://localhost:{}/", server.addr.port());
    let p = zpinger::WebSocketPinger::new(target);
    assert!(p.ping().is_err());
}

#[test]
fn ws_pinger_usable_as_trait_object() {
    let addr = testserver::start_ws_ok("127.0.0.1:0").unwrap();
    let target = format!("ws://{addr}/");
    let p: Box<dyn Pinger> = Box::new(zpinger::WebSocketPinger::new(target));
    p.ping().unwrap();
}

#[test]
fn dns_pinger_succeeds_on_test_server() {
    let addr = testserver::start_dns_ok("127.0.0.1:0").unwrap();
    let p = zpinger::DnsPinger::new(addr.to_string(), "example.com");
    p.ping().unwrap();
}

#[test]
fn dns_pinger_via_timed_helper() {
    let addr = testserver::start_dns_ok("127.0.0.1:0").unwrap();
    let p = zpinger::DnsPinger::new(addr.to_string(), "example.com");
    let elapsed = zpinger::timed(&p).unwrap();
    assert!(elapsed > Duration::from_nanos(0));
}

#[test]
fn dns_pinger_with_record_type() {
    let addr = testserver::start_dns_ok("127.0.0.1:0").unwrap();
    let p = zpinger::DnsPinger::new(addr.to_string(), "example.com")
        .with_record_type(zpinger::RecordType::Aaaa);
    p.ping().unwrap();
}

#[test]
fn dns_pinger_rejects_empty_query() {
    let addr = testserver::start_dns_ok("127.0.0.1:0").unwrap();
    let p = zpinger::DnsPinger::new(addr.to_string(), "");
    assert!(p.ping().is_err());
}

#[test]
fn dns_pinger_times_out_on_silent_port() {
    // UDP has no "connection refused" — bind a port that never responds
    // and confirm DnsPinger surfaces the timeout instead of hanging.
    let silent = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
    let addr = silent.local_addr().unwrap();
    // intentionally do NOT spawn a reader; the socket stays alive but
    // never replies.
    let p = zpinger::DnsPinger::new(addr.to_string(), "example.com")
        .with_timeout(Duration::from_millis(150));
    assert!(p.ping().is_err());
    drop(silent);
}

#[test]
fn dns_pinger_usable_as_trait_object() {
    let addr = testserver::start_dns_ok("127.0.0.1:0").unwrap();
    let p: Box<dyn Pinger> = Box::new(zpinger::DnsPinger::new(addr.to_string(), "example.com"));
    p.ping().unwrap();
}

#[test]
fn mqtt_pinger_succeeds_against_test_broker() {
    let addr = testserver::start_mqtt_ok("127.0.0.1:0").unwrap();
    let p = zpinger::MqttPinger::new(format!("mqtt://{addr}"));
    p.ping().unwrap();
}

#[test]
fn mqtt_pinger_via_timed_helper() {
    let addr = testserver::start_mqtt_ok("127.0.0.1:0").unwrap();
    let p = zpinger::MqttPinger::new(format!("mqtt://{addr}"));
    let elapsed = zpinger::timed(&p).unwrap();
    assert!(elapsed > Duration::from_nanos(0));
}

#[test]
fn mqtt_pinger_with_custom_client_id() {
    let addr = testserver::start_mqtt_ok("127.0.0.1:0").unwrap();
    let p = zpinger::MqttPinger::new(format!("mqtt://{addr}")).with_client_id("custom-test");
    p.ping().unwrap();
}

#[test]
fn mqtt_pinger_schemeless_uses_plain_path() {
    let addr = testserver::start_mqtt_ok("127.0.0.1:0").unwrap();
    let p = zpinger::MqttPinger::new(addr.to_string());
    p.ping().unwrap();
}

#[test]
fn mqtt_pinger_rejects_unknown_scheme() {
    let p = zpinger::MqttPinger::new("ftp://example.com:21");
    let err = p
        .ping()
        .expect_err("non-mqtt scheme must be rejected up front");
    assert!(
        err.to_string().contains("ftp"),
        "unexpected error message: {err}"
    );
}

#[test]
fn mqtt_pinger_fails_on_closed_port() {
    let p = zpinger::MqttPinger::new(format!("mqtt://{}", closed_tcp_addr()))
        .with_timeout(Duration::from_millis(500));
    assert!(p.ping().is_err());
}

#[test]
fn mqtt_pinger_v5_succeeds_against_test_broker() {
    let addr = testserver::start_mqtt_ok("127.0.0.1:0").unwrap();
    let p =
        zpinger::MqttPinger::new(format!("mqtt://{addr}")).with_version(zpinger::MqttVersion::V5);
    p.ping().unwrap();
}

#[test]
fn mqtts_pinger_v5_succeeds_with_trusted_cert() {
    let server = testserver::start_mqtts_ok("127.0.0.1:0").unwrap();
    let p = zpinger::MqttPinger::new(format!("mqtts://localhost:{}", server.addr.port()))
        .with_version(zpinger::MqttVersion::V5)
        .with_tls_config(server.client_config);
    p.ping().unwrap();
}

#[test]
fn mqtt_pinger_usable_as_trait_object() {
    let addr = testserver::start_mqtt_ok("127.0.0.1:0").unwrap();
    let p: Box<dyn Pinger> = Box::new(zpinger::MqttPinger::new(format!("mqtt://{addr}")));
    p.ping().unwrap();
}

#[test]
fn mqtts_pinger_succeeds_with_trusted_cert() {
    let server = testserver::start_mqtts_ok("127.0.0.1:0").unwrap();
    let p = zpinger::MqttPinger::new(format!("mqtts://localhost:{}", server.addr.port()))
        .with_tls_config(server.client_config);
    p.ping().unwrap();
}

#[test]
fn mqtts_pinger_fails_without_trust_anchor() {
    let server = testserver::start_mqtts_ok("127.0.0.1:0").unwrap();
    let p = zpinger::MqttPinger::new(format!("mqtts://localhost:{}", server.addr.port()))
        .with_timeout(Duration::from_millis(500));
    assert!(p.ping().is_err());
}

#[test]
fn dns_pinger_rejects_response_with_tampered_question() {
    // Hand-rolled tiny UDP server: receive the query, set QR=1 +
    // RCODE=0 like the real testserver, but corrupt one byte inside
    // the question section before sending the packet back. The
    // pinger's question-echo check must catch this.
    use std::net::UdpSocket;
    use std::thread;

    let server = UdpSocket::bind("127.0.0.1:0").unwrap();
    let addr = server.local_addr().unwrap();
    thread::spawn(move || {
        let mut buf = [0u8; 512];
        if let Ok((n, src)) = server.recv_from(&mut buf) {
            if n >= 14 {
                buf[2] |= 0x80; // QR=1
                buf[3] &= 0xF0; // RCODE=0
                buf[13] ^= 0xFF; // tamper with the question section
                let _ = server.send_to(&buf[..n], src);
            }
        }
    });

    let p = zpinger::DnsPinger::new(addr.to_string(), "example.com")
        .with_timeout(Duration::from_millis(500));
    let err = p.ping().expect_err("tampered question must be rejected");
    assert!(
        err.to_string().contains("question"),
        "unexpected error message: {err}"
    );
}