xanderbailey commented on code in PR #3236:
URL: https://github.com/apache/iceberg-rust/pull/3236#discussion_r4024536065


##########
crates/iceberg/src/encryption/io.rs:
##########
@@ -215,8 +236,134 @@ mod tests {
             "encrypted file should be larger than plaintext (header + nonce + 
tag)"
         );
 
-        let input = EncryptedInputFile::new(fileio.new_input(path).unwrap(), 
key_metadata());
+        // A missing path proves the size comes from the key metadata rather 
than a stat call.
+        let input = EncryptedInputFile::new(
+            fileio.new_input("memory:///does-not-exist").unwrap(),
+            key_metadata().with_file_length(file_metadata.size),
+        );
         let meta = input.metadata().await.unwrap();
         assert_eq!(meta.size, plaintext.len() as u64);
     }
+
+    #[tokio::test]
+    async fn test_missing_file_length_is_rejected() {
+        let fileio = FileIO::new_with_memory();
+        let path = "memory:///test/missing_length.bin";
+        let output = 
EncryptedOutputFile::new(fileio.new_output(path).unwrap(), key_metadata());
+        output.write(Bytes::from_static(b"data")).await.unwrap();
+        let input = EncryptedInputFile::new(fileio.new_input(path).unwrap(), 
key_metadata());
+
+        for err in [
+            input.metadata().await.err().unwrap(),
+            input.reader().await.err().unwrap(),
+            input.read().await.unwrap_err(),
+        ] {
+            assert_eq!(err.kind(), ErrorKind::DataInvalid);
+            assert!(
+                err.to_string()
+                    .contains("missing the encrypted file length")
+            );
+        }
+    }
+
+    #[tokio::test]
+    async fn test_invalid_file_length_is_rejected() {
+        let fileio = FileIO::new_with_memory();
+        for length in [
+            0,
+            u64::from(GCM_STREAM_HEADER_LENGTH),
+            u64::from(MIN_STREAM_LENGTH - 1),
+        ] {
+            let input = EncryptedInputFile::new(
+                fileio
+                    .new_input("memory:///test/invalid_length.bin")
+                    .unwrap(),
+                key_metadata().with_file_length(length),
+            );
+            assert_eq!(
+                input.metadata().await.err().unwrap().kind(),
+                ErrorKind::DataInvalid
+            );
+            assert_eq!(
+                input.reader().await.err().unwrap().kind(),
+                ErrorKind::DataInvalid
+            );
+        }
+    }
+
+    #[tokio::test]
+    async fn test_truncated_file_is_rejected() {

Review Comment:
   
[849d9b4](https://github.com/apache/iceberg-rust/pull/3236/commits/849d9b488a9f400c9270443de41b2bd2ffd17bbb)



##########
crates/iceberg/src/encryption/io.rs:
##########
@@ -215,8 +236,134 @@ mod tests {
             "encrypted file should be larger than plaintext (header + nonce + 
tag)"
         );
 
-        let input = EncryptedInputFile::new(fileio.new_input(path).unwrap(), 
key_metadata());
+        // A missing path proves the size comes from the key metadata rather 
than a stat call.
+        let input = EncryptedInputFile::new(
+            fileio.new_input("memory:///does-not-exist").unwrap(),
+            key_metadata().with_file_length(file_metadata.size),
+        );
         let meta = input.metadata().await.unwrap();
         assert_eq!(meta.size, plaintext.len() as u64);
     }
+
+    #[tokio::test]
+    async fn test_missing_file_length_is_rejected() {
+        let fileio = FileIO::new_with_memory();
+        let path = "memory:///test/missing_length.bin";
+        let output = 
EncryptedOutputFile::new(fileio.new_output(path).unwrap(), key_metadata());
+        output.write(Bytes::from_static(b"data")).await.unwrap();
+        let input = EncryptedInputFile::new(fileio.new_input(path).unwrap(), 
key_metadata());
+
+        for err in [
+            input.metadata().await.err().unwrap(),
+            input.reader().await.err().unwrap(),
+            input.read().await.unwrap_err(),
+        ] {
+            assert_eq!(err.kind(), ErrorKind::DataInvalid);
+            assert!(
+                err.to_string()
+                    .contains("missing the encrypted file length")
+            );
+        }
+    }
+
+    #[tokio::test]
+    async fn test_invalid_file_length_is_rejected() {
+        let fileio = FileIO::new_with_memory();
+        for length in [
+            0,
+            u64::from(GCM_STREAM_HEADER_LENGTH),
+            u64::from(MIN_STREAM_LENGTH - 1),
+        ] {
+            let input = EncryptedInputFile::new(
+                fileio
+                    .new_input("memory:///test/invalid_length.bin")
+                    .unwrap(),
+                key_metadata().with_file_length(length),
+            );
+            assert_eq!(
+                input.metadata().await.err().unwrap().kind(),
+                ErrorKind::DataInvalid
+            );
+            assert_eq!(
+                input.reader().await.err().unwrap().kind(),
+                ErrorKind::DataInvalid
+            );
+        }
+    }
+
+    #[tokio::test]
+    async fn test_truncated_file_is_rejected() {

Review Comment:
   Good catch! 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to