fix MWP hole punch for production use. bind to 0.0.0.0 instead of
published IP, resend packets every 500ms instead of single-shot, fix Docker iptables-legacy chains so miniupnpd port mapping fully works without warnings
This commit is contained in:
207
tests/integration/test_mwp_connection.zig
Normal file
207
tests/integration/test_mwp_connection.zig
Normal file
@@ -0,0 +1,207 @@
|
||||
//! MWP Integration Test — Two P2P Nodes Complete a Hole Punch on Localhost
|
||||
//!
|
||||
//! This test creates two real P2P nodes, wires up MWP, publishes meeting
|
||||
//! publications via the DHT, discovers each other, and attempts a simultaneous
|
||||
//! hole punch over UDP on localhost.
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
const p2p_lib = @import("p2p");
|
||||
const P2P = p2p_lib.P2P;
|
||||
|
||||
const core = @import("core");
|
||||
const Address = core.Address;
|
||||
|
||||
const network = @import("network");
|
||||
const PeerId = network.peer.PeerId;
|
||||
|
||||
const mwp = @import("mwp");
|
||||
const MWP = mwp.MWP;
|
||||
const MWPAddress = mwp.Address;
|
||||
const MeetingPublication = mwp.MeetingPublication;
|
||||
const ConnectionResult = mwp.ConnectionResult;
|
||||
const KeyPair = mwp.KeyPair;
|
||||
|
||||
const helpers = @import("helpers");
|
||||
const TestP2PPair = helpers.TestP2PPair;
|
||||
|
||||
test "MWP: two P2P nodes complete hole punch on localhost" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Step 1: Create connected P2P pair
|
||||
var pair = try TestP2PPair.init(allocator);
|
||||
defer pair.deinit();
|
||||
|
||||
const peer_id = pair.connect() catch |err| {
|
||||
std.debug.print("Connection failed (may be CI): {}\n", .{err});
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
_ = peer_id;
|
||||
|
||||
// Step 2: Get DHT node IDs and register cross-mappings
|
||||
const alice_dht_id = pair.server.getDhtLocalId() orelse {
|
||||
std.debug.print("Server has no DHT ID\n", .{});
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
const bob_dht_id = pair.client.getDhtLocalId() orelse {
|
||||
std.debug.print("Client has no DHT ID\n", .{});
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
|
||||
// Register Bob's DHT ID on Alice's side (so Alice can route DHT messages to Bob)
|
||||
const server_peer_id = pair.getPeerId().?;
|
||||
// Get Bob's view of Alice's peer ID from the server's perspective
|
||||
// The server sees the client as server_peer_id, and the client sees the server as its connected peer
|
||||
pair.server.registerDhtNodeId(bob_dht_id, server_peer_id) catch {};
|
||||
|
||||
// Register Alice's DHT ID on Bob's side
|
||||
// The client's connected peer is the server
|
||||
pair.client.registerDhtNodeId(alice_dht_id, server_peer_id) catch {};
|
||||
|
||||
// Add to DHT routing tables
|
||||
var alice_addr = Address.init();
|
||||
alice_addr.port = pair.server_port;
|
||||
alice_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
|
||||
pair.client.dhtAddPeer(alice_dht_id, alice_addr) catch {};
|
||||
|
||||
var bob_addr = Address.init();
|
||||
bob_addr.port = pair.client_port;
|
||||
bob_addr.data = .{ .ipv4 = .{ .bytes = .{ 127, 0, 0, 1 } } };
|
||||
pair.server.dhtAddPeer(bob_dht_id, bob_addr) catch {};
|
||||
|
||||
// Let DHT stabilize
|
||||
pair.updateBothN(10);
|
||||
|
||||
// Step 3: Create MWP instances with localhost addresses
|
||||
// Use port offsets to avoid conflict with the P2P listener ports
|
||||
const alice_mwp_port: u16 = pair.server_port +| 100;
|
||||
const bob_mwp_port: u16 = pair.client_port +| 100;
|
||||
|
||||
const alice_mwp_addr = MWPAddress.initV4(.{ 127, 0, 0, 1 }, alice_mwp_port);
|
||||
const bob_mwp_addr = MWPAddress.initV4(.{ 127, 0, 0, 1 }, bob_mwp_port);
|
||||
|
||||
const alice_keypair = KeyPair.generate();
|
||||
const bob_keypair = KeyPair.generate();
|
||||
|
||||
var alice_mwp = try MWP.init(allocator, pair.server, .{
|
||||
.keypair = alice_keypair,
|
||||
.addresses = &[_]MWPAddress{alice_mwp_addr},
|
||||
});
|
||||
defer alice_mwp.deinit();
|
||||
|
||||
var bob_mwp = try MWP.init(allocator, pair.client, .{
|
||||
.keypair = bob_keypair,
|
||||
.addresses = &[_]MWPAddress{bob_mwp_addr},
|
||||
});
|
||||
defer bob_mwp.deinit();
|
||||
|
||||
// Step 4: Alice publishes for Bob
|
||||
const alice_pub = alice_mwp.publishFor(bob_keypair.public, 300) catch |err| {
|
||||
std.debug.print("Alice publishFor failed: {}\n", .{err});
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
|
||||
// Propagate through DHT
|
||||
pair.updateBothN(20);
|
||||
|
||||
// Step 5: Bob publishes for Alice
|
||||
const bob_pub = bob_mwp.publishFor(alice_keypair.public, 300) catch |err| {
|
||||
std.debug.print("Bob publishFor failed: {}\n", .{err});
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
|
||||
// Propagate through DHT
|
||||
pair.updateBothN(20);
|
||||
|
||||
// Step 6: Both try to find each other via DHT
|
||||
// On localhost with direct DHT connection, findPeer checks local storage
|
||||
const bob_found = alice_mwp.findPeer(bob_keypair.public) catch null;
|
||||
const alice_found = bob_mwp.findPeer(alice_keypair.public) catch null;
|
||||
|
||||
// If DHT propagation hasn't completed, use direct publications
|
||||
const alice_pub_for_connect = alice_pub;
|
||||
const bob_pub_for_connect = if (bob_found) |f| f else bob_pub;
|
||||
const alice_pub_for_bob = if (alice_found) |f| f else alice_pub;
|
||||
_ = alice_pub_for_bob;
|
||||
|
||||
// Step 7: Spawn two threads — both attempt hole punch simultaneously
|
||||
const ConnectContext = struct {
|
||||
mwp_instance: *MWP,
|
||||
our_pub: MeetingPublication,
|
||||
peer_pub: MeetingPublication,
|
||||
result: ConnectionResult = .timeout,
|
||||
|
||||
fn run(ctx: *@This()) void {
|
||||
ctx.result = ctx.mwp_instance.connectWithPublication(
|
||||
ctx.our_pub,
|
||||
ctx.peer_pub,
|
||||
.{
|
||||
.timeout_ms = 10_000,
|
||||
.rendezvous_buffer_ms = 500, // Short buffer for localhost
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
var alice_ctx = ConnectContext{
|
||||
.mwp_instance = alice_mwp,
|
||||
.our_pub = alice_pub_for_connect,
|
||||
.peer_pub = bob_pub_for_connect,
|
||||
};
|
||||
|
||||
var bob_ctx = ConnectContext{
|
||||
.mwp_instance = bob_mwp,
|
||||
.our_pub = bob_pub,
|
||||
.peer_pub = alice_pub_for_connect,
|
||||
};
|
||||
|
||||
// Launch both threads
|
||||
const alice_thread = std.Thread.spawn(.{}, ConnectContext.run, .{&alice_ctx}) catch |err| {
|
||||
std.debug.print("Failed to spawn alice thread: {}\n", .{err});
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
const bob_thread = std.Thread.spawn(.{}, ConnectContext.run, .{&bob_ctx}) catch |err| {
|
||||
std.debug.print("Failed to spawn bob thread: {}\n", .{err});
|
||||
alice_thread.join();
|
||||
return error.SkipZigTest;
|
||||
};
|
||||
|
||||
// Wait for both to complete
|
||||
alice_thread.join();
|
||||
bob_thread.join();
|
||||
|
||||
// Step 8: Verify results
|
||||
const alice_success = switch (alice_ctx.result) {
|
||||
.success => true,
|
||||
else => false,
|
||||
};
|
||||
const bob_success = switch (bob_ctx.result) {
|
||||
.success => true,
|
||||
else => false,
|
||||
};
|
||||
|
||||
std.debug.print("Alice result: {s}\n", .{@tagName(alice_ctx.result)});
|
||||
std.debug.print("Bob result: {s}\n", .{@tagName(bob_ctx.result)});
|
||||
|
||||
// At least one side should succeed (the other may timeout if the packet
|
||||
// arrives before the recv loop starts, which is fine for hole punching)
|
||||
try testing.expect(alice_success or bob_success);
|
||||
|
||||
// If both succeeded, verify connection IDs match
|
||||
if (alice_success and bob_success) {
|
||||
const alice_info = alice_ctx.result.success;
|
||||
const bob_info = bob_ctx.result.success;
|
||||
try testing.expect(std.mem.eql(u8, &alice_info.connection_id, &bob_info.connection_id));
|
||||
}
|
||||
|
||||
// Clean up found publications
|
||||
if (bob_found) |f| {
|
||||
var copy = f;
|
||||
copy.deinit(allocator);
|
||||
}
|
||||
if (alice_found) |f| {
|
||||
var copy = f;
|
||||
copy.deinit(allocator);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user