aria2-core 0.3.4

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use std::sync::Arc;

use super::*;
use crate::checksum::message_digest::HashType;

// ── Test fixture helpers ─────────────────────────────────────────────────

/// Create a test `Arc<DownloadContext>` with the given piece length and total length.
fn make_dctx(piece_length: u32, total_length: u64) -> Arc<crate::download::DownloadContext> {
    Arc::new(crate::download::DownloadContext::new(
        piece_length,
        total_length,
        "/tmp/test_check_integrity.bin".to_string(),
    ))
}

/// Create a test `Arc<dyn PieceStorage>` with the given piece length and total length.
fn make_ps(
    piece_length: u64,
    total_length: u64,
) -> Arc<dyn crate::segment::piece_storage::PieceStorage> {
    Arc::new(crate::segment::piece_storage::DefaultPieceStorage::new(
        piece_length,
        total_length,
    ))
}

// ── ValidatorKind enum dispatch tests ─────────────────────────────────

#[test]
fn test_validator_kind_none_is_finished() {
    let v = ValidatorKind::None;
    assert!(
        v.is_finished(),
        "None variant should be finished (nothing to validate)"
    );
}

#[test]
fn test_validator_kind_none_zero_metrics() {
    let v = ValidatorKind::None;
    assert_eq!(v.current_offset(), 0);
    assert_eq!(v.total_length(), 0);
}

#[test]
fn test_validator_kind_none_init_and_validate_noop() {
    let mut v = ValidatorKind::None;
    // Should not panic or change state
    v.init();
    v.validate_chunk();
    assert!(v.is_finished());
}

#[test]
fn test_validator_kind_piece_hash_dispatch() {
    let ctx = make_dctx(1_048_576, 5_242_880);
    let ps = make_ps(1_048_576, 5_242_880);
    let v = ValidatorKind::PieceHash(PieceHashValidator::new(ctx, ps, 5, 5_242_880, 1_048_576));
    assert!(
        !v.is_finished(),
        "PieceHash with 5 pieces should not be finished initially"
    );
    assert_eq!(v.total_length(), 5_242_880);
    assert_eq!(v.current_offset(), 0);
}

// ── PieceHashValidator init and state tracking tests ──────────────────

#[test]
fn test_piece_hash_validator_new() {
    let ctx = make_dctx(1_048_576, 4_194_304);
    let ps = make_ps(1_048_576, 4_194_304);
    let v = PieceHashValidator::new(ctx, ps, 4, 4_194_304, 1_048_576);
    assert_eq!(v.current_piece_index(), 0);
    assert_eq!(v.total_pieces(), 4);
    assert!(!v.is_finished());
    assert_eq!(v.current_offset(), 0);
    assert_eq!(v.total_length(), 4_194_304);
}

#[test]
fn test_piece_hash_validator_zero_pieces_is_finished() {
    let ctx = make_dctx(1_048_576, 0);
    let ps = make_ps(1_048_576, 0);
    let v = PieceHashValidator::new(ctx, ps, 0, 0, 1_048_576);
    assert!(
        v.is_finished(),
        "Zero pieces should be immediately finished"
    );
}

#[test]
fn test_piece_hash_validator_init_resets_state() {
    let ctx = make_dctx(1_048_576, 3_145_728);
    let ps = make_ps(1_048_576, 3_145_728);
    let mut v = PieceHashValidator::new(ctx, ps, 3, 3_145_728, 1_048_576);
    // Simulate partial progress
    v.validate_chunk();
    v.validate_chunk();
    assert_eq!(v.current_piece_index(), 2);

    // Init should reset
    v.init();
    assert_eq!(v.current_piece_index(), 0);
    assert_eq!(v.current_offset(), 0);
    assert!(!v.is_finished());
}

#[test]
fn test_piece_hash_validator_init_with_zero_pieces() {
    let ctx = make_dctx(1024, 0);
    let ps = make_ps(1024, 0);
    let mut v = PieceHashValidator::new(ctx, ps, 0, 0, 1024);
    v.init();
    assert!(v.is_finished(), "Init with zero pieces should set finished");
}

