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,138 @@
//! Integration tests for Noise Protocol encrypted P2P connections
//!
//! These tests verify that two P2P nodes can establish encrypted connections
//! using Noise_XX_25519_ChaChaPoly_BLAKE2b and exchange encrypted messages.
const std = @import("std");
const testing = std.testing;
const p2p_lib = @import("p2p");
const core = @import("core");
const network = @import("network");
const noise = @import("noise");
const P2P = p2p_lib.P2P;
const P2PConfig = p2p_lib.P2PConfig;
const Address = core.Address;
const PeerId = network.peer.PeerId;
const SecureConnection = network.SecureConnection;
const KeyPair = noise.KeyPair;
test "noise: P2P with encryption enabled has correct configuration" {
const allocator = testing.allocator;
// Create two P2P instances with encryption enabled
var config1 = P2PConfig.default();
config1.network.listen_port = 19801;
config1.network.idle_timeout_ms = 120000;
config1.network.enable_noise_encryption = true;
var config2 = P2PConfig.default();
config2.network.listen_port = 19802;
config2.network.idle_timeout_ms = 120000;
config2.network.enable_noise_encryption = true;
var peer1 = try P2P.init(allocator, config1);
defer peer1.deinit();
var peer2 = try P2P.init(allocator, config2);
defer peer2.deinit();
// Verify both nodes have noise enabled
try testing.expect(peer1.isNoiseEnabled());
try testing.expect(peer2.isNoiseEnabled());
// Verify both have generated keypairs
const pk1 = peer1.getNoisePublicKey();
const pk2 = peer2.getNoisePublicKey();
try testing.expect(pk1 != null);
try testing.expect(pk2 != null);
// Keys should be different (randomly generated)
try testing.expect(!std.mem.eql(u8, &pk1.?, &pk2.?));
// Verify keys are not all zeros
const zero_key = [_]u8{0} ** 32;
try testing.expect(!std.mem.eql(u8, &pk1.?, &zero_key));
try testing.expect(!std.mem.eql(u8, &pk2.?, &zero_key));
// Start peers (should succeed with encryption enabled)
try peer1.start();
try peer2.start();
// Clear initialization events
_ = peer1.processAllEvents();
_ = peer2.processAllEvents();
}
test "noise: keypair generation produces valid keys" {
// Test that Noise keypairs are generated correctly
const keypair1 = KeyPair.generate();
const keypair2 = KeyPair.generate();
// Public keys should not be zero
const zero_key = [_]u8{0} ** 32;
try testing.expect(!std.mem.eql(u8, &keypair1.public, &zero_key));
try testing.expect(!std.mem.eql(u8, &keypair2.public, &zero_key));
// Different keypairs should have different public keys
try testing.expect(!std.mem.eql(u8, &keypair1.public, &keypair2.public));
}
test "noise: SecureConnection init without handshake is not encrypted" {
const allocator = testing.allocator;
// Just test initialization (can't test full handshake without network)
var socket: core.socket.Socket = undefined;
const keypair = KeyPair.generate();
const secure = SecureConnection.init(&socket, keypair, allocator);
try testing.expect(!secure.isEncrypted());
try testing.expect(secure.getRemotePublicKey() == null);
}
test "noise: P2P without encryption enabled works" {
const allocator = testing.allocator;
// Create P2P instance without encryption
var config = P2PConfig.default();
config.network.listen_port = 19803;
config.network.enable_noise_encryption = false;
var peer = try P2P.init(allocator, config);
defer peer.deinit();
// Should not have noise enabled
try testing.expect(!peer.isNoiseEnabled());
try testing.expect(peer.getNoisePublicKey() == null);
}
test "noise: static key can be provided in config" {
const allocator = testing.allocator;
// Create a deterministic static key
var static_key: [32]u8 = undefined;
for (&static_key, 0..) |*b, i| {
b.* = @truncate(i + 42);
}
var config = P2PConfig.default();
config.network.listen_port = 19804;
config.network.enable_noise_encryption = true;
config.network.noise_static_key = static_key;
var peer = try P2P.init(allocator, config);
defer peer.deinit();
// Verify noise is enabled
try testing.expect(peer.isNoiseEnabled());
// The public key should be derived from the static key
const pk = peer.getNoisePublicKey();
try testing.expect(pk != null);
}
test "noise: message size limits are respected" {
// Verify the constants are correct (from secure_connection module)
try testing.expect(network.secure_connection.MAX_MESSAGE_SIZE == 65535);
try testing.expect(network.secure_connection.TAG_SIZE == 16);
}