x0x 0.19.2

Agent-to-agent gossip network for AI systems — no winners, no losers, just cooperation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Integration tests for the file transfer protocol.
//!
//! Tests cover FileMessage serialization, TransferState logic,
//! chunk arithmetic, path traversal protection, and SHA-256 verification.

use base64::Engine;
use sha2::{Digest, Sha256};
use x0x::files::{
    FileChunk, FileComplete, FileMessage, FileOffer, TransferDirection, TransferState,
    TransferStatus, DEFAULT_CHUNK_SIZE, MAX_TRANSFER_SIZE,
};

// ---------------------------------------------------------------------------
// Helper: build a TransferState for testing
// ---------------------------------------------------------------------------

fn make_sending_transfer(size: u64, chunk_size: usize) -> TransferState {
    let total_chunks = if size == 0 {
        0
    } else {
        size.div_ceil(chunk_size as u64)
    };
    TransferState {
        transfer_id: "test-xfer".to_string(),
        direction: TransferDirection::Sending,
        remote_agent_id: "0".repeat(64),
        filename: "test.bin".to_string(),
        total_size: size,
        bytes_transferred: 0,
        status: TransferStatus::Pending,
        sha256: "abc123".to_string(),
        error: None,
        started_at: 0,
        started_at_unix_ms: 0,
        completed_at_unix_ms: None,
        source_path: Some("/tmp/test.bin".to_string()),
        output_path: None,
        chunk_size,
        total_chunks,
    }
}

fn make_receiving_transfer(size: u64, chunk_size: usize) -> TransferState {
    let total_chunks = if size == 0 {
        0
    } else {
        size.div_ceil(chunk_size as u64)
    };
    TransferState {
        transfer_id: "test-recv".to_string(),
        direction: TransferDirection::Receiving,
        remote_agent_id: "f".repeat(64),
        filename: "received.bin".to_string(),
        total_size: size,
        bytes_transferred: 0,
        status: TransferStatus::InProgress,
        sha256: "def456".to_string(),
        error: None,
        started_at: 0,
        started_at_unix_ms: 0,
        completed_at_unix_ms: None,
        source_path: None,
        output_path: Some("/tmp/received.bin".to_string()),
        chunk_size,
        total_chunks,
    }
}

// ---------------------------------------------------------------------------
// 1. FileMessage serialization roundtrip
// ---------------------------------------------------------------------------

#[test]
fn file_offer_roundtrip() {
    let msg = FileMessage::Offer(FileOffer {
        transfer_id: "xfer-001".into(),
        filename: "report.pdf".into(),
        size: 123_456,
        sha256: "abc123".into(),
        chunk_size: DEFAULT_CHUNK_SIZE,
        total_chunks: 2,
    });
    let json = serde_json::to_vec(&msg).unwrap();
    let decoded: FileMessage = serde_json::from_slice(&json).unwrap();
    let json_str = String::from_utf8_lossy(&json);
    assert!(
        json_str.contains("\"type\":\"file-offer\""),
        "got: {json_str}"
    );
    // Verify Offer fields survived roundtrip
    if let FileMessage::Offer(offer) = decoded {
        assert_eq!(offer.transfer_id, "xfer-001");
        assert_eq!(offer.filename, "report.pdf");
        assert_eq!(offer.size, 123_456);
        assert_eq!(offer.total_chunks, 2);
    } else {
        panic!("expected Offer variant");
    }
}

#[test]
fn file_accept_roundtrip() {
    let msg = FileMessage::Accept {
        transfer_id: "xfer-002".into(),
    };
    let json = serde_json::to_vec(&msg).unwrap();
    let json_str = String::from_utf8_lossy(&json);
    assert!(
        json_str.contains("\"type\":\"file-accept\""),
        "got: {json_str}"
    );
    let decoded: FileMessage = serde_json::from_slice(&json).unwrap();
    if let FileMessage::Accept { transfer_id } = decoded {
        assert_eq!(transfer_id, "xfer-002");
    } else {
        panic!("expected Accept variant");
    }
}

