Files
zigp2p/tests/integration/test_webrtc_p2p_integration.zig
2026-02-19 11:38:56 -03:00

193 lines
6.6 KiB
Zig

//! WebRTC P2P Integration Test — verify WebRTCChannelTransport works
//! as a PeerConnection transport, completing the T5.17 integration.
//!
//! Flow:
//! 1. Two native WebRTC peers establish a DataChannel (like loopback test)
//! 2. Wrap each ChannelState in a WebRTCChannelTransport
//! 3. Create PeerConnection using initWithTransport (no socket)
//! 4. Send/receive data through PeerConnection.transport_handle
//! 5. Verify PeerConnection.socket is null (WebRTC path)
//!
//! Requires: libdatachannel.dylib available
const std = @import("std");
const testing = std.testing;
const webrtc = @import("webrtc");
const rtc = webrtc.rtc;
const core = @import("core");
const network = @import("network");
const Transport = core.transport.Transport;
const PeerConnection = network.PeerConnection;
/// Wait for a condition with timeout (milliseconds).
fn waitFor(comptime predicate: anytype, ctx: anytype, timeout_ms: u64) bool {
var waited: u64 = 0;
const poll_ms: u64 = 10;
while (waited < timeout_ms) : (waited += poll_ms) {
if (predicate(ctx)) return true;
std.Thread.sleep(poll_ms * std.time.ns_per_ms);
}
return false;
}
test "WebRTC P2P integration: PeerConnection with WebRTCChannelTransport" {
if (!rtc.load()) return error.SkipZigTest;
const allocator = testing.allocator;
rtc.rtcInitLogger(.warning, null);
var mgr = webrtc.NativeWebRTCManager.init(allocator) catch |err| switch (err) {
error.WebRTCUnavailable => return error.SkipZigTest,
};
defer mgr.deinit();
// --- Step 1: Establish WebRTC DataChannel between Alice and Bob ---
const alice = try mgr.createOffer(null);
defer mgr.destroyChannel(alice);
// Wait for Alice's SDP
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.getLocalSdp() != null;
}
}.check, alice, 5000));
// Wait for Alice's ICE gathering
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.gathering_complete.load(.acquire);
}
}.check, alice, 5000));
const alice_sdp_z = try allocator.dupeZ(u8, alice.getLocalSdp().?);
defer allocator.free(alice_sdp_z);
const bob = try mgr.acceptOffer(alice_sdp_z, null);
defer mgr.destroyChannel(bob);
// Wait for Bob's SDP + ICE gathering
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.getLocalSdp() != null;
}
}.check, bob, 5000));
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.gathering_complete.load(.acquire);
}
}.check, bob, 5000));
// Exchange SDPs
const bob_sdp_z = try allocator.dupeZ(u8, bob.getLocalSdp().?);
defer allocator.free(bob_sdp_z);
const result = rtc.rtcSetRemoteDescription(alice.pc_id, bob_sdp_z, "answer");
try testing.expect(result >= 0);
// Exchange ICE candidates: Bob → Alice
{
const bob_candidates = try bob.drainLocalCandidates(allocator);
defer {
for (bob_candidates) |c| allocator.free(c);
if (bob_candidates.len > 0) allocator.free(bob_candidates);
}
for (bob_candidates) |cand| {
const cand_z = allocator.dupeZ(u8, cand) catch continue;
defer allocator.free(cand_z);
mgr.addRemoteCandidate(alice, cand_z);
}
}
// Exchange ICE candidates: Alice → Bob
{
const alice_candidates = try alice.drainLocalCandidates(allocator);
defer {
for (alice_candidates) |c| allocator.free(c);
if (alice_candidates.len > 0) allocator.free(alice_candidates);
}
for (alice_candidates) |cand| {
const cand_z = allocator.dupeZ(u8, cand) catch continue;
defer allocator.free(cand_z);
mgr.addRemoteCandidate(bob, cand_z);
}
}
// Wait for DataChannels to open
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.dc_open.load(.acquire);
}
}.check, alice, 10000));
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.dc_open.load(.acquire);
}
}.check, bob, 10000));
// --- Step 2: Wrap in WebRTCChannelTransport ---
var alice_wrtc_transport = webrtc.WebRTCChannelTransport.init(&mgr, alice);
var bob_wrtc_transport = webrtc.WebRTCChannelTransport.init(&mgr, bob);
const alice_transport = Transport.init(&alice_wrtc_transport);
const bob_transport = Transport.init(&bob_wrtc_transport);
// --- Step 3: Create PeerConnections via initWithTransport ---
const alice_pc = try PeerConnection.initWithTransport(allocator, alice_transport);
defer {
alice_pc.deinit();
}
const bob_pc = try PeerConnection.initWithTransport(allocator, bob_transport);
defer {
bob_pc.deinit();
}
// Verify: no socket associated
try testing.expect(alice_pc.socket == null);
try testing.expect(bob_pc.socket == null);
// Verify: transport handle is connected state
try testing.expectEqual(core.transport.TransportState.connected, alice_pc.transport_handle.getState());
try testing.expectEqual(core.transport.TransportState.connected, bob_pc.transport_handle.getState());
// --- Step 4: Send/receive through transport_handle ---
const msg = "hello via PeerConnection";
const sent = try alice_pc.transport_handle.send(msg);
try testing.expectEqual(msg.len, sent);
// Wait for Bob to receive
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
cs.mutex.lock();
defer cs.mutex.unlock();
return cs.recv_queue.items.len > 0;
}
}.check, bob, 5000));
// Read through transport_handle
var recv_buf: [256]u8 = undefined;
const received = try bob_pc.transport_handle.receive(&recv_buf);
try testing.expectEqualStrings(msg, recv_buf[0..received]);
// --- Step 5: Verify bidirectional ---
const reply = "reply via PeerConnection";
_ = try bob_pc.transport_handle.send(reply);
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
cs.mutex.lock();
defer cs.mutex.unlock();
return cs.recv_queue.items.len > 0;
}
}.check, alice, 5000));
var reply_buf: [256]u8 = undefined;
const reply_received = try alice_pc.transport_handle.receive(&reply_buf);
try testing.expectEqualStrings(reply, reply_buf[0..reply_received]);
}