ab ovo
This commit is contained in:
267
tests/protocol/test_handler_complete.zig
Normal file
267
tests/protocol/test_handler_complete.zig
Normal file
@@ -0,0 +1,267 @@
|
||||
//! Comprehensive tests for complete protocol handler implementations
|
||||
//! Tests that all handlers actually process and send responses
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const handler = @import("../../src/protocol/handler.zig");
|
||||
const handler_impl = @import("../../src/protocol/handler_impl.zig");
|
||||
const message = @import("../../src/protocol/message.zig");
|
||||
const protocol = @import("../../src/protocol/protocol.zig");
|
||||
|
||||
test "handleDiscoveryHello sends response" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Setup handler context
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var source_id: message.NodeId = [_]u8{1} ** protocol.Network.NODE_ID_SIZE;
|
||||
context.setSource(source_id);
|
||||
|
||||
// Create discovery hello message
|
||||
const hello = message.DiscoveryHello{
|
||||
.version = protocol.PROTOCOL_VERSION,
|
||||
.capabilities = 0x0F,
|
||||
.address = .{
|
||||
.ip = [_]u8{127} ++ [_]u8{0} ++ [_]u8{0} ++ [_]u8{1},
|
||||
.port = 8888,
|
||||
},
|
||||
};
|
||||
|
||||
// Test handler (without full network setup, this will return NoNetworkSender error)
|
||||
const result = handler_impl.handleDiscoveryHello(hello, &context);
|
||||
|
||||
// Verify it attempts to send (will fail without network_sender, but that's expected)
|
||||
try testing.expectError(error.NoNetworkSender, result);
|
||||
}
|
||||
|
||||
test "handlePing sends pong response" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var source_id: message.NodeId = [_]u8{5} ** protocol.Network.NODE_ID_SIZE;
|
||||
context.setSource(source_id);
|
||||
|
||||
// Create a ping times map (required by handlePing)
|
||||
var ping_times = std.AutoHashMap(u32, i64).init(allocator);
|
||||
defer ping_times.deinit();
|
||||
|
||||
const ping = message.Ping{
|
||||
.sequence = 789,
|
||||
};
|
||||
|
||||
// Test handler
|
||||
const result = handler_impl.handlePing(ping, &context, &ping_times);
|
||||
|
||||
// Should attempt to send pong (will fail without network_sender)
|
||||
try testing.expectError(error.NoNetworkSender, result);
|
||||
}
|
||||
|
||||
test "handlePong updates metrics" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var source_id: message.NodeId = [_]u8{6} ** protocol.Network.NODE_ID_SIZE;
|
||||
context.setSource(source_id);
|
||||
|
||||
// Create a ping times map
|
||||
var ping_times = std.AutoHashMap(u32, i64).init(allocator);
|
||||
defer ping_times.deinit();
|
||||
|
||||
const pong = message.Pong{
|
||||
.sequence = 789,
|
||||
.rtt = 42,
|
||||
};
|
||||
|
||||
// Test handler (should complete without peer_manager)
|
||||
try handler_impl.handlePong(pong, &context, &ping_times);
|
||||
|
||||
// Verify metrics were updated
|
||||
try testing.expectEqual(@as(u64, 1), metrics.messages_processed.load(.monotonic));
|
||||
}
|
||||
|
||||
test "handleError logs and updates metrics" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var source_id: message.NodeId = [_]u8{7} ** protocol.Network.NODE_ID_SIZE;
|
||||
context.setSource(source_id);
|
||||
|
||||
const error_msg = message.Error{
|
||||
.error_code = .routing_error,
|
||||
.message = "Test error message",
|
||||
.original_id = 999,
|
||||
};
|
||||
|
||||
// Test handler
|
||||
try handler_impl.handleError(error_msg, &context);
|
||||
|
||||
// Verify error metrics were updated
|
||||
try testing.expectEqual(@as(u64, 1), metrics.messages_failed.load(.monotonic));
|
||||
}
|
||||
|
||||
test "handleDataRequest attempts to send response" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var source_id: message.NodeId = [_]u8{8} ** protocol.Network.NODE_ID_SIZE;
|
||||
context.setSource(source_id);
|
||||
|
||||
var data_id: [32]u8 = [_]u8{0xAB} ** 32;
|
||||
|
||||
const request = message.DataRequest{
|
||||
.request_id = 111,
|
||||
.data_id = &data_id,
|
||||
.offset = 0,
|
||||
.size = 16384,
|
||||
};
|
||||
|
||||
// Test handler (will error without file_manager)
|
||||
const result = handler_impl.handleDataRequest(request, &context);
|
||||
|
||||
// Should return error due to missing file manager
|
||||
try testing.expectError(error.FileManagerNotConfigured, result);
|
||||
}
|
||||
|
||||
test "handleDataResponse processes data" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var source_id: message.NodeId = [_]u8{9} ** protocol.Network.NODE_ID_SIZE;
|
||||
context.setSource(source_id);
|
||||
|
||||
var data_id: [32]u8 = [_]u8{0xCD} ** 32;
|
||||
var test_data: [100]u8 = [_]u8{0xFF} ** 100;
|
||||
|
||||
const response = message.DataResponse{
|
||||
.request_id = 111,
|
||||
.data_id = &data_id,
|
||||
.data = &test_data,
|
||||
.total_size = null,
|
||||
};
|
||||
|
||||
// Test handler (will error without request_tracker - new behavior)
|
||||
const result = handler_impl.handleDataResponse(response, &context);
|
||||
|
||||
// Should return error due to missing request tracker
|
||||
try testing.expectError(error.InvalidData, result);
|
||||
}
|
||||
|
||||
test "handleRouteQuery sends route response" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var source_id: message.NodeId = [_]u8{3} ** protocol.Network.NODE_ID_SIZE;
|
||||
context.setSource(source_id);
|
||||
|
||||
var target: message.NodeId = [_]u8{10} ** protocol.Network.NODE_ID_SIZE;
|
||||
|
||||
const query = message.RouteQuery{
|
||||
.query_id = 456,
|
||||
.target = target,
|
||||
};
|
||||
|
||||
// Test handler (will error without routing_table)
|
||||
const result = handler_impl.handleRouteQuery(query, &context);
|
||||
|
||||
// Should attempt to send response (will fail without network_sender or routing_table)
|
||||
try testing.expect(result == error.NoNetworkSender or result == void{});
|
||||
}
|
||||
|
||||
test "Handler context sendResponse validates network_sender" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var dest_id: message.NodeId = [_]u8{12} ** protocol.Network.NODE_ID_SIZE;
|
||||
|
||||
const ping_msg = message.Message{
|
||||
.ping = message.Ping{ .sequence = 123 },
|
||||
};
|
||||
|
||||
// Try to send without network_sender - should error
|
||||
const result = context.sendResponse(dest_id, ping_msg);
|
||||
try testing.expectError(error.NoNetworkSender, result);
|
||||
}
|
||||
|
||||
test "Ping/Pong roundtrip flow" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Setup context
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var peer_id: message.NodeId = [_]u8{13} ** protocol.Network.NODE_ID_SIZE;
|
||||
context.setSource(peer_id);
|
||||
|
||||
var ping_times = std.AutoHashMap(u32, i64).init(allocator);
|
||||
defer ping_times.deinit();
|
||||
|
||||
// Receive ping
|
||||
const ping = message.Ping{ .sequence = 999 };
|
||||
|
||||
// Process ping (will error without network_sender, but validates flow)
|
||||
const ping_result = handler_impl.handlePing(ping, &context, &ping_times);
|
||||
try testing.expectError(error.NoNetworkSender, ping_result);
|
||||
|
||||
// Receive pong (simulated response)
|
||||
const pong = message.Pong{ .sequence = 999, .rtt = 25 };
|
||||
|
||||
// Process pong
|
||||
try handler_impl.handlePong(pong, &context, &ping_times);
|
||||
|
||||
// Verify processing completed
|
||||
try testing.expectEqual(@as(u64, 1), metrics.messages_processed.load(.monotonic));
|
||||
}
|
||||
|
||||
test "Discovery handshake flow" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Setup context
|
||||
var metrics = handler.HandlerMetrics.init();
|
||||
var context = handler.HandlerContext.init(allocator, &metrics);
|
||||
|
||||
var peer_id: message.NodeId = [_]u8{14} ** protocol.Network.NODE_ID_SIZE;
|
||||
context.setSource(peer_id);
|
||||
|
||||
// Step 1: Receive Hello
|
||||
const hello = message.DiscoveryHello{
|
||||
.version = protocol.PROTOCOL_VERSION,
|
||||
.capabilities = 0x0F,
|
||||
.address = .{
|
||||
.ip = [_]u8{192} ++ [_]u8{168} ++ [_]u8{1} ++ [_]u8{100},
|
||||
.port = 9000,
|
||||
},
|
||||
};
|
||||
|
||||
const hello_result = handler_impl.handleDiscoveryHello(hello, &context);
|
||||
try testing.expectError(error.NoNetworkSender, hello_result);
|
||||
}
|
||||
|
||||
test "All handler implementations are accessible" {
|
||||
// This test verifies that all handler functions are accessible
|
||||
_ = handler_impl.handleDiscoveryHello;
|
||||
_ = handler_impl.handlePing;
|
||||
_ = handler_impl.handlePong;
|
||||
_ = handler_impl.handleError;
|
||||
_ = handler_impl.handleDataRequest;
|
||||
_ = handler_impl.handleDataResponse;
|
||||
_ = handler_impl.handleDataChunk;
|
||||
_ = handler_impl.handleRouteQuery;
|
||||
_ = handler_impl.handleRouteUpdate;
|
||||
_ = handler_impl.handleKeyExchangeInit;
|
||||
_ = handler_impl.handleKeyExchangeResponse;
|
||||
}
|
||||
Reference in New Issue
Block a user