#[test]
fn file_reject_roundtrip() {
    let msg = FileMessage::Reject {
        transfer_id: "xfer-003".into(),
        reason: "too large".into(),
    };
    let json = serde_json::to_vec(&msg).unwrap();
    let json_str = String::from_utf8_lossy(&json);
    assert!(
        json_str.contains("\"type\":\"file-reject\""),
        "got: {json_str}"
    );
    let decoded: FileMessage = serde_json::from_slice(&json).unwrap();
    if let FileMessage::Reject {
        transfer_id,
        reason,
    } = decoded
    {
        assert_eq!(transfer_id, "xfer-003");
        assert_eq!(reason, "too large");
    } else {
        panic!("expected Reject variant");
    }
}

#[test]
fn file_chunk_roundtrip() {
    let msg = FileMessage::Chunk(FileChunk {
        transfer_id: "xfer-004".into(),
        sequence: 7,
        data: base64::engine::general_purpose::STANDARD.encode([0xDE, 0xAD, 0xBE, 0xEF]),
    });
    let json = serde_json::to_vec(&msg).unwrap();
    let json_str = String::from_utf8_lossy(&json);
    assert!(
        json_str.contains("\"type\":\"file-chunk\""),
        "got: {json_str}"
    );
    let decoded: FileMessage = serde_json::from_slice(&json).unwrap();
    if let FileMessage::Chunk(chunk) = decoded {
        assert_eq!(chunk.transfer_id, "xfer-004");
        assert_eq!(chunk.sequence, 7);
    } else {
        panic!("expected Chunk variant");
    }
}

#[test]
fn file_complete_roundtrip() {
    let msg = FileMessage::Complete(FileComplete {
        transfer_id: "xfer-005".into(),
        sha256: "abc".into(),
    });
    let json = serde_json::to_vec(&msg).unwrap();
    let json_str = String::from_utf8_lossy(&json);
    assert!(
        json_str.contains("\"type\":\"file-complete\""),
        "got: {json_str}"
    );
    let decoded: FileMessage = serde_json::from_slice(&json).unwrap();
    if let FileMessage::Complete(complete) = decoded {
        assert_eq!(complete.transfer_id, "xfer-005");
        assert_eq!(complete.sha256, "abc");
    } else {
        panic!("expected Complete variant");
    }
}

// ---------------------------------------------------------------------------
// 2. TransferState creation and fields
// ---------------------------------------------------------------------------

#[test]
fn transfer_state_sending_fields() {
    // Chunk-size-agnostic: 3 full chunks + a remainder = 4 total.
    let size = (DEFAULT_CHUNK_SIZE as u64) * 3 + 200;
    let ts = make_sending_transfer(size, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.direction, TransferDirection::Sending);
    assert_eq!(ts.status, TransferStatus::Pending);
    assert_eq!(ts.bytes_transferred, 0);
    assert_eq!(ts.source_path, Some("/tmp/test.bin".to_string()));
    assert!(ts.output_path.is_none());
    assert_eq!(ts.chunk_size, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.total_chunks, 4);
}

#[test]
fn transfer_state_receiving_fields() {
    // Exactly one chunk regardless of chunk size constant.
    let ts = make_receiving_transfer(DEFAULT_CHUNK_SIZE as u64, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.direction, TransferDirection::Receiving);
    assert_eq!(ts.status, TransferStatus::InProgress);
    assert!(ts.source_path.is_none());
    assert_eq!(ts.output_path, Some("/tmp/received.bin".to_string()));
    assert_eq!(ts.total_chunks, 1);
}

// ---------------------------------------------------------------------------
// 3. Chunk count computation for various file sizes
// ---------------------------------------------------------------------------

#[test]
fn total_chunks_zero_size() {
    let ts = make_sending_transfer(0, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.total_chunks, 0);
}

#[test]
fn total_chunks_one_byte() {
    let ts = make_sending_transfer(1, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.total_chunks, 1);
}

#[test]
fn total_chunks_exact_chunk_size() {
    let ts = make_sending_transfer(DEFAULT_CHUNK_SIZE as u64, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.total_chunks, 1);
}

#[test]
fn total_chunks_chunk_size_plus_one() {
    let ts = make_sending_transfer(DEFAULT_CHUNK_SIZE as u64 + 1, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.total_chunks, 2);
}

