328 lines
14 KiB
Zig
328 lines
14 KiB
Zig
//! Message serialization tests
|
|
const std = @import("std");
|
|
const protocol = @import("../../src/protocol/protocol.zig");
|
|
const message = @import("../../src/protocol/message.zig");
|
|
const serializer = @import("../../src/protocol/serializer.zig");
|
|
|
|
// SEC-M9 FIX: Removed CRC32 tests - CRC32 was redundant with HMAC-SHA256 integrity.
|
|
// Message integrity is now provided by HMAC which is tested in the HMAC tests.
|
|
// HMAC provides both integrity AND authentication, making CRC32 unnecessary.
|
|
|
|
test "serializer helper functions" {
|
|
// Test safeAdd overflow protection
|
|
const max_val = std.math.maxInt(usize);
|
|
try std.testing.expect(serializer.safeAdd(max_val, 1) == null);
|
|
try std.testing.expect(serializer.safeAdd(100, 200) != null);
|
|
try std.testing.expectEqual(@as(?usize, 300), serializer.safeAdd(100, 200));
|
|
|
|
// Test checkBounds
|
|
const buffer = [_]u8{0} ** 100;
|
|
try serializer.checkBounds(&buffer, 0, 50);
|
|
try serializer.checkBounds(&buffer, 50, 50);
|
|
try std.testing.expectError(serializer.SerializationError.BufferTooSmall, serializer.checkBounds(&buffer, 50, 51));
|
|
try std.testing.expectError(serializer.SerializationError.BufferTooSmall, serializer.checkBounds(&buffer, 101, 0));
|
|
}
|
|
|
|
test "serializer initialization" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
|
|
try std.testing.expect(serializer_instance.allocator == allocator);
|
|
}
|
|
|
|
test "ping message serialization" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
var source: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
source[0] = 1;
|
|
var dest: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
dest[0] = 2;
|
|
|
|
const ping = message.Ping.init(123);
|
|
const full_msg = message.Message{ .ping = ping };
|
|
|
|
var buffer: [1024]u8 = undefined;
|
|
const size = try serializer_instance.serialize(full_msg, &buffer);
|
|
|
|
// Should write positive number of bytes
|
|
try std.testing.expect(size > 0);
|
|
try std.testing.expect(size <= buffer.len);
|
|
|
|
// Should be at least header size
|
|
try std.testing.expect(size >= protocol.Serialization.HEADER_SIZE + @sizeOf(message.MessageHeader));
|
|
|
|
// Should fit in buffer
|
|
try std.testing.expect(size <= buffer.len);
|
|
}
|
|
|
|
test "ping message deserialization" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
var source: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
source[0] = 1;
|
|
var dest: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
dest[0] = 2;
|
|
|
|
const ping = message.Ping.init(456);
|
|
const full_msg = message.Message{ .ping = ping };
|
|
|
|
var buffer: [1024]u8 = undefined;
|
|
const size = try serializer_instance.serialize(full_msg, &buffer);
|
|
|
|
// Deserialize base message
|
|
const base_msg = try serializer_instance.deserialize(&buffer[0..size]);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.ping, base_msg.header.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 456), base_msg.header.id);
|
|
try std.testing.expectEqual(source, base_msg.header.source);
|
|
try std.testing.expectEqual(dest, base_msg.header.dest);
|
|
try std.testing.expectEqual(protocol.PROTOCOL_VERSION, base_msg.header.version);
|
|
try std.testing.expectEqual(protocol.Serialization.MAGIC_NUMBER, base_msg.header.magic);
|
|
|
|
// Deserialize full message
|
|
const full_msg_deserialized = try serializer_instance.deserializeFull(base_msg);
|
|
try std.testing.expectEqual(protocol.MessageType.ping, full_msg_deserialized.getType());
|
|
|
|
const ping_deserialized = switch (full_msg_deserialized) {
|
|
.ping => |p| p,
|
|
else => return error.UnexpectedMessageType,
|
|
};
|
|
|
|
try std.testing.expectEqual(@as(u32, 456), ping_deserialized.sequence);
|
|
}
|
|
|
|
test "hello message serialization" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
const hello = message.DiscoveryHello.init(protocol.PROTOCOL_VERSION, 0x01, .{
|
|
.ip = .{127, 0, 0, 1} ** 4,
|
|
.port = 8080,
|
|
});
|
|
const hello_msg = message.Message{ .discovery_hello = hello };
|
|
|
|
var buffer: [1024]u8 = undefined;
|
|
const size = try serializer_instance.serialize(hello_msg, &buffer);
|
|
|
|
try std.testing.expect(size > 0);
|
|
|
|
const base_msg = try serializer_instance.deserialize(&buffer[0..size]);
|
|
try std.testing.expectEqual(protocol.MessageType.discovery_hello, base_msg.header.msg_type);
|
|
|
|
const full_msg_deserialized = try serializer_instance.deserializeFull(base_msg);
|
|
try std.testing.expectEqual(protocol.MessageType.discovery_hello, full_msg_deserialized.getType());
|
|
}
|
|
|
|
test "pong message serialization" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
const pong = message.Pong.init(789, 150);
|
|
const pong_msg = message.Message{ .pong = pong };
|
|
|
|
var buffer: [1024]u8 = undefined;
|
|
const size = try serializer_instance.serialize(pong_msg, &buffer);
|
|
|
|
try std.testing.expect(size > 0);
|
|
|
|
const base_msg = try serializer_instance.deserialize(&buffer[0..size]);
|
|
try std.testing.expectEqual(protocol.MessageType.pong, base_msg.header.msg_type);
|
|
|
|
const full_msg_deserialized = try serializer_instance.deserializeFull(base_msg);
|
|
try std.testing.expectEqual(protocol.MessageType.pong, full_msg_deserialized.getType());
|
|
|
|
const pong_deserialized = switch (full_msg_deserialized) {
|
|
.pong => |p| p,
|
|
else => return error.UnexpectedMessageType,
|
|
};
|
|
|
|
try std.testing.expectEqual(@as(u32, 789), pong_deserialized.sequence);
|
|
try std.testing.expectEqual(@as(u32, 150), pong_deserialized.rtt);
|
|
}
|
|
|
|
test "error message serialization" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
const error_msg = message.Error.init(999, .invalid_message, "test error");
|
|
const error_full_msg = message.Message{ .error = error_msg };
|
|
|
|
var buffer: [1024]u8 = undefined;
|
|
const size = try serializer_instance.serialize(error_full_msg, &buffer);
|
|
|
|
try std.testing.expect(size > 0);
|
|
|
|
const base_msg = try serializer_instance.deserialize(&buffer[0..size]);
|
|
try std.testing.expectEqual(protocol.MessageType.error, base_msg.header.msg_type);
|
|
|
|
const full_msg_deserialized = try serializer_instance.deserializeFull(base_msg);
|
|
try std.testing.expectEqual(protocol.MessageType.error, full_msg_deserialized.getType());
|
|
}
|
|
|
|
test "serialization error handling" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
var source: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
source[0] = 1;
|
|
var dest: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
dest[0] = 2;
|
|
|
|
const ping = message.Ping.init(123);
|
|
const full_msg = message.Message{ .ping = ping };
|
|
|
|
var small_buffer: [5]u8 = undefined;
|
|
|
|
// Buffer too small for header
|
|
try std.testing.expectError(serializer.SerializationError.BufferTooSmall,
|
|
serializer_instance.serialize(full_msg, &small_buffer));
|
|
|
|
// Deserialization with invalid magic number
|
|
var invalid_buffer = [_]u8{ 0x00, 0x01, 0x02, 0x03 };
|
|
try std.testing.expectError(serializer.SerializationError.InvalidMagicNumber,
|
|
serializer_instance.deserialize(&invalid_buffer));
|
|
|
|
// Deserialization with too small buffer
|
|
try std.testing.expectError(serializer.SerializationError.BufferTooSmall,
|
|
serializer_instance.deserialize(&invalid_buffer[0..2]));
|
|
}
|
|
|
|
test "message integrity" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
const ping = message.Ping.init(123);
|
|
const full_msg = message.Message{ .ping = ping };
|
|
|
|
var buffer: [1024]u8 = undefined;
|
|
const size = try serializer_instance.serialize(full_msg, &buffer);
|
|
|
|
// Calculate and verify checksum
|
|
const checksum = serializer.calculateChecksum(&buffer[0..size]);
|
|
try std.testing.expect(serializer.verifyChecksum(&buffer[0..size], checksum));
|
|
|
|
// Verify that modifying data breaks checksum
|
|
buffer[0] +%= 1;
|
|
try std.testing.expect(!serializer.verifyChecksum(&buffer[0..size], checksum));
|
|
}
|
|
|
|
test "round trip serialization" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
var source: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
source[0] = 1;
|
|
var dest: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
dest[0] = 2;
|
|
|
|
const original_ping = message.Ping.init(12345);
|
|
const original_msg = message.Message{ .ping = original_ping };
|
|
|
|
// Serialize
|
|
var buffer: [1024]u8 = undefined;
|
|
const size = try serializer_instance.serialize(original_msg, &buffer);
|
|
|
|
// Deserialize
|
|
const base_msg = try serializer_instance.deserialize(&buffer[0..size]);
|
|
const deserialized_msg = try serializer_instance.deserializeFull(base_msg);
|
|
|
|
// Verify round trip
|
|
try std.testing.expectEqual(protocol.MessageType.ping, deserialized_msg.getType());
|
|
|
|
const deserialized_ping = switch (deserialized_msg) {
|
|
.ping => |p| p,
|
|
else => return error.UnexpectedMessageType,
|
|
};
|
|
|
|
try std.testing.expectEqual(original_ping.sequence, deserialized_ping.sequence);
|
|
}
|
|
|
|
test "multiple message types serialization" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
var source: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
source[0] = 1;
|
|
var dest: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
dest[0] = 2;
|
|
|
|
const messages = [_]message.Message{
|
|
message.Message{ .ping = message.Ping.init(1) },
|
|
message.Message{ .pong = message.Pong.init(2, 100) },
|
|
message.Message{ .discovery_hello = message.DiscoveryHello.init(protocol.PROTOCOL_VERSION, 0x01, .{
|
|
.ip = .{127, 0, 0, 1} ** 4,
|
|
.port = 8080,
|
|
})},
|
|
message.Message{ .error = message.Error.init(3, .unknown_error, "test") },
|
|
};
|
|
|
|
var buffer: [1024]u8 = undefined;
|
|
|
|
for (messages, 0..) |original_msg, i| {
|
|
const size = try serializer_instance.serialize(original_msg, &buffer);
|
|
try std.testing.expect(size > 0);
|
|
|
|
const base_msg = try serializer_instance.deserialize(&buffer[0..size]);
|
|
const deserialized_msg = try serializer_instance.deserializeFull(base_msg);
|
|
|
|
try std.testing.expectEqual(original_msg.getType(), deserialized_msg.getType());
|
|
}
|
|
}
|
|
|
|
test "serialization buffer management" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
const ping = message.Ping.init(123);
|
|
const full_msg = message.Message{ .ping = ping };
|
|
|
|
// Test with exactly the right buffer size
|
|
var exact_buffer: [200]u8 = undefined;
|
|
const size = try serializer_instance.serialize(full_msg, &exact_buffer);
|
|
try std.testing.expect(size > 0);
|
|
|
|
// Test with larger buffer
|
|
var large_buffer: [1024]u8 = undefined;
|
|
const large_size = try serializer_instance.serialize(full_msg, &large_buffer);
|
|
try std.testing.expectEqual(size, large_size);
|
|
|
|
// Test serialization doesn't write beyond claimed size
|
|
try std.testing.expect(large_buffer[size] != 0 or size == large_buffer.len); // Might be zero if at end
|
|
}
|
|
|
|
test "message header validation" {
|
|
const allocator = std.testing.allocator;
|
|
var serializer_instance = serializer.MessageSerializer.init(allocator);
|
|
defer serializer_instance.allocator.destroy(&serializer_instance);
|
|
|
|
const ping = message.Ping.init(123);
|
|
const full_msg = message.Message{ .ping = ping };
|
|
|
|
var buffer: [1024]u8 = undefined;
|
|
const size = try serializer_instance.serialize(full_msg, &buffer);
|
|
|
|
const base_msg = try serializer_instance.deserialize(&buffer[0..size]);
|
|
|
|
// Validate all header fields
|
|
try std.testing.expectEqual(protocol.Serialization.MAGIC_NUMBER, base_msg.header.magic);
|
|
try std.testing.expectEqual(protocol.PROTOCOL_VERSION, base_msg.header.version);
|
|
try std.testing.expectEqual(protocol.MessageType.ping, base_msg.header.msg_type);
|
|
try std.testing.expectEqual(@as(u8, 0), base_msg.header.flags);
|
|
try std.testing.expect(base_msg.header.id > 0);
|
|
try std.testing.expectEqual(protocol.Priority.normal, base_msg.header.priority);
|
|
try std.testing.expectEqual(@as(u8, protocol.Limits.MAX_HOPS), base_msg.header.ttl);
|
|
try std.testing.expect(base_msg.header.timestamp > 0);
|
|
try std.testing.expectEqual(@as(u8, 0), base_msg.header.reserved);
|
|
} |