multiprobe 0.1.0

Enterprise-grade multi-protocol network probing with Paris Traceroute, path analytics, MTU discovery, and TLS analysis
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
//! Comprehensive edge case tests for multiprobe library

use multiprobe::{Probe, Protocol, Classifier, TracerouteOptions};
use std::time::Duration;

// ============================================================================
// INPUT VALIDATION TESTS
// ============================================================================

#[tokio::test]
async fn test_empty_hostname() {
    let result = Probe::tcp("", 80)
        .timeout(Duration::from_secs(2))
        .send()
        .await;
    assert!(result.is_err(), "Empty hostname should fail");
}

#[tokio::test]
async fn test_whitespace_hostname() {
    let result = Probe::tcp("   ", 80).send().await;
    assert!(result.is_err(), "Whitespace-only hostname should fail");
}

#[tokio::test]
async fn test_invalid_hostname_characters() {
    let result = Probe::tcp("invalid<>hostname", 80).send().await;
    assert!(result.is_err(), "Invalid characters in hostname should fail");
}

#[tokio::test]
async fn test_very_long_hostname() {
    let long_hostname = "a".repeat(300) + ".com";
    let result = Probe::tcp(&long_hostname, 80).send().await;
    assert!(result.is_err(), "Very long hostname should fail");
}

#[tokio::test]
async fn test_hostname_with_port_in_string() {
    // Should not parse port from hostname string
    let result = Probe::tcp("localhost:8080", 80)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    // This should fail because "localhost:8080" is not a valid hostname
    assert!(result.is_err(), "Hostname with embedded port should fail");
}

// ============================================================================
// PORT EDGE CASES
// ============================================================================

#[tokio::test]
async fn test_port_zero() {
    let result = Probe::tcp("127.0.0.1", 0)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    // Port 0 is technically valid but usually not usable
    // The result depends on OS behavior
    println!("Port 0 result: {result:?}");
}

#[tokio::test]
async fn test_port_max() {
    let result = Probe::tcp("127.0.0.1", 65535)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    // Should work but likely refused on localhost
    match result {
        Ok(r) => assert!(!r.success, "Port 65535 should be closed on localhost"),
        Err(e) => println!("Port 65535 error (expected): {e}"),
    }
}

#[tokio::test]
async fn test_privileged_port() {
    let result = Probe::tcp("127.0.0.1", 22)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    // Port 22 (SSH) - behavior depends on whether SSH is running
    println!("Port 22 result: {result:?}");
}

// ============================================================================
// TIMEOUT EDGE CASES
// ============================================================================

#[tokio::test]
async fn test_very_short_timeout() {
    let result = Probe::tcp("8.8.8.8", 53)
        .timeout(Duration::from_millis(1))
        .send()
        .await;
    // Very short timeout - should likely fail
    if let Ok(r) = result {
        println!("Short timeout succeeded unexpectedly: {r:?}");
    }
}

#[tokio::test]
async fn test_zero_timeout() {
    let result = Probe::tcp("127.0.0.1", 80)
        .timeout(Duration::ZERO)
        .send()
        .await;
    // Zero timeout behavior
    println!("Zero timeout result: {result:?}");
}

#[tokio::test]
async fn test_reasonable_timeout() {
    let result = Probe::tcp("127.0.0.1", 80)
        .timeout(Duration::from_secs(1))
        .send()
        .await;
    // Should complete (success or failure) within timeout
    assert!(result.is_ok() || result.is_err());
}

// ============================================================================
// IP ADDRESS FORMATS
// ============================================================================

#[tokio::test]
async fn test_ipv4_localhost() {
    let result = Probe::tcp("127.0.0.1", 80)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    assert!(result.is_ok(), "Localhost probe should complete");
}

#[tokio::test]
async fn test_ipv4_loopback_full() {
    let result = Probe::tcp("127.0.0.1", 12345)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    match result {
        Ok(r) => assert!(!r.success, "Random port on localhost should be closed"),
        Err(e) => println!("Error: {e}"),
    }
}

