//! Tests for the WebRTC file transfer v1 wire protocol. //! //! These tests validate the binary encoding/decoding of all v1 message //! types independently of the demo application, serving as a protocol //! specification test suite. const std = @import("std"); const testing = std.testing; const Sha256 = std.crypto.hash.sha2.Sha256; // Wire Protocol v1 constants (must match examples/webrtc_bridge_demo.zig + browser/demo/demo.js) const PROTOCOL_VERSION: u8 = 0x01; const APP_ID_FILE_OFFER: u8 = 0x20; const APP_ID_FILE_ACCEPT: u8 = 0x21; const APP_ID_FILE_CHUNK: u8 = 0x22; const APP_ID_FILE_COMPLETE: u8 = 0x23; const APP_ID_FILE_CANCEL: u8 = 0x24; const APP_ID_FILE_RESUME: u8 = 0x25; const APP_ID_FILE_PAUSE: u8 = 0x26; // ============================================================================ // v1 Offer: [ver:1][xfer_id:8LE][name_len:2LE][name:N][size:8LE][sha256:32] // ============================================================================ fn encodeOffer(buf: []u8, transfer_id: u64, name: []const u8, file_size: u64, hash: [32]u8) ?[]u8 { const needed = 1 + 8 + 2 + name.len + 8 + 32; if (buf.len < needed) return null; var pos: usize = 0; buf[pos] = PROTOCOL_VERSION; pos += 1; std.mem.writeInt(u64, buf[pos..][0..8], transfer_id, .little); pos += 8; std.mem.writeInt(u16, buf[pos..][0..2], @intCast(name.len), .little); pos += 2; @memcpy(buf[pos..][0..name.len], name); pos += name.len; std.mem.writeInt(u64, buf[pos..][0..8], file_size, .little); pos += 8; @memcpy(buf[pos..][0..32], &hash); pos += 32; return buf[0..pos]; } const OfferFields = struct { version: u8, transfer_id: u64, name: []const u8, file_size: u64, hash: [32]u8, }; fn decodeOffer(data: []const u8) ?OfferFields { if (data.len < 1) return null; if (data[0] != PROTOCOL_VERSION) return null; if (data.len < 1 + 8 + 2) return null; var pos: usize = 1; const transfer_id = std.mem.readInt(u64, data[pos..][0..8], .little); pos += 8; const name_len = std.mem.readInt(u16, data[pos..][0..2], .little); pos += 2; if (data.len < pos + name_len + 8 + 32) return null; const name = data[pos..][0..name_len]; pos += name_len; const file_size = std.mem.readInt(u64, data[pos..][0..8], .little); pos += 8; var hash: [32]u8 = undefined; @memcpy(&hash, data[pos..][0..32]); return .{ .version = PROTOCOL_VERSION, .transfer_id = transfer_id, .name = name, .file_size = file_size, .hash = hash, }; } // ============================================================================ // v1 Accept: [xfer_id:8LE][name_len:2LE][name:N] // ============================================================================ fn encodeAccept(buf: []u8, transfer_id: u64, name: []const u8) ?[]u8 { const needed = 8 + 2 + name.len; if (buf.len < needed) return null; std.mem.writeInt(u64, buf[0..8], transfer_id, .little); std.mem.writeInt(u16, buf[8..10], @intCast(name.len), .little); @memcpy(buf[10..][0..name.len], name); return buf[0..needed]; } const AcceptFields = struct { transfer_id: u64, name: []const u8, }; fn decodeAccept(data: []const u8) ?AcceptFields { if (data.len < 10) return null; const transfer_id = std.mem.readInt(u64, data[0..8], .little); const name_len = std.mem.readInt(u16, data[8..10], .little); if (data.len < 10 + name_len) return null; return .{ .transfer_id = transfer_id, .name = data[10..][0..name_len], }; } // ============================================================================ // v1 Chunk: [0xFF][0x22][xfer_id:8LE][offset:8LE][data:N] // ============================================================================ fn encodeChunk(buf: []u8, transfer_id: u64, offset: u64, payload: []const u8) ?[]u8 { const needed = 2 + 8 + 8 + payload.len; if (buf.len < needed) return null; buf[0] = 0xFF; buf[1] = APP_ID_FILE_CHUNK; std.mem.writeInt(u64, buf[2..10], transfer_id, .little); std.mem.writeInt(u64, buf[10..18], offset, .little); @memcpy(buf[18..][0..payload.len], payload); return buf[0..needed]; } const ChunkFields = struct { transfer_id: u64, offset: u64, data: []const u8, }; /// Decode chunk payload (after 0xFF + app_id prefix has been stripped). fn decodeChunkPayload(payload: []const u8) ?ChunkFields { if (payload.len < 16) return null; return .{ .transfer_id = std.mem.readInt(u64, payload[0..8], .little), .offset = std.mem.readInt(u64, payload[8..16], .little), .data = payload[16..], }; } // ============================================================================ // v1 Complete: [0xFF][0x23][xfer_id:8LE][name_len:2LE][name:N] // ============================================================================ fn encodeComplete(buf: []u8, transfer_id: u64, name: []const u8) ?[]u8 { const needed = 2 + 8 + 2 + name.len; if (buf.len < needed) return null; buf[0] = 0xFF; buf[1] = APP_ID_FILE_COMPLETE; std.mem.writeInt(u64, buf[2..10], transfer_id, .little); std.mem.writeInt(u16, buf[10..12], @intCast(name.len), .little); @memcpy(buf[12..][0..name.len], name); return buf[0..needed]; } const CompleteFields = struct { transfer_id: u64, name: []const u8, }; /// Decode complete payload (after 0xFF + app_id prefix has been stripped). fn decodeCompletePayload(payload: []const u8) ?CompleteFields { if (payload.len < 10) return null; const transfer_id = std.mem.readInt(u64, payload[0..8], .little); const name_len = std.mem.readInt(u16, payload[8..10], .little); if (payload.len < 10 + name_len) return null; return .{ .transfer_id = transfer_id, .name = payload[10..][0..name_len], }; } // ============================================================================ // v1 Cancel: [0xFF][0x24][xfer_id:8LE][name_len:2LE][name:N] // ============================================================================ fn encodeCancel(buf: []u8, transfer_id: u64, name: []const u8) ?[]u8 { const needed = 2 + 8 + 2 + name.len; if (buf.len < needed) return null; buf[0] = 0xFF; buf[1] = APP_ID_FILE_CANCEL; std.mem.writeInt(u64, buf[2..10], transfer_id, .little); std.mem.writeInt(u16, buf[10..12], @intCast(name.len), .little); @memcpy(buf[12..][0..name.len], name); return buf[0..needed]; } // ============================================================================ // v1 Resume: [xfer_id:8LE][last_offset:8LE] // ============================================================================ fn encodeResume(buf: []u8, transfer_id: u64, last_offset: u64) ?[]u8 { if (buf.len < 16) return null; std.mem.writeInt(u64, buf[0..8], transfer_id, .little); std.mem.writeInt(u64, buf[8..16], last_offset, .little); return buf[0..16]; } const ResumeFields = struct { transfer_id: u64, last_offset: u64, }; fn decodeResume(data: []const u8) ?ResumeFields { if (data.len < 16) return null; return .{ .transfer_id = std.mem.readInt(u64, data[0..8], .little), .last_offset = std.mem.readInt(u64, data[8..16], .little), }; } // ============================================================================ // Tests: Protocol Constants // ============================================================================ test "protocol version is 0x01" { try testing.expectEqual(@as(u8, 0x01), PROTOCOL_VERSION); } test "app IDs are contiguous 0x20–0x26" { try testing.expectEqual(@as(u8, 0x20), APP_ID_FILE_OFFER); try testing.expectEqual(@as(u8, 0x21), APP_ID_FILE_ACCEPT); try testing.expectEqual(@as(u8, 0x22), APP_ID_FILE_CHUNK); try testing.expectEqual(@as(u8, 0x23), APP_ID_FILE_COMPLETE); try testing.expectEqual(@as(u8, 0x24), APP_ID_FILE_CANCEL); try testing.expectEqual(@as(u8, 0x25), APP_ID_FILE_RESUME); try testing.expectEqual(@as(u8, 0x26), APP_ID_FILE_PAUSE); } // ============================================================================ // Tests: Offer Roundtrip // ============================================================================ test "offer: encode/decode roundtrip" { var buf: [1024]u8 = undefined; const name = "photo.jpg"; const xfer_id: u64 = 0xDEADBEEF_CAFEBABE; const file_size: u64 = 1_073_741_824; // 1 GB var hash: [32]u8 = undefined; @memset(&hash, 0xAB); const encoded = encodeOffer(&buf, xfer_id, name, file_size, hash) orelse { return error.EncodeFailed; }; try testing.expectEqual(@as(usize, 1 + 8 + 2 + name.len + 8 + 32), encoded.len); try testing.expectEqual(PROTOCOL_VERSION, encoded[0]); const decoded = decodeOffer(encoded) orelse return error.DecodeFailed; try testing.expectEqual(PROTOCOL_VERSION, decoded.version); try testing.expectEqual(xfer_id, decoded.transfer_id); try testing.expectEqualStrings(name, decoded.name); try testing.expectEqual(file_size, decoded.file_size); try testing.expectEqualSlices(u8, &hash, &decoded.hash); } test "offer: empty filename" { var buf: [128]u8 = undefined; const hash = [_]u8{0} ** 32; const encoded = encodeOffer(&buf, 1, "", 0, hash) orelse return error.EncodeFailed; const decoded = decodeOffer(encoded) orelse return error.DecodeFailed; try testing.expectEqualStrings("", decoded.name); try testing.expectEqual(@as(u64, 0), decoded.file_size); } test "offer: max u64 values" { var buf: [1024]u8 = undefined; const hash = [_]u8{0xFF} ** 32; const encoded = encodeOffer(&buf, std.math.maxInt(u64), "a", std.math.maxInt(u64), hash) orelse { return error.EncodeFailed; }; const decoded = decodeOffer(encoded) orelse return error.DecodeFailed; try testing.expectEqual(std.math.maxInt(u64), decoded.transfer_id); try testing.expectEqual(std.math.maxInt(u64), decoded.file_size); } test "offer: decode rejects truncated data" { try testing.expect(decodeOffer("") == null); try testing.expect(decodeOffer(&[_]u8{0x01}) == null); // version only try testing.expect(decodeOffer(&[_]u8{ 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }) == null); // no name/size/hash } test "offer: decode rejects wrong version" { var buf: [128]u8 = undefined; const hash = [_]u8{0} ** 32; const encoded = encodeOffer(&buf, 1, "a", 10, hash) orelse return error.EncodeFailed; // Corrupt version var corrupted: [128]u8 = undefined; @memcpy(corrupted[0..encoded.len], encoded); corrupted[0] = 0x02; try testing.expect(decodeOffer(corrupted[0..encoded.len]) == null); } // ============================================================================ // Tests: Accept Roundtrip // ============================================================================ test "accept: encode/decode roundtrip" { var buf: [1024]u8 = undefined; const xfer_id: u64 = 42; const name = "document.pdf"; const encoded = encodeAccept(&buf, xfer_id, name) orelse return error.EncodeFailed; try testing.expectEqual(@as(usize, 8 + 2 + name.len), encoded.len); const decoded = decodeAccept(encoded) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, decoded.transfer_id); try testing.expectEqualStrings(name, decoded.name); } test "accept: decode rejects truncated data" { try testing.expect(decodeAccept("") == null); try testing.expect(decodeAccept(&[_]u8{0} ** 9) == null); // 9 bytes < 10 minimum } // ============================================================================ // Tests: Chunk Roundtrip // ============================================================================ test "chunk: encode/decode roundtrip" { var buf: [256]u8 = undefined; const xfer_id: u64 = 100; const offset: u64 = 65536; const payload = "hello chunk data"; const encoded = encodeChunk(&buf, xfer_id, offset, payload) orelse return error.EncodeFailed; try testing.expectEqual(@as(usize, 2 + 8 + 8 + payload.len), encoded.len); try testing.expectEqual(@as(u8, 0xFF), encoded[0]); try testing.expectEqual(APP_ID_FILE_CHUNK, encoded[1]); // Decode from after the 0xFF + app_id prefix (what the app receives) const decoded = decodeChunkPayload(encoded[2..]) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, decoded.transfer_id); try testing.expectEqual(offset, decoded.offset); try testing.expectEqualStrings(payload, decoded.data); } test "chunk: zero-length data" { var buf: [32]u8 = undefined; const encoded = encodeChunk(&buf, 1, 0, "") orelse return error.EncodeFailed; const decoded = decodeChunkPayload(encoded[2..]) orelse return error.DecodeFailed; try testing.expectEqual(@as(u64, 0), decoded.offset); try testing.expectEqualStrings("", decoded.data); } test "chunk: decode rejects too-short payload" { try testing.expect(decodeChunkPayload("") == null); try testing.expect(decodeChunkPayload(&[_]u8{0} ** 15) == null); // 15 < 16 } // ============================================================================ // Tests: Complete Roundtrip // ============================================================================ test "complete: encode/decode roundtrip" { var buf: [256]u8 = undefined; const xfer_id: u64 = 999; const name = "video.mp4"; const encoded = encodeComplete(&buf, xfer_id, name) orelse return error.EncodeFailed; try testing.expectEqual(@as(u8, 0xFF), encoded[0]); try testing.expectEqual(APP_ID_FILE_COMPLETE, encoded[1]); const decoded = decodeCompletePayload(encoded[2..]) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, decoded.transfer_id); try testing.expectEqualStrings(name, decoded.name); } test "complete: decode rejects truncated" { try testing.expect(decodeCompletePayload("") == null); try testing.expect(decodeCompletePayload(&[_]u8{0} ** 9) == null); } // ============================================================================ // Tests: Cancel Roundtrip // ============================================================================ test "cancel: encode roundtrip via complete decoder" { var buf: [256]u8 = undefined; const xfer_id: u64 = 0x1234567890ABCDEF; const name = "abandoned.bin"; const encoded = encodeCancel(&buf, xfer_id, name) orelse return error.EncodeFailed; try testing.expectEqual(@as(u8, 0xFF), encoded[0]); try testing.expectEqual(APP_ID_FILE_CANCEL, encoded[1]); // Cancel has same payload format as complete after the prefix const decoded = decodeCompletePayload(encoded[2..]) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, decoded.transfer_id); try testing.expectEqualStrings(name, decoded.name); } // ============================================================================ // Tests: Resume Roundtrip // ============================================================================ test "resume: encode/decode roundtrip" { var buf: [32]u8 = undefined; const xfer_id: u64 = 42; const last_offset: u64 = 536_870_912; // 512 MB const encoded = encodeResume(&buf, xfer_id, last_offset) orelse return error.EncodeFailed; try testing.expectEqual(@as(usize, 16), encoded.len); const decoded = decodeResume(encoded) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, decoded.transfer_id); try testing.expectEqual(last_offset, decoded.last_offset); } test "resume: decode rejects truncated" { try testing.expect(decodeResume("") == null); try testing.expect(decodeResume(&[_]u8{0} ** 15) == null); } test "resume: zero offset (restart from beginning)" { var buf: [16]u8 = undefined; const encoded = encodeResume(&buf, 1, 0) orelse return error.EncodeFailed; const decoded = decodeResume(encoded) orelse return error.DecodeFailed; try testing.expectEqual(@as(u64, 0), decoded.last_offset); } // ============================================================================ // Tests: Legacy Format Detection // ============================================================================ test "legacy offer detection: first byte != PROTOCOL_VERSION" { // Legacy format starts with name_len:2LE — first byte is unlikely to be 0x01 // for most filenames. The decoder checks data[0] == PROTOCOL_VERSION. // A legacy offer for "hello.txt" (name_len=9) starts with 0x09, 0x00 var buf: [64]u8 = undefined; const name = "hello.txt"; std.mem.writeInt(u16, buf[0..2], @intCast(name.len), .little); @memcpy(buf[2..][0..name.len], name); std.mem.writeInt(u64, buf[2 + name.len ..][0..8], 1024, .little); // Should NOT decode as v1 offer try testing.expect(decodeOffer(buf[0 .. 2 + name.len + 8]) == null); } test "legacy offer edge case: name_len == 1 collides with PROTOCOL_VERSION byte" { // name_len=1 has first byte 0x01 == PROTOCOL_VERSION // The v1 decoder should fail because the rest of the format doesn't match var buf: [64]u8 = undefined; buf[0] = 0x01; // name_len low byte = 1 (also looks like PROTOCOL_VERSION) buf[1] = 0x00; // name_len high byte buf[2] = 'a'; // name std.mem.writeInt(u64, buf[3..11], 42, .little); // Only 11 bytes — v1 needs at least 1+8+2=11 for header, then name+size+hash // v1 decode: pos=1, reads 8 bytes xfer_id (buf[1..9]), then 2 bytes name_len (buf[9..11]) // This will likely give a garbage name_len and fail the size check const result = decodeOffer(buf[0..11]); // It should either be null or give garbage — the point is it shouldn't silently succeed // as a valid v1 offer for filename "a" with size 42 if (result) |r| { // If it decoded something, it definitely shouldn't match the legacy interpretation try testing.expect(!std.mem.eql(u8, r.name, "a") or r.file_size != 42); } } // ============================================================================ // Tests: SHA-256 Hash Verification // ============================================================================ test "SHA-256: known test vector" { // SHA-256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 var hasher = Sha256.init(.{}); const hash = hasher.finalResult(); const expected = [_]u8{ 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, }; try testing.expectEqualSlices(u8, &expected, &hash); } test "SHA-256: hash roundtrip through offer encoding" { const data = "Hello, WebRTC file transfer!"; var hasher = Sha256.init(.{}); hasher.update(data); const original_hash = hasher.finalResult(); var buf: [1024]u8 = undefined; const encoded = encodeOffer(&buf, 1, "test.txt", data.len, original_hash) orelse { return error.EncodeFailed; }; const decoded = decodeOffer(encoded) orelse return error.DecodeFailed; try testing.expectEqualSlices(u8, &original_hash, &decoded.hash); } // ============================================================================ // Tests: Multi-Transfer Routing // ============================================================================ test "multi-transfer: different transfer IDs in chunks are distinguishable" { var buf1: [64]u8 = undefined; var buf2: [64]u8 = undefined; const xfer1: u64 = 0x1111111111111111; const xfer2: u64 = 0x2222222222222222; const enc1 = encodeChunk(&buf1, xfer1, 0, "chunk-a") orelse return error.EncodeFailed; const enc2 = encodeChunk(&buf2, xfer2, 0, "chunk-b") orelse return error.EncodeFailed; const dec1 = decodeChunkPayload(enc1[2..]) orelse return error.DecodeFailed; const dec2 = decodeChunkPayload(enc2[2..]) orelse return error.DecodeFailed; try testing.expect(dec1.transfer_id != dec2.transfer_id); try testing.expectEqual(xfer1, dec1.transfer_id); try testing.expectEqual(xfer2, dec2.transfer_id); try testing.expectEqualStrings("chunk-a", dec1.data); try testing.expectEqualStrings("chunk-b", dec2.data); } test "multi-transfer: offer/accept/complete share transfer_id" { const xfer_id: u64 = 0xAABBCCDDEEFF0011; const name = "shared.dat"; var offer_buf: [1024]u8 = undefined; var accept_buf: [256]u8 = undefined; var complete_buf: [256]u8 = undefined; const hash = [_]u8{0} ** 32; const offer_enc = encodeOffer(&offer_buf, xfer_id, name, 100, hash) orelse return error.EncodeFailed; const accept_enc = encodeAccept(&accept_buf, xfer_id, name) orelse return error.EncodeFailed; const complete_enc = encodeComplete(&complete_buf, xfer_id, name) orelse return error.EncodeFailed; const offer_dec = decodeOffer(offer_enc) orelse return error.DecodeFailed; const accept_dec = decodeAccept(accept_enc) orelse return error.DecodeFailed; const complete_dec = decodeCompletePayload(complete_enc[2..]) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, offer_dec.transfer_id); try testing.expectEqual(xfer_id, accept_dec.transfer_id); try testing.expectEqual(xfer_id, complete_dec.transfer_id); } // ============================================================================ // Tests: Full Transfer Lifecycle Simulation // ============================================================================ test "lifecycle: offer → accept → chunks → complete with hash verification" { const file_content = "The quick brown fox jumps over the lazy dog"; // 1. Compute file hash var hasher = Sha256.init(.{}); hasher.update(file_content); const file_hash = hasher.finalResult(); // 2. Sender creates offer const xfer_id: u64 = 12345; const name = "fox.txt"; var offer_buf: [1024]u8 = undefined; const offer = encodeOffer(&offer_buf, xfer_id, name, file_content.len, file_hash) orelse { return error.EncodeFailed; }; // 3. Receiver decodes offer const offer_dec = decodeOffer(offer) orelse return error.DecodeFailed; try testing.expectEqualStrings(name, offer_dec.name); try testing.expectEqual(@as(u64, file_content.len), offer_dec.file_size); // 4. Receiver sends accept var accept_buf: [256]u8 = undefined; const accept = encodeAccept(&accept_buf, offer_dec.transfer_id, offer_dec.name) orelse { return error.EncodeFailed; }; const accept_dec = decodeAccept(accept) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, accept_dec.transfer_id); // 5. Sender sends file in chunks (simulate 16-byte chunks) const chunk_size = 16; var received_data: [file_content.len]u8 = undefined; var offset: u64 = 0; while (offset < file_content.len) { const end = @min(offset + chunk_size, file_content.len); const chunk_data = file_content[offset..end]; var chunk_buf: [256]u8 = undefined; const chunk = encodeChunk(&chunk_buf, xfer_id, offset, chunk_data) orelse { return error.EncodeFailed; }; // Receiver decodes chunk const chunk_dec = decodeChunkPayload(chunk[2..]) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, chunk_dec.transfer_id); try testing.expectEqual(offset, chunk_dec.offset); // Write to received buffer at correct offset const dest_start: usize = @intCast(chunk_dec.offset); @memcpy(received_data[dest_start..][0..chunk_dec.data.len], chunk_dec.data); offset = end; } // 6. Sender sends complete var complete_buf: [256]u8 = undefined; const complete = encodeComplete(&complete_buf, xfer_id, name) orelse { return error.EncodeFailed; }; const complete_dec = decodeCompletePayload(complete[2..]) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, complete_dec.transfer_id); try testing.expectEqualStrings(name, complete_dec.name); // 7. Receiver verifies data integrity try testing.expectEqualStrings(file_content, &received_data); // 8. Receiver computes SHA-256 and verifies against offer var verify_hasher = Sha256.init(.{}); verify_hasher.update(&received_data); const computed_hash = verify_hasher.finalResult(); try testing.expectEqualSlices(u8, &offer_dec.hash, &computed_hash); } // ============================================================================ // Tests: Cancel / Resume Scenarios // ============================================================================ test "cancel interrupts transfer: cancel message carries correct transfer_id" { const xfer_id: u64 = 0xFEDCBA9876543210; const name = "interrupted.zip"; var buf: [256]u8 = undefined; const encoded = encodeCancel(&buf, xfer_id, name) orelse return error.EncodeFailed; // Verify prefix try testing.expectEqual(@as(u8, 0xFF), encoded[0]); try testing.expectEqual(APP_ID_FILE_CANCEL, encoded[1]); // Decode payload const decoded = decodeCompletePayload(encoded[2..]) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, decoded.transfer_id); try testing.expectEqualStrings(name, decoded.name); } test "resume: resume message requests correct offset" { const xfer_id: u64 = 42; const resume_offset: u64 = 536_870_912; // 512 MB var buf: [16]u8 = undefined; const encoded = encodeResume(&buf, xfer_id, resume_offset) orelse return error.EncodeFailed; const decoded = decodeResume(encoded) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, decoded.transfer_id); try testing.expectEqual(resume_offset, decoded.last_offset); } // ============================================================================ // Tests: Edge Cases // ============================================================================ test "offer: maximum name length (512 bytes)" { var buf: [1024]u8 = undefined; const name = "a" ** 512; const hash = [_]u8{0} ** 32; const encoded = encodeOffer(&buf, 1, name, 0, hash) orelse return error.EncodeFailed; const decoded = decodeOffer(encoded) orelse return error.DecodeFailed; try testing.expectEqual(@as(usize, 512), decoded.name.len); } test "offer: file_size = 0 is valid" { var buf: [128]u8 = undefined; const hash = [_]u8{0} ** 32; const encoded = encodeOffer(&buf, 1, "empty.txt", 0, hash) orelse return error.EncodeFailed; const decoded = decodeOffer(encoded) orelse return error.DecodeFailed; try testing.expectEqual(@as(u64, 0), decoded.file_size); } test "chunk: large offset values" { var buf: [64]u8 = undefined; const offset: u64 = std.math.maxInt(u64) - 1; const encoded = encodeChunk(&buf, 1, offset, "x") orelse return error.EncodeFailed; const decoded = decodeChunkPayload(encoded[2..]) orelse return error.DecodeFailed; try testing.expectEqual(offset, decoded.offset); } test "direct message routing: 0xFF prefix distinguishes direct from P2P framed" { // Direct messages use 0xFF prefix, P2P framed messages don't var chunk_buf: [64]u8 = undefined; const chunk = encodeChunk(&chunk_buf, 1, 0, "data") orelse return error.EncodeFailed; try testing.expectEqual(@as(u8, 0xFF), chunk[0]); // An accept message (P2P framed) does NOT start with 0xFF var accept_buf: [64]u8 = undefined; const accept = encodeAccept(&accept_buf, 1, "f") orelse return error.EncodeFailed; try testing.expect(accept[0] != 0xFF); } test "app_id byte distinguishes message types in direct path" { var chunk_buf: [64]u8 = undefined; var complete_buf: [64]u8 = undefined; var cancel_buf: [64]u8 = undefined; const chunk = encodeChunk(&chunk_buf, 1, 0, "data") orelse return error.EncodeFailed; const complete = encodeComplete(&complete_buf, 1, "f") orelse return error.EncodeFailed; const cancel = encodeCancel(&cancel_buf, 1, "f") orelse return error.EncodeFailed; // All start with 0xFF but have different app_ids at byte 1 try testing.expectEqual(@as(u8, 0xFF), chunk[0]); try testing.expectEqual(@as(u8, 0xFF), complete[0]); try testing.expectEqual(@as(u8, 0xFF), cancel[0]); try testing.expectEqual(APP_ID_FILE_CHUNK, chunk[1]); try testing.expectEqual(APP_ID_FILE_COMPLETE, complete[1]); try testing.expectEqual(APP_ID_FILE_CANCEL, cancel[1]); } // ============================================================================ // v1 Pause: [0xFF][0x26][xfer_id:8LE] // ============================================================================ fn encodePause(buf: []u8, transfer_id: u64) ?[]u8 { if (buf.len < 10) return null; buf[0] = 0xFF; buf[1] = APP_ID_FILE_PAUSE; std.mem.writeInt(u64, buf[2..10], transfer_id, .little); return buf[0..10]; } const PauseFields = struct { transfer_id: u64, }; /// Decode pause payload (after 0xFF + app_id prefix has been stripped). fn decodePausePayload(payload: []const u8) ?PauseFields { if (payload.len < 8) return null; return .{ .transfer_id = std.mem.readInt(u64, payload[0..8], .little), }; } // ============================================================================ // Tests: Pause Roundtrip // ============================================================================ test "pause: encode/decode roundtrip" { var buf: [16]u8 = undefined; const xfer_id: u64 = 0xCAFEBABEDEADBEEF; const encoded = encodePause(&buf, xfer_id) orelse return error.EncodeFailed; try testing.expectEqual(@as(usize, 10), encoded.len); try testing.expectEqual(@as(u8, 0xFF), encoded[0]); try testing.expectEqual(APP_ID_FILE_PAUSE, encoded[1]); // Decode from after the 0xFF + app_id prefix const decoded = decodePausePayload(encoded[2..]) orelse return error.DecodeFailed; try testing.expectEqual(xfer_id, decoded.transfer_id); } test "pause: payload is exactly 8 bytes (xfer_id only)" { var buf: [16]u8 = undefined; const encoded = encodePause(&buf, 42) orelse return error.EncodeFailed; // Total wire message is 10 bytes: [0xFF][0x26][xfer_id:8LE] try testing.expectEqual(@as(usize, 10), encoded.len); // Payload after prefix strip is 8 bytes const payload = encoded[2..]; try testing.expectEqual(@as(usize, 8), payload.len); } test "pause: decode rejects truncated payload" { try testing.expect(decodePausePayload("") == null); try testing.expect(decodePausePayload(&[_]u8{0} ** 7) == null); // 7 < 8 } test "pause: app_id is 0x26 and follows contiguous range" { try testing.expectEqual(@as(u8, 0x26), APP_ID_FILE_PAUSE); try testing.expectEqual(APP_ID_FILE_RESUME + 1, APP_ID_FILE_PAUSE); }