@@ -167,6 +167,7 @@ def test_producer_send(self):
167167 consumer .acknowledge (msg )
168168 print ("receive from {}" .format (msg .message_id ()))
169169 self .assertEqual (msg_id , msg .message_id ())
170+ self .assertIsNone (msg .encryption_context ())
170171 client .close ()
171172
172173 def test_producer_access_mode_exclusive (self ):
@@ -489,15 +490,36 @@ def test_encryption_failure(self):
489490 client = Client (self .serviceUrl )
490491 topic = "my-python-test-end-to-end-encryption-failure-" + str (time .time ())
491492 producer = client .create_producer (
492- topic = topic , encryption_key = "client-rsa.pem" , crypto_key_reader = crypto_key_reader
493+ topic = topic , encryption_key = "client-rsa.pem" , crypto_key_reader = crypto_key_reader ,
494+ compression_type = CompressionType .LZ4
493495 )
494496 producer .send (b"msg-0" )
495497
498+ def verify_encryption_context (context : pulsar .EncryptionContext | None , failed : bool , batch_size : int ):
499+ if context is None :
500+ self .fail ("Encryption context is None" )
501+ keys = context .keys ()
502+ self .assertEqual (len (keys ), 1 )
503+ key = keys [0 ]
504+ self .assertEqual (key .key , "client-rsa.pem" )
505+ self .assertGreater (len (key .value ), 0 )
506+ self .assertEqual (key .metadata , {})
507+ self .assertGreater (len (context .param ()), 0 )
508+ self .assertEqual (context .algorithm (), "" )
509+ self .assertEqual (context .compression_type (), CompressionType .LZ4 )
510+ if batch_size == - 1 :
511+ self .assertEqual (context .uncompressed_message_size (), len (b"msg-0" ))
512+ else :
513+ self .assertGreater (context .uncompressed_message_size (), len (b"msg-0" ))
514+ self .assertEqual (context .batch_size (), batch_size )
515+ self .assertEqual (context .is_decryption_failed (), failed )
516+
496517 def verify_next_message (value : bytes ):
497518 consumer = client .subscribe (topic , subscription ,
498519 crypto_key_reader = crypto_key_reader )
499520 msg = consumer .receive (3000 )
500521 self .assertEqual (msg .data (), value )
522+ verify_encryption_context (msg .encryption_context (), False , - 1 )
501523 consumer .acknowledge (msg )
502524 consumer .close ()
503525
@@ -520,22 +542,40 @@ def verify_next_message(value: bytes):
520542
521543 producer .send (b"msg-2" )
522544 verify_next_message (b"msg-2" ) # msg-1 is skipped since the crypto failure action is DISCARD
545+ producer .close ()
546+
547+ # send batched messages
548+ producer = client .create_producer (
549+ topic = topic ,
550+ encryption_key = "client-rsa.pem" ,
551+ crypto_key_reader = crypto_key_reader ,
552+ compression_type = CompressionType .LZ4 ,
553+ batching_enabled = True ,
554+ )
555+ producer .send_async (b"msg-3" , None )
556+ producer .send_async (b"msg-4" , None )
557+ producer .flush ()
558+
559+ def verify_undecrypted_message (msg : pulsar .Message , i : int ):
560+ self .assertNotEqual (msg .data (), f"msg-{ i } " .encode ())
561+ self .assertGreater (len (msg .data ()), 5 , f"msg.data() is { msg .data ()} " )
562+ verify_encryption_context (msg .encryption_context (), True , 2 if i >= 3 else - 1 )
523563
524564 # Encrypted messages will be consumed since the crypto failure action is CONSUME
565+ # Only 4 messages can be received because msg-3 and msg-4 are sent in batch and they are delivered
566+ # as a single message when decryption fails.
525567 consumer = client .subscribe (topic , 'another-sub' ,
526568 initial_position = InitialPosition .Earliest ,
527569 crypto_failure_action = pulsar .ConsumerCryptoFailureAction .CONSUME )
528- for i in range (3 ):
570+ for i in range (4 ):
529571 msg = consumer .receive (3000 )
530- self .assertNotEqual (msg .data (), f"msg-{ i } " .encode ())
531- self .assertTrue (len (msg .data ()) > 5 , f"msg.data() is { msg .data ()} " )
572+ verify_undecrypted_message (msg , i )
532573
533574 reader = client .create_reader (topic , MessageId .earliest ,
534575 crypto_failure_action = pulsar .ConsumerCryptoFailureAction .CONSUME )
535- for i in range (3 ):
576+ for i in range (4 ):
536577 msg = reader .read_next (3000 )
537- self .assertNotEqual (msg .data (), f"msg-{ i } " .encode ())
538- self .assertTrue (len (msg .data ()) > 5 , f"msg.data() is { msg .data ()} " )
578+ verify_undecrypted_message (msg , i )
539579
540580 client .close ()
541581
0 commit comments