qudag-network 0.5.0

P2P networking layer for QuDAG - LibP2P with onion routing, dark addressing, and quantum encryption
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
use qudag_network::{
    ConnectionManager, ConnectionStatus, MessagePriority, NetworkAddress, NetworkError,
    NetworkMessage, PeerId, Router, RoutingStrategy,
};
use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;

/// Test various network error scenarios and their handling
#[tokio::test]
async fn test_network_error_types() {
    let router = Router::new();

    // Test routing error with invalid peer ID format
    let msg = NetworkMessage {
        id: "test".into(),
        source: vec![1; 32],
        destination: vec![2; 32],
        payload: vec![0; 100],
        priority: MessagePriority::Normal,
        ttl: Duration::from_secs(60),
    };

    let result = router
        .route(&msg, RoutingStrategy::Direct(vec![1, 2, 3]))
        .await;
    assert!(matches!(result, Err(NetworkError::RoutingError(_))));

    if let Err(NetworkError::RoutingError(msg)) = result {
        assert!(msg.contains("Invalid peer ID format"));
    }
}

/// Test error propagation through the network stack
#[tokio::test]
async fn test_error_propagation() {
    let manager = ConnectionManager::new(1);
    let router = Router::new();

    // Test cascading errors
    let peer_id = PeerId::random();

    // Connect peer
    manager.connect(peer_id).await.unwrap();
    manager
        .update_status(peer_id, ConnectionStatus::Connected)
        .await;

    // Simulate network failure
    manager
        .update_status(
            peer_id,
            ConnectionStatus::Failed("Network unreachable".into()),
        )
        .await;

    // Verify error state is maintained
    let status = manager.get_status(&peer_id).await;
    assert!(matches!(status, Some(ConnectionStatus::Failed(_))));

    if let Some(ConnectionStatus::Failed(error_msg)) = status {
        assert_eq!(error_msg, "Network unreachable");
    }

    // Test routing with failed peer
    router.add_peer(peer_id).await;

    let msg = NetworkMessage {
        id: "error_test".into(),
        source: vec![1; 32],
        destination: peer_id.to_bytes().to_vec(),
        payload: vec![0; 100],
        priority: MessagePriority::Normal,
        ttl: Duration::from_secs(60),
    };

    let result = router
        .route(&msg, RoutingStrategy::Direct(peer_id.to_bytes().to_vec()))
        .await;
    assert!(result.is_ok()); // Router should handle peer failure gracefully
}

/// Test error recovery mechanisms
#[tokio::test]
async fn test_error_recovery() {
    let manager = ConnectionManager::new(5);
    let peer_id = PeerId::random();

    // Normal connection
    manager.connect(peer_id).await.unwrap();
    manager
        .update_status(peer_id, ConnectionStatus::Connected)
        .await;

    // Simulate transient failure
    manager
        .update_status(
            peer_id,
            ConnectionStatus::Failed("Temporary network issue".into()),
        )
        .await;

    // Attempt recovery
    manager.disconnect(&peer_id).await;
    let recovery_result = manager.connect(peer_id).await;
    assert!(recovery_result.is_ok());

    // Update to connected state
    manager
        .update_status(peer_id, ConnectionStatus::Connected)
        .await;

    // Verify recovery
    let final_status = manager.get_status(&peer_id).await;
    assert_eq!(final_status, Some(ConnectionStatus::Connected));
}

/// Test timeout handling in various scenarios
#[tokio::test]
async fn test_timeout_handling() {
    let manager = ConnectionManager::new(10);
    let router = Router::new();

    // Test connection timeout
    let peer_id = PeerId::random();

    // Simulate slow connection
    manager.connect(peer_id).await.unwrap();

    // Don't immediately update to connected - simulates timeout scenario
    tokio::time::sleep(Duration::from_millis(100)).await;

    let status = manager.get_status(&peer_id).await;
    assert_eq!(status, Some(ConnectionStatus::Connecting));

    // Test routing timeout with insufficient peers
    let msg_with_short_ttl = NetworkMessage {
        id: "timeout_test".into(),
        source: vec![1; 32],
        destination: vec![2; 32],
        payload: vec![0; 100],
        priority: MessagePriority::Normal,
        ttl: Duration::from_millis(1), // Very short TTL
    };

    // Add a peer for routing
    router.add_peer(PeerId::random()).await;

    let result = router
        .route(&msg_with_short_ttl, RoutingStrategy::Anonymous { hops: 10 })
        .await;

    // Should handle timeout gracefully
    match result {
        Ok(_) => {}                              // Routing succeeded despite short TTL
        Err(NetworkError::RoutingError(_)) => {} // Expected for insufficient peers
        Err(e) => panic!("Unexpected error: {:?}", e),
    }
}