#[test]
fn total_chunks_exact_multiple() {
    // Exactly 2 chunks at whatever DEFAULT_CHUNK_SIZE is today.
    let ts = make_sending_transfer((DEFAULT_CHUNK_SIZE as u64) * 2, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.total_chunks, 2);
}

#[test]
fn total_chunks_large_file() {
    // 1 MiB / current chunk size, rounded up.
    const ONE_MIB: u64 = 1 << 20;
    let expected = ONE_MIB.div_ceil(DEFAULT_CHUNK_SIZE as u64);
    let ts = make_sending_transfer(ONE_MIB, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.total_chunks, expected);
}

// ---------------------------------------------------------------------------
// 4. MAX_TRANSFER_SIZE constant
// ---------------------------------------------------------------------------

#[test]
fn max_transfer_size_is_one_gib() {
    assert_eq!(MAX_TRANSFER_SIZE, 1 << 30);
    assert_eq!(MAX_TRANSFER_SIZE, 1_073_741_824);
}

// ---------------------------------------------------------------------------
// 5. Chunk sequence validation
// ---------------------------------------------------------------------------

#[test]
fn expected_sequence_starts_at_zero() {
    let ts = make_receiving_transfer(200_000, DEFAULT_CHUNK_SIZE);
    let expected = ts.bytes_transferred / ts.chunk_size as u64;
    assert_eq!(expected, 0);
}

#[test]
fn expected_sequence_advances_with_chunks() {
    // Size the transfer relative to the current DEFAULT_CHUNK_SIZE so the
    // test stays correct when the constant changes (64 KiB → 32 KiB etc.).
    let two_chunks_plus_tail = (DEFAULT_CHUNK_SIZE as u64) * 2 + 1_024;
    let mut ts = make_receiving_transfer(two_chunks_plus_tail, DEFAULT_CHUNK_SIZE);

    // After one full chunk
    ts.bytes_transferred = DEFAULT_CHUNK_SIZE as u64;
    let expected = ts.bytes_transferred / ts.chunk_size as u64;
    assert_eq!(expected, 1);

    // After two full chunks
    ts.bytes_transferred = (DEFAULT_CHUNK_SIZE * 2) as u64;
    let expected = ts.bytes_transferred / ts.chunk_size as u64;
    assert_eq!(expected, 2);

    // After partial third chunk
    ts.bytes_transferred = two_chunks_plus_tail;
    let expected = ts.bytes_transferred / ts.chunk_size as u64;
    assert_eq!(expected, 2);
}

// ---------------------------------------------------------------------------
// 6. Size limit enforcement (cumulative)
// ---------------------------------------------------------------------------

#[test]
fn cumulative_size_detects_overflow() {
    let mut ts = make_receiving_transfer(100, DEFAULT_CHUNK_SIZE);
    // Within limit
    assert!(ts.bytes_transferred + 100 <= ts.total_size);
    // One byte over
    assert!(ts.bytes_transferred + 101 > ts.total_size);

    // After partial transfer
    ts.bytes_transferred = 60;
    assert!(ts.bytes_transferred + 40 <= ts.total_size);
    assert!(ts.bytes_transferred + 41 > ts.total_size);
}

// ---------------------------------------------------------------------------
// 7. Path traversal protection
// ---------------------------------------------------------------------------

fn sanitize_filename(name: &str) -> String {
    std::path::Path::new(name)
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_else(|| "download".to_string())
}

#[test]
fn sanitize_strips_parent_traversal() {
    assert_eq!(sanitize_filename("../../../etc/passwd"), "passwd");
}

#[test]
fn sanitize_strips_absolute_path() {
    assert_eq!(sanitize_filename("/etc/shadow"), "shadow");
}

#[test]
fn sanitize_preserves_normal_filename() {
    assert_eq!(sanitize_filename("report.pdf"), "report.pdf");
}

#[test]
fn sanitize_handles_nested_traversal() {
    assert_eq!(sanitize_filename("foo/../../bar/baz.txt"), "baz.txt");
}

#[test]
fn sanitize_handles_dots_only() {
    assert_eq!(sanitize_filename(".."), "download");
}

#[test]
fn sanitize_handles_empty_after_strip() {
    // "/" has no file_name component
    assert_eq!(sanitize_filename("/"), "download");
}

