xanderbailey commented on code in PR #3963:
URL: https://github.com/apache/iceberg-python/pull/3963#discussion_r4009928025


##########
tests/encryption/test_ciphers.py:
##########
@@ -0,0 +1,137 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import pytest
+from pytest_mock import MockFixture
+
+from pyiceberg.encryption.ciphers import AesGcmCipher, AesKeySize, SecureKey
+from pyiceberg.exceptions import NotInstalledError
+
+AES128_KEY = b"0123456789012345"
+PLAINTEXT = b"the quick brown fox"
+
+
[email protected](
+    "key_length, key_size",
+    [(16, AesKeySize.BITS_128), (24, AesKeySize.BITS_192), (32, 
AesKeySize.BITS_256)],
+)
+def test_key_size_from_key_length(key_length: int, key_size: AesKeySize) -> 
None:
+    assert AesKeySize.from_key_length(key_length) == key_size
+    assert key_size.key_length == key_length
+
+
[email protected]("key_length", [0, 4, 15, 20, 33])
+def test_key_size_rejects_invalid_key_length(key_length: int) -> None:
+    with pytest.raises(ValueError, match=f"Unsupported key length: 
{key_length}"):
+        AesKeySize.from_key_length(key_length)
+
+
[email protected]("key_length", [0, 4, 15, 20, 33])
+def test_secure_key_rejects_invalid_key_length(key_length: int) -> None:
+    with pytest.raises(ValueError, match="Unsupported key length"):
+        SecureKey(bytes(key_length))
+
+
[email protected]("key_size", list(AesKeySize))
+def test_secure_key_generate(key_size: AesKeySize) -> None:
+    key = SecureKey.generate(key_size)
+
+    assert len(key.key) == key_size.key_length
+    assert key.key_size == key_size
+    assert SecureKey.generate(key_size) != key
+
+
+def test_secure_key_repr_redacts_key() -> None:
+    key = SecureKey(AES128_KEY)
+
+    assert repr(key) == "SecureKey()"
+    assert repr(AES128_KEY) not in repr(key)
+
+
[email protected]("key_size", list(AesKeySize))
[email protected]("aad", [None, b"", b"aad"])
+def test_encrypt_decrypt_round_trip(key_size: AesKeySize, aad: bytes | None) 
-> None:
+    cipher = AesGcmCipher(SecureKey.generate(key_size))
+
+    ciphertext = cipher.encrypt(PLAINTEXT, aad)
+
+    assert ciphertext != PLAINTEXT
+    assert cipher.decrypt(ciphertext, aad) == PLAINTEXT
+
+

Review Comment:
   Test cases I added are verified against iceberg-rust 



-- 
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