// ── Saturated validation progress tests ───────────────────────────────

#[test]
fn test_piece_hash_validator_validate_chunk_advances() {
    let ctx = make_dctx(1_048_576, 3_145_728);
    let ps = make_ps(1_048_576, 3_145_728);
    let mut v = PieceHashValidator::new(ctx, ps, 3, 3_145_728, 1_048_576);

    v.validate_chunk();
    assert_eq!(v.current_piece_index(), 1);
    assert_eq!(v.current_offset(), 1_048_576);
    assert!(!v.is_finished());

    v.validate_chunk();
    assert_eq!(v.current_piece_index(), 2);
    assert_eq!(v.current_offset(), 2_097_152);
    assert!(!v.is_finished());
}

#[test]
fn test_piece_hash_validator_saturates_at_total_length() {
    let ctx = make_dctx(1_048_576, 2_097_152);
    let ps = make_ps(1_048_576, 2_097_152);
    let mut v = PieceHashValidator::new(ctx, ps, 2, 2_097_152, 1_048_576);

    v.validate_chunk(); // piece 0 → piece 1
    v.validate_chunk(); // piece 1 → finished

    assert!(v.is_finished());
    // After finishing, offset should not exceed total_length
    assert!(v.current_offset() <= v.total_length());
}

// ── Finished flag management tests ────────────────────────────────────

#[test]
fn test_piece_hash_validator_finished_after_all_chunks() {
    let ctx = make_dctx(1_048_576, 2_097_152);
    let ps = make_ps(1_048_576, 2_097_152);
    let mut v = PieceHashValidator::new(ctx, ps, 2, 2_097_152, 1_048_576);

    assert!(!v.is_finished());
    v.validate_chunk();
    assert!(!v.is_finished());
    v.validate_chunk();
    assert!(v.is_finished());
}

#[test]
fn test_piece_hash_validator_validate_after_finished_is_noop() {
    let ctx = make_dctx(1_048_576, 1_048_576);
    let ps = make_ps(1_048_576, 1_048_576);
    let mut v = PieceHashValidator::new(ctx, ps, 1, 1_048_576, 1_048_576);

    v.validate_chunk();
    assert!(v.is_finished());

    // Calling validate_chunk again should not panic or change state
    v.validate_chunk();
    assert!(v.is_finished());
    assert_eq!(v.current_piece_index(), 1);
}

// ── Validation result collection tests ─────────────────────────────────

#[test]
fn test_piece_hash_validator_collects_failed_results() {
    // No disk adaptor connected → all reads fail → all pieces marked failed
    let ctx = make_dctx(1_048_576, 2_097_152);
    let ps = make_ps(1_048_576, 2_097_152);
    let mut v = PieceHashValidator::new(ctx, ps, 2, 2_097_152, 1_048_576);

    v.validate_chunk();
    v.validate_chunk();
    assert!(v.is_finished());

    // All pieces should have failed (no disk adaptor)
    let results = v.validation_results();
    assert_eq!(results.len(), 2);
    assert!(matches!(
        results[0],
        PieceValidationResult::Failed { piece_index: 0 }
    ));
    assert!(matches!(
        results[1],
        PieceValidationResult::Failed { piece_index: 1 }
    ));
    assert_eq!(v.pieces_failed(), 2);
    assert_eq!(v.pieces_ok(), 0);
}

#[test]
fn test_piece_hash_validator_apply_results() {
    let ctx = make_dctx(1_048_576, 2_097_152);
    let ps = make_ps(1_048_576, 2_097_152);
    let mut v = PieceHashValidator::new(ctx, ps, 2, 2_097_152, 1_048_576);

    v.validate_chunk();
    v.validate_chunk();

    // Apply results to a fresh PieceStorage
    let mut ps2 = crate::segment::piece_storage::DefaultPieceStorage::new(1_048_576, 2_097_152);
    v.apply_validation_results(&mut ps2);
    // Should not panic
}