#[tokio::test]
async fn test_private_ip_10() {
    let result = Probe::tcp("10.0.0.1", 80)
        .timeout(Duration::from_millis(500))
        .send()
        .await;
    // Private IP - might timeout or refuse
    println!("Private IP 10.x result: {result:?}");
}

#[tokio::test]
async fn test_private_ip_192() {
    let result = Probe::tcp("192.168.1.1", 80)
        .timeout(Duration::from_millis(500))
        .send()
        .await;
    // Common router IP - might work or timeout
    println!("Private IP 192.168.x result: {result:?}");
}

#[tokio::test]
async fn test_broadcast_address() {
    let result = Probe::tcp("255.255.255.255", 80)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    // Broadcast - should fail
    assert!(result.is_err() || !result.unwrap().success);
}

#[tokio::test]
async fn test_ipv6_localhost() {
    let result = Probe::tcp("::1", 80)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    // IPv6 localhost - may or may not work depending on system config
    println!("IPv6 localhost result: {result:?}");
}

// ============================================================================
// MULTI-PROBE EDGE CASES
// ============================================================================

#[tokio::test]
async fn test_multi_probe_empty_protocols() {
    let result = Probe::multi("127.0.0.1")
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    assert!(result.is_err(), "Multi-probe with no protocols should fail");
}

#[tokio::test]
async fn test_multi_probe_single_protocol() {
    let result = Probe::multi("127.0.0.1")
        .tcp(80)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    assert!(result.is_ok(), "Single protocol should work");
    assert_eq!(result.unwrap().results.len(), 1);
}

#[tokio::test]
async fn test_multi_probe_duplicate_protocols() {
    let result = Probe::multi("127.0.0.1")
        .tcp(80)
        .tcp(80)  // Duplicate
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    assert!(result.is_ok(), "Duplicate protocols should be allowed");
    assert_eq!(result.unwrap().results.len(), 2);
}

#[tokio::test]
async fn test_multi_probe_many_protocols() {
    let mut probe = Probe::multi("127.0.0.1");
    for port in 80..90 {
        probe = probe.tcp(port);
    }
    let result = probe
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    assert!(result.is_ok(), "Many protocols should work");
    assert_eq!(result.unwrap().results.len(), 10);
}

#[tokio::test]
async fn test_multi_probe_mixed_protocols() {
    let result = Probe::multi("127.0.0.1")
        .tcp(80)
        .udp(53)
        .tcp(443)
        .timeout(Duration::from_millis(100))
        .send()
        .await;
    assert!(result.is_ok());
    let r = result.unwrap();
    assert_eq!(r.results.len(), 3);
}

// ============================================================================
// UDP EDGE CASES
// ============================================================================

#[tokio::test]
async fn test_udp_localhost() {
    let result = Probe::udp("127.0.0.1", 53)
        .timeout(Duration::from_millis(500))
        .send()
        .await;
    // UDP to localhost DNS - probably no DNS server running
    println!("UDP localhost result: {result:?}");
}

#[tokio::test]
async fn test_udp_real_dns() {
    let result = Probe::udp("8.8.8.8", 53)
        .timeout(Duration::from_secs(2))
        .send()
        .await;
    // Google DNS should accept UDP packets
    assert!(result.is_ok(), "UDP to Google DNS should work");
}

#[tokio::test]
async fn test_udp_non_dns_port() {
    let result = Probe::udp("127.0.0.1", 12345)
        .timeout(Duration::from_millis(500))
        .send()
        .await;
    // Random UDP port - might get port unreachable or timeout
    println!("UDP non-DNS result: {result:?}");
}

// ============================================================================
// CLASSIFIER EDGE CASES
// ============================================================================

#[tokio::test]
async fn test_classifier_empty_results() {
    let results: Vec<multiprobe::ProbeResult> = vec![];
    let classification = Classifier::classify(&results);
    assert_eq!(classification, multiprobe::PathClassification::Unknown);
}

