This commit is contained in:
2026-02-06 21:03:59 -03:00
commit 2b7ab5f570
267 changed files with 176026 additions and 0 deletions

View File

@@ -0,0 +1,326 @@
//! Integration tests for message encryption in the serialization pipeline
const std = @import("std");
const testing = std.testing;
// Import from protocol module
const protocol_mod = @import("protocol");
const crypto = protocol_mod.crypto;
const serializer = protocol_mod.serializer;
const message = protocol_mod.message;
const protocol = protocol_mod.protocol;
test "message roundtrip with encryption enabled" {
const allocator = testing.allocator;
// Set up crypto managers for two peers
var crypto_mgr_a = crypto.CryptoManager.init(allocator);
defer crypto_mgr_a.deinit();
var crypto_mgr_b = crypto.CryptoManager.init(allocator);
defer crypto_mgr_b.deinit();
// Create peer IDs
const peer_a_id = [_]u8{0xAA} ** 32;
const peer_b_id = [_]u8{0xBB} ** 32;
// Exchange keys - in real scenario, this would be done via key exchange protocol
const key_a = crypto_mgr_a.local_key;
const key_b = crypto_mgr_b.local_key;
// Set encryption keys
try crypto_mgr_a.setKeyForPeer(peer_b_id, key_b);
try crypto_mgr_b.setKeyForPeer(peer_a_id, key_a);
// Set HMAC keys (required for message authentication)
const hmac_key = [_]u8{0x55} ** 32;
try crypto_mgr_a.setHmacKeyForPeer(peer_b_id, hmac_key);
try crypto_mgr_b.setHmacKeyForPeer(peer_a_id, hmac_key);
// Create serializer with encryption enabled for peer A (sending to peer B)
// initFull takes: allocator, crypto_manager, enable_encryption, local_peer_id, remote_peer_id
var ser_a = serializer.MessageSerializer.initFull(allocator, &crypto_mgr_a, peer_a_id, peer_b_id);
// Create serializer with encryption enabled for peer B (sending to peer A)
var ser_b = serializer.MessageSerializer.initFull(allocator, &crypto_mgr_b, peer_b_id, peer_a_id);
// Create a test message
const ping = message.Ping.init(12345);
const msg = message.Message{ .ping = ping };
// Serialize with encryption (A -> B)
var send_buffer: [2048]u8 = undefined;
const serialized_size = try ser_a.serialize(msg, &send_buffer);
try testing.expect(serialized_size > 0);
try testing.expect(serialized_size <= send_buffer.len);
// Deserialize and decrypt on B's side
const base_msg = try ser_b.deserialize(send_buffer[0..serialized_size]);
defer if (base_msg.isEncrypted()) allocator.free(base_msg.payload);
// Verify the message was decrypted correctly
try testing.expectEqual(protocol.MessageType.ping, base_msg.header.msg_type);
// Deserialize the full message
const full_msg = try ser_b.deserializeFull(base_msg);
try testing.expectEqual(protocol.MessageType.ping, full_msg.getType());
try testing.expectEqual(@as(u32, 12345), full_msg.ping.sequence);
}
test "encrypted messages have ENCRYPTED flag set" {
const allocator = testing.allocator;
// Set up crypto manager
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
// Create peer ID for destination (peer_b)
const peer_b_id = [_]u8{0xBB} ** 32;
// Set encryption key for peer B
const key_b = [_]u8{0x42} ** 32;
try crypto_mgr.setKeyForPeer(peer_b_id, key_b);
// Set HMAC key for peer B (required for authentication)
const hmac_key = [_]u8{0x55} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_b_id, hmac_key);
// Create serializer with encryption enabled - peer_id is the DESTINATION peer (peer_b) for key lookup
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_b_id);
// Create a test message with destination = peer_b_id
const ping = message.Ping.init(999);
const msg = message.Message{ .ping = ping };
// Serialize with encryption (use serializeEncrypted for actual encryption)
var send_buffer: [2048]u8 = undefined;
const serialized_size = try ser.serializeEncrypted(msg, &send_buffer);
// Deserialize to check encryption flag (only the serialized portion)
const base_msg = try ser.deserialize(send_buffer[0..serialized_size]);
// Verify ENCRYPTED flag is set
try testing.expect((base_msg.header.flags & protocol.MessageFlags.ENCRYPTED) != 0);
try testing.expect(base_msg.isEncrypted());
}
test "decryption failure on tampered ciphertext" {
const allocator = testing.allocator;
// Set up crypto managers
var crypto_mgr_a = crypto.CryptoManager.init(allocator);
defer crypto_mgr_a.deinit();
var crypto_mgr_b = crypto.CryptoManager.init(allocator);
defer crypto_mgr_b.deinit();
// Create peer IDs
const peer_a_id = [_]u8{0xAA} ** 32;
const peer_b_id = [_]u8{0xBB} ** 32;
// Exchange encryption keys
const key_a = crypto_mgr_a.local_key;
const key_b = crypto_mgr_b.local_key;
try crypto_mgr_a.setKeyForPeer(peer_b_id, key_b);
try crypto_mgr_b.setKeyForPeer(peer_a_id, key_a);
// Set HMAC keys for both peers
const hmac_key = [_]u8{0x77} ** 32;
try crypto_mgr_a.setHmacKeyForPeer(peer_b_id, hmac_key);
try crypto_mgr_b.setHmacKeyForPeer(peer_a_id, hmac_key);
// Create serializers - initFull(allocator, crypto_manager, enable_encryption, local_peer_id, remote_peer_id)
var ser_a = serializer.MessageSerializer.initFull(allocator, &crypto_mgr_a, peer_a_id, peer_b_id);
var ser_b = serializer.MessageSerializer.initFull(allocator, &crypto_mgr_b, peer_b_id, peer_a_id);
// Create and serialize a message
const ping = message.Ping.init(777);
const msg = message.Message{ .ping = ping };
var send_buffer: [2048]u8 = undefined;
const serialized_size = try ser_a.serialize(msg, &send_buffer);
// Tamper with the ciphertext (modify a byte in the payload)
const payload_start = protocol.Serialization.HEADER_SIZE + @sizeOf(message.MessageHeader);
if (payload_start < serialized_size) {
send_buffer[payload_start + 10] ^= 0xFF; // Flip bits
}
// Attempt to deserialize - should fail due to tampered ciphertext
// Tampering causes HMAC authentication to fail first
const result = ser_b.deserialize(send_buffer[0..serialized_size]);
try testing.expectError(serializer.SerializationError.AuthenticationFailed, result);
}
test "authentication is mandatory and works correctly" {
const allocator = testing.allocator;
// Set up crypto manager with HMAC key for authentication
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
const peer_id = [_]u8{0xAA} ** 32;
const hmac_key = [_]u8{0x55} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_id, hmac_key);
// Create serializer WITH authentication (mandatory now)
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_id);
// Create a test message
const ping = message.Ping.init(555);
const msg = message.Message{ .ping = ping };
// Serialize with authentication
var send_buffer: [2048]u8 = undefined;
const serialized_size = try ser.serialize(msg, &send_buffer);
try testing.expect(serialized_size > 0);
// Deserialize (authentication is verified)
const base_msg = try ser.deserialize(send_buffer[0..serialized_size]);
// Verify message was authenticated
try testing.expect((base_msg.header.flags & protocol.MessageFlags.AUTHENTICATED) != 0);
// Verify message content
const full_msg = try ser.deserializeFull(base_msg);
try testing.expectEqual(protocol.MessageType.ping, full_msg.getType());
try testing.expectEqual(@as(u32, 555), full_msg.ping.sequence);
}
test "encryption enabled but no encryption key for peer - uses HMAC only" {
const allocator = testing.allocator;
// Set up crypto manager with HMAC key but no encryption key
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
// Destination peer ID
const peer_b_id = [_]u8{0xBB} ** 32;
// Set HMAC key but no encryption key
const hmac_key = [_]u8{0x55} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_b_id, hmac_key);
// Create serializer with encryption enabled but no encryption key
// peer_id is the DESTINATION peer (peer_b) for key lookup
var ser = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_b_id);
// Create a test message
const ping = message.Ping.init(333);
const msg = message.Message{ .ping = ping };
// Serialize - should work but without encryption (no encryption key)
var send_buffer: [2048]u8 = undefined;
const serialized_size = try ser.serialize(msg, &send_buffer);
// Deserialize to check flag - should NOT be encrypted (but is authenticated)
const base_msg = try ser.deserialize(send_buffer[0..serialized_size]);
try testing.expect(!base_msg.isEncrypted());
try testing.expect((base_msg.header.flags & protocol.MessageFlags.ENCRYPTED) == 0);
}
test "roundtrip with different message types" {
const allocator = testing.allocator;
// Set up crypto managers
var crypto_mgr_a = crypto.CryptoManager.init(allocator);
defer crypto_mgr_a.deinit();
var crypto_mgr_b = crypto.CryptoManager.init(allocator);
defer crypto_mgr_b.deinit();
const peer_a_id = [_]u8{0xAA} ** 32;
const peer_b_id = [_]u8{0xBB} ** 32;
// Set encryption keys
const key_a = crypto_mgr_a.local_key;
const key_b = crypto_mgr_b.local_key;
try crypto_mgr_a.setKeyForPeer(peer_b_id, key_b);
try crypto_mgr_b.setKeyForPeer(peer_a_id, key_a);
// Set HMAC keys for message authentication
const hmac_key = [_]u8{0x88} ** 32;
try crypto_mgr_a.setHmacKeyForPeer(peer_b_id, hmac_key);
try crypto_mgr_b.setHmacKeyForPeer(peer_a_id, hmac_key);
// initFull(allocator, crypto_manager, enable_encryption, local_peer_id, remote_peer_id)
var ser_a = serializer.MessageSerializer.initFull(allocator, &crypto_mgr_a, peer_a_id, peer_b_id);
var ser_b = serializer.MessageSerializer.initFull(allocator, &crypto_mgr_b, peer_b_id, peer_a_id);
// Test with Pong message
{
const pong = message.Pong.init(123, 456);
const msg = message.Message{ .pong = pong };
var buffer: [2048]u8 = undefined;
const size = try ser_a.serialize(msg, &buffer);
const base_msg = try ser_b.deserialize(buffer[0..size]);
defer if (base_msg.isEncrypted()) allocator.free(base_msg.payload);
const full_msg = try ser_b.deserializeFull(base_msg);
try testing.expectEqual(protocol.MessageType.pong, full_msg.getType());
try testing.expectEqual(@as(u32, 123), full_msg.pong.sequence);
try testing.expectEqual(@as(u32, 456), full_msg.pong.rtt);
}
// Test with TopologyQuery message
{
const query = message.TopologyQuery.init(789, 5);
const msg = message.Message{ .topology_query = query };
var buffer: [2048]u8 = undefined;
const size = try ser_a.serialize(msg, &buffer);
const base_msg = try ser_b.deserialize(buffer[0..size]);
defer if (base_msg.isEncrypted()) allocator.free(base_msg.payload);
const full_msg = try ser_b.deserializeFull(base_msg);
try testing.expectEqual(protocol.MessageType.topology_query, full_msg.getType());
try testing.expectEqual(@as(u64, 789), full_msg.topology_query.query_id);
try testing.expectEqual(@as(u8, 5), full_msg.topology_query.topology_type);
}
}
test "encrypted payload is larger than plaintext" {
const allocator = testing.allocator;
var crypto_mgr = crypto.CryptoManager.init(allocator);
defer crypto_mgr.deinit();
// Destination peer ID
const peer_b_id = [_]u8{0xBB} ** 32;
// Set encryption key for peer B
const key_b = [_]u8{0x42} ** 32;
try crypto_mgr.setKeyForPeer(peer_b_id, key_b);
// Set HMAC key for peer B
const hmac_key = [_]u8{0x99} ** 32;
try crypto_mgr.setHmacKeyForPeer(peer_b_id, hmac_key);
// Serialize with encryption - peer_id is the DESTINATION peer (peer_b) for key lookup
var ser_encrypted = serializer.MessageSerializer.initWithCrypto(allocator, &crypto_mgr, peer_b_id);
// Serialize without encryption
// CRIT-NEW-9: Use test mode to allow null crypto_manager for comparison
var ser_plain = serializer.MessageSerializer.initInsecureForTesting(allocator);
const ping = message.Ping.init(111);
const msg = message.Message{ .ping = ping };
var encrypted_buffer: [2048]u8 = undefined;
var plain_buffer: [2048]u8 = undefined;
// Use serializeEncrypted to actually encrypt (not just authenticate)
const encrypted_size = try ser_encrypted.serializeEncrypted(msg, &encrypted_buffer);
const plain_size = try ser_plain.serialize(msg, &plain_buffer);
// Encrypted size should be larger due to nonce (12) + tag (16) + HMAC (32) = 60 extra bytes
try testing.expect(encrypted_size > plain_size);
try testing.expectEqual(plain_size + 60, encrypted_size);
}