/// Test resource exhaustion error handling
#[tokio::test]
async fn test_resource_exhaustion() {
    let small_manager = ConnectionManager::new(2); // Very limited capacity
    let mut peers = Vec::new();

    // Fill capacity
    for _ in 0..2 {
        let peer_id = PeerId::random();
        peers.push(peer_id);
        small_manager.connect(peer_id).await.unwrap();
        small_manager
            .update_status(peer_id, ConnectionStatus::Connected)
            .await;
    }

    // Verify at capacity
    assert_eq!(small_manager.connection_count().await, 2);

    // Try to exceed capacity
    let excess_peer = PeerId::random();
    let result = small_manager.connect(excess_peer).await;

    // Should handle gracefully (not error, but apply limits)
    assert!(result.is_ok());

    // Should not exceed capacity
    let final_count = small_manager.connection_count().await;
    assert!(final_count <= 2);

    // Test that existing connections still work
    for peer in &peers {
        let status = small_manager.get_status(peer).await;
        assert!(status.is_some());
    }
}

/// Test malformed data handling
#[tokio::test]
async fn test_malformed_data_handling() {
    let router = Router::new();

    // Test with null bytes in message ID
    let msg_with_nulls = NetworkMessage {
        id: "test\0\0\0message".into(),
        source: vec![1; 32],
        destination: vec![2; 32],
        payload: vec![0; 100],
        priority: MessagePriority::Normal,
        ttl: Duration::from_secs(60),
    };

    router.add_peer(PeerId::random()).await;
    let result = router.route(&msg_with_nulls, RoutingStrategy::Flood).await;
    assert!(result.is_ok()); // Should handle gracefully

    // Test with extremely long message ID
    let msg_with_long_id = NetworkMessage {
        id: "x".repeat(100_000),
        source: vec![1; 32],
        destination: vec![2; 32],
        payload: vec![0; 100],
        priority: MessagePriority::Normal,
        ttl: Duration::from_secs(60),
    };

    let result = router
        .route(&msg_with_long_id, RoutingStrategy::Flood)
        .await;
    assert!(result.is_ok()); // Should handle gracefully

    // Test with malformed peer ID bytes
    let msg_with_bad_peer = NetworkMessage {
        id: "test".into(),
        source: vec![0xFF; 32], // Valid size but potentially problematic values
        destination: vec![0x00; 32],
        payload: vec![0; 100],
        priority: MessagePriority::Normal,
        ttl: Duration::from_secs(60),
    };

    let result = router
        .route(&msg_with_bad_peer, RoutingStrategy::Anonymous { hops: 1 })
        .await;
    // Should either succeed or fail gracefully, not panic
    assert!(result.is_ok() || matches!(result, Err(NetworkError::RoutingError(_))));
}

/// Test network address validation and error handling
#[tokio::test]
async fn test_network_address_errors() {
    // Test valid address creation
    let valid_addr = NetworkAddress::new([127, 0, 0, 1], 8080);
    assert_eq!(valid_addr.to_socket_addr(), "127.0.0.1:8080");

    // Test edge cases
    let addr_zero_port = NetworkAddress::new([0, 0, 0, 0], 0);
    assert_eq!(addr_zero_port.to_socket_addr(), "0.0.0.0:0");

    let addr_max_port = NetworkAddress::new([255, 255, 255, 255], 65535);
    assert_eq!(addr_max_port.to_socket_addr(), "255.255.255.255:65535");

    // Test with IPv6
    let ipv6_addr = NetworkAddress::from_ip_port(IpAddr::V6("::1".parse().unwrap()), 8080);
    assert_eq!(ipv6_addr.to_socket_addr(), "[::1]:8080");
}

/// Test concurrent error conditions
#[tokio::test]
async fn test_concurrent_error_conditions() {
    let manager = ConnectionManager::new(5);
    let peer_id = PeerId::random();

    // Connect peer
    manager.connect(peer_id).await.unwrap();
    manager
        .update_status(peer_id, ConnectionStatus::Connected)
        .await;

    // Spawn concurrent operations that may cause errors
    let mut handles = Vec::new();

    // Task 1: Rapid status updates
    let manager1 = manager.clone();
    let handle1 = tokio::spawn(async move {
        for i in 0..100 {
            let status = if i % 3 == 0 {
                ConnectionStatus::Failed(format!("Error {}", i))
            } else {
                ConnectionStatus::Connected
            };
            manager1.update_status(peer_id, status).await;
        }
    });
    handles.push(handle1);

    // Task 2: Rapid disconnects/reconnects
    let manager2 = manager.clone();
    let handle2 = tokio::spawn(async move {
        for _ in 0..50 {
            manager2.disconnect(&peer_id).await;
            let _ = manager2.connect(peer_id).await;
        }
    });
    handles.push(handle2);

    // Task 3: Metrics updates
    let manager3 = manager.clone();
    let handle3 = tokio::spawn(async move {
        for i in 0..100 {
            manager3.update_metrics(i as f64, (i % 1000) as u64).await;
        }
    });
    handles.push(handle3);

    // Wait for all tasks to complete
    for handle in handles {
        handle.await.unwrap();
    }

    // Verify system is still in a consistent state
    let final_status = manager.get_status(&peer_id).await;
    assert!(final_status.is_some() || final_status.is_none()); // Any state is valid

    let metrics = manager.get_metrics().await;
    assert!(metrics.messages_per_second >= 0.0);
    assert!(metrics.connections >= 0);
}

