- // Test that if the persister's path to channel data is read-only, writing
- // data to it fails. Windows ignores the read-only flag for folders, so this
- // test is Unix-only.
- #[cfg(not(target_os = "windows"))]
- #[test]
- fn test_readonly_dir() {
- let persister = FilesystemPersister::new("test_readonly_dir_persister".to_string());
- let test_writeable = TestWriteable{};
- let test_txo = OutPoint {
- txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
- index: 0
- };
- // Create the persister's directory and set it to read-only.
- let path = &persister.path_to_channel_data;
- fs::create_dir_all(path).unwrap();
- let mut perms = fs::metadata(path).unwrap().permissions();
- perms.set_readonly(true);
- fs::set_permissions(path, perms).unwrap();
- match persister.write_channel_data(test_txo, &test_writeable) {
- Err(e) => assert_eq!(e.kind(), io::ErrorKind::PermissionDenied),
- _ => panic!("Unexpected error message")
- }
- }
-
- // Test failure to rename in the process of atomically creating a channel
- // monitor's file. We induce this failure by making the `tmp` file a
- // directory.
- // Explanation: given "from" = the file being renamed, "to" = the
- // renamee that already exists: Windows should fail because it'll fail
- // whenever "to" is a directory, and Unix should fail because if "from" is a
- // file, then "to" is also required to be a file.
- #[test]
- fn test_rename_failure() {
- let persister = FilesystemPersister::new("test_rename_failure".to_string());
- let test_writeable = TestWriteable{};
- let txid_hex = "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be";
- let outp_idx = 0;
- let test_txo = OutPoint {
- txid: Txid::from_hex(txid_hex).unwrap(),
- index: outp_idx,
- };
- // Create the channel data file and make it a directory.
- let path = &persister.path_to_channel_data;
- fs::create_dir_all(format!("{}/{}_{}", path, txid_hex, outp_idx)).unwrap();
- match persister.write_channel_data(test_txo, &test_writeable) {
- Err(e) => {
- #[cfg(not(target_os = "windows"))]
- assert_eq!(e.kind(), io::ErrorKind::Other);
- #[cfg(target_os = "windows")]
- assert_eq!(e.kind(), io::ErrorKind::PermissionDenied);
- }
- _ => panic!("Unexpected error message")
- }
- }
-
- // Test failure to create the temporary file in the persistence process.
- // We induce this failure by having the temp file already exist and be a
- // directory.
- #[test]
- fn test_tmp_file_creation_failure() {
- let persister = FilesystemPersister::new("test_tmp_file_creation_failure".to_string());
- let test_writeable = TestWriteable{};
- let txid_hex = "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be";
- let outp_idx = 0;
- let test_txo = OutPoint {
- txid: Txid::from_hex(txid_hex).unwrap(),
- index: outp_idx,
- };
- // Create the tmp file and make it a directory.
- let path = &persister.path_to_channel_data;
- fs::create_dir_all(format!("{}/{}_{}.tmp", path, txid_hex, outp_idx)).unwrap();
- match persister.write_channel_data(test_txo, &test_writeable) {
- Err(e) => {
- #[cfg(not(target_os = "windows"))]
- assert_eq!(e.kind(), io::ErrorKind::Other);
- #[cfg(target_os = "windows")]
- assert_eq!(e.kind(), io::ErrorKind::PermissionDenied);
- }
- _ => panic!("Unexpected error message")
- }
- }
-