feat: add non-blocking Noise handshake, SecureConnection transport
refactor
This commit is contained in:
@@ -42,7 +42,8 @@ test "SecureConnection: NotEncrypted error when sending before handshake" {
|
||||
defer socket.deinit();
|
||||
|
||||
const keypair = KeyPair.generate();
|
||||
var secure = SecureConnection.init(&socket, keypair, allocator);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&socket, keypair, allocator);
|
||||
|
||||
// Verify not encrypted
|
||||
try testing.expect(!secure.isEncrypted());
|
||||
@@ -59,7 +60,8 @@ test "SecureConnection: NotEncrypted error when receiving before handshake" {
|
||||
defer socket.deinit();
|
||||
|
||||
const keypair = KeyPair.generate();
|
||||
var secure = SecureConnection.init(&socket, keypair, allocator);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&socket, keypair, allocator);
|
||||
|
||||
// Attempt to receive should return NotEncrypted error
|
||||
var buf: [1024]u8 = undefined;
|
||||
@@ -82,24 +84,26 @@ test "SecureConnection: initial state is not encrypted" {
|
||||
defer socket.deinit();
|
||||
|
||||
const keypair = KeyPair.generate();
|
||||
const secure = SecureConnection.init(&socket, keypair, allocator);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&socket, keypair, allocator);
|
||||
|
||||
try testing.expect(!secure.isEncrypted());
|
||||
try testing.expect(secure.getRemotePublicKey() == null);
|
||||
try testing.expect(secure.getHandshakeHash() == null);
|
||||
}
|
||||
|
||||
test "SecureConnection: getSocket returns underlying socket" {
|
||||
test "SecureConnection: socket field accessible after init" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var socket = try Socket.init(.tcp, allocator);
|
||||
defer socket.deinit();
|
||||
|
||||
const keypair = KeyPair.generate();
|
||||
var secure = SecureConnection.init(&socket, keypair, allocator);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&socket, keypair, allocator);
|
||||
|
||||
// getSocket should return pointer to the same socket
|
||||
try testing.expect(secure.getSocket() == &socket);
|
||||
// socket field should point to the same socket
|
||||
try testing.expect(secure.socket == &socket);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -150,7 +154,8 @@ test "SecureConnection: full handshake between initiator and responder" {
|
||||
defer client_conn.deinit();
|
||||
|
||||
// Create secure connection and perform handshake as responder
|
||||
var secure = SecureConnection.init(&client_conn, srv_keypair, alloc);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&client_conn, srv_keypair, alloc);
|
||||
|
||||
secure.acceptHandshake() catch |e| {
|
||||
srv_error.* = e;
|
||||
@@ -182,7 +187,8 @@ test "SecureConnection: full handshake between initiator and responder" {
|
||||
const connect_addr = Address.ipv4(.{ 127, 0, 0, 1 }, server_port);
|
||||
try client_socket.connect(connect_addr);
|
||||
|
||||
var client_secure = SecureConnection.init(&client_socket, client_keypair, allocator);
|
||||
var client_secure: SecureConnection = undefined;
|
||||
client_secure.init(&client_socket, client_keypair, allocator);
|
||||
|
||||
client_secure.initiateHandshake() catch |e| {
|
||||
client_error = e;
|
||||
@@ -254,7 +260,8 @@ test "SecureConnection: encrypted message round-trip" {
|
||||
};
|
||||
defer client_conn.deinit();
|
||||
|
||||
var secure = SecureConnection.init(&client_conn, srv_keypair, alloc);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&client_conn, srv_keypair, alloc);
|
||||
|
||||
secure.acceptHandshake() catch |e| {
|
||||
srv_error.* = e;
|
||||
@@ -278,7 +285,8 @@ test "SecureConnection: encrypted message round-trip" {
|
||||
const connect_addr = Address.ipv4(.{ 127, 0, 0, 1 }, server_port);
|
||||
try client_socket.connect(connect_addr);
|
||||
|
||||
var client_secure = SecureConnection.init(&client_socket, client_keypair, allocator);
|
||||
var client_secure: SecureConnection = undefined;
|
||||
client_secure.init(&client_socket, client_keypair, allocator);
|
||||
try client_secure.initiateHandshake();
|
||||
|
||||
// Send encrypted message
|
||||
@@ -335,7 +343,8 @@ test "SecureConnection: bidirectional encrypted communication" {
|
||||
};
|
||||
defer client_conn.deinit();
|
||||
|
||||
var secure = SecureConnection.init(&client_conn, srv_keypair, alloc);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&client_conn, srv_keypair, alloc);
|
||||
|
||||
secure.acceptHandshake() catch |e| {
|
||||
srv_error.* = e;
|
||||
@@ -364,7 +373,8 @@ test "SecureConnection: bidirectional encrypted communication" {
|
||||
const connect_addr = Address.ipv4(.{ 127, 0, 0, 1 }, server_port);
|
||||
try client_socket.connect(connect_addr);
|
||||
|
||||
var client_secure = SecureConnection.init(&client_socket, client_keypair, allocator);
|
||||
var client_secure: SecureConnection = undefined;
|
||||
client_secure.init(&client_socket, client_keypair, allocator);
|
||||
try client_secure.initiateHandshake();
|
||||
|
||||
// Send message to server
|
||||
@@ -418,7 +428,8 @@ test "SecureConnection: rekey maintains communication" {
|
||||
};
|
||||
defer client_conn.deinit();
|
||||
|
||||
var secure = SecureConnection.init(&client_conn, srv_keypair, alloc);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&client_conn, srv_keypair, alloc);
|
||||
|
||||
secure.acceptHandshake() catch |e| {
|
||||
srv_error.* = e;
|
||||
@@ -450,7 +461,8 @@ test "SecureConnection: rekey maintains communication" {
|
||||
const connect_addr = Address.ipv4(.{ 127, 0, 0, 1 }, server_port);
|
||||
try client_socket.connect(connect_addr);
|
||||
|
||||
var client_secure = SecureConnection.init(&client_socket, client_keypair, allocator);
|
||||
var client_secure: SecureConnection = undefined;
|
||||
client_secure.init(&client_socket, client_keypair, allocator);
|
||||
try client_secure.initiateHandshake();
|
||||
|
||||
// Send message before rekey
|
||||
@@ -506,7 +518,8 @@ test "SecureConnection: handshake hash is consistent" {
|
||||
};
|
||||
defer client_conn.deinit();
|
||||
|
||||
var secure = SecureConnection.init(&client_conn, srv_keypair, alloc);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&client_conn, srv_keypair, alloc);
|
||||
|
||||
secure.acceptHandshake() catch |e| {
|
||||
srv_error.* = e;
|
||||
@@ -524,7 +537,8 @@ test "SecureConnection: handshake hash is consistent" {
|
||||
const connect_addr = Address.ipv4(.{ 127, 0, 0, 1 }, server_port);
|
||||
try client_socket.connect(connect_addr);
|
||||
|
||||
var client_secure = SecureConnection.init(&client_socket, client_keypair, allocator);
|
||||
var client_secure: SecureConnection = undefined;
|
||||
client_secure.init(&client_socket, client_keypair, allocator);
|
||||
try client_secure.initiateHandshake();
|
||||
|
||||
const client_handshake_hash = client_secure.getHandshakeHash();
|
||||
@@ -580,7 +594,8 @@ test "SecureConnection: multiple messages in sequence" {
|
||||
};
|
||||
defer client_conn.deinit();
|
||||
|
||||
var secure = SecureConnection.init(&client_conn, srv_keypair, alloc);
|
||||
var secure: SecureConnection = undefined;
|
||||
secure.init(&client_conn, srv_keypair, alloc);
|
||||
|
||||
secure.acceptHandshake() catch |e| {
|
||||
srv_error.* = e;
|
||||
@@ -614,7 +629,8 @@ test "SecureConnection: multiple messages in sequence" {
|
||||
const connect_addr = Address.ipv4(.{ 127, 0, 0, 1 }, server_port);
|
||||
try client_socket.connect(connect_addr);
|
||||
|
||||
var client_secure = SecureConnection.init(&client_socket, client_keypair, allocator);
|
||||
var client_secure: SecureConnection = undefined;
|
||||
client_secure.init(&client_socket, client_keypair, allocator);
|
||||
try client_secure.initiateHandshake();
|
||||
|
||||
// Send multiple messages
|
||||
|
||||
Reference in New Issue
Block a user