/// Test error message formatting and content
#[tokio::test]
async fn test_error_message_quality() {
    // Test that error messages are informative
    let router = Router::new();

    let msg = NetworkMessage {
        id: "test".into(),
        source: vec![1; 32],
        destination: vec![2; 32],
        payload: vec![0; 100],
        priority: MessagePriority::Normal,
        ttl: Duration::from_secs(60),
    };

    // Test insufficient peers error
    let result = router
        .route(&msg, RoutingStrategy::Anonymous { hops: 10 })
        .await;

    if let Err(NetworkError::RoutingError(error_msg)) = result {
        // Error message should be descriptive
        assert!(error_msg.contains("peers") || error_msg.contains("routing"));
        assert!(!error_msg.is_empty());
        println!("Routing error message: {}", error_msg);
    }

    // Test invalid peer ID error
    let result = router
        .route(&msg, RoutingStrategy::Direct(vec![1, 2]))
        .await;

    if let Err(NetworkError::RoutingError(error_msg)) = result {
        assert!(error_msg.contains("Invalid") || error_msg.contains("format"));
        assert!(!error_msg.is_empty());
        println!("Invalid peer ID error message: {}", error_msg);
    }
}

/// Test error boundaries and isolation
#[tokio::test]
async fn test_error_isolation() {
    let manager1 = ConnectionManager::new(10);
    let manager2 = ConnectionManager::new(10);

    let peer1 = PeerId::random();
    let peer2 = PeerId::random();

    // Setup both managers
    manager1.connect(peer1).await.unwrap();
    manager2.connect(peer2).await.unwrap();

    manager1
        .update_status(peer1, ConnectionStatus::Connected)
        .await;
    manager2
        .update_status(peer2, ConnectionStatus::Connected)
        .await;

    // Cause error in manager1
    manager1
        .update_status(peer1, ConnectionStatus::Failed("Isolated error".into()))
        .await;

    // Verify manager2 is unaffected
    let status2 = manager2.get_status(&peer2).await;
    assert_eq!(status2, Some(ConnectionStatus::Connected));

    let metrics2 = manager2.get_metrics().await;
    assert_eq!(metrics2.connections, 1);

    // Verify manager1 error is contained
    let status1 = manager1.get_status(&peer1).await;
    assert!(matches!(status1, Some(ConnectionStatus::Failed(_))));

    // Manager1 should still be functional for other operations
    let new_peer = PeerId::random();
    let result = manager1.connect(new_peer).await;
    assert!(result.is_ok());
}

/// Test graceful degradation under error conditions
#[tokio::test]
async fn test_graceful_degradation_with_errors() {
    let manager = ConnectionManager::new(5);
    let mut peers = Vec::new();

    // Setup several connections
    for _ in 0..5 {
        let peer_id = PeerId::random();
        peers.push(peer_id);
        manager.connect(peer_id).await.unwrap();
        manager
            .update_status(peer_id, ConnectionStatus::Connected)
            .await;
    }

    // Introduce progressive failures
    for (i, peer) in peers.iter().enumerate() {
        if i % 2 == 0 {
            manager
                .update_status(*peer, ConnectionStatus::Failed(format!("Failure {}", i)))
                .await;
        }
    }

    // System should still be partially functional
    let remaining_connections = peers
        .iter()
        .filter(|peer| {
            futures::executor::block_on(async {
                matches!(
                    manager.get_status(peer).await,
                    Some(ConnectionStatus::Connected)
                )
            })
        })
        .count();

    assert!(
        remaining_connections >= 2,
        "Too few connections remain: {}",
        remaining_connections
    );

    // Metrics should still be accessible
    let metrics = manager.get_metrics().await;
    assert!(metrics.connections >= 0);

    // New connections should still work
    let new_peer = PeerId::random();
    let result = manager.connect(new_peer).await;
    assert!(result.is_ok());
}