// ── CheckIntegrityKind enum dispatch tests ────────────────────────────

#[test]
fn test_check_integrity_kind_stream() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let entry = CheckIntegrityKind::Stream(StreamCheckIntegrity::new(ctx, ps, false));
    assert!(!entry.is_validation_ready()); // No piece hashes set
    assert!(entry.is_finished()); // No validator yet (None), so finished
    assert_eq!(entry.total_length(), 0);
    assert_eq!(entry.current_length(), 0);
    assert!(entry.should_report_incomplete_as_error());
}

#[test]
fn test_check_integrity_kind_bt() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let entry = CheckIntegrityKind::Bt(BtCheckIntegrity::new(ctx, ps));
    assert!(!entry.is_validation_ready()); // No piece hashes set
    assert!(entry.is_finished()); // No validator yet (None), so finished
    assert_eq!(entry.total_length(), 0);
    assert_eq!(entry.current_length(), 0);
    assert!(!entry.should_report_incomplete_as_error());
}

#[test]
fn test_check_integrity_kind_init_and_validate_stream() {
    let ctx = make_dctx(1_048_576, 3_145_728);
    let ps = make_ps(1_048_576, 3_145_728);
    let mut entry = CheckIntegrityKind::Stream(StreamCheckIntegrity::new(ctx, ps, false));
    entry.init_validator();
    // Without piece hashes, the validator won't be created,
    // so it remains finished (ValidatorKind::None).
    assert!(entry.is_finished());
}

#[test]
fn test_check_integrity_kind_init_and_validate_bt() {
    let ctx = make_dctx(1_048_576, 2_097_152);
    let ps = make_ps(1_048_576, 2_097_152);
    let mut entry = CheckIntegrityKind::Bt(BtCheckIntegrity::new(ctx, ps));
    entry.init_validator();
    // Without piece hashes, the validator won't be created,
    // so it remains finished (ValidatorKind::None).
    assert!(entry.is_finished());
}

// ── StreamCheckIntegrity creation and validation_ready tests ──────────

#[test]
fn test_stream_check_integrity_new() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let s = StreamCheckIntegrity::new(ctx, ps, false);
    assert!(!s.hash_check_only());
    assert!(s.is_finished()); // No validator → finished
}

#[test]
fn test_stream_check_integrity_hash_check_only() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let mut s = StreamCheckIntegrity::new(ctx, ps, true);
    assert!(s.hash_check_only());
    s.set_hash_check_only(false);
    assert!(!s.hash_check_only());
}

#[test]
fn test_stream_check_integrity_validation_ready() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let s = StreamCheckIntegrity::new(ctx, ps, false);
    // No piece hashes set → not ready
    assert!(!s.is_validation_ready());
}

#[test]
fn test_stream_check_integrity_validation_ready_with_hashes() {
    let mut ctx = crate::download::DownloadContext::new(1024, 4096, "/tmp/test.bin".to_string());
    ctx.set_piece_hashes(
        "sha-1".to_string(),
        vec![
            "h1".to_string(),
            "h2".to_string(),
            "h3".to_string(),
            "h4".to_string(),
        ],
    );
    let ctx = Arc::new(ctx);
    let ps = make_ps(1024, 4096);
    let s = StreamCheckIntegrity::new(ctx, ps, false);
    assert!(s.is_validation_ready());
}

#[test]
fn test_stream_check_integrity_init_validator() {
    let ctx = make_dctx(1_048_576, 4_194_304);
    let ps = make_ps(1_048_576, 4_194_304);
    let mut s = StreamCheckIntegrity::new(ctx, ps, false);
    assert!(s.is_finished()); // No validator yet

    // Without piece hashes, init_validator is a no-op (validator stays None)
    s.init_validator();
    assert!(s.is_finished()); // Still finished (no validator created)
}