#[tokio::test]
async fn test_classifier_fingerprint_empty() {
    let results: Vec<multiprobe::ProbeResult> = vec![];
    let fingerprint = Classifier::fingerprint(&results);
    assert_eq!(fingerprint, "Unknown");
}

#[tokio::test]
async fn test_differential_score_empty() {
    let results: Vec<multiprobe::ProbeResult> = vec![];
    let score = Classifier::differential_score(&results);
    assert_eq!(score.consistency, 1.0); // No differences means consistent
}

// ============================================================================
// TRACEROUTE EDGE CASES
// ============================================================================

#[tokio::test]
async fn test_traceroute_localhost() {
    let result = Probe::traceroute("127.0.0.1")
        .max_hops(5)
        .timeout_per_hop(Duration::from_millis(500))
        .send()
        .await;
    // Localhost traceroute - may need privileges
    println!("Traceroute localhost result: {result:?}");
}

#[tokio::test]
async fn test_traceroute_max_hops_one() {
    let result = Probe::traceroute("8.8.8.8")
        .max_hops(1)
        .timeout_per_hop(Duration::from_secs(1))
        .send()
        .await;
    match result {
        Ok(r) => {
            assert_eq!(r.hops.len(), 1, "Should have exactly 1 hop");
            assert!(!r.reached_destination, "Should not reach destination with 1 hop");
        }
        Err(e) => println!("Traceroute error (may need privileges): {e}"),
    }
}

#[tokio::test]
async fn test_traceroute_options_default() {
    let opts = TracerouteOptions::default();
    assert_eq!(opts.max_hops, 30);
    assert_eq!(opts.timeout_per_hop, Duration::from_secs(2));
    assert_eq!(opts.probes_per_hop, 1);
}

// ============================================================================
// TIMING BREAKDOWN TESTS
// ============================================================================

#[tokio::test]
async fn test_timing_breakdown_dns_skip() {
    // IP address should have zero DNS time
    let result = Probe::tcp("127.0.0.1", 80)
        .timeout(Duration::from_millis(100))
        .send()
        .await;

    if let Ok(r) = result {
        assert!(r.timing.dns_ms().is_none() || r.timing.dns_ms() == Some(0.0),
            "Direct IP should have no/zero DNS time");
    }
}

#[tokio::test]
async fn test_timing_breakdown_with_dns() {
    let result = Probe::tcp("google.com", 443)
        .timeout(Duration::from_secs(5))
        .send()
        .await;

    if let Ok(r) = result {
        // DNS time should be recorded for hostname
        println!("DNS time: {:?}ms", r.timing.dns_ms());
        println!("Connect time: {:?}ms", r.timing.connect_ms());
        println!("Total time: {:?}ms", r.timing.total_ms());
        assert!(r.timing.total_ms() > 0.0);
    }
}

// ============================================================================
// CONCURRENT OPERATION TESTS
// ============================================================================

#[tokio::test]
async fn test_concurrent_probes_same_target() {
    let futures: Vec<_> = (0..5).map(|_| {
        Probe::tcp("127.0.0.1", 80)
            .timeout(Duration::from_millis(100))
            .send()
    }).collect();

    let results = futures::future::join_all(futures).await;

    // All should complete
    for (i, result) in results.iter().enumerate() {
        assert!(result.is_ok(), "Probe {i} should complete");
    }
}

#[tokio::test]
async fn test_concurrent_probes_different_targets() {
    let targets = ["127.0.0.1", "8.8.8.8", "1.1.1.1"];
    let futures: Vec<_> = targets.iter().map(|target| {
        Probe::tcp(target, 443)
            .timeout(Duration::from_secs(3))
            .send()
    }).collect();

    let results = futures::future::join_all(futures).await;

    // All should complete (success or failure)
    for result in results {
        assert!(result.is_ok() || result.is_err());
    }
}

// ============================================================================
// ERROR RECOVERY TESTS
// ============================================================================

