qudag-network 0.5.0

P2P networking layer for QuDAG - LibP2P with onion routing, dark addressing, and quantum encryption
Documentation
use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;

// Import types module (simplified version for testing)
mod types {
    use serde::{Serialize, Deserialize};
    use std::time::Duration;
    use std::net::{IpAddr, Ipv4Addr};
    use thiserror::Error;

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub struct NetworkAddress {
        pub ip: IpAddr,
        pub port: u16,
    }

    impl NetworkAddress {
        pub fn new(ip_parts: [u8; 4], port: u16) -> Self {
            Self {
                ip: IpAddr::V4(Ipv4Addr::new(ip_parts[0], ip_parts[1], ip_parts[2], ip_parts[3])),
                port,
            }
        }
        
        pub fn from_ip_port(ip: IpAddr, port: u16) -> Self {
            Self { ip, port }
        }
        
        pub fn to_socket_addr(&self) -> String {
            format!("{}:{}", self.ip, self.port)
        }
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum MessagePriority {
        High,
        Normal,
        Low,
    }

    #[derive(Debug, Clone)]
    pub struct NetworkMetrics {
        pub messages_per_second: f64,
        pub connections: usize,
        pub avg_latency: Duration,
        pub memory_usage: usize,
    }

    impl Default for NetworkMetrics {
        fn default() -> Self {
            Self {
                messages_per_second: 0.0,
                connections: 0,
                avg_latency: Duration::from_millis(0),
                memory_usage: 0,
            }
        }
    }
}

fn main() {
    println!("๐Ÿงช Running QuDAG Network Types Tests");
    println!("====================================");

    // Test NetworkAddress
    test_network_address();
    
    // Test MessagePriority
    test_message_priority();
    
    // Test NetworkMetrics
    test_network_metrics();
    
    println!("\nโœ… All basic type tests passed!");
}

fn test_network_address() {
    println!("\n๐Ÿ“ก Testing NetworkAddress...");
    
    // Test creation from IP parts
    let addr1 = types::NetworkAddress::new([192, 168, 1, 1], 8080);
    assert_eq!(addr1.ip, IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)));
    assert_eq!(addr1.port, 8080);
    println!("  โœ“ Creation from IP parts");
    
    // Test creation from IP and port
    let ip = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1));
    let addr2 = types::NetworkAddress::from_ip_port(ip, 9090);
    assert_eq!(addr2.ip, ip);
    assert_eq!(addr2.port, 9090);
    println!("  โœ“ Creation from IP and port");
    
    // Test socket address string
    let socket_str = addr1.to_socket_addr();
    assert_eq!(socket_str, "192.168.1.1:8080");
    println!("  โœ“ Socket address string formatting");
    
    // Test equality
    let addr3 = types::NetworkAddress::new([192, 168, 1, 1], 8080);
    assert_eq!(addr1, addr3);
    println!("  โœ“ Address equality");
}

fn test_message_priority() {
    println!("\n๐Ÿ“ Testing MessagePriority...");
    
    let high = types::MessagePriority::High;
    let normal = types::MessagePriority::Normal;
    let low = types::MessagePriority::Low;
    
    // Test equality
    assert_eq!(high, types::MessagePriority::High);
    assert_ne!(high, normal);
    println!("  โœ“ Priority equality");
    
    // Test ordering (would need PartialOrd implementation)
    println!("  โœ“ Priority types created successfully");
}

fn test_network_metrics() {
    println!("\n๐Ÿ“Š Testing NetworkMetrics...");
    
    // Test default construction
    let metrics = types::NetworkMetrics::default();
    assert_eq!(metrics.messages_per_second, 0.0);
    assert_eq!(metrics.connections, 0);
    assert_eq!(metrics.avg_latency, Duration::from_millis(0));
    assert_eq!(metrics.memory_usage, 0);
    println!("  โœ“ Default metrics creation");
    
    // Test custom construction
    let custom_metrics = types::NetworkMetrics {
        messages_per_second: 100.5,
        connections: 10,
        avg_latency: Duration::from_millis(50),
        memory_usage: 1024,
    };
    assert_eq!(custom_metrics.messages_per_second, 100.5);
    assert_eq!(custom_metrics.connections, 10);
    println!("  โœ“ Custom metrics creation");
}

#[cfg(test)]
mod tests {
    use super::types::*;
    use std::net::{IpAddr, Ipv4Addr};
    use std::time::Duration;

    #[test]
    fn test_network_address_serialization() {
        let addr = NetworkAddress::new([127, 0, 0, 1], 8080);
        
        // This would test serialization if we had proper serde setup
        // For now, just test the structure
        assert_eq!(addr.port, 8080);
        assert_eq!(addr.ip, IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
    }
    
    #[test]
    fn test_message_priority_memory_safety() {
        // Test that enum variants don't cause memory issues
        let priorities = vec![
            MessagePriority::High,
            MessagePriority::Normal,
            MessagePriority::Low,
        ];
        
        for priority in priorities {
            // Just ensure we can access the enum safely
            match priority {
                MessagePriority::High => {},
                MessagePriority::Normal => {},
                MessagePriority::Low => {},
            }
        }
    }
    
    #[test]
    fn test_network_metrics_performance() {
        let mut metrics = NetworkMetrics::default();
        
        // Simulate updating metrics (constant time operations)
        for i in 0..1000 {
            metrics.connections = i;
            metrics.messages_per_second = i as f64;
        }
        
        assert_eq!(metrics.connections, 999);
        assert_eq!(metrics.messages_per_second, 999.0);
    }
}