#[test]
fn test_stream_check_integrity_init_validator_with_hashes() {
    let mut ctx =
        crate::download::DownloadContext::new(1_048_576, 4_194_304, "/tmp/test.bin".to_string());
    ctx.set_piece_hashes(
        "sha-1".to_string(),
        vec![
            "h1".to_string(),
            "h2".to_string(),
            "h3".to_string(),
            "h4".to_string(),
        ],
    );
    let ctx = Arc::new(ctx);
    let ps = make_ps(1_048_576, 4_194_304);
    let mut s = StreamCheckIntegrity::new(ctx, ps, false);
    assert!(s.is_finished()); // No validator yet

    s.init_validator();
    assert!(!s.is_finished()); // Validator created, not yet finished
    assert_eq!(s.total_length(), 4_194_304);
}

#[test]
fn test_stream_check_integrity_validator_access() {
    let ctx = make_dctx(1_048_576, 1_048_576);
    let ps = make_ps(1_048_576, 1_048_576);
    let mut s = StreamCheckIntegrity::new(ctx, ps, false);
    assert!(matches!(s.validator(), ValidatorKind::None));

    // Without piece hashes, init_validator is a no-op
    s.init_validator();
    assert!(matches!(s.validator(), ValidatorKind::None));
}

#[test]
fn test_stream_check_integrity_on_download_finished_noop() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let s = StreamCheckIntegrity::new(ctx, ps, false);
    assert_eq!(s.on_download_finished(), IntegrityFinishedAction::default());
}

#[test]
fn test_stream_check_integrity_on_download_incomplete() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let s = StreamCheckIntegrity::new(ctx, ps, false);
    let action = s.on_download_incomplete();
    assert!(action.reset_piece_storage);
    assert_eq!(action.file_allocation.as_ref().unwrap().len(), 1);
    assert_eq!(action.file_allocation.unwrap()[0].length, 4096);
}

#[test]
fn test_integrity_incomplete_action_applies_piece_storage_reset() {
    use crate::segment::piece_storage::{
        DefaultPieceStorage, PieceStorage, StreamPieceSelectorKind,
    };

    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let action = StreamCheckIntegrity::new(ctx, ps, false).on_download_incomplete();

    let mut storage = DefaultPieceStorage::new(1024, 4096);
    storage.set_stream_piece_selector(StreamPieceSelectorKind::Geom);
    storage.set_bitfield(&[0b1100_0000]);
    storage.mark_piece_missing(0);

    // Leave the geometric selector stale, then apply the owner-side plan.
    // The reset must make the newly missing first piece selectable again.
    action.apply_piece_storage(&mut storage);

    let piece = storage
        .get_missing_piece(0, &[0], 0, 1)
        .expect("the reset selector should return the first missing piece");
    assert_eq!(piece.index(), 0);
}

#[test]
fn test_stream_check_integrity_hash_check_only_skips_allocation() {
    let ctx1 = make_dctx(1024, 4096);
    let ps1 = make_ps(1024, 4096);
    let ctx2 = make_dctx(1024, 4096);
    let ps2 = make_ps(1024, 4096);
    let s_with = StreamCheckIntegrity::new(ctx1, ps1, true);
    let s_without = StreamCheckIntegrity::new(ctx2, ps2, false);
    assert!(s_with.on_download_incomplete().file_allocation.is_none());
    assert!(s_without.on_download_incomplete().file_allocation.is_some());
}

// ── BtCheckIntegrity tests ────────────────────────────────────────────

#[test]
fn test_bt_check_integrity_new() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let b = BtCheckIntegrity::new(ctx, ps);
    assert!(b.is_finished()); // No validator yet
    assert!(!b.should_report_incomplete_as_error());
}

#[test]
fn test_bt_check_integrity_init_validator() {
    let mut ctx =
        crate::download::DownloadContext::new(1_048_576, 2_097_152, "/tmp/test.bin".to_string());
    ctx.set_piece_hashes(
        "sha-1".to_string(),
        vec!["h1".to_string(), "h2".to_string()],
    );
    let ctx = Arc::new(ctx);
    let ps = make_ps(1_048_576, 2_097_152);
    let mut b = BtCheckIntegrity::new(ctx, ps);
    b.init_validator();
    assert!(!b.is_finished());
    assert_eq!(b.total_length(), 2_097_152);
}