#[tokio::test]
async fn test_probe_after_dns_failure() {
    // First, a failing DNS lookup
    let _ = Probe::tcp("invalid.hostname.xyz", 80)
        .timeout(Duration::from_secs(1))
        .send()
        .await;

    // Then a valid probe - should still work
    let result = Probe::tcp("127.0.0.1", 80)
        .timeout(Duration::from_millis(100))
        .send()
        .await;

    assert!(result.is_ok(), "Probe should work after DNS failure");
}

#[tokio::test]
async fn test_probe_after_timeout() {
    // First, a timing out probe (very short timeout to distant server)
    let _ = Probe::tcp("8.8.8.8", 80)
        .timeout(Duration::from_millis(1))
        .send()
        .await;

    // Then a valid probe - should still work
    let result = Probe::tcp("127.0.0.1", 80)
        .timeout(Duration::from_millis(100))
        .send()
        .await;

    assert!(result.is_ok(), "Probe should work after timeout");
}

// ============================================================================
// PROTOCOL ENUM TESTS
// ============================================================================

#[test]
fn test_protocol_display() {
    assert_eq!(Protocol::Icmp.to_string(), "ICMP");
    assert_eq!(Protocol::Tcp(80).to_string(), "TCP/80");
    assert_eq!(Protocol::Tcp(443).to_string(), "TCP/443");
    assert_eq!(Protocol::Udp(53).to_string(), "UDP/53");
}

#[test]
fn test_protocol_port() {
    assert_eq!(Protocol::Icmp.port(), None);
    assert_eq!(Protocol::Tcp(80).port(), Some(80));
    assert_eq!(Protocol::Udp(53).port(), Some(53));
}

#[test]
fn test_protocol_equality() {
    assert_eq!(Protocol::Tcp(80), Protocol::Tcp(80));
    assert_ne!(Protocol::Tcp(80), Protocol::Tcp(443));
    assert_ne!(Protocol::Tcp(80), Protocol::Udp(80));
}

// ============================================================================
// RESULT TYPE TESTS
// ============================================================================

#[tokio::test]
async fn test_multi_probe_result_classify() {
    let result = Probe::multi("127.0.0.1")
        .tcp(80)
        .tcp(443)
        .timeout(Duration::from_millis(100))
        .send()
        .await;

    if let Ok(r) = result {
        let classification = r.classify();
        // Should return a valid classification string
        assert!(!classification.to_string().is_empty());
    }
}

#[tokio::test]
async fn test_traceroute_result_methods() {
    let result = Probe::traceroute("127.0.0.1")
        .max_hops(3)
        .timeout_per_hop(Duration::from_millis(500))
        .send()
        .await;

    if let Ok(r) = result {
        // Test all result methods
        let _count = r.hop_count();
        let _responsive = r.responsive_hops();
        let _path = r.path();
        let _contains = r.path_contains(std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)));
    }
}

// ============================================================================
// PARIS TRACEROUTE TESTS
// ============================================================================

#[tokio::test]
async fn test_paris_localhost() {
    let result = Probe::paris("127.0.0.1")
        .max_hops(5)
        .timeout_per_hop(Duration::from_millis(500))
        .send()
        .await;

    // May fail due to permissions, but should not panic
    println!("Paris localhost result: {:?}", result.is_ok());
}

#[tokio::test]
async fn test_paris_invalid_hostname() {
    let result = Probe::paris("")
        .max_hops(5)
        .send()
        .await;

    assert!(result.is_err(), "Empty hostname should fail");
}

#[tokio::test]
async fn test_paris_max_hops_one() {
    let result = Probe::paris("127.0.0.1")
        .max_hops(1)
        .timeout_per_hop(Duration::from_millis(500))
        .send()
        .await;

    if let Ok(r) = result {
        assert!(r.hops.len() <= 1, "Should have at most 1 hop");
    }
}

#[tokio::test]
async fn test_paris_mode_icmp() {
    use multiprobe::ParisMode;

    let result = Probe::paris("127.0.0.1")
        .mode(ParisMode::Icmp)
        .max_hops(3)
        .timeout_per_hop(Duration::from_millis(500))
        .send()
        .await;

    println!("Paris ICMP mode result: {:?}", result.is_ok());
}

