| | @@ -807,3 +807,185 @@ fn split_publication_changed_source_stops_before_creating_the_first_directory() |
| 807 | 807 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 808 | 808 | assert_ne!(hashes(&paths), before); |
| 809 | 809 | } |
| 810 | +추가: |
| 811 | +추가: #[test] |
| 812 | +추가: fn split_publication_target_changes_during_verification_are_preserved() { |
| 813 | +추가: use std::io::{Cursor, Read}; |
| 814 | +추가: use std::sync::{Arc, Mutex, mpsc}; |
| 815 | +추가: const USER_EDIT: &[u8] = b"new user edit after overwrite approval"; |
| 816 | +추가: struct Input { |
| 817 | +추가: receive: mpsc::Receiver<Vec<u8>>, |
| 818 | +추가: pending: Cursor<Vec<u8>>, |
| 819 | +추가: } |
| 820 | +추가: impl Read for Input { |
| 821 | +추가: fn read(&mut self, bytes: &mut [u8]) -> std::io::Result<usize> { |
| 822 | +추가: if bytes.is_empty() { |
| 823 | +추가: return Ok(0); |
| 824 | +추가: } |
| 825 | +추가: loop { |
| 826 | +추가: let count = self.pending.read(bytes)?; |
| 827 | +추가: if count != 0 { |
| 828 | +추가: return Ok(count); |
| 829 | +추가: } |
| 830 | +추가: match self |
| 831 | +추가: .receive |
| 832 | +추가: .recv_timeout(std::time::Duration::from_secs(20)) |
| 833 | +추가: { |
| 834 | +추가: Ok(frame) => self.pending = Cursor::new(frame), |
| 835 | +추가: Err(mpsc::RecvTimeoutError::Disconnected) => return Ok(0), |
| 836 | +추가: Err(error) => return Err(std::io::Error::other(error)), |
| 837 | +추가: } |
| 838 | +추가: } |
| 839 | +추가: } |
| 840 | +추가: } |
| 841 | +추가: struct Reply { |
| 842 | +추가: send: mpsc::Sender<Vec<u8>>, |
| 843 | +추가: pending: Vec<u8>, |
| 844 | +추가: } |
| 845 | +추가: impl Write for Reply { |
| 846 | +추가: fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> { |
| 847 | +추가: self.pending.extend_from_slice(bytes); |
| 848 | +추가: Ok(bytes.len()) |
| 849 | +추가: } |
| 850 | +추가: fn flush(&mut self) -> std::io::Result<()> { |
| 851 | +추가: self.send |
| 852 | +추가: .send(std::mem::take(&mut self.pending)) |
| 853 | +추가: .map_err(std::io::Error::other) |
| 854 | +추가: } |
| 855 | +추가: } |
| 856 | +추가: struct ChangingOutput { |
| 857 | +추가: frame: Vec<u8>, |
| 858 | +추가: output: Arc<Mutex<Vec<u8>>>, |
| 859 | +추가: reply: Option<Writer<Reply>>, |
| 860 | +추가: target: PathBuf, |
| 861 | +추가: mode: u8, |
| 862 | +추가: modified: Option<std::time::SystemTime>, |
| 863 | +추가: changed: bool, |
| 864 | +추가: } |
| 865 | +추가: impl Write for ChangingOutput { |
| 866 | +추가: fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> { |
| 867 | +추가: self.frame.extend_from_slice(bytes); |
| 868 | +추가: Ok(bytes.len()) |
| 869 | +추가: } |
| 870 | +추가: fn flush(&mut self) -> std::io::Result<()> { |
| 871 | +추가: let event = { |
| 872 | +추가: let mut output = self.output.lock().unwrap(); |
| 873 | +추가: output.extend_from_slice(&self.frame); |
| 874 | +추가: let mut reader = Reader::new(output.as_slice()); |
| 875 | +추가: let mut last = None; |
| 876 | +추가: while let Some((_, _, event)) = reader.read()? { |
| 877 | +추가: last = Some(event); |
| 878 | +추가: } |
| 879 | +추가: last.unwrap() |
| 880 | +추가: }; |
| 881 | +추가: match event { |
| 882 | +추가: Message::ConflictRequired { challenge_id, .. } => { |
| 883 | +추가: self.reply.as_mut().unwrap().send(&Message::ConflictReply { |
| 884 | +추가: challenge_id, |
| 885 | +추가: choice: ConflictChoice::Overwrite, |
| 886 | +추가: apply_to_remaining: false, |
| 887 | +추가: })?; |
| 888 | +추가: } |
| 889 | +추가: Message::Progress { |
| 890 | +추가: stage: Stage::Verify, |
| 891 | +추가: processed_bytes: 0, |
| 892 | +추가: total_bytes: None, |
| 893 | +추가: .. |
| 894 | +추가: } if !self.changed => { |
| 895 | +추가: // 재해시 시작과 동기화해 승인 이후 대상 변경을 재현함. |
| 896 | +추가: match self.mode { |
| 897 | +추가: 0 => { |
| 898 | +추가: fs::write(&self.target, USER_EDIT)?; |
| 899 | +추가: fs::File::options() |
| 900 | +추가: .write(true) |
| 901 | +추가: .open(&self.target)? |
| 902 | +추가: .set_times(fs::FileTimes::new().set_modified( |
| 903 | +추가: self.modified.unwrap() + std::time::Duration::from_secs(2), |
| 904 | +추가: ))?; |
| 905 | +추가: } |
| 906 | +추가: 1 => { |
| 907 | +추가: let old = self.target.with_file_name("approved-old"); |
| 908 | +추가: fs::rename(&self.target, &old)?; |
| 909 | +추가: fs::write(&self.target, USER_EDIT)?; |
| 910 | +추가: fs::File::options() |
| 911 | +추가: .write(true) |
| 912 | +추가: .open(&self.target)? |
| 913 | +추가: .set_times( |
| 914 | +추가: fs::FileTimes::new().set_modified(self.modified.unwrap()), |
| 915 | +추가: )?; |
| 916 | +추가: fs::remove_file(old)?; |
| 917 | +추가: } |
| 918 | +추가: 2 => fs::remove_file(&self.target)?, |
| 919 | +추가: 3 => fs::write(&self.target, USER_EDIT)?, |
| 920 | +추가: _ => unreachable!(), |
| 921 | +추가: } |
| 922 | +추가: self.changed = true; |
| 923 | +추가: } |
| 924 | +추가: Message::Terminal(_) => self.reply = None, |
| 925 | +추가: _ => {} |
| 926 | +추가: } |
| 927 | +추가: self.frame.clear(); |
| 928 | +추가: Ok(()) |
| 929 | +추가: } |
| 930 | +추가: } |
| 931 | +추가: for mode in 0..4 { |
| 932 | +추가: let s = Scratch::new(); |
| 933 | +추가: let paths = publication_fixture(&s, true, true); |
| 934 | +추가: let before = hashes(&paths); |
| 935 | +추가: let target = s.0.join("output/payload.bin"); |
| 936 | +추가: let modified = if mode == 3 { |
| 937 | +추가: None |
| 938 | +추가: } else { |
| 939 | +추가: fs::write(&target, vec![b'o'; USER_EDIT.len()]).unwrap(); |
| 940 | +추가: Some(fs::metadata(&target).unwrap().modified().unwrap()) |
| 941 | +추가: }; |
| 942 | +추가: let (send, receive) = mpsc::channel(); |
| 943 | +추가: let mut reply = Writer::new( |
| 944 | +추가: Reply { |
| 945 | +추가: send, |
| 946 | +추가: pending: Vec::new(), |
| 947 | +추가: }, |
| 948 | +추가: 17, |
| 949 | +추가: ); |
| 950 | +추가: reply |
| 951 | +추가: .send(&Message::StartGuarded(publication_request(&s))) |
| 952 | +추가: .unwrap(); |
| 953 | +추가: let output = Arc::new(Mutex::new(Vec::new())); |
| 954 | +추가: zipelo_archive_worker::runtime::serve( |
| 955 | +추가: Input { |
| 956 | +추가: receive, |
| 957 | +추가: pending: Cursor::new(Vec::new()), |
| 958 | +추가: }, |
| 959 | +추가: ChangingOutput { |
| 960 | +추가: frame: Vec::new(), |
| 961 | +추가: output: Arc::clone(&output), |
| 962 | +추가: reply: Some(reply), |
| 963 | +추가: target: target.clone(), |
| 964 | +추가: mode, |
| 965 | +추가: modified, |
| 966 | +추가: changed: false, |
| 967 | +추가: }, |
| 968 | +추가: ) |
| 969 | +추가: .unwrap(); |
| 970 | +추가: let output = output.lock().unwrap(); |
| 971 | +추가: let mut reader = Reader::new(output.as_slice()); |
| 972 | +추가: let mut events = Vec::new(); |
| 973 | +추가: while let Some((_, _, event)) = reader.read().unwrap() { |
| 974 | +추가: events.push(event); |
| 975 | +추가: } |
| 976 | +추가: failure(&events, ErrorCode::Conflict); |
| 977 | +추가: assert_eq!(publication_verifications(&events).len(), 1); |
| 978 | +추가: assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 979 | +추가: assert_eq!(terminal(&events).retained_partial_count, 0); |
| 980 | +추가: if mode == 2 { |
| 981 | +추가: assert!(!target.exists()); |
| 982 | +추가: } else { |
| 983 | +추가: assert_eq!(fs::read(&target).unwrap(), USER_EDIT); |
| 984 | +추가: } |
| 985 | +추가: assert_eq!( |
| 986 | +추가: fs::read_dir(s.0.join("output")).unwrap().count(), |
| 987 | +추가: if mode == 2 { 16 } else { 17 } |
| 988 | +추가: ); |
| 989 | +추가: assert_eq!(hashes(&paths), before); |
| 990 | +추가: } |
| 991 | +추가: } |