#[test]
fn test_bt_check_integrity_on_download_handlers() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let b = BtCheckIntegrity::new(ctx, ps);
    assert!(b.on_download_finished().file_allocation.is_some());
    let incomplete = b.on_download_incomplete();
    assert!(incomplete.reset_piece_storage);
    assert!(incomplete.file_allocation.is_some());
}

#[test]
fn test_bt_check_integrity_options_gate_dispatch() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let b = BtCheckIntegrity::new_with_options(ctx, ps, true, false, true);

    let finished = b.on_download_finished();
    assert!(finished.file_allocation.is_none());
    assert!(finished.run_completion_hook);
    assert!(b.on_download_incomplete().file_allocation.is_none());
}

#[test]
fn test_integrity_callback_trailing_garbage_lists_physical_files() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let stream = StreamCheckIntegrity::new(ctx, ps, false);

    let action = stream.cut_trailing_garbage();
    assert_eq!(action.files.len(), 1);
    assert_eq!(
        action.files[0].path.to_string_lossy(),
        "/tmp/test_check_integrity.bin"
    );
    assert_eq!(action.files[0].length, 4096);
}

#[tokio::test]
async fn test_integrity_callback_trailing_garbage_applies_truncation() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("payload.bin");
    tokio::fs::write(&path, vec![0u8; 12]).await.unwrap();
    let ctx = Arc::new(crate::download::DownloadContext::new(
        1024,
        8,
        path.to_string_lossy().into_owned(),
    ));
    let ps = make_ps(1024, 8);
    let action = StreamCheckIntegrity::new(ctx, ps, false).cut_trailing_garbage();

    action.apply().await.unwrap();

    assert_eq!(tokio::fs::metadata(path).await.unwrap().len(), 8);
}

// ── Cross-cutting: ValidatorKind after PieceHashValidator assignment ───

#[test]
fn test_validator_kind_piece_hash_full_lifecycle() {
    let ctx = make_dctx(1_048_576, 2_097_152);
    let ps = make_ps(1_048_576, 2_097_152);
    let mut v = ValidatorKind::PieceHash(PieceHashValidator::new(ctx, ps, 2, 2_097_152, 1_048_576));

    v.init();
    assert!(!v.is_finished());
    assert_eq!(v.current_offset(), 0);

    v.validate_chunk();
    assert_eq!(v.current_offset(), 1_048_576);

    v.validate_chunk();
    assert!(v.is_finished());
    assert_eq!(v.current_offset(), 2_097_152);

    // Validate after finish should be no-op
    v.validate_chunk();
    assert!(v.is_finished());
}

#[test]
fn test_validator_kind_init_on_piece_hash() {
    let ctx = make_dctx(1_048_576, 3_145_728);
    let ps = make_ps(1_048_576, 3_145_728);
    let mut v = ValidatorKind::PieceHash(PieceHashValidator::new(ctx, ps, 3, 3_145_728, 1_048_576));
    v.validate_chunk(); // advance to piece 1
    assert_eq!(v.current_offset(), 1_048_576);

    v.init(); // reset
    assert_eq!(v.current_offset(), 0);
    assert!(!v.is_finished());
}

#[test]
fn test_validator_kind_apply_validation_results() {
    let ctx = make_dctx(1_048_576, 2_097_152);
    let ps = make_ps(1_048_576, 2_097_152);
    let mut v = ValidatorKind::PieceHash(PieceHashValidator::new(ctx, ps, 2, 2_097_152, 1_048_576));

    v.validate_chunk();
    v.validate_chunk();
    assert!(v.is_finished());

    // Apply results to a fresh PieceStorage
    let mut ps2 = crate::segment::piece_storage::DefaultPieceStorage::new(1_048_576, 2_097_152);
    v.apply_validation_results(&mut ps2);
    // Should not panic
}