#[tokio::test]
async fn test_paris_flow_id() {
    use multiprobe::FlowId;

    let flow = FlowId::udp(12345, 54321);
    assert_eq!(flow.src_port, 12345);
    assert_eq!(flow.dst_port, 54321);
    assert_eq!(flow.protocol, 17); // UDP

    let tcp_flow = FlowId::tcp(80, 443);
    assert_eq!(tcp_flow.protocol, 6); // TCP

    let icmp_flow = FlowId::icmp(1234);
    assert_eq!(icmp_flow.protocol, 1); // ICMP
}

#[tokio::test]
async fn test_paris_with_load_balancing_detection() {
    let result = Probe::paris("127.0.0.1")
        .max_hops(3)
        .detect_load_balancing(true)
        .timeout_per_hop(Duration::from_millis(500))
        .send()
        .await;

    if let Ok(r) = result {
        // Just verify the field exists and has a value
        println!("Load balancing type: {}", r.load_balancing);
    }
}

// ============================================================================
// TLS PROBE TESTS
// ============================================================================

#[tokio::test]
async fn test_tls_invalid_hostname() {
    let result = Probe::tls("")
        .timeout(Duration::from_secs(2))
        .send()
        .await;

    assert!(result.is_err(), "Empty hostname should fail");
}

#[tokio::test]
async fn test_tls_localhost_no_server() {
    let result = Probe::tls("127.0.0.1")
        .port(44444) // Random port with no TLS server
        .timeout(Duration::from_millis(500))
        .send()
        .await;

    // Should complete but show failure
    if let Ok(r) = result {
        assert!(!r.success, "No TLS server should fail handshake");
        assert!(r.error.is_some(), "Should have error message");
    }
}

#[tokio::test]
async fn test_tls_custom_port() {
    let result = Probe::tls("127.0.0.1")
        .port(8443)
        .timeout(Duration::from_millis(200))
        .send()
        .await;

    // Just verify it doesn't panic
    println!("TLS custom port result: {:?}", result.is_ok());
}

#[tokio::test]
async fn test_tls_with_sni() {
    let result = Probe::tls("127.0.0.1")
        .sni("example.com")
        .timeout(Duration::from_millis(200))
        .send()
        .await;

    println!("TLS with SNI result: {:?}", result.is_ok());
}

#[tokio::test]
async fn test_tls_skip_verify() {
    let result = Probe::tls("127.0.0.1")
        .skip_verify()
        .timeout(Duration::from_millis(200))
        .send()
        .await;

    println!("TLS skip verify result: {:?}", result.is_ok());
}

#[tokio::test]
async fn test_tls_version_display() {
    use multiprobe::TlsVersion;

    assert_eq!(TlsVersion::Tls10.to_string(), "TLS 1.0");
    assert_eq!(TlsVersion::Tls11.to_string(), "TLS 1.1");
    assert_eq!(TlsVersion::Tls12.to_string(), "TLS 1.2");
    assert_eq!(TlsVersion::Tls13.to_string(), "TLS 1.3");
    assert_eq!(TlsVersion::Unknown.to_string(), "Unknown");
}

// ============================================================================
// LATENCY STATS TESTS
// ============================================================================

#[tokio::test]
async fn test_latency_localhost() {
    let result = Probe::latency("127.0.0.1", 80)
        .samples(3)
        .interval(Duration::from_millis(10))
        .send()
        .await;

    if let Ok(stats) = result {
        assert_eq!(stats.sample_count, 3);
        // Localhost probes might fail (no server), but stats should be computed
        println!("Latency stats: min={:?}, max={:?}, loss={:.1}%",
            stats.min_rtt, stats.max_rtt, stats.loss_rate * 100.0);
    }
}

#[tokio::test]
async fn test_latency_invalid_hostname() {
    let result = Probe::latency("", 80)
        .samples(1)
        .send()
        .await;

    assert!(result.is_err(), "Empty hostname should fail");
}

