ftr 0.10.0

A fast, parallel ICMP traceroute with ASN lookup, reverse DNS, and ISP detection
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
//! Integration tests for the ftr library

use ftr::{
    AsnInfo, ProbeProtocol, SegmentType, SocketMode, TracerouteConfigBuilder, TracerouteError,
    TracerouteResult, trace, trace_with_config,
};
use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;

// Helper to run traces with timeout
async fn trace_with_timeout(target: &str) -> Result<TracerouteResult, String> {
    match tokio::time::timeout(Duration::from_secs(10), trace(target)).await {
        Ok(Ok(result)) => Ok(result),
        Ok(Err(e)) => Err(format!("Trace error: {}", e)),
        Err(_) => Err("Timeout after 10 seconds".to_string()),
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_basic_trace() {
    // Test basic tracing to localhost
    println!("Starting test_basic_trace");

    match trace_with_timeout("127.0.0.1").await {
        Ok(trace_result) => {
            assert_eq!(trace_result.target, "127.0.0.1");
            assert!(!trace_result.hops.is_empty());
            assert!(trace_result.total_duration > Duration::ZERO);
        }
        Err(e) => {
            // Permission errors are expected in test environments
            eprintln!("Trace failed (expected in test environment): {}", e);
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_trace_with_custom_config() {
    let config = TracerouteConfigBuilder::new()
        .target("localhost")
        .max_hops(5)
        .probe_timeout(Duration::from_millis(100))
        .overall_timeout(Duration::from_secs(2))
        .enable_asn_lookup(false)
        .enable_rdns(false)
        .build()
        .expect("failed to build traceroute config");

    let result = trace_with_config(config).await;

    match result {
        Ok(trace_result) => {
            assert_eq!(trace_result.target, "localhost");
            assert!(trace_result.max_ttl().unwrap_or(0) <= 5);

            // With ASN and rDNS disabled, these should be empty
            for hop in &trace_result.hops {
                assert!(hop.asn_info.is_none());
                assert!(hop.hostname.is_none());
            }
        }
        Err(e) => {
            eprintln!("Trace failed (expected in test environment): {}", e);
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_config_validation() {
    // Test invalid configurations

    // Empty target
    let result = TracerouteConfigBuilder::new().build();
    assert!(result.is_err());
    assert_eq!(
        result.expect_err("empty target must fail validation"),
        ftr::ConfigError::MissingTarget
    );

    // Invalid TTL values
    let result = TracerouteConfigBuilder::new()
        .target("example.com")
        .start_ttl(0)
        .build();
    assert!(result.is_err());

    // max_hops < start_ttl
    let result = TracerouteConfigBuilder::new()
        .target("example.com")
        .start_ttl(10)
        .max_hops(5)
        .probe_timeout(Duration::from_millis(500))
        .overall_timeout(Duration::from_secs(3))
        .build();
    assert!(result.is_err());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_trace_with_ip_and_hostname() {
    // Test with IP address
    let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
    let config = TracerouteConfigBuilder::new()
        .target("127.0.0.1")
        .target_ip(ip)
        .max_hops(3)
        .build()
        .expect("failed to build traceroute config");

    let result = trace_with_config(config).await;

    match result {
        Ok(trace_result) => {
            assert_eq!(trace_result.target_ip, ip);
        }
        Err(_) => {
            // Permission errors are acceptable
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_hop_classification() {
    let config = TracerouteConfigBuilder::new()
        .target("8.8.8.8")
        .max_hops(10)
        .enable_asn_lookup(true)
        .build()
        .expect("failed to build traceroute config");

    match trace_with_config(config).await {
        Ok(trace_result) => {
            // Check that hops are classified
            let segments: Vec<SegmentType> = trace_result.hops.iter().map(|h| h.segment).collect();

            // Should have at least one hop
            assert!(!segments.is_empty());

            // Segments generally progress outward: LAN -> ISP -> TRANSIT/DESTINATION
            // (though not all may be present)
            let mut last_segment = SegmentType::Unknown;
            for segment in segments {
                match (last_segment, segment) {
                    (SegmentType::Unknown, _) => {}
                    (SegmentType::Lan, SegmentType::Isp) => {}
                    (SegmentType::Lan, SegmentType::Transit) => {}
                    (SegmentType::Lan, SegmentType::Destination) => {}
                    (SegmentType::Isp, SegmentType::Transit) => {}
                    (SegmentType::Isp, SegmentType::Destination) => {}
                    (a, b) if a == b => {} // Same segment is fine
                    _ => {
                        // Unexpected transition
                    }
                }
                last_segment = segment;
            }
        }
        Err(_) => {
            // Network/permission errors are acceptable
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_result_methods() {
    // Create a mock result to test methods
    let hops = vec![
        ftr::ClassifiedHopInfo {
            ttl: 1,
            segment: SegmentType::Lan,
            hostname: Some("router.local".to_string()),
            addr: Some(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1))),
            asn_info: None,
            rtt: Some(Duration::from_millis(5)),
        },
        ftr::ClassifiedHopInfo {
            ttl: 2,
            segment: SegmentType::Isp,
            hostname: None,
            addr: Some(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))),
            asn_info: Some(AsnInfo {
                asn: 12345,
                prefix: "10.0.0.0/8".to_string(),
                country_code: "US".to_string(),
                registry: "ARIN".to_string(),
                name: "Example ISP".to_string(),
            }),
            rtt: Some(Duration::from_millis(15)),
        },
        ftr::ClassifiedHopInfo {
            ttl: 3,
            segment: SegmentType::Destination,
            hostname: Some("destination.com".to_string()),
            addr: Some(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))),
            asn_info: None,
            rtt: None, // No response
        },
    ];

    let result = ftr::TracerouteResult {
        target: "8.8.8.8".to_string(),
        target_ip: IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)),
        hops,
        isp_info: Some(ftr::IspInfo {
            public_ip: IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)),
            asn: 12345,
            name: "Example ISP".to_string(),
            hostname: None,
        }),
        destination_asn: None,
        protocol_used: ProbeProtocol::Icmp,
        socket_mode_used: SocketMode::Raw,
        destination_reached: true,
        total_duration: Duration::from_millis(100),
    };

    // Test hop_count
    assert_eq!(result.hop_count(), 3);

    // Test max_ttl
    assert_eq!(result.max_ttl(), Some(3));

    // Test destination_hop
    let dest_hop = result.destination_hop();
    assert!(dest_hop.is_some());
    assert_eq!(dest_hop.expect("destination hop should exist").ttl, 3);

    // Test has_response_at_ttl
    assert!(result.has_response_at_ttl(1));
    assert!(result.has_response_at_ttl(2));
    assert!(result.has_response_at_ttl(3));
    assert!(!result.has_response_at_ttl(4));

    // Test hops_with_asn
    let asn_hops = result.hops_with_asn();
    assert_eq!(asn_hops.len(), 1);
    assert_eq!(asn_hops[0].ttl, 2);

    // Test hops_in_segment
    let lan_hops = result.hops_in_segment(SegmentType::Lan);
    assert_eq!(lan_hops.len(), 1);
    let isp_hops = result.hops_in_segment(SegmentType::Isp);
    assert_eq!(isp_hops.len(), 1);
    let dest_hops = result.hops_in_segment(SegmentType::Destination);
    assert_eq!(dest_hops.len(), 1);

    // Test average_rtt_ms
    let avg_rtt = result.average_rtt_ms();
    assert!(avg_rtt.is_some());
    assert_eq!(avg_rtt.expect("average RTT should be present"), 10.0); // (5 + 15) / 2, hop 3 has no RTT
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_error_types() {
    // Test various error conditions with structured errors

    // Invalid target (empty) - should get ConfigError
    let result = trace("").await;
    assert!(
        matches!(&result, Err(TracerouteError::ConfigError(_))),
        "Expected ConfigError for empty target, got: {:?}",
        result
    );
    if let Err(TracerouteError::ConfigError(err)) = &result {
        println!("Got expected ConfigError: {}", err);
        assert_eq!(*err, ftr::ConfigError::MissingTarget);
    }

    // Test unimplemented TCP protocol
    let config = TracerouteConfigBuilder::new()
        .target("127.0.0.1")
        .protocol(ProbeProtocol::Tcp)
        .build()
        .expect("failed to build traceroute config");

    let result = trace_with_config(config).await;
    assert!(
        matches!(
            &result,
            Err(TracerouteError::NotImplemented { feature }) if feature == "TCP traceroute"
        ),
        "Expected NotImplemented error for TCP, got: {:?}",
        result
    );

    // IPv6 targets: unprivileged DGRAM ICMPv6 traceroute on macOS,
    // unprivileged UDP + IPV6_RECVERR on Linux, and the unprivileged
    // Icmp6SendEcho2 API on Windows; the typed Ipv6NotSupported error on
    // platforms without v6 probe support.
    let result = trace("::1").await;
    #[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
    {
        let v6_trace = result.expect("IPv6 loopback trace should succeed unprivileged");
        assert!(v6_trace.destination_reached);
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    assert!(
        matches!(&result, Err(TracerouteError::Ipv6NotSupported)),
        "Expected Ipv6NotSupported error, got: {:?}",
        result
    );

    // Test resolution error
    let config = TracerouteConfigBuilder::new()
        .target("invalid.host.that.does.not.exist.example")
        .build()
        .expect("failed to build traceroute config");

    let result = trace_with_config(config).await;
    match result {
        Err(TracerouteError::ResolutionError(msg)) => {
            println!("Got expected ResolutionError: {}", msg);
            // Just check that we got a non-empty error message
            assert!(!msg.is_empty());
        }
        Err(e) => {
            // Could be other error if DNS somehow resolved it
            println!("Got different error (might be OK): {:?}", e);
        }
        Ok(_) => {
            println!("Warning: invalid hostname somehow resolved, skipping test");
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_caching_behavior() {
    // Test that caching works by doing multiple traces
    let config1 = TracerouteConfigBuilder::new()
        .target("127.0.0.1")
        .max_hops(3)
        .enable_asn_lookup(true)
        .enable_rdns(true)
        .build()
        .expect("failed to build traceroute config");

    let config2 = TracerouteConfigBuilder::new()
        .target("127.0.0.1")
        .max_hops(3)
        .enable_asn_lookup(true)
        .enable_rdns(true)
        .build()
        .expect("failed to build traceroute config");

    // Clear caches before test
    // Cache clearing removed - each Ftr instance has its own caches

    // First trace - cold cache
    let result1 = trace_with_config(config1).await;

    // Second trace - warm cache
    let result2 = trace_with_config(config2).await;

    // Both should have same success/failure status
    assert_eq!(result1.is_ok(), result2.is_ok());

    if result1.is_ok() && result2.is_ok() {
        // Check cache has entries
        // Cache check removed - caches are now instance-specific
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_public_ip_parameter() {
    let public_ip = IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1));

    let config = TracerouteConfigBuilder::new()
        .target("127.0.0.1")
        .max_hops(1)
        .public_ip(public_ip)
        .enable_asn_lookup(true)
        .build()
        .expect("failed to build traceroute config");

    match trace_with_config(config).await {
        Ok(result) => {
            if let Some(isp_info) = result.isp_info {
                // Should use the provided public IP
                assert_eq!(isp_info.public_ip, public_ip);
            }
        }
        Err(_) => {
            // Errors are acceptable in test environment
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_different_protocols() {
    // Test that we can specify different protocols
    let protocols = vec![ProbeProtocol::Icmp, ProbeProtocol::Udp, ProbeProtocol::Tcp];

    for protocol in protocols {
        let config = TracerouteConfigBuilder::new()
            .target("127.0.0.1")
            .protocol(protocol)
            .max_hops(3)
            .build()
            .expect("failed to build traceroute config");

        let _ = trace_with_config(config).await;
        // We don't assert on results as different protocols have different permission requirements
    }
}