// ---------------------------------------------------------------------------
// 8. SHA-256 incremental verification
// ---------------------------------------------------------------------------

#[test]
fn sha256_incremental_matches_whole() {
    // Create test data spanning multiple chunks
    let data: Vec<u8> = (0u8..=255)
        .cycle()
        .take(DEFAULT_CHUNK_SIZE * 3 + 42)
        .collect();
    let whole_hash = hex::encode(Sha256::digest(&data));

    // Hash incrementally, chunk by chunk
    let mut hasher = Sha256::new();
    for chunk in data.chunks(DEFAULT_CHUNK_SIZE) {
        hasher.update(chunk);
    }
    let incremental_hash = hex::encode(hasher.finalize());

    assert_eq!(whole_hash, incremental_hash);
}

#[test]
fn sha256_empty_data_known_hash() {
    let hash = hex::encode(Sha256::digest(b""));
    assert_eq!(
        hash,
        "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    );
}

// ---------------------------------------------------------------------------
// 9. State machine: only InProgress+Receiving accepts chunks
// ---------------------------------------------------------------------------

#[test]
fn chunk_rejected_if_status_pending() {
    let ts = TransferState {
        status: TransferStatus::Pending,
        ..make_receiving_transfer(100_000, DEFAULT_CHUNK_SIZE)
    };
    // Chunks should only be processed when status is InProgress
    assert_ne!(ts.status, TransferStatus::InProgress);
}

#[test]
fn chunk_rejected_if_direction_sending() {
    let ts = make_sending_transfer(100_000, DEFAULT_CHUNK_SIZE);
    // Chunks should only be written for Receiving transfers
    assert_eq!(ts.direction, TransferDirection::Sending);
    assert_ne!(ts.direction, TransferDirection::Receiving);
}

#[test]
fn chunk_accepted_only_when_in_progress_receiving() {
    let ts = make_receiving_transfer(100_000, DEFAULT_CHUNK_SIZE);
    assert_eq!(ts.direction, TransferDirection::Receiving);
    assert_eq!(ts.status, TransferStatus::InProgress);
}

// ---------------------------------------------------------------------------
// 10. TransferState serde: new fields have defaults for backward compat
// ---------------------------------------------------------------------------

#[test]
fn transfer_state_deserializes_without_optional_fields() {
    // Simulate a JSON from an older version that lacks new fields
    let json = serde_json::json!({
        "transfer_id": "old-xfer",
        "direction": "Sending",
        "remote_agent_id": "abc123",
        "filename": "old.bin",
        "total_size": 1000,
        "bytes_transferred": 500,
        "status": "InProgress",
        "sha256": "deadbeef",
        "error": null,
        "started_at": 12345
    });
    let ts: TransferState = serde_json::from_value(json).unwrap();
    assert_eq!(ts.transfer_id, "old-xfer");
    assert!(ts.source_path.is_none());
    assert!(ts.output_path.is_none());
    assert_eq!(ts.chunk_size, DEFAULT_CHUNK_SIZE); // serde default
    assert_eq!(ts.total_chunks, 0); // serde default
    assert_eq!(ts.started_at_unix_ms, 0); // serde default
    assert!(ts.completed_at_unix_ms.is_none()); // serde default
}

// ---------------------------------------------------------------------------
// 11. FileOffer total_chunks matches declared size
// ---------------------------------------------------------------------------

#[test]
fn file_offer_total_chunks_consistent() {
    // Use a size chosen relative to DEFAULT_CHUNK_SIZE so the assertion
    // stays honest when the constant changes.
    let size = (DEFAULT_CHUNK_SIZE as u64) * 3 + 200;
    let computed = size.div_ceil(DEFAULT_CHUNK_SIZE as u64);
    let offer = FileOffer {
        transfer_id: "tc1".into(),
        filename: "f.bin".into(),
        size,
        sha256: "h".into(),
        chunk_size: DEFAULT_CHUNK_SIZE,
        total_chunks: computed,
    };
    assert_eq!(offer.total_chunks, computed);
    assert_eq!(computed, 4); // 3 full chunks + 1 remainder regardless of chunk size
}