347 lines
14 KiB
Zig
347 lines
14 KiB
Zig
//! Message structures tests
|
|
const std = @import("std");
|
|
const protocol = @import("../../src/protocol/protocol.zig");
|
|
const message = @import("../../src/protocol/message.zig");
|
|
|
|
test "message header creation" {
|
|
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 header = message.MessageHeader.init(.ping, 123, source, dest);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.ping, header.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 123), header.id);
|
|
try std.testing.expectEqual(source, header.source);
|
|
try std.testing.expectEqual(dest, header.dest);
|
|
try std.testing.expectEqual(protocol.PROTOCOL_VERSION, header.version);
|
|
try std.testing.expectEqual(protocol.Serialization.MAGIC_NUMBER, header.magic);
|
|
try std.testing.expectEqual(protocol.Priority.normal, header.priority);
|
|
try std.testing.expectEqual(@as(u8, protocol.Limits.MAX_HOPS), header.ttl);
|
|
try std.testing.expect(header.timestamp > 0);
|
|
try std.testing.expectEqual(@as(u8, 0), header.flags);
|
|
try std.testing.expectEqual(@as(u8, 0), header.reserved);
|
|
}
|
|
|
|
test "base message operations" {
|
|
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 header = message.MessageHeader.init(.ping, 123, source, dest);
|
|
const payload = "test payload";
|
|
const base_msg = message.BaseMessage{ .header = header, .payload = payload };
|
|
|
|
// Test getters
|
|
try std.testing.expectEqual(protocol.MessageType.ping, base_msg.getType());
|
|
try std.testing.expectEqual(@as(message.MessageId, 123), base_msg.getId());
|
|
try std.testing.expectEqual(source, base_msg.getSource());
|
|
try std.testing.expectEqual(dest, base_msg.getDest());
|
|
try std.testing.expectEqual(payload, base_msg.payload);
|
|
|
|
// Test flag checks
|
|
try std.testing.expect(!base_msg.isBroadcast());
|
|
try std.testing.expect(!base_msg.isEncrypted());
|
|
|
|
// Set flags and test again
|
|
base_msg.header.flags |= protocol.MessageFlags.BROADCAST;
|
|
try std.testing.expect(base_msg.isBroadcast());
|
|
|
|
base_msg.header.flags |= protocol.MessageFlags.ENCRYPTED;
|
|
try std.testing.expect(base_msg.isEncrypted());
|
|
|
|
base_msg.header.flags &= ~protocol.MessageFlags.BROADCAST;
|
|
try std.testing.expect(!base_msg.isBroadcast());
|
|
}
|
|
|
|
test "ttl operations" {
|
|
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;
|
|
|
|
var header = message.MessageHeader.init(.ping, 123, source, dest);
|
|
const base_msg = message.BaseMessage{ .header = header, .payload = &.{} };
|
|
|
|
// Test normal TTL decrement
|
|
try std.testing.expect(base_msg.decrementTTL());
|
|
try std.testing.expectEqual(@as(u8, protocol.Limits.MAX_HOPS - 1), base_msg.header.ttl);
|
|
|
|
// Test multiple decrements
|
|
var msg = base_msg;
|
|
var decrements: u8 = 0;
|
|
while (msg.decrementTTL()) {
|
|
decrements += 1;
|
|
}
|
|
try std.testing.expectEqual(@as(u8, protocol.Limits.MAX_HOPS - 1), decrements);
|
|
try std.testing.expectEqual(@as(u8, 0), msg.header.ttl);
|
|
|
|
// Test TTL exhausted
|
|
msg.header.ttl = 0;
|
|
try std.testing.expect(!msg.decrementTTL());
|
|
}
|
|
|
|
test "discovery messages" {
|
|
// Test DiscoveryHello
|
|
const hello = message.DiscoveryHello.init(protocol.PROTOCOL_VERSION, 0x01, .{
|
|
.ip = .{127, 0, 0, 1} ** 4,
|
|
.port = 8080,
|
|
});
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.discovery_hello, hello.msg_type);
|
|
try std.testing.expectEqual(protocol.PROTOCOL_VERSION, hello.version);
|
|
try std.testing.expectEqual(@as(u32, 0x01), hello.capabilities);
|
|
try std.testing.expectEqual(@as(u16, 8080), hello.address.port);
|
|
|
|
// Test DiscoveryResponse
|
|
var known_nodes: [2]message.NodeId = undefined;
|
|
known_nodes[0][0] = 3;
|
|
known_nodes[1][0] = 4;
|
|
|
|
const response = message.DiscoveryResponse.init(456, 0x02, .{
|
|
.ip = .{192, 168, 1, 1} ** 4,
|
|
.port = 9090,
|
|
});
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.discovery_response, response.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 456), response.original_id);
|
|
try std.testing.expectEqual(@as(u32, 0x02), response.capabilities);
|
|
try std.testing.expectEqual(@as(u16, 9090), response.address.port);
|
|
|
|
// Test DiscoveryAnnounce
|
|
const announce = message.DiscoveryAnnounce.init(0x03, .{
|
|
.ip = .{10, 0, 0, 1} ** 4,
|
|
.port = 7070,
|
|
});
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.discovery_announce, announce.msg_type);
|
|
try std.testing.expectEqual(@as(u32, 0x03), announce.capabilities);
|
|
try std.testing.expectEqual(@as(u16, 7070), announce.address.port);
|
|
}
|
|
|
|
test "topology messages" {
|
|
// Test TopologyQuery
|
|
const query = message.TopologyQuery.init(789, 1);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.topology_query, query.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 789), query.query_id);
|
|
try std.testing.expectEqual(@as(u8, 1), query.topology_type);
|
|
|
|
// Test TopologyResponse
|
|
const topology_data = "topology information";
|
|
const response = message.TopologyResponse.init(789, topology_data);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.topology_response, response.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 789), response.query_id);
|
|
try std.testing.expectEqual(topology_data, response.topology_data);
|
|
|
|
// Test TopologyUpdate
|
|
const changes = "topology changes";
|
|
const update = message.TopologyUpdate.init(1000, changes);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.topology_update, update.msg_type);
|
|
try std.testing.expectEqual(@as(u32, 1000), update.sequence);
|
|
try std.testing.expectEqual(changes, update.changes);
|
|
}
|
|
|
|
test "data messages" {
|
|
// Test DataRequest
|
|
const data_id = "file123";
|
|
const request = message.DataRequest.init(111, data_id, 1024, 4096);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.data_request, request.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 111), request.request_id);
|
|
try std.testing.expectEqual(data_id, request.data_id);
|
|
try std.testing.expectEqual(@as(u64, 1024), request.offset);
|
|
try std.testing.expectEqual(@as(u32, 4096), request.size);
|
|
|
|
// Test DataResponse
|
|
const data = "response data";
|
|
const response = message.DataResponse.init(111, data_id, data);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.data_response, response.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 111), response.request_id);
|
|
try std.testing.expectEqual(data_id, response.data_id);
|
|
try std.testing.expectEqual(data, response.data);
|
|
try std.testing.expectEqual(null, response.total_size);
|
|
|
|
// Test DataChunk
|
|
const chunk_data = "chunk data here";
|
|
const chunk = message.DataChunk.init(5, data_id, chunk_data, false);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.data_chunk, chunk.msg_type);
|
|
try std.testing.expectEqual(@as(u32, 5), chunk.sequence);
|
|
try std.testing.expectEqual(data_id, chunk.data_id);
|
|
try std.testing.expectEqual(chunk_data, chunk.chunk_data);
|
|
try std.testing.expectEqual(false, chunk.is_final);
|
|
|
|
// Test final chunk
|
|
const final_chunk = message.DataChunk.init(5, data_id, chunk_data, true);
|
|
try std.testing.expectEqual(true, final_chunk.is_final);
|
|
}
|
|
|
|
test "routing messages" {
|
|
// Test RouteQuery
|
|
var target: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
target[0] = 5;
|
|
|
|
const query = message.RouteQuery.init(222, target);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.route_query, query.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 222), query.query_id);
|
|
try std.testing.expectEqual(target, query.target);
|
|
|
|
// Test RouteResponse
|
|
var next_hop: message.NodeId = .{0} ** protocol.Network.NODE_ID_SIZE;
|
|
next_hop[0] = 6;
|
|
|
|
const route_info = "route information";
|
|
const response = message.RouteResponse.init(222, route_info);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.route_response, response.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 222), response.query_id);
|
|
try std.testing.expectEqual(route_info, response.route_info);
|
|
try std.testing.expectEqual(null, response.next_hop);
|
|
|
|
// Test RouteUpdate
|
|
const route_changes = "route changes";
|
|
const update = message.RouteUpdate.init(2000, route_changes);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.route_update, update.msg_type);
|
|
try std.testing.expectEqual(@as(u32, 2000), update.sequence);
|
|
try std.testing.expectEqual(route_changes, update.route_changes);
|
|
}
|
|
|
|
test "control messages" {
|
|
// Test Ping
|
|
const ping = message.Ping.init(333);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.ping, ping.msg_type);
|
|
try std.testing.expectEqual(@as(u32, 333), ping.sequence);
|
|
|
|
// Test Pong
|
|
const pong = message.Pong.init(333, 150);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.pong, pong.msg_type);
|
|
try std.testing.expectEqual(@as(u32, 333), pong.sequence);
|
|
try std.testing.expectEqual(@as(u32, 150), pong.rtt);
|
|
|
|
// Test Error
|
|
const error_msg = "error message";
|
|
const error = message.Error.init(444, .invalid_message, error_msg);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.error, error.msg_type);
|
|
try std.testing.expectEqual(@as(message.MessageId, 444), error.original_id.?);
|
|
try std.testing.expectEqual(protocol.ErrorCode.invalid_message, error.error_code);
|
|
try std.testing.expectEqual(error_msg, error.message);
|
|
|
|
// Test Error without original ID
|
|
const error_no_id = message.Error.init(null, .unknown_error, error_msg);
|
|
try std.testing.expectEqual(null, error_no_id.original_id);
|
|
}
|
|
|
|
test "application messages" {
|
|
// Test AppData
|
|
const app_data = "application data";
|
|
const app_msg = message.AppData.init(1000, app_data);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.app_data, app_msg.msg_type);
|
|
try std.testing.expectEqual(@as(u16, 1000), app_msg.app_id);
|
|
try std.testing.expectEqual(app_data, app_msg.data);
|
|
|
|
// Test AppControl
|
|
const params = "control parameters";
|
|
const control_msg = message.AppControl.init(1000, 2000, params);
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.app_control, control_msg.msg_type);
|
|
try std.testing.expectEqual(@as(u16, 1000), control_msg.app_id);
|
|
try std.testing.expectEqual(@as(u16, 2000), control_msg.command);
|
|
try std.testing.expectEqual(params, control_msg.params);
|
|
}
|
|
|
|
test "message union operations" {
|
|
// Test message type detection
|
|
const ping = message.Ping.init(555);
|
|
const ping_msg = message.Message{ .ping = ping };
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.ping, ping_msg.getType());
|
|
try std.testing.expectEqual(@as(message.MessageId, 555), ping_msg.getId().?);
|
|
|
|
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 };
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.discovery_hello, hello_msg.getType());
|
|
try std.testing.expectEqual(null, hello_msg.getId());
|
|
|
|
// Test different message types
|
|
const messages = [_]message.Message{
|
|
message.Message{ .ping = message.Ping.init(1) },
|
|
message.Message{ .pong = message.Pong.init(1, 100) },
|
|
message.Message{ .error = message.Error.init(null, .none, "") },
|
|
};
|
|
|
|
try std.testing.expectEqual(protocol.MessageType.ping, messages[0].getType());
|
|
try std.testing.expectEqual(protocol.MessageType.pong, messages[1].getType());
|
|
try std.testing.expectEqual(protocol.MessageType.error, messages[2].getType());
|
|
}
|
|
|
|
test "message builder" {
|
|
const allocator = std.testing.allocator;
|
|
var builder = message.MessageBuilder.init(allocator);
|
|
|
|
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;
|
|
|
|
// Test ping message creation
|
|
const ping_msg = try builder.createMessage(.ping, source, dest);
|
|
try std.testing.expectEqual(protocol.MessageType.ping, ping_msg.getType());
|
|
|
|
// Test hello message creation
|
|
const hello_msg = try builder.createMessage(.discovery_hello, source, dest);
|
|
try std.testing.expectEqual(protocol.MessageType.discovery_hello, hello_msg.getType());
|
|
|
|
// Test unsupported message type
|
|
try std.testing.expectError(error.UnsupportedMessageType,
|
|
builder.createMessage(.data_chunk, source, dest));
|
|
}
|
|
|
|
test "network address" {
|
|
const addr = message.NetworkAddress{
|
|
.ip = .{192, 168, 1, 100} ** 4,
|
|
.port = 8080,
|
|
};
|
|
|
|
try std.testing.expectEqual(@as(u16, 8080), addr.port);
|
|
|
|
// Test formatting
|
|
var buffer: [50]u8 = undefined;
|
|
const formatted = std.fmt.bufPrint(&buffer, "{}", .{addr}) catch "error";
|
|
try std.testing.expect(buffer[0] != 0); // Should have content
|
|
}
|
|
|
|
test "message ID generation" {
|
|
const allocator = std.testing.allocator;
|
|
var builder = message.MessageBuilder.init(allocator);
|
|
|
|
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;
|
|
|
|
// Create multiple messages and check IDs are different
|
|
const msg1 = try builder.createMessage(.ping, source, dest);
|
|
const msg2 = try builder.createMessage(.ping, source, dest);
|
|
|
|
try std.testing.expect(msg1.getId().? != msg2.getId().?);
|
|
|
|
// IDs should be non-zero
|
|
try std.testing.expect(msg1.getId().? > 0);
|
|
try std.testing.expect(msg2.getId().? > 0);
|
|
} |