#[test]
fn test_validator_kind_apply_validation_results_none_variant() {
    let v = ValidatorKind::None;
    let mut ps = crate::segment::piece_storage::DefaultPieceStorage::new(1_048_576, 2_097_152);
    // Should not panic — no-op for None variant
    v.apply_validation_results(&mut ps);
}

// ── Debug trait tests ──────────────────────────────────────────────────

#[test]
fn test_stream_check_integrity_debug() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let s = StreamCheckIntegrity::new(ctx, ps, false);
    let debug_str = format!("{:?}", s);
    assert!(debug_str.contains("StreamCheckIntegrity"));
    assert!(debug_str.contains("hash_check_only: false"));
}

#[test]
fn test_bt_check_integrity_debug() {
    let ctx = make_dctx(1024, 4096);
    let ps = make_ps(1024, 4096);
    let b = BtCheckIntegrity::new(ctx, ps);
    let debug_str = format!("{:?}", b);
    assert!(debug_str.contains("BtCheckIntegrity"));
}

#[test]
fn test_piece_validation_result_debug() {
    let r1 = PieceValidationResult::Verified { piece_index: 0 };
    let r2 = PieceValidationResult::Failed { piece_index: 1 };
    assert!(format!("{:?}", r1).contains("Verified"));
    assert!(format!("{:?}", r2).contains("Failed"));
}

// ── Piece hash algorithm selection ────────────────────────────────────
//
// `compute_hash` used to hardcode SHA-1 while C++
// `IteratableChunkChecksumValidator` builds its MessageDigest from
// `dctx->getPieceHashType()`. Any download whose piece hashes were not SHA-1
// (Metalink `<pieces type="sha-256">`, HTTP piece hashes) therefore compared a
// 40-char digest against a 64-char expected value and failed every piece.

/// Build a validator whose DownloadContext declares `hash_type` piece hashes.
fn validator_with_hash_type(hash_type: &str) -> PieceHashValidator {
    let mut ctx = crate::download::DownloadContext::new(4, 8, "/tmp/hash_type.bin".to_string());
    if !hash_type.is_empty() {
        ctx.set_piece_hashes(
            hash_type.to_string(),
            vec!["x".to_string(), "y".to_string()],
        );
    }
    PieceHashValidator::new(Arc::new(ctx), make_ps(4, 8), 2, 8, 4)
}

#[test]
fn test_compute_hash_honours_sha256_piece_hash_type() {
    let v = validator_with_hash_type("sha-256");
    let got = v.compute_hash(b"abc").expect("sha-256 must be supported");
    assert_eq!(
        got, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
        "must hash with SHA-256, not SHA-1"
    );
}

#[test]
fn test_compute_hash_honours_sha512_piece_hash_type() {
    let v = validator_with_hash_type("sha-512");
    let got = v.compute_hash(b"abc").expect("sha-512 must be supported");
    assert_eq!(got.len(), 128, "SHA-512 hex digest is 128 chars");
}

#[test]
fn test_compute_hash_honours_md5_piece_hash_type() {
    let v = validator_with_hash_type("md5");
    let got = v.compute_hash(b"abc").expect("md5 must be supported");
    assert_eq!(got, "900150983cd24fb0d6963f7d28e17f72");
}

#[test]
fn test_compute_hash_defaults_to_sha1_when_type_empty() {
    // Empty piece hash type means BitTorrent, whose piece hashes are SHA-1.
    let v = validator_with_hash_type("");
    assert_eq!(v.resolve_hash_type(), Some(HashType::Sha1));
    let got = v.compute_hash(b"abc").expect("sha-1 fallback");
    assert_eq!(got, "a9993e364706816aba3e25717850c26c9cd0d89d");
}

#[test]
fn test_compute_hash_rejects_unknown_algorithm() {
    let v = validator_with_hash_type("not-a-real-hash");
    assert_eq!(v.resolve_hash_type(), None);
    assert!(
        v.compute_hash(b"abc").is_none(),
        "an unrecognised algorithm must not silently fall back to SHA-1"
    );
}