@@ -80,10 +80,8 @@ static std::vector<std::string> decryptValue(const Message& message) {
8080 return values;
8181}
8282
83- TEST (EncryptionTests, testEncryptionContext) {
84- Client client{lookupUrl};
85- std::string topic = " test-encryption-context-" + std::to_string (time (nullptr ));
86-
83+ static void testDecryption (Client& client, const std::string& topic, bool decryptionSucceed,
84+ int numMessageReceived) {
8785 ProducerConfiguration producerConf;
8886 producerConf.setCompressionType (CompressionLZ4);
8987 producerConf.addEncryptionKey (" client-rsa.pem" );
@@ -104,22 +102,49 @@ TEST(EncryptionTests, testEncryptionContext) {
104102 }
105103 producer.flush ();
106104 send (" last-msg" );
105+ producer.flush ();
106+
107+ ASSERT_EQ (ResultOk, client.createProducer (topic, producer));
108+ send (" unencrypted-msg" );
109+ producer.flush ();
110+ producer.close ();
107111
108112 ConsumerConfiguration consumerConf;
109113 consumerConf.setSubscriptionInitialPosition (InitialPositionEarliest);
110- consumerConf.setCryptoFailureAction (ConsumerCryptoFailureAction::CONSUME);
114+ if (decryptionSucceed) {
115+ consumerConf.setCryptoKeyReader (getDefaultCryptoKeyReader ());
116+ } else {
117+ consumerConf.setCryptoFailureAction (ConsumerCryptoFailureAction::CONSUME);
118+ }
111119 Consumer consumer;
112120 ASSERT_EQ (ResultOk, client.subscribe (topic, " sub" , consumerConf, consumer));
113121
114122 std::vector<std::string> values;
115- for (int i = 0 ; i < 2 ; i++) {
123+ for (int i = 0 ; i < numMessageReceived ; i++) {
116124 Message msg;
117125 ASSERT_EQ (ResultOk, consumer.receive (msg, 3000 ));
126+ if (i < numMessageReceived - 1 ) {
127+ ASSERT_TRUE (msg.getEncryptionContext ().has_value ());
128+ }
118129 for (auto && value : decryptValue (msg)) {
119130 values.emplace_back (value);
120131 }
121132 }
122133 ASSERT_EQ (values, sentValues);
134+ consumer.close ();
135+ }
123136
137+ TEST (EncryptionTests, testDecryptionSuccess) {
138+ Client client{lookupUrl};
139+ std::string topic = " test-decryption-success-" + std::to_string (time (nullptr ));
140+ testDecryption (client, topic, true , 7 );
141+ client.close ();
142+ }
143+
144+ TEST (EncryptionTests, testDecryptionFailure) {
145+ Client client{lookupUrl};
146+ std::string topic = " test-decryption-failure-" + std::to_string (time (nullptr ));
147+ // The 1st batch that has 5 messages cannot be decrypted, so they can be received only once
148+ testDecryption (client, topic, false , 3 );
124149 client.close ();
125150}
0 commit comments