| | @@ -553,3 +553,257 @@ fn encrypted_seven_zip_volumes_keep_the_existing_unsupported_policy() { |
| 553 | 553 | assert_eq!(hashes(&paths), before); |
| 554 | 554 | } |
| 555 | 555 | } |
| 556 | +추가: |
| 557 | +추가: fn publication_fixture(s: &Scratch, existing: bool, with_file: bool) -> Vec<PathBuf> { |
| 558 | +추가: assert!( |
| 559 | +추가: Command::new("python3") |
| 560 | +추가: .env("PYTHONDONTWRITEBYTECODE", "1") |
| 561 | +추가: .arg( |
| 562 | +추가: PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 563 | +추가: .join("../../tests/rust-archive-worker/split_publication_cases.py") |
| 564 | +추가: ) |
| 565 | +추가: .arg(&s.0) |
| 566 | +추가: .arg(if with_file { "file" } else { "directories" }) |
| 567 | +추가: .status() |
| 568 | +추가: .unwrap() |
| 569 | +추가: .success() |
| 570 | +추가: ); |
| 571 | +추가: directory(s, "output"); |
| 572 | +추가: if existing { |
| 573 | +추가: for index in 0..16 { |
| 574 | +추가: directory(s, &format!("output/d{index:03}")); |
| 575 | +추가: } |
| 576 | +추가: } |
| 577 | +추가: (1..=5) |
| 578 | +추가: .map(|index| s.0.join(format!("sample.7z.{index:03}"))) |
| 579 | +추가: .take_while(|path| path.is_file()) |
| 580 | +추가: .collect() |
| 581 | +추가: } |
| 582 | +추가: fn publication_verifications(events: &[Message]) -> Vec<usize> { |
| 583 | +추가: events |
| 584 | +추가: .iter() |
| 585 | +추가: .enumerate() |
| 586 | +추가: .filter_map(|(index, event)| { |
| 587 | +추가: matches!( |
| 588 | +추가: event, |
| 589 | +추가: Message::Progress { |
| 590 | +추가: stage: Stage::Verify, |
| 591 | +추가: processed_bytes: 0, |
| 592 | +추가: total_bytes: None, |
| 593 | +추가: .. |
| 594 | +추가: } |
| 595 | +추가: ) |
| 596 | +추가: .then_some(index) |
| 597 | +추가: }) |
| 598 | +추가: .collect() |
| 599 | +추가: } |
| 600 | +추가: fn publication_request(s: &Scratch) -> GuardedStart { |
| 601 | +추가: GuardedStart { |
| 602 | +추가: start: request(s, None, Selection::All), |
| 603 | +추가: ceiling: Limits::default(), |
| 604 | +추가: interactive: true, |
| 605 | +추가: } |
| 606 | +추가: } |
| 607 | +추가: |
| 608 | +추가: #[test] |
| 609 | +추가: fn split_publication_verifies_once_for_fresh_or_many_existing_directories() { |
| 610 | +추가: for existing in [false, true] { |
| 611 | +추가: let s = Scratch::new(); |
| 612 | +추가: let paths = publication_fixture(&s, existing, true); |
| 613 | +추가: let before = hashes(&paths); |
| 614 | +추가: let events = guarded::run(&s, publication_request(&s), |_| None); |
| 615 | +추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 616 | +추가: let verifies = publication_verifications(&events); |
| 617 | +추가: assert_eq!(verifies.len(), 1, "existing={existing}: {events:?}"); |
| 618 | +추가: let first_output = events |
| 619 | +추가: .iter() |
| 620 | +추가: .position(|event| matches!(event, Message::Output { .. })) |
| 621 | +추가: .unwrap(); |
| 622 | +추가: assert!(verifies[0] < first_output); |
| 623 | +추가: if existing { |
| 624 | +추가: let write = events |
| 625 | +추가: .iter() |
| 626 | +추가: .position(|event| { |
| 627 | +추가: matches!( |
| 628 | +추가: event, |
| 629 | +추가: Message::Progress { |
| 630 | +추가: stage: Stage::Write, |
| 631 | +추가: .. |
| 632 | +추가: } |
| 633 | +추가: ) |
| 634 | +추가: }) |
| 635 | +추가: .unwrap(); |
| 636 | +추가: assert!(write < verifies[0]); |
| 637 | +추가: } |
| 638 | +추가: assert_eq!( |
| 639 | +추가: terminal(&events).created_count, |
| 640 | +추가: if existing { 1 } else { 17 } |
| 641 | +추가: ); |
| 642 | +추가: assert_eq!( |
| 643 | +추가: fs::read(s.0.join("output/payload.bin")).unwrap(), |
| 644 | +추가: (0..=255u8).cycle().take(1024 * 1024).collect::<Vec<_>>() |
| 645 | +추가: ); |
| 646 | +추가: assert_eq!(hashes(&paths), before); |
| 647 | +추가: } |
| 648 | +추가: } |
| 649 | +추가: |
| 650 | +추가: #[test] |
| 651 | +추가: fn split_publication_existing_directories_do_not_cache_before_conflict_changes() { |
| 652 | +추가: let s = Scratch::new(); |
| 653 | +추가: let paths = publication_fixture(&s, true, true); |
| 654 | +추가: fs::write(s.0.join("output/payload.bin"), b"keep").unwrap(); |
| 655 | +추가: let mut changed = None; |
| 656 | +추가: let events = guarded::run(&s, publication_request(&s), |event| { |
| 657 | +추가: if let Message::ConflictRequired { challenge_id, .. } = event { |
| 658 | +추가: let mut data = fs::read(&paths[1]).unwrap(); |
| 659 | +추가: data[0] ^= 1; |
| 660 | +추가: fs::write(&paths[1], data).unwrap(); |
| 661 | +추가: changed = Some(hashes(&paths)); |
| 662 | +추가: return Some(vec![Message::ConflictReply { |
| 663 | +추가: challenge_id: *challenge_id, |
| 664 | +추가: choice: ConflictChoice::Overwrite, |
| 665 | +추가: apply_to_remaining: false, |
| 666 | +추가: }]); |
| 667 | +추가: } |
| 668 | +추가: None |
| 669 | +추가: }); |
| 670 | +추가: failure(&events, ErrorCode::SourceChanged); |
| 671 | +추가: let verifies = publication_verifications(&events); |
| 672 | +추가: assert_eq!(verifies.len(), 1, "{events:?}"); |
| 673 | +추가: let conflict = events |
| 674 | +추가: .iter() |
| 675 | +추가: .position(|event| matches!(event, Message::ConflictRequired { .. })) |
| 676 | +추가: .unwrap(); |
| 677 | +추가: assert!(conflict < verifies[0]); |
| 678 | +추가: assert_eq!(fs::read(s.0.join("output/payload.bin")).unwrap(), b"keep"); |
| 679 | +추가: assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 17); |
| 680 | +추가: assert_eq!(hashes(&paths), changed.unwrap()); |
| 681 | +추가: assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 682 | +추가: } |
| 683 | +추가: |
| 684 | +추가: #[test] |
| 685 | +추가: fn split_publication_no_outputs_verify_once_and_cancel_does_not_rehash() { |
| 686 | +추가: for mode in 0..3 { |
| 687 | +추가: let s = Scratch::new(); |
| 688 | +추가: let paths = publication_fixture(&s, true, mode != 0); |
| 689 | +추가: let before = hashes(&paths); |
| 690 | +추가: if mode != 0 { |
| 691 | +추가: fs::write(s.0.join("output/payload.bin"), b"keep").unwrap(); |
| 692 | +추가: } |
| 693 | +추가: let events = guarded::run(&s, publication_request(&s), |event| { |
| 694 | +추가: if let Message::ConflictRequired { challenge_id, .. } = event { |
| 695 | +추가: return Some(if mode == 2 { |
| 696 | +추가: vec![Message::Cancel] |
| 697 | +추가: } else { |
| 698 | +추가: vec![Message::ConflictReply { |
| 699 | +추가: challenge_id: *challenge_id, |
| 700 | +추가: choice: ConflictChoice::Skip, |
| 701 | +추가: apply_to_remaining: false, |
| 702 | +추가: }] |
| 703 | +추가: }); |
| 704 | +추가: } |
| 705 | +추가: None |
| 706 | +추가: }); |
| 707 | +추가: assert_eq!( |
| 708 | +추가: terminal(&events).outcome, |
| 709 | +추가: if mode == 2 { |
| 710 | +추가: Outcome::Cancelled |
| 711 | +추가: } else { |
| 712 | +추가: Outcome::Completed |
| 713 | +추가: }, |
| 714 | +추가: "{events:?}" |
| 715 | +추가: ); |
| 716 | +추가: assert_eq!( |
| 717 | +추가: publication_verifications(&events).len(), |
| 718 | +추가: usize::from(mode != 2), |
| 719 | +추가: "{events:?}" |
| 720 | +추가: ); |
| 721 | +추가: assert_eq!(terminal(&events).publication, Publication::None); |
| 722 | +추가: assert_eq!(terminal(&events).created_count, 0); |
| 723 | +추가: assert!( |
| 724 | +추가: !events |
| 725 | +추가: .iter() |
| 726 | +추가: .any(|event| matches!(event, Message::Output { .. })) |
| 727 | +추가: ); |
| 728 | +추가: if mode != 0 { |
| 729 | +추가: assert_eq!(fs::read(s.0.join("output/payload.bin")).unwrap(), b"keep"); |
| 730 | +추가: } |
| 731 | +추가: assert_eq!(hashes(&paths), before); |
| 732 | +추가: } |
| 733 | +추가: } |
| 734 | +추가: |
| 735 | +추가: #[test] |
| 736 | +추가: fn split_publication_changed_source_stops_before_creating_the_first_directory() { |
| 737 | +추가: use std::sync::{Arc, Mutex}; |
| 738 | +추가: struct ChangingOutput { |
| 739 | +추가: frame: Vec<u8>, |
| 740 | +추가: output: Arc<Mutex<Vec<u8>>>, |
| 741 | +추가: path: PathBuf, |
| 742 | +추가: changed: bool, |
| 743 | +추가: } |
| 744 | +추가: impl Write for ChangingOutput { |
| 745 | +추가: fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> { |
| 746 | +추가: self.frame.extend_from_slice(bytes); |
| 747 | +추가: Ok(bytes.len()) |
| 748 | +추가: } |
| 749 | +추가: fn flush(&mut self) -> std::io::Result<()> { |
| 750 | +추가: let event = { |
| 751 | +추가: let mut output = self.output.lock().unwrap(); |
| 752 | +추가: output.extend_from_slice(&self.frame); |
| 753 | +추가: let mut reader = Reader::new(output.as_slice()); |
| 754 | +추가: let mut last = None; |
| 755 | +추가: while let Some((_, _, event)) = reader.read()? { |
| 756 | +추가: last = Some(event); |
| 757 | +추가: } |
| 758 | +추가: last.unwrap() |
| 759 | +추가: }; |
| 760 | +추가: if !self.changed |
| 761 | +추가: && matches!( |
| 762 | +추가: event, |
| 763 | +추가: Message::Progress { |
| 764 | +추가: stage: Stage::Verify, |
| 765 | +추가: processed_bytes: 0, |
| 766 | +추가: total_bytes: None, |
| 767 | +추가: .. |
| 768 | +추가: } |
| 769 | +추가: ) |
| 770 | +추가: { |
| 771 | +추가: // 검증 시작 프레임을 보낼 때 바꿔 스케줄링 경합 없이 생성 직전을 재현함. |
| 772 | +추가: let mut data = fs::read(&self.path)?; |
| 773 | +추가: data[0] ^= 1; |
| 774 | +추가: fs::write(&self.path, data)?; |
| 775 | +추가: self.changed = true; |
| 776 | +추가: } |
| 777 | +추가: self.frame.clear(); |
| 778 | +추가: Ok(()) |
| 779 | +추가: } |
| 780 | +추가: } |
| 781 | +추가: let s = Scratch::new(); |
| 782 | +추가: let paths = publication_fixture(&s, false, true); |
| 783 | +추가: let before = hashes(&paths); |
| 784 | +추가: let mut input = Vec::new(); |
| 785 | +추가: Writer::new(&mut input, 17) |
| 786 | +추가: .send(&Message::Start(request(&s, None, Selection::All))) |
| 787 | +추가: .unwrap(); |
| 788 | +추가: let output = Arc::new(Mutex::new(Vec::new())); |
| 789 | +추가: zipelo_archive_worker::runtime::serve( |
| 790 | +추가: std::io::Cursor::new(input), |
| 791 | +추가: ChangingOutput { |
| 792 | +추가: frame: Vec::new(), |
| 793 | +추가: output: Arc::clone(&output), |
| 794 | +추가: path: paths[1].clone(), |
| 795 | +추가: changed: false, |
| 796 | +추가: }, |
| 797 | +추가: ) |
| 798 | +추가: .unwrap(); |
| 799 | +추가: let output = output.lock().unwrap(); |
| 800 | +추가: let mut reader = Reader::new(output.as_slice()); |
| 801 | +추가: let mut events = Vec::new(); |
| 802 | +추가: while let Some((_, _, event)) = reader.read().unwrap() { |
| 803 | +추가: events.push(event); |
| 804 | +추가: } |
| 805 | +추가: failure(&events, ErrorCode::SourceChanged); |
| 806 | +추가: assert_eq!(publication_verifications(&events).len(), 1); |
| 807 | +추가: assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 808 | +추가: assert_ne!(hashes(&paths), before); |
| 809 | +추가: } |