3737COL_KEY = b"1234567890123450"
3838COL_KEY_NAME = "col_key"
3939
40+ DIRECT_KEY_128 = b"0123456789abcdef"
41+ DIRECT_KEY_192 = b"0123456789abcdef01234567"
42+ DIRECT_KEY_256 = b"0123456789abcdef0123456789abcdef"
43+ DIRECT_AAD_PREFIX = b"test_aad_prefix"
44+
4045
4146# Marks all of the tests in this module
4247# Ignore these with pytest ... -m 'not parquet_encryption'
@@ -727,15 +732,8 @@ def test_encrypted_parquet_read_table(tempdir, data_table, basic_encryption_conf
727732class TestDirectKeyEncryption :
728733 """Tests for create_encryption_properties / create_decryption_properties."""
729734
730- KEY_128 = b"0123456789abcdef"
731- KEY_192 = b"0123456789abcdef01234567"
732- KEY_256 = b"0123456789abcdef0123456789abcdef"
733- AAD_PREFIX = b"test_aad_prefix"
734-
735735 @pytest .mark .parametrize ("key" , [
736- b"0123456789abcdef" ,
737- b"0123456789abcdef01234567" ,
738- b"0123456789abcdef0123456789abcdef" ,
736+ DIRECT_KEY_128 , DIRECT_KEY_192 , DIRECT_KEY_256 ,
739737 ], ids = ["aes128" , "aes192" , "aes256" ])
740738 def test_roundtrip_key_sizes (self , tempdir , data_table , key ):
741739 path = tempdir / f"direct_{ len (key ) * 8 } .parquet"
@@ -751,14 +749,14 @@ def test_roundtrip_with_aad_prefix(self, tempdir, data_table):
751749 path = tempdir / "direct_aad.parquet"
752750
753751 enc_props = pe .create_encryption_properties (
754- footer_key = self . KEY_128 ,
755- aad_prefix = self . AAD_PREFIX ,
752+ footer_key = DIRECT_KEY_128 ,
753+ aad_prefix = DIRECT_AAD_PREFIX ,
756754 )
757755 pq .write_table (data_table , path , encryption_properties = enc_props )
758756
759757 dec_props = pe .create_decryption_properties (
760- footer_key = self . KEY_128 ,
761- aad_prefix = self . AAD_PREFIX ,
758+ footer_key = DIRECT_KEY_128 ,
759+ aad_prefix = DIRECT_AAD_PREFIX ,
762760 )
763761 result = pq .read_table (path , decryption_properties = dec_props )
764762 assert data_table .equals (result )
@@ -768,23 +766,23 @@ def test_roundtrip_aad_prefix_not_stored(self, tempdir, data_table):
768766 path = tempdir / "direct_aad_not_stored.parquet"
769767
770768 enc_props = pe .create_encryption_properties (
771- footer_key = self . KEY_128 ,
772- aad_prefix = self . AAD_PREFIX ,
769+ footer_key = DIRECT_KEY_128 ,
770+ aad_prefix = DIRECT_AAD_PREFIX ,
773771 store_aad_prefix = False ,
774772 )
775773 pq .write_table (data_table , path , encryption_properties = enc_props )
776774
777775 # Reading without aad_prefix should fail
778776 dec_props_no_aad = pe .create_decryption_properties (
779- footer_key = self . KEY_128 ,
777+ footer_key = DIRECT_KEY_128 ,
780778 )
781- with pytest .raises (IOError ):
779+ with pytest .raises (IOError , match = "AAD" ):
782780 pq .read_table (path , decryption_properties = dec_props_no_aad )
783781
784782 # Reading with correct aad_prefix should succeed
785783 dec_props = pe .create_decryption_properties (
786- footer_key = self . KEY_128 ,
787- aad_prefix = self . AAD_PREFIX ,
784+ footer_key = DIRECT_KEY_128 ,
785+ aad_prefix = DIRECT_AAD_PREFIX ,
788786 )
789787 result = pq .read_table (path , decryption_properties = dec_props )
790788 assert data_table .equals (result )
@@ -793,22 +791,23 @@ def test_wrong_aad_prefix_fails(self, tempdir, data_table):
793791 path = tempdir / "direct_wrong_aad.parquet"
794792
795793 enc_props = pe .create_encryption_properties (
796- footer_key = self . KEY_128 ,
797- aad_prefix = self . AAD_PREFIX ,
794+ footer_key = DIRECT_KEY_128 ,
795+ aad_prefix = DIRECT_AAD_PREFIX ,
798796 )
799797 pq .write_table (data_table , path , encryption_properties = enc_props )
800798
801799 dec_props = pe .create_decryption_properties (
802- footer_key = self . KEY_128 ,
800+ footer_key = DIRECT_KEY_128 ,
803801 aad_prefix = b"wrong_prefix" ,
804802 )
805- with pytest .raises (IOError ):
803+ with pytest .raises (IOError , match = "AAD" ):
806804 pq .read_table (path , decryption_properties = dec_props )
807805
808806 def test_encrypted_file_has_pare_magic (self , tempdir , data_table ):
809807 path = tempdir / "direct_magic.parquet"
810808
811- enc_props = pe .create_encryption_properties (footer_key = self .KEY_128 )
809+ enc_props = pe .create_encryption_properties (
810+ footer_key = DIRECT_KEY_128 )
812811 pq .write_table (data_table , path , encryption_properties = enc_props )
813812
814813 with open (path , "rb" ) as f :
@@ -819,43 +818,47 @@ def test_plaintext_footer(self, tempdir, data_table):
819818 path = tempdir / "direct_plaintext_footer.parquet"
820819
821820 enc_props = pe .create_encryption_properties (
822- footer_key = self . KEY_128 ,
821+ footer_key = DIRECT_KEY_128 ,
823822 plaintext_footer = True ,
824823 )
825824 pq .write_table (data_table , path , encryption_properties = enc_props )
826825
827- dec_props = pe .create_decryption_properties (footer_key = self .KEY_128 )
826+ dec_props = pe .create_decryption_properties (
827+ footer_key = DIRECT_KEY_128 )
828828 result = pq .read_table (path , decryption_properties = dec_props )
829829 assert data_table .equals (result )
830830
831831 def test_aes_gcm_ctr_v1_algorithm (self , tempdir , data_table ):
832832 path = tempdir / "direct_ctr.parquet"
833833
834834 enc_props = pe .create_encryption_properties (
835- footer_key = self . KEY_128 ,
835+ footer_key = DIRECT_KEY_128 ,
836836 encryption_algorithm = "AES_GCM_CTR_V1" ,
837837 )
838838 pq .write_table (data_table , path , encryption_properties = enc_props )
839839
840- dec_props = pe .create_decryption_properties (footer_key = self .KEY_128 )
840+ dec_props = pe .create_decryption_properties (
841+ footer_key = DIRECT_KEY_128 )
841842 result = pq .read_table (path , decryption_properties = dec_props )
842843 assert data_table .equals (result )
843844
844845 def test_wrong_key_fails (self , tempdir , data_table ):
845846 path = tempdir / "direct_wrong_key.parquet"
846847
847- enc_props = pe .create_encryption_properties (footer_key = self .KEY_128 )
848+ enc_props = pe .create_encryption_properties (
849+ footer_key = DIRECT_KEY_128 )
848850 pq .write_table (data_table , path , encryption_properties = enc_props )
849851
850852 wrong_key = b"fedcba9876543210"
851853 dec_props = pe .create_decryption_properties (footer_key = wrong_key )
852- with pytest .raises (IOError ):
854+ with pytest .raises (IOError , match = "decrypt" ):
853855 pq .read_table (path , decryption_properties = dec_props )
854856
855857 def test_reading_without_decryption_fails (self , tempdir , data_table ):
856858 path = tempdir / "direct_no_decrypt.parquet"
857859
858- enc_props = pe .create_encryption_properties (footer_key = self .KEY_128 )
860+ enc_props = pe .create_encryption_properties (
861+ footer_key = DIRECT_KEY_128 )
859862 pq .write_table (data_table , path , encryption_properties = enc_props )
860863
861864 with pytest .raises (IOError ):
@@ -867,7 +870,7 @@ def test_allow_plaintext_files(self, tempdir, data_table):
867870 pq .write_table (data_table , path )
868871
869872 dec_props = pe .create_decryption_properties (
870- footer_key = self . KEY_128 ,
873+ footer_key = DIRECT_KEY_128 ,
871874 allow_plaintext_files = True ,
872875 )
873876 result = pq .read_table (path , decryption_properties = dec_props )
@@ -878,19 +881,21 @@ def test_plaintext_file_rejected_by_default(self, tempdir, data_table):
878881 path = tempdir / "plaintext_rejected.parquet"
879882 pq .write_table (data_table , path )
880883
881- dec_props = pe .create_decryption_properties (footer_key = self .KEY_128 )
882- with pytest .raises (IOError ):
884+ dec_props = pe .create_decryption_properties (
885+ footer_key = DIRECT_KEY_128 )
886+ with pytest .raises (IOError , match = "plaintext" ):
883887 pq .read_table (path , decryption_properties = dec_props )
884888
885889 def test_check_footer_integrity_false (self , tempdir , data_table ):
886890 """check_footer_integrity=False should still allow decryption."""
887891 path = tempdir / "direct_no_footer_check.parquet"
888892
889- enc_props = pe .create_encryption_properties (footer_key = self .KEY_128 )
893+ enc_props = pe .create_encryption_properties (
894+ footer_key = DIRECT_KEY_128 )
890895 pq .write_table (data_table , path , encryption_properties = enc_props )
891896
892897 dec_props = pe .create_decryption_properties (
893- footer_key = self . KEY_128 ,
898+ footer_key = DIRECT_KEY_128 ,
894899 check_footer_integrity = False ,
895900 )
896901 result = pq .read_table (path , decryption_properties = dec_props )
@@ -901,7 +906,7 @@ def test_plaintext_footer_has_par1_magic(self, tempdir, data_table):
901906 path = tempdir / "direct_plaintext_magic.parquet"
902907
903908 enc_props = pe .create_encryption_properties (
904- footer_key = self . KEY_128 ,
909+ footer_key = DIRECT_KEY_128 ,
905910 plaintext_footer = True ,
906911 )
907912 pq .write_table (data_table , path , encryption_properties = enc_props )
@@ -914,12 +919,32 @@ def test_invalid_key_length_raises(self):
914919 with pytest .raises (ValueError , match = "16, 24, or 32 bytes" ):
915920 pe .create_encryption_properties (footer_key = b"short" )
916921
922+ with pytest .raises (ValueError , match = "16, 24, or 32 bytes" ):
923+ pe .create_encryption_properties (footer_key = b"" )
924+
917925 with pytest .raises (ValueError , match = "16, 24, or 32 bytes" ):
918926 pe .create_decryption_properties (footer_key = b"short" )
919927
920928 def test_invalid_algorithm_raises (self ):
921929 with pytest .raises (ValueError ):
922930 pe .create_encryption_properties (
923- footer_key = self . KEY_128 ,
931+ footer_key = DIRECT_KEY_128 ,
924932 encryption_algorithm = "INVALID" ,
925933 )
934+
935+ def test_footer_key_rejects_non_bytes (self ):
936+ with pytest .raises (TypeError , match = "footer_key must be bytes" ):
937+ pe .create_encryption_properties (footer_key = "0123456789abcdef" )
938+
939+ with pytest .raises (TypeError , match = "footer_key must be bytes" ):
940+ pe .create_decryption_properties (footer_key = "0123456789abcdef" )
941+
942+ with pytest .raises (TypeError , match = "footer_key must be bytes" ):
943+ pe .create_encryption_properties (footer_key = None )
944+
945+ def test_aad_prefix_rejects_str (self , tempdir , data_table ):
946+ with pytest .raises (TypeError , match = "aad_prefix must be bytes" ):
947+ pe .create_encryption_properties (
948+ footer_key = DIRECT_KEY_128 ,
949+ aad_prefix = "not_bytes" ,
950+ )
0 commit comments