innodb-utils 5.0.0

InnoDB file analysis toolkit
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
#![cfg(feature = "cli")]
//! Edge-case integration tests for v3.0 write operations (repair, defrag, transplant, corrupt).

use byteorder::{BigEndian, ByteOrder};
use std::io::Write;
use tempfile::NamedTempFile;

use idb::innodb::checksum::{recalculate_checksum, validate_checksum, ChecksumAlgorithm};
use idb::innodb::constants::*;
use idb::innodb::vendor::{MariaDbFormat, VendorInfo};
use idb::innodb::write;

const PAGE_SIZE: u32 = 16384;
const PS: usize = PAGE_SIZE as usize;

const PAGE_SIZE_8K: u32 = 8192;
const PS_8K: usize = PAGE_SIZE_8K as usize;

fn build_fsp_hdr_page(space_id: u32, total_pages: u32) -> Vec<u8> {
    write::build_fsp_page(
        space_id,
        total_pages,
        0,
        1000,
        PAGE_SIZE,
        ChecksumAlgorithm::Crc32c,
    )
}

fn build_index_page(page_num: u32, space_id: u32, lsn: u64) -> Vec<u8> {
    let mut page = vec![0u8; PS];
    BigEndian::write_u32(&mut page[FIL_PAGE_OFFSET..], page_num);
    BigEndian::write_u32(&mut page[FIL_PAGE_PREV..], FIL_NULL);
    BigEndian::write_u32(&mut page[FIL_PAGE_NEXT..], FIL_NULL);
    BigEndian::write_u64(&mut page[FIL_PAGE_LSN..], lsn);
    BigEndian::write_u16(&mut page[FIL_PAGE_TYPE..], 17855); // INDEX
    BigEndian::write_u32(&mut page[FIL_PAGE_SPACE_ID..], space_id);
    let trailer = PS - SIZE_FIL_TRAILER;
    BigEndian::write_u32(&mut page[trailer + 4..], (lsn & 0xFFFFFFFF) as u32);
    recalculate_checksum(&mut page, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    page
}

fn build_index_page_with_chain(
    page_num: u32,
    space_id: u32,
    lsn: u64,
    prev: u32,
    next: u32,
    index_id: u64,
    level: u16,
) -> Vec<u8> {
    let mut page = vec![0u8; PS];
    BigEndian::write_u32(&mut page[FIL_PAGE_OFFSET..], page_num);
    BigEndian::write_u32(&mut page[FIL_PAGE_PREV..], prev);
    BigEndian::write_u32(&mut page[FIL_PAGE_NEXT..], next);
    BigEndian::write_u64(&mut page[FIL_PAGE_LSN..], lsn);
    BigEndian::write_u16(&mut page[FIL_PAGE_TYPE..], 17855); // INDEX
    BigEndian::write_u32(&mut page[FIL_PAGE_SPACE_ID..], space_id);

    // INDEX page header
    let ph = FIL_PAGE_DATA;
    BigEndian::write_u16(&mut page[ph + PAGE_N_DIR_SLOTS..], 2);
    BigEndian::write_u16(&mut page[ph + PAGE_N_HEAP..], 0x8002);
    BigEndian::write_u16(&mut page[ph + PAGE_LEVEL..], level);
    BigEndian::write_u64(&mut page[ph + PAGE_INDEX_ID..], index_id);

    let trailer = PS - SIZE_FIL_TRAILER;
    BigEndian::write_u32(&mut page[trailer + 4..], (lsn & 0xFFFFFFFF) as u32);
    recalculate_checksum(&mut page, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    page
}

fn build_undo_page(page_num: u32, space_id: u32, lsn: u64) -> Vec<u8> {
    let mut page = vec![0u8; PS];
    BigEndian::write_u32(&mut page[FIL_PAGE_OFFSET..], page_num);
    BigEndian::write_u32(&mut page[FIL_PAGE_PREV..], FIL_NULL);
    BigEndian::write_u32(&mut page[FIL_PAGE_NEXT..], FIL_NULL);
    BigEndian::write_u64(&mut page[FIL_PAGE_LSN..], lsn);
    BigEndian::write_u16(&mut page[FIL_PAGE_TYPE..], 2); // UNDO_LOG
    BigEndian::write_u32(&mut page[FIL_PAGE_SPACE_ID..], space_id);
    let trailer = PS - SIZE_FIL_TRAILER;
    BigEndian::write_u32(&mut page[trailer + 4..], (lsn & 0xFFFFFFFF) as u32);
    recalculate_checksum(&mut page, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    page
}

fn build_index_page_defrag(
    page_num: u32,
    space_id: u32,
    lsn: u64,
    index_id: u64,
    level: u16,
) -> Vec<u8> {
    let mut page = vec![0u8; PS];
    BigEndian::write_u32(&mut page[FIL_PAGE_OFFSET..], page_num);
    BigEndian::write_u32(&mut page[FIL_PAGE_PREV..], FIL_NULL);
    BigEndian::write_u32(&mut page[FIL_PAGE_NEXT..], FIL_NULL);
    BigEndian::write_u64(&mut page[FIL_PAGE_LSN..], lsn);
    BigEndian::write_u16(&mut page[FIL_PAGE_TYPE..], 17855); // INDEX
    BigEndian::write_u32(&mut page[FIL_PAGE_SPACE_ID..], space_id);

    let ph = FIL_PAGE_DATA;
    BigEndian::write_u16(&mut page[ph + PAGE_N_DIR_SLOTS..], 2);
    BigEndian::write_u16(&mut page[ph + PAGE_N_HEAP..], 0x8002);
    BigEndian::write_u16(&mut page[ph + PAGE_LEVEL..], level);
    BigEndian::write_u64(&mut page[ph + PAGE_INDEX_ID..], index_id);

    let trailer = PS - SIZE_FIL_TRAILER;
    BigEndian::write_u32(&mut page[trailer + 4..], (lsn & 0xFFFFFFFF) as u32);
    recalculate_checksum(&mut page, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    page
}

fn build_8k_index_page(page_num: u32, space_id: u32, lsn: u64) -> Vec<u8> {
    let mut page = vec![0u8; PS_8K];
    BigEndian::write_u32(&mut page[FIL_PAGE_OFFSET..], page_num);
    BigEndian::write_u32(&mut page[FIL_PAGE_PREV..], FIL_NULL);
    BigEndian::write_u32(&mut page[FIL_PAGE_NEXT..], FIL_NULL);
    BigEndian::write_u64(&mut page[FIL_PAGE_LSN..], lsn);
    BigEndian::write_u16(&mut page[FIL_PAGE_TYPE..], 17855); // INDEX
    BigEndian::write_u32(&mut page[FIL_PAGE_SPACE_ID..], space_id);
    let trailer = PS_8K - SIZE_FIL_TRAILER;
    BigEndian::write_u32(&mut page[trailer + 4..], (lsn & 0xFFFFFFFF) as u32);
    recalculate_checksum(&mut page, PAGE_SIZE_8K, ChecksumAlgorithm::Crc32c);
    page
}

fn write_tablespace(pages: &[Vec<u8>]) -> NamedTempFile {
    let mut tmp = NamedTempFile::new().unwrap();
    for page in pages {
        tmp.write_all(page).unwrap();
    }
    tmp.flush().unwrap();
    tmp
}

fn corrupt_checksum(page: &mut [u8]) {
    BigEndian::write_u32(&mut page[FIL_PAGE_SPACE_OR_CHKSUM..], 0xDEADDEAD);
}

// ---------------------------------------------------------------------------
// 1. MariaDB full_crc32 repair
// ---------------------------------------------------------------------------

#[test]
fn test_mariadb_full_crc32_repair() {
    let flags = MARIADB_FSP_FLAGS_FCRC32_MARKER_MASK; // bit 4 = 0x10
    let page0 = write::build_fsp_page(
        42,
        3,
        flags,
        1000,
        PAGE_SIZE,
        ChecksumAlgorithm::MariaDbFullCrc32,
    );
    let mut page1 = vec![0u8; PS];
    BigEndian::write_u32(&mut page1[FIL_PAGE_OFFSET..], 1);
    BigEndian::write_u32(&mut page1[FIL_PAGE_PREV..], FIL_NULL);
    BigEndian::write_u32(&mut page1[FIL_PAGE_NEXT..], FIL_NULL);
    BigEndian::write_u64(&mut page1[FIL_PAGE_LSN..], 2000);
    BigEndian::write_u16(&mut page1[FIL_PAGE_TYPE..], 17855);
    BigEndian::write_u32(&mut page1[FIL_PAGE_SPACE_ID..], 42);
    let trailer = PS - SIZE_FIL_TRAILER;
    BigEndian::write_u32(&mut page1[trailer + 4..], 2000);
    recalculate_checksum(&mut page1, PAGE_SIZE, ChecksumAlgorithm::MariaDbFullCrc32);

    let mut page2 = page1.clone();
    BigEndian::write_u32(&mut page2[FIL_PAGE_OFFSET..], 2);
    BigEndian::write_u64(&mut page2[FIL_PAGE_LSN..], 3000);
    let trailer = PS - SIZE_FIL_TRAILER;
    BigEndian::write_u32(&mut page2[trailer + 4..], 3000);
    recalculate_checksum(&mut page2, PAGE_SIZE, ChecksumAlgorithm::MariaDbFullCrc32);

    // Corrupt page 1 by zeroing the last 4 bytes (where MariaDB stores the checksum)
    BigEndian::write_u32(&mut page1[PS - 4..], 0xBADBAD00);

    let vendor = VendorInfo::mariadb(MariaDbFormat::FullCrc32);
    assert!(!validate_checksum(&page1, PAGE_SIZE, Some(&vendor)).valid);

    let tmp = write_tablespace(&[page0, page1, page2]);
    let path = tmp.path().to_str().unwrap();

    let mut output = Vec::new();
    idb::cli::repair::execute(
        &idb::cli::repair::RepairOptions {
            file: Some(path.to_string()),
            batch: None,
            page: None,
            algorithm: "full_crc32".to_string(),
            no_backup: true,
            dry_run: false,
            verbose: false,
            json: false,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    // All pages should now be valid under MariaDB full_crc32
    for i in 0..3 {
        let page = write::read_page_raw(path, i, PAGE_SIZE).unwrap();
        assert!(
            validate_checksum(&page, PAGE_SIZE, Some(&vendor)).valid,
            "Page {} invalid after MariaDB full_crc32 repair",
            i
        );
    }
}

// ---------------------------------------------------------------------------
// 2. Repair single page leaves other pages untouched
// ---------------------------------------------------------------------------

#[test]
fn test_repair_single_page_leaves_others_untouched() {
    let page0 = build_fsp_hdr_page(42, 4);
    let page1 = build_index_page(1, 42, 2000);
    let mut page2 = build_index_page(2, 42, 3000);
    let page3 = build_index_page(3, 42, 4000);

    // Save originals before corruption
    let original_page1 = page1.clone();
    let original_page3 = page3.clone();

    corrupt_checksum(&mut page2);

    let tmp = write_tablespace(&[page0, page1, page2, page3]);
    let path = tmp.path().to_str().unwrap();

    let mut output = Vec::new();
    idb::cli::repair::execute(
        &idb::cli::repair::RepairOptions {
            file: Some(path.to_string()),
            batch: None,
            page: Some(2),
            algorithm: "crc32c".to_string(),
            no_backup: true,
            dry_run: false,
            verbose: false,
            json: false,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    // Page 2 should now be valid
    let p2 = write::read_page_raw(path, 2, PAGE_SIZE).unwrap();
    assert!(validate_checksum(&p2, PAGE_SIZE, None).valid);

    // Pages 1 and 3 should be byte-identical to originals
    let p1 = write::read_page_raw(path, 1, PAGE_SIZE).unwrap();
    let p3 = write::read_page_raw(path, 3, PAGE_SIZE).unwrap();
    assert_eq!(
        p1, original_page1,
        "Page 1 was modified by single-page repair"
    );
    assert_eq!(
        p3, original_page3,
        "Page 3 was modified by single-page repair"
    );
}

// ---------------------------------------------------------------------------
// 3. Repair dry-run does not modify file
// ---------------------------------------------------------------------------

#[test]
fn test_repair_dry_run_file_unchanged() {
    let page0 = build_fsp_hdr_page(42, 3);
    let page1 = build_index_page(1, 42, 2000);
    let mut page2 = build_index_page(2, 42, 3000);
    corrupt_checksum(&mut page2);

    let tmp = write_tablespace(&[page0, page1, page2]);
    let path = tmp.path().to_str().unwrap();

    // Read entire file before repair
    let before_bytes = std::fs::read(path).unwrap();

    let mut output = Vec::new();
    idb::cli::repair::execute(
        &idb::cli::repair::RepairOptions {
            file: Some(path.to_string()),
            batch: None,
            page: None,
            algorithm: "auto".to_string(),
            no_backup: true,
            dry_run: true,
            verbose: false,
            json: false,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    // File should be completely unchanged
    let after_bytes = std::fs::read(path).unwrap();
    assert_eq!(before_bytes, after_bytes, "Dry-run modified the file");
}

// ---------------------------------------------------------------------------
// 4. Defrag with mixed page types (INDEX + UNDO)
// ---------------------------------------------------------------------------

#[test]
fn test_defrag_mixed_page_types() {
    let page0 = write::build_fsp_page(42, 5, 0, 1000, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    let page1 = build_index_page_defrag(1, 42, 2000, 100, 0);
    let page2 = build_index_page_defrag(2, 42, 3000, 100, 0);
    let page3 = build_undo_page(3, 42, 4000);
    let page4 = build_index_page_defrag(4, 42, 5000, 200, 0);

    let tmp = write_tablespace(&[page0, page1, page2, page3, page4]);
    let path = tmp.path().to_str().unwrap();

    let output_tmp = NamedTempFile::new().unwrap();
    let output_path = output_tmp.path().to_str().unwrap().to_string();
    drop(output_tmp);

    let mut output = Vec::new();
    idb::cli::defrag::execute(
        &idb::cli::defrag::DefragOptions {
            file: path.to_string(),
            output: output_path.clone(),
            verbose: false,
            json: true,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    let text = String::from_utf8(output).unwrap();
    let json: serde_json::Value = serde_json::from_str(&text).unwrap();

    // 5 source pages, all should survive (none empty, none corrupt)
    assert_eq!(json["source_pages"], 5);
    assert_eq!(json["output_pages"], 5);
    assert_eq!(json["index_pages"], 3);
    assert_eq!(json["empty_removed"], 0);

    // All output pages should have valid checksums
    for i in 0..5u64 {
        let page = write::read_page_raw(&output_path, i, PAGE_SIZE).unwrap();
        assert!(
            validate_checksum(&page, PAGE_SIZE, None).valid,
            "Page {} invalid in mixed-type defrag output",
            i
        );
    }

    let _ = std::fs::remove_file(&output_path);
}

// ---------------------------------------------------------------------------
// 5. Defrag removes empty pages
// ---------------------------------------------------------------------------

#[test]
fn test_defrag_removes_empty_pages_edge() {
    let page0 = write::build_fsp_page(42, 5, 0, 1000, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    let page1 = build_index_page_defrag(1, 42, 2000, 100, 0);
    let empty1 = vec![0u8; PS];
    let page3 = build_index_page_defrag(3, 42, 3000, 100, 0);
    let empty2 = vec![0u8; PS];

    let tmp = write_tablespace(&[page0, page1, empty1, page3, empty2]);
    let path = tmp.path().to_str().unwrap();

    let output_tmp = NamedTempFile::new().unwrap();
    let output_path = output_tmp.path().to_str().unwrap().to_string();
    drop(output_tmp);

    let mut output = Vec::new();
    idb::cli::defrag::execute(
        &idb::cli::defrag::DefragOptions {
            file: path.to_string(),
            output: output_path.clone(),
            verbose: false,
            json: true,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    let text = String::from_utf8(output).unwrap();
    let json: serde_json::Value = serde_json::from_str(&text).unwrap();

    assert_eq!(json["source_pages"], 5);
    assert_eq!(json["output_pages"], 3); // page0 + 2 INDEX pages
    assert_eq!(json["empty_removed"], 2);

    // Verify output is smaller than input
    let output_size = std::fs::metadata(&output_path).unwrap().len();
    assert_eq!(output_size, 3 * PS as u64);

    let _ = std::fs::remove_file(&output_path);
}

// ---------------------------------------------------------------------------
// 6. Defrag preserves prev/next chain for leaf pages
// ---------------------------------------------------------------------------

#[test]
fn test_defrag_preserves_prev_next_chain() {
    let page0 = write::build_fsp_page(42, 4, 0, 1000, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    // Three leaf pages from the same index, with an initial (broken) chain
    let page1 = build_index_page_with_chain(1, 42, 2000, FIL_NULL, 2, 100, 0);
    let page2 = build_index_page_with_chain(2, 42, 3000, 1, 3, 100, 0);
    let page3 = build_index_page_with_chain(3, 42, 4000, 2, FIL_NULL, 100, 0);

    let tmp = write_tablespace(&[page0, page1, page2, page3]);
    let path = tmp.path().to_str().unwrap();

    let output_tmp = NamedTempFile::new().unwrap();
    let output_path = output_tmp.path().to_str().unwrap().to_string();
    drop(output_tmp);

    let mut output = Vec::new();
    idb::cli::defrag::execute(
        &idb::cli::defrag::DefragOptions {
            file: path.to_string(),
            output: output_path.clone(),
            verbose: false,
            json: false,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    // Verify the prev/next chain in output: 1 <-> 2 <-> 3
    let p1 = write::read_page_raw(&output_path, 1, PAGE_SIZE).unwrap();
    let p2 = write::read_page_raw(&output_path, 2, PAGE_SIZE).unwrap();
    let p3 = write::read_page_raw(&output_path, 3, PAGE_SIZE).unwrap();

    assert_eq!(BigEndian::read_u32(&p1[FIL_PAGE_PREV..]), FIL_NULL);
    assert_eq!(BigEndian::read_u32(&p1[FIL_PAGE_NEXT..]), 2);

    assert_eq!(BigEndian::read_u32(&p2[FIL_PAGE_PREV..]), 1);
    assert_eq!(BigEndian::read_u32(&p2[FIL_PAGE_NEXT..]), 3);

    assert_eq!(BigEndian::read_u32(&p3[FIL_PAGE_PREV..]), 2);
    assert_eq!(BigEndian::read_u32(&p3[FIL_PAGE_NEXT..]), FIL_NULL);

    // All checksums valid
    for i in 0..4u64 {
        let page = write::read_page_raw(&output_path, i, PAGE_SIZE).unwrap();
        assert!(
            validate_checksum(&page, PAGE_SIZE, None).valid,
            "Page {} invalid after defrag chain preservation",
            i
        );
    }

    let _ = std::fs::remove_file(&output_path);
}

// ---------------------------------------------------------------------------
// 7. Transplant single page replaces only the targeted page
// ---------------------------------------------------------------------------

#[test]
fn test_transplant_single_page_only_target_differs() {
    let page0 = write::build_fsp_page(42, 3, 0, 1000, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    let donor_page1 = build_index_page(1, 42, 9999); // different LSN
    let donor_page2 = build_index_page(2, 42, 8888);
    let donor = write_tablespace(&[page0.clone(), donor_page1, donor_page2]);

    let target_page1 = build_index_page(1, 42, 2000);
    let target_page2 = build_index_page(2, 42, 3000);
    let target = write_tablespace(&[page0, target_page1, target_page2]);

    let donor_path = donor.path().to_str().unwrap();
    let target_path = target.path().to_str().unwrap();

    // Save original target pages
    let original_target_p0 = write::read_page_raw(target_path, 0, PAGE_SIZE).unwrap();
    let original_target_p2 = write::read_page_raw(target_path, 2, PAGE_SIZE).unwrap();

    let mut output = Vec::new();
    idb::cli::transplant::execute(
        &idb::cli::transplant::TransplantOptions {
            donor: donor_path.to_string(),
            target: target_path.to_string(),
            pages: vec![1],
            no_backup: true,
            force: false,
            dry_run: false,
            verbose: false,
            json: false,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    // Page 1 should match donor's page 1
    let transplanted_p1 = write::read_page_raw(target_path, 1, PAGE_SIZE).unwrap();
    let donor_p1 = write::read_page_raw(donor_path, 1, PAGE_SIZE).unwrap();
    assert_eq!(
        transplanted_p1, donor_p1,
        "Transplanted page does not match donor"
    );

    // Pages 0 and 2 should be unchanged
    let after_p0 = write::read_page_raw(target_path, 0, PAGE_SIZE).unwrap();
    let after_p2 = write::read_page_raw(target_path, 2, PAGE_SIZE).unwrap();
    assert_eq!(
        after_p0, original_target_p0,
        "Page 0 was modified by transplant"
    );
    assert_eq!(
        after_p2, original_target_p2,
        "Page 2 was modified by transplant"
    );
}

// ---------------------------------------------------------------------------
// 8. Transplant page 0 rejected without --force
// ---------------------------------------------------------------------------

#[test]
fn test_transplant_page0_rejected_without_force() {
    let page0 = write::build_fsp_page(42, 2, 0, 1000, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    let page1 = build_index_page(1, 42, 2000);
    let donor = write_tablespace(&[page0.clone(), page1.clone()]);
    let target = write_tablespace(&[page0, page1]);

    let target_path = target.path().to_str().unwrap();
    let original_p0 = write::read_page_raw(target_path, 0, PAGE_SIZE).unwrap();

    let mut output = Vec::new();
    idb::cli::transplant::execute(
        &idb::cli::transplant::TransplantOptions {
            donor: donor.path().to_str().unwrap().to_string(),
            target: target_path.to_string(),
            pages: vec![0],
            no_backup: true,
            force: false,
            dry_run: false,
            verbose: false,
            json: true,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    let text = String::from_utf8(output).unwrap();
    let json: serde_json::Value = serde_json::from_str(&text).unwrap();

    // Page 0 should be skipped, not transplanted
    assert_eq!(json["skipped"], 1);
    assert_eq!(json["transplanted"], 0);

    // Target page 0 should be unchanged
    let after_p0 = write::read_page_raw(target_path, 0, PAGE_SIZE).unwrap();
    assert_eq!(
        after_p0, original_p0,
        "Page 0 was modified despite no --force"
    );
}

// ---------------------------------------------------------------------------
// 9. Transplant with corrupt donor page skipped without --force
// ---------------------------------------------------------------------------

#[test]
fn test_transplant_corrupt_donor_page_skipped() {
    let page0 = write::build_fsp_page(42, 3, 0, 1000, PAGE_SIZE, ChecksumAlgorithm::Crc32c);

    // Donor: page 1 is corrupt, page 2 is valid
    let mut donor_page1 = build_index_page(1, 42, 5000);
    corrupt_checksum(&mut donor_page1);
    let donor_page2 = build_index_page(2, 42, 6000);
    let donor = write_tablespace(&[page0.clone(), donor_page1, donor_page2]);

    let target_page1 = build_index_page(1, 42, 2000);
    let target_page2 = build_index_page(2, 42, 3000);
    let target = write_tablespace(&[page0, target_page1, target_page2]);

    let target_path = target.path().to_str().unwrap();
    let original_target_p1 = write::read_page_raw(target_path, 1, PAGE_SIZE).unwrap();

    let mut output = Vec::new();
    idb::cli::transplant::execute(
        &idb::cli::transplant::TransplantOptions {
            donor: donor.path().to_str().unwrap().to_string(),
            target: target_path.to_string(),
            pages: vec![1, 2],
            no_backup: true,
            force: false,
            dry_run: false,
            verbose: false,
            json: true,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    let text = String::from_utf8(output).unwrap();
    let json: serde_json::Value = serde_json::from_str(&text).unwrap();

    // Page 1 (corrupt donor) should be skipped; page 2 transplanted
    assert_eq!(json["skipped"], 1);
    assert_eq!(json["transplanted"], 1);

    // Target page 1 should be unchanged (corrupt donor page was skipped)
    let after_p1 = write::read_page_raw(target_path, 1, PAGE_SIZE).unwrap();
    assert_eq!(
        after_p1, original_target_p1,
        "Target page 1 was overwritten by corrupt donor page"
    );

    // Target page 2 should have been transplanted (donor page 2 was valid)
    let after_p2 = write::read_page_raw(target_path, 2, PAGE_SIZE).unwrap();
    let donor_p2 = write::read_page_raw(donor.path().to_str().unwrap(), 2, PAGE_SIZE).unwrap();
    assert_eq!(
        after_p2, donor_p2,
        "Target page 2 was not transplanted from donor"
    );
}

// ---------------------------------------------------------------------------
// 10. Transplant dry-run does not modify target
// ---------------------------------------------------------------------------

#[test]
fn test_transplant_dry_run_no_modification() {
    let page0 = write::build_fsp_page(42, 3, 0, 1000, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    let donor_page1 = build_index_page(1, 42, 9999);
    let donor = write_tablespace(&[page0.clone(), donor_page1, build_index_page(2, 42, 3000)]);

    let target_page1 = build_index_page(1, 42, 2000);
    let target = write_tablespace(&[page0, target_page1, build_index_page(2, 42, 3000)]);

    let target_path = target.path().to_str().unwrap();
    let before_bytes = std::fs::read(target_path).unwrap();

    let mut output = Vec::new();
    idb::cli::transplant::execute(
        &idb::cli::transplant::TransplantOptions {
            donor: donor.path().to_str().unwrap().to_string(),
            target: target_path.to_string(),
            pages: vec![1],
            no_backup: true,
            force: false,
            dry_run: true,
            verbose: false,
            json: false,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    let after_bytes = std::fs::read(target_path).unwrap();
    assert_eq!(
        before_bytes, after_bytes,
        "Transplant dry-run modified the target file"
    );
}

// ---------------------------------------------------------------------------
// 11. Corrupt with specific byte count
// ---------------------------------------------------------------------------

#[test]
fn test_corrupt_specific_byte_count() {
    let page0 = build_fsp_hdr_page(42, 2);
    let page1 = build_index_page(1, 42, 2000);
    let original_page1 = page1.clone();

    let tmp = write_tablespace(&[page0, page1]);
    let path = tmp.path().to_str().unwrap();

    let mut output = Vec::new();
    idb::cli::corrupt::execute(
        &idb::cli::corrupt::CorruptOptions {
            file: path.to_string(),
            page: Some(1),
            bytes: 16,
            header: false,
            records: false,
            offset: None,
            verify: false,
            json: false,
            page_size: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    let corrupted = write::read_page_raw(path, 1, PAGE_SIZE).unwrap();

    // Count how many bytes differ
    let diff_count = original_page1
        .iter()
        .zip(corrupted.iter())
        .filter(|(a, b)| a != b)
        .count();

    // At least 1 byte should differ (could be fewer than 16 if random bytes
    // happen to match originals, but with 16 random bytes this is extremely unlikely).
    // We check that at most 16 bytes differ (the corruption doesn't write more than requested).
    assert!(diff_count > 0, "No bytes were corrupted");
    assert!(
        diff_count <= 16,
        "More than 16 bytes differ: {}",
        diff_count
    );
}

// ---------------------------------------------------------------------------
// 12. Corrupt header mode targets bytes 0-37
// ---------------------------------------------------------------------------

#[test]
fn test_corrupt_header_mode() {
    let page0 = build_fsp_hdr_page(42, 2);
    let page1 = build_index_page(1, 42, 2000);
    let original_page1 = page1.clone();

    let tmp = write_tablespace(&[page0, page1]);
    let path = tmp.path().to_str().unwrap();

    let mut output = Vec::new();
    idb::cli::corrupt::execute(
        &idb::cli::corrupt::CorruptOptions {
            file: path.to_string(),
            page: Some(1),
            bytes: 8,
            header: true,
            records: false,
            offset: None,
            verify: false,
            json: false,
            page_size: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    let corrupted = write::read_page_raw(path, 1, PAGE_SIZE).unwrap();

    // Find the range of differing bytes
    let diffs: Vec<usize> = original_page1
        .iter()
        .zip(corrupted.iter())
        .enumerate()
        .filter(|(_, (a, b))| a != b)
        .map(|(i, _)| i)
        .collect();

    assert!(!diffs.is_empty(), "No bytes were corrupted in header mode");

    // All differences should be within the FIL header (bytes 0-37)
    for &pos in &diffs {
        assert!(
            pos < SIZE_FIL_HEAD,
            "Corruption at byte {} is outside FIL header (expected < {})",
            pos,
            SIZE_FIL_HEAD
        );
    }
}

// ---------------------------------------------------------------------------
// 13. Corrupt record area targets bytes 120+
// ---------------------------------------------------------------------------

#[test]
fn test_corrupt_record_area() {
    let page0 = build_fsp_hdr_page(42, 2);
    let page1 = build_index_page(1, 42, 2000);
    let original_page1 = page1.clone();

    let tmp = write_tablespace(&[page0, page1]);
    let path = tmp.path().to_str().unwrap();

    let mut output = Vec::new();
    idb::cli::corrupt::execute(
        &idb::cli::corrupt::CorruptOptions {
            file: path.to_string(),
            page: Some(1),
            bytes: 8,
            header: false,
            records: true,
            offset: None,
            verify: false,
            json: false,
            page_size: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    let corrupted = write::read_page_raw(path, 1, PAGE_SIZE).unwrap();

    let diffs: Vec<usize> = original_page1
        .iter()
        .zip(corrupted.iter())
        .enumerate()
        .filter(|(_, (a, b))| a != b)
        .map(|(i, _)| i)
        .collect();

    assert!(!diffs.is_empty(), "No bytes were corrupted in records mode");

    // All differences should be in the user data area (offset 120+, before trailer)
    let user_data_start = 120;
    let trailer_start = PS - SIZE_FIL_TRAILER;
    for &pos in &diffs {
        assert!(
            pos >= user_data_start && pos < trailer_start,
            "Corruption at byte {} is outside record area ({}-{})",
            pos,
            user_data_start,
            trailer_start
        );
    }
}

// ---------------------------------------------------------------------------
// 14. Cross-page-size (8K) repair roundtrip
// ---------------------------------------------------------------------------

#[test]
fn test_8k_page_size_repair_roundtrip() {
    // ssize=4 encodes 8K: 1 << (4+9) = 8192
    let flags_8k = 4 << 6; // ssize=4 in bits 6-9
    let page0 = write::build_fsp_page(
        42,
        3,
        flags_8k,
        1000,
        PAGE_SIZE_8K,
        ChecksumAlgorithm::Crc32c,
    );
    let page1 = build_8k_index_page(1, 42, 2000);
    let mut page2 = build_8k_index_page(2, 42, 3000);

    // Verify page 2 starts valid
    assert!(validate_checksum(&page2, PAGE_SIZE_8K, None).valid);

    // Corrupt page 2
    corrupt_checksum(&mut page2);
    assert!(!validate_checksum(&page2, PAGE_SIZE_8K, None).valid);

    let tmp = write_tablespace(&[page0, page1, page2]);
    let path = tmp.path().to_str().unwrap();

    let mut output = Vec::new();
    idb::cli::repair::execute(
        &idb::cli::repair::RepairOptions {
            file: Some(path.to_string()),
            batch: None,
            page: None,
            algorithm: "auto".to_string(),
            no_backup: true,
            dry_run: false,
            verbose: false,
            json: false,
            page_size: Some(PAGE_SIZE_8K),
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    )
    .unwrap();

    // All 3 pages should now be valid
    for i in 0..3 {
        let page = write::read_page_raw(path, i, PAGE_SIZE_8K).unwrap();
        assert!(
            validate_checksum(&page, PAGE_SIZE_8K, None).valid,
            "8K page {} invalid after repair",
            i
        );
    }
}

// ---------------------------------------------------------------------------
// 15. Encrypted tablespace flags without keyring
// ---------------------------------------------------------------------------

#[test]
fn test_encrypted_flags_repair_without_keyring() {
    // Bit 13 set in FSP flags indicates encryption
    let flags = 1 << 13;
    let page0 = write::build_fsp_page(42, 3, flags, 1000, PAGE_SIZE, ChecksumAlgorithm::Crc32c);
    let page1 = build_index_page(1, 42, 2000);
    let mut page2 = build_index_page(2, 42, 3000);
    corrupt_checksum(&mut page2);

    let tmp = write_tablespace(&[page0, page1, page2]);
    let path = tmp.path().to_str().unwrap();

    let mut output = Vec::new();
    let result = idb::cli::repair::execute(
        &idb::cli::repair::RepairOptions {
            file: Some(path.to_string()),
            batch: None,
            page: None,
            algorithm: "crc32c".to_string(),
            no_backup: true,
            dry_run: false,
            verbose: false,
            json: false,
            page_size: None,
            keyring: None,
            mmap: false,
            audit_logger: None,
        },
        &mut output,
    );

    // Repair should succeed even with encryption flags when no keyring is provided,
    // because repair operates on raw page bytes (checksums are not encrypted).
    // If the implementation chooses to error, the error message should be clear.
    match result {
        Ok(()) => {
            // Repair succeeded: verify the previously-corrupt page is now valid
            let p2 = write::read_page_raw(path, 2, PAGE_SIZE).unwrap();
            assert!(
                validate_checksum(&p2, PAGE_SIZE, None).valid,
                "Page 2 should be valid after repair despite encryption flags"
            );
        }
        Err(e) => {
            let msg = format!("{}", e);
            // If it errors, the message should reference encryption or keyring
            assert!(
                msg.to_lowercase().contains("encrypt") || msg.to_lowercase().contains("keyring"),
                "Error should mention encryption or keyring, got: {}",
                msg
            );
        }
    }
}