#[tokio::test]
async fn test_latency_stats_methods() {
    use multiprobe::LatencyStats;

    let samples = vec![
        Some(Duration::from_millis(10)),
        Some(Duration::from_millis(20)),
        Some(Duration::from_millis(15)),
        None, // Lost packet
        Some(Duration::from_millis(12)),
    ];

    let stats = LatencyStats::from_samples(&samples);

    assert_eq!(stats.sample_count, 5);
    assert_eq!(stats.success_count, 4);
    assert!((stats.loss_rate - 0.2).abs() < 0.01, "Loss rate should be 20%");
    assert_eq!(stats.min_rtt, Duration::from_millis(10));
    assert_eq!(stats.max_rtt, Duration::from_millis(20));

    // Test helper methods
    let _ = stats.has_high_jitter();
    let _ = stats.has_packet_loss();
}

#[tokio::test]
async fn test_latency_stats_empty() {
    use multiprobe::LatencyStats;

    let samples: Vec<Option<Duration>> = vec![];
    let stats = LatencyStats::from_samples(&samples);

    assert_eq!(stats.sample_count, 0);
    assert_eq!(stats.success_count, 0);
    assert_eq!(stats.loss_rate, 1.0);
}

#[tokio::test]
async fn test_latency_stats_all_loss() {
    use multiprobe::LatencyStats;

    let samples: Vec<Option<Duration>> = vec![None, None, None];
    let stats = LatencyStats::from_samples(&samples);

    assert_eq!(stats.sample_count, 3);
    assert_eq!(stats.success_count, 0);
    assert_eq!(stats.loss_rate, 1.0);
}

// ============================================================================
// MTU DISCOVERY TESTS
// ============================================================================

#[tokio::test]
async fn test_mtu_localhost() {
    let result = Probe::mtu("127.0.0.1")
        .min_mtu(68)
        .max_mtu(1500)
        .timeout(Duration::from_millis(500))
        .send()
        .await;

    // May need elevated privileges
    println!("MTU discovery result: {:?}", result.is_ok());
}

#[tokio::test]
async fn test_mtu_invalid_hostname() {
    let result = Probe::mtu("")
        .send()
        .await;

    assert!(result.is_err(), "Empty hostname should fail");
}

#[tokio::test]
async fn test_mtu_options_default() {
    use multiprobe::PmtudOptions;

    let opts = PmtudOptions::default();
    assert_eq!(opts.min_mtu, 68);
    assert_eq!(opts.max_mtu, 1500);
}

// ============================================================================
// BUFFERBLOAT TESTS
// ============================================================================

#[tokio::test]
async fn test_bufferbloat_localhost() {
    let result = Probe::bufferbloat("127.0.0.1")
        .port(80)
        .baseline_samples(2)
        .loaded_samples(2)
        .send()
        .await;

    // Will likely fail on localhost with no server
    println!("Bufferbloat result: {:?}", result.is_ok());
}

#[tokio::test]
async fn test_bufferbloat_invalid_hostname() {
    let result = Probe::bufferbloat("")
        .send()
        .await;

    assert!(result.is_err(), "Empty hostname should fail");
}

#[tokio::test]
async fn test_bufferbloat_grade() {
    use multiprobe::BufferbloatGrade;

    assert_eq!(BufferbloatGrade::A.to_string(), "A (Excellent)");
    assert_eq!(BufferbloatGrade::B.to_string(), "B (Good)");
    assert_eq!(BufferbloatGrade::C.to_string(), "C (Fair)");
    assert_eq!(BufferbloatGrade::D.to_string(), "D (Poor)");
    assert_eq!(BufferbloatGrade::F.to_string(), "F (Bad)");
}

#[tokio::test]
async fn test_bufferbloat_options_default() {
    use multiprobe::BufferbloatOptions;

    let opts = BufferbloatOptions::default();
    assert_eq!(opts.baseline_samples, 10);
    assert_eq!(opts.loaded_samples, 10);
    assert_eq!(opts.load_connections, 4);
    assert_eq!(opts.port, 443);
}

