518 lines
18 KiB
Zig
518 lines
18 KiB
Zig
//! Comprehensive tests for node management and routing
|
|
//!
|
|
//! Tests cover node lifecycle, routing decisions, message processing,
|
|
//! and routing table management.
|
|
|
|
const std = @import("std");
|
|
const testing = std.testing;
|
|
const Allocator = std.mem.Allocator;
|
|
const Node = @import("../../src/network/node.zig").Node;
|
|
const NodeState = @import("../../src/network/node.zig").NodeState;
|
|
const NodeConfig = @import("../../src/network/node.zig").NodeConfig;
|
|
const NodeStats = @import("../../src/network/node.zig").NodeStats;
|
|
const RoutingMessage = @import("../../src/network/node.zig").RoutingMessage;
|
|
const RoutingMessageType = @import("../../src/network/node.zig").RoutingMessageType;
|
|
const RoutingDecision = @import("../../src/network/node.zig").RoutingDecision;
|
|
const RoutingReason = @import("../../src/network/node.zig").RoutingReason;
|
|
const RoutingTableEntry = @import("../../src/network/node.zig").RoutingTableEntry;
|
|
const NetworkTopology = @import("../../src/network/topology.zig").NetworkTopology;
|
|
const PeerId = @import("../../src/network/peer.zig").PeerId;
|
|
const Address = @import("../../src/core/address.zig").Address;
|
|
|
|
test "Node creation and initialization" {
|
|
const allocator = testing.allocator;
|
|
|
|
const node_id = PeerId.init();
|
|
var address = Address.init();
|
|
address.port = 8080;
|
|
|
|
var topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer topology.deinit();
|
|
|
|
const config = NodeConfig{};
|
|
var node = Node.init(allocator, node_id, address, &topology, config);
|
|
defer node.deinit();
|
|
|
|
try testing.expectEqual(node_id, node.id);
|
|
try testing.expectEqual(address, node.address);
|
|
try testing.expectEqual(NodeState.Offline, node.state);
|
|
try testing.expectEqual(@as(u64, 0), node.sequence_counter);
|
|
try testing.expectEqual(config, node.config);
|
|
try testing.expectEqual(@as(usize, 0), node.neighbors.items.len);
|
|
try testing.expectEqual(@as(usize, 0), node.routing_table.size());
|
|
}
|
|
|
|
test "Node lifecycle management" {
|
|
const allocator = testing.allocator;
|
|
|
|
const node_id = PeerId.init();
|
|
var address = Address.init();
|
|
|
|
var topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer topology.deinit();
|
|
|
|
const config = NodeConfig{};
|
|
var node = Node.init(allocator, node_id, address, &topology, config);
|
|
defer node.deinit();
|
|
|
|
// Start node
|
|
try node.start();
|
|
try testing.expectEqual(NodeState.Online, node.state);
|
|
try testing.expect(node.isOperational());
|
|
|
|
// Stop node
|
|
node.stop();
|
|
try testing.expectEqual(NodeState.Offline, node.state);
|
|
try testing.expect(!node.isOperational());
|
|
|
|
// Try to start again
|
|
try node.start();
|
|
try testing.expectEqual(NodeState.Online, node.state);
|
|
|
|
node.stop();
|
|
}
|
|
|
|
test "Node configuration options" {
|
|
const config1 = NodeConfig{
|
|
.is_hub = true,
|
|
.is_super_node = false,
|
|
.is_gateway = false,
|
|
.max_routing_table_size = 500,
|
|
.routing_update_interval = 30,
|
|
.msg_rate_limit = 2000,
|
|
};
|
|
|
|
try testing.expect(config1.is_hub);
|
|
try testing.expect(!config1.is_super_node);
|
|
try testing.expect(!config1.is_gateway);
|
|
try testing.expectEqual(@as(usize, 500), config1.max_routing_table_size);
|
|
try testing.expectEqual(@as(u32, 30), config1.routing_update_interval);
|
|
try testing.expectEqual(@as(u32, 2000), config1.msg_rate_limit);
|
|
|
|
const config2 = NodeConfig{
|
|
.is_super_node = true,
|
|
.preferred_role = .Hub,
|
|
};
|
|
|
|
try testing.expect(config2.is_super_node);
|
|
try testing.expectEqual(.Hub, config2.preferred_role.?);
|
|
}
|
|
|
|
test "Routing message creation and encoding" {
|
|
const allocator = testing.allocator;
|
|
|
|
const source_id = PeerId.init();
|
|
const dest_id = PeerId.init();
|
|
const payload = "Hello, routing world!";
|
|
|
|
var message = RoutingMessage.init(.Data, source_id, dest_id, 42, payload);
|
|
|
|
try testing.expectEqual(RoutingMessageType.Data, message.msg_type);
|
|
try testing.expectEqual(source_id, message.source_id);
|
|
try testing.expectEqual(dest_id, message.dest_id.?);
|
|
try testing.expectEqual(@as(u64, 42), message.sequence);
|
|
try testing.expectEqual(@as(u8, 64), message.ttl); // Default TTL
|
|
try testing.expect(std.mem.eql(u8, payload, message.payload));
|
|
try testing.expect(message.timestamp > 0);
|
|
|
|
// Test encoding and decoding
|
|
var buffer = Buffer.init(allocator);
|
|
defer buffer.deinit();
|
|
|
|
try message.encode(&buffer);
|
|
|
|
const decoded = try RoutingMessage.decode(&buffer);
|
|
try testing.expectEqual(RoutingMessageType.Data, decoded.msg_type);
|
|
try testing.expectEqual(source_id, decoded.source_id);
|
|
try testing.expectEqual(dest_id, decoded.dest_id.?);
|
|
try testing.expectEqual(@as(u64, 42), decoded.sequence);
|
|
try testing.expectEqual(@as(u8, 64), decoded.ttl);
|
|
try testing.expect(std.mem.eql(u8, payload, decoded.payload));
|
|
}
|
|
|
|
test "Routing message with broadcast (no destination)" {
|
|
const allocator = testing.allocator;
|
|
|
|
const source_id = PeerId.init();
|
|
const payload = "Broadcast message";
|
|
|
|
var message = RoutingMessage.init(.Broadcast, source_id, null, 1, payload);
|
|
|
|
try testing.expectEqual(null, message.dest_id);
|
|
|
|
var buffer = Buffer.init(allocator);
|
|
defer buffer.deinit();
|
|
|
|
try message.encode(&buffer);
|
|
|
|
const decoded = try RoutingMessage.decode(&buffer);
|
|
try testing.expectEqual(null, decoded.dest_id);
|
|
try testing.expect(std.mem.eql(u8, payload, decoded.payload));
|
|
}
|
|
|
|
test "Routing message TTL and expiration" {
|
|
const source_id = PeerId.init();
|
|
const dest_id = PeerId.init();
|
|
const payload = "Test message";
|
|
|
|
var message = RoutingMessage.init(.Data, source_id, dest_id, 1, payload);
|
|
|
|
// Initial TTL should be 64
|
|
try testing.expectEqual(@as(u8, 64), message.ttl);
|
|
|
|
// Decrement TTL
|
|
var valid = message.decrementTTL();
|
|
try testing.expect(valid);
|
|
try testing.expectEqual(@as(u8, 63), message.ttl);
|
|
|
|
// Decrement multiple times
|
|
for (0..10) |_| {
|
|
valid = message.decrementTTL();
|
|
}
|
|
try testing.expect(valid);
|
|
try testing.expectEqual(@as(u8, 53), message.ttl);
|
|
|
|
// Set TTL to 1 and decrement
|
|
message.ttl = 1;
|
|
valid = message.decrementTTL();
|
|
try testing.expect(valid);
|
|
try testing.expectEqual(@as(u8, 0), message.ttl);
|
|
|
|
// Next decrement should return false
|
|
valid = message.decrementTTL();
|
|
try testing.expect(!valid);
|
|
|
|
// Test expiration
|
|
message.timestamp = std.time.timestamp() - 600; // 10 minutes ago
|
|
try testing.expect(message.isExpired());
|
|
|
|
message.timestamp = std.time.timestamp() - 200; // 3 minutes ago
|
|
try testing.expect(!message.isExpired());
|
|
}
|
|
|
|
test "Routing table entry management" {
|
|
const dest_id = PeerId.init();
|
|
const next_hop = PeerId.init();
|
|
|
|
var entry = RoutingTableEntry.init(dest_id, next_hop, 3, false);
|
|
|
|
try testing.expectEqual(dest_id, entry.dest_id);
|
|
try testing.expectEqual(next_hop, entry.next_hop);
|
|
try testing.expectEqual(@as(u8, 3), entry.distance);
|
|
try testing.expectEqual(@as(u8, 70), entry.quality); // Non-direct connection
|
|
try testing.expect(!entry.is_direct);
|
|
try testing.expect(entry.isValid()); // Should be valid initially
|
|
|
|
// Update entry
|
|
const new_next_hop = PeerId.init();
|
|
entry.update(new_next_hop, 2, 90);
|
|
|
|
try testing.expectEqual(new_next_hop, entry.next_hop);
|
|
try testing.expectEqual(@as(u8, 2), entry.distance);
|
|
try testing.expectEqual(@as(u8, 90), entry.quality);
|
|
|
|
// Test direct connection entry
|
|
var direct_entry = RoutingTableEntry.init(dest_id, next_hop, 1, true);
|
|
try testing.expectEqual(@as(u8, 100), direct_entry.quality);
|
|
try testing.expect(direct_entry.is_direct);
|
|
}
|
|
|
|
test "Node neighbor management" {
|
|
const allocator = testing.allocator;
|
|
|
|
const node_id = PeerId.init();
|
|
var address = Address.init();
|
|
|
|
var topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer topology.deinit();
|
|
|
|
const config = NodeConfig{};
|
|
var node = Node.init(allocator, node_id, address, &topology, config);
|
|
defer node.deinit();
|
|
|
|
try node.start();
|
|
|
|
const neighbor1_id = PeerId.init();
|
|
const neighbor2_id = PeerId.init();
|
|
|
|
// Add neighbors
|
|
try node.addNeighbor(neighbor1_id);
|
|
try testing.expectEqual(@as(usize, 1), node.neighbors.items.len);
|
|
|
|
try node.addNeighbor(neighbor2_id);
|
|
try testing.expectEqual(@as(usize, 2), node.neighbors.items.len);
|
|
|
|
// Add same neighbor again (should not duplicate)
|
|
try node.addNeighbor(neighbor1_id);
|
|
try testing.expectEqual(@as(usize, 2), node.neighbors.items.len);
|
|
|
|
// Check routing table
|
|
const next_hop1 = node.getNextHop(neighbor1_id);
|
|
try testing.expect(next_hop1 != null);
|
|
try testing.expect(std.mem.eql(u8, &next_hop1.?.bytes, &neighbor1_id.bytes));
|
|
|
|
const next_hop2 = node.getNextHop(neighbor2_id);
|
|
try testing.expect(next_hop2 != null);
|
|
try testing.expect(std.mem.eql(u8, &next_hop2.?.bytes, &neighbor2_id.bytes));
|
|
|
|
// Remove neighbor
|
|
node.removeNeighbor(neighbor1_id);
|
|
try testing.expectEqual(@as(usize, 1), node.neighbors.items.len);
|
|
|
|
const next_hop_after_remove = node.getNextHop(neighbor1_id);
|
|
try testing.expect(next_hop_after_remove == null);
|
|
|
|
// Other neighbor should still exist
|
|
const next_hop2_after = node.getNextHop(neighbor2_id);
|
|
try testing.expect(next_hop2_after != null);
|
|
}
|
|
|
|
test "Message processing and routing decisions" {
|
|
const allocator = testing.allocator;
|
|
|
|
const node_id = PeerId.init();
|
|
var address = Address.init();
|
|
|
|
var topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer topology.deinit();
|
|
|
|
const config = NodeConfig{};
|
|
var node = Node.init(allocator, node_id, address, &topology, config);
|
|
defer node.deinit();
|
|
|
|
try node.start();
|
|
|
|
const dest_id = PeerId.init();
|
|
const payload = "Test routing message";
|
|
|
|
var message = try node.createMessage(.Data, dest_id, payload);
|
|
|
|
// Test direct delivery (message for this node)
|
|
const local_message = try node.createMessage(.Data, node_id, "Local message");
|
|
var source_address = Address.init();
|
|
|
|
const decision = try node.processMessage(&local_message, source_address);
|
|
|
|
try testing.expect(!decision.should_forward);
|
|
try testing.expectEqual(RoutingReason.Direct, decision.reason);
|
|
try testing.expectEqual(@as(u8, 100), decision.confidence);
|
|
|
|
// Test broadcast message
|
|
const broadcast_message = try node.createMessage(.Broadcast, null, "Broadcast");
|
|
const broadcast_decision = try node.processMessage(&broadcast_message, source_address);
|
|
|
|
try testing.expect(broadcast_decision.should_forward);
|
|
try testing.expectEqual(RoutingReason.Broadcast, broadcast_decision.reason);
|
|
try testing.expectEqual(@as(u8, 50), broadcast_decision.confidence);
|
|
}
|
|
|
|
test "Routing information updates" {
|
|
const allocator = testing.allocator;
|
|
|
|
const node_id = PeerId.init();
|
|
var address = Address.init();
|
|
|
|
var topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer topology.deinit();
|
|
|
|
const config = NodeConfig{};
|
|
var node = Node.init(allocator, node_id, address, &topology, config);
|
|
defer node.deinit();
|
|
|
|
try node.start();
|
|
|
|
const dest_id = PeerId.init();
|
|
const next_hop = PeerId.init();
|
|
|
|
// Update routing information
|
|
try node.updateRoutingInfo(dest_id, next_hop, 3, 75);
|
|
|
|
// Check if route is available
|
|
const route_next_hop = node.getNextHop(dest_id);
|
|
try testing.expect(route_next_hop != null);
|
|
try testing.expect(std.mem.eql(u8, &route_next_hop.?.bytes, &next_hop.bytes));
|
|
|
|
// Update same route with better metrics
|
|
const better_next_hop = PeerId.init();
|
|
try node.updateRoutingInfo(dest_id, better_next_hop, 2, 90);
|
|
|
|
const updated_route = node.getNextHop(dest_id);
|
|
try testing.expect(std.mem.eql(u8, &updated_route.?.bytes, &better_next_hop.bytes));
|
|
}
|
|
|
|
test "Node statistics tracking" {
|
|
const allocator = testing.allocator;
|
|
|
|
const node_id = PeerId.init();
|
|
var address = Address.init();
|
|
|
|
var topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer topology.deinit();
|
|
|
|
const config = NodeConfig{};
|
|
var node = Node.init(allocator, node_id, address, &topology, config);
|
|
defer node.deinit();
|
|
|
|
const initial_stats = node.getStats();
|
|
try testing.expectEqual(@as(u64, 0), initial_stats.messages_processed);
|
|
try testing.expectEqual(@as(u64, 0), initial_stats.messages_forwarded);
|
|
try testing.expectEqual(@as(u64, 0), initial_stats.messages_dropped);
|
|
|
|
try node.start();
|
|
|
|
// Process some messages
|
|
const dest_id = PeerId.init();
|
|
var message1 = try node.createMessage(.Data, dest_id, "Message 1");
|
|
var message2 = try node.createMessage(.Data, dest_id, "Message 2");
|
|
var source_address = Address.init();
|
|
|
|
_ = try node.processMessage(&message1, source_address);
|
|
_ = try node.processMessage(&message2, source_address);
|
|
|
|
const updated_stats = node.getStats();
|
|
try testing.expectEqual(@as(u64, 2), updated_stats.messages_processed);
|
|
try testing.expect(updated_stats.uptime_seconds > 0);
|
|
try testing.expect(updated_stats.routing_table_size >= 0);
|
|
}
|
|
|
|
test "Routing decision confidence levels" {
|
|
const allocator = testing.allocator;
|
|
|
|
const node_id = PeerId.init();
|
|
var address = Address.init();
|
|
|
|
var topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer topology.deinit();
|
|
|
|
const config = NodeConfig{};
|
|
var node = Node.init(allocator, node_id, address, &topology, config);
|
|
defer node.deinit();
|
|
|
|
try node.start();
|
|
|
|
// Add neighbor
|
|
const neighbor_id = PeerId.init();
|
|
try node.addNeighbor(neighbor_id);
|
|
|
|
// Create message for neighbor (direct connection)
|
|
var neighbor_message = try node.createMessage(.Data, neighbor_id, "Neighbor message");
|
|
var source_address = Address.init();
|
|
|
|
const neighbor_decision = try node.processMessage(&neighbor_message, source_address);
|
|
try testing.expect(neighbor_decision.should_forward);
|
|
try testing.expectEqual(RoutingReason.ForwardToNeighbor, neighbor_decision.reason);
|
|
try testing.expectEqual(@as(u8, 90), neighbor_decision.confidence);
|
|
|
|
// Create message for unknown destination
|
|
const unknown_dest = PeerId.init();
|
|
var unknown_message = try node.createMessage(.Data, unknown_dest, "Unknown destination");
|
|
|
|
const unknown_decision = try node.processMessage(&unknown_message, source_address);
|
|
try testing.expect(!unknown_decision.should_forward);
|
|
try testing.expectEqual(RoutingReason.NoRoute, unknown_decision.reason);
|
|
try testing.expectEqual(@as(u8, 0), unknown_decision.confidence);
|
|
}
|
|
|
|
test "Message TTL expiration handling" {
|
|
const allocator = testing.allocator;
|
|
|
|
const node_id = PeerId.init();
|
|
var address = Address.init();
|
|
|
|
var topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer topology.deinit();
|
|
|
|
const config = NodeConfig{};
|
|
var node = Node.init(allocator, node_id, address, &topology, config);
|
|
defer node.deinit();
|
|
|
|
try node.start();
|
|
|
|
// Create message with TTL = 1
|
|
const dest_id = PeerId.init();
|
|
var message = try node.createMessage(.Data, dest_id, "TTL test");
|
|
message.ttl = 1;
|
|
|
|
var source_address = Address.init();
|
|
|
|
// First processing should work
|
|
const decision1 = try node.processMessage(&message, source_address);
|
|
try testing.expect(decision1.should_forward);
|
|
|
|
// Second processing should drop (TTL expired)
|
|
const decision2 = try node.processMessage(&message, source_address);
|
|
try testing.expect(!decision2.should_forward);
|
|
try testing.expectEqual(RoutingReason.Expired, decision2.reason);
|
|
}
|
|
|
|
test "Node with different roles" {
|
|
const allocator = testing.allocator;
|
|
|
|
// Test hub node
|
|
var hub_config = NodeConfig{
|
|
.is_hub = true,
|
|
.preferred_role = .Hub,
|
|
};
|
|
|
|
const hub_id = PeerId.init();
|
|
var hub_address = Address.init();
|
|
var hub_topology = NetworkTopology.init(allocator, .Star);
|
|
defer hub_topology.deinit();
|
|
|
|
var hub_node = Node.init(allocator, hub_id, hub_address, &hub_topology, hub_config);
|
|
defer hub_node.deinit();
|
|
|
|
try hub_node.start();
|
|
|
|
// Test regular node
|
|
var regular_config = NodeConfig{};
|
|
|
|
const regular_id = PeerId.init();
|
|
var regular_address = Address.init();
|
|
var regular_topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer regular_topology.deinit();
|
|
|
|
var regular_node = Node.init(allocator, regular_id, regular_address, ®ular_topology, regular_config);
|
|
defer regular_node.deinit();
|
|
|
|
try regular_node.start();
|
|
|
|
// Both nodes should be operational
|
|
try testing.expect(hub_node.isOperational());
|
|
try testing.expect(regular_node.isOperational());
|
|
|
|
// They should have different configurations
|
|
try testing.expect(hub_node.config.is_hub);
|
|
try testing.expect(!regular_node.config.is_hub);
|
|
}
|
|
|
|
test "Node memory management" {
|
|
const allocator = testing.allocator;
|
|
|
|
const node_id = PeerId.init();
|
|
var address = Address.init();
|
|
|
|
var topology = NetworkTopology.init(allocator, .Mesh);
|
|
defer topology.deinit();
|
|
|
|
const config = NodeConfig{};
|
|
var node = Node.init(allocator, node_id, address, &topology, config);
|
|
|
|
try node.start();
|
|
|
|
// Add many neighbors
|
|
for (0..100) |i| {
|
|
const neighbor_id = PeerId.init();
|
|
try node.addNeighbor(neighbor_id);
|
|
|
|
// Update routing info for some
|
|
if (i % 2 == 0) {
|
|
try node.updateRoutingInfo(neighbor_id, neighbor_id, 1, 80);
|
|
}
|
|
}
|
|
|
|
try testing.expect(node.neighbors.items.len > 0);
|
|
try testing.expect(node.routing_table.size() > 0);
|
|
|
|
node.deinit();
|
|
} |