// ============================================================================
// LOAD BALANCING TYPE TESTS
// ============================================================================

#[test]
fn test_load_balancing_type_display() {
    use multiprobe::LoadBalancingType;

    assert_eq!(LoadBalancingType::None.to_string(), "None");
    assert_eq!(LoadBalancingType::PerFlow.to_string(), "Per-flow ECMP");
    assert_eq!(LoadBalancingType::PerPacket.to_string(), "Per-packet ECMP");
    assert_eq!(LoadBalancingType::Unknown.to_string(), "Unknown");
}

// ============================================================================
// CONCURRENT PARIS TESTS
// ============================================================================

#[tokio::test]
async fn test_concurrent_paris_probes() {
    let futures: Vec<_> = (0..3).map(|_| {
        Probe::paris("127.0.0.1")
            .max_hops(2)
            .timeout_per_hop(Duration::from_millis(200))
            .send()
    }).collect();

    let results = futures::future::join_all(futures).await;

    // All should complete without panic
    for result in results {
        let _ = result; // Just verify no panic
    }
}

// ============================================================================
// CONCURRENT TLS TESTS
// ============================================================================

#[tokio::test]
async fn test_concurrent_tls_probes() {
    let futures: Vec<_> = (0..3).map(|_| {
        Probe::tls("127.0.0.1")
            .port(44444)
            .timeout(Duration::from_millis(200))
            .send()
    }).collect();

    let results = futures::future::join_all(futures).await;

    // All should complete without panic
    for result in results {
        let _ = result;
    }
}

// ============================================================================
// EDGE CASE: VERY SHORT TIMEOUTS
// ============================================================================

#[tokio::test]
async fn test_paris_very_short_timeout() {
    let result = Probe::paris("8.8.8.8")
        .max_hops(1)
        .timeout_per_hop(Duration::from_millis(1))
        .send()
        .await;

    // Should complete (likely all timeouts) but not panic
    println!("Paris short timeout: {:?}", result.is_ok());
}

#[tokio::test]
async fn test_tls_very_short_timeout() {
    let result = Probe::tls("google.com")
        .timeout(Duration::from_millis(1))
        .send()
        .await;

    // Should complete but likely fail
    if let Ok(r) = result {
        // Very short timeout should cause failure
        println!("TLS short timeout success: {}", r.success);
    }
}

// ============================================================================
// REAL TARGET TESTS (require network)
// ============================================================================

#[tokio::test]
async fn test_tls_real_target() {
    let result = Probe::tls("google.com")
        .timeout(Duration::from_secs(10))
        .send()
        .await;

    match result {
        Ok(r) => {
            println!("TLS to google.com:");
            println!("  Success: {}", r.success);
            println!("  Version: {}", r.tls_version);
            println!("  Cipher: {:?}", r.cipher_suite);
            println!("  Timing: DNS={:.2}ms TCP={:.2}ms TLS={:.2}ms",
                r.timing.dns_ms(), r.timing.tcp_ms(), r.timing.tls_ms());
        }
        Err(e) => {
            println!("TLS probe failed (network issue?): {}", e);
        }
    }
}

#[tokio::test]
async fn test_latency_real_target() {
    let result = Probe::latency("google.com", 443)
        .samples(5)
        .interval(Duration::from_millis(100))
        .send()
        .await;

    match result {
        Ok(stats) => {
            println!("Latency to google.com:443:");
            println!("  Samples: {}/{}", stats.success_count, stats.sample_count);
            println!("  Min: {:.2}ms", stats.min_rtt.as_secs_f64() * 1000.0);
            println!("  Max: {:.2}ms", stats.max_rtt.as_secs_f64() * 1000.0);
            println!("  Mean: {:.2}ms", stats.mean_rtt.as_secs_f64() * 1000.0);
            println!("  Jitter: {:.2}ms", stats.jitter.as_secs_f64() * 1000.0);
        }
        Err(e) => {
            println!("Latency measurement failed: {}", e);
        }
    }
}