Skip to content

Commit 4dbb238

Browse files
committed
(WIP) add tests
1 parent f441e7a commit 4dbb238

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

lib/MessageImpl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class BatchMessageContainer;
4040

4141
class MessageImpl {
4242
public:
43-
// TODO: remove this default constructor
4443
explicit MessageImpl() : encryptionContext_(std::nullopt) {}
4544
MessageImpl(const MessageId& messageId, const proto::BrokerEntryMetadata& brokerEntryMetadata,
4645
const proto::MessageMetadata& metadata, const SharedBuffer& payload,

tests/EncryptionTests.cc

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
#include <gtest/gtest.h>
20+
#include <pulsar/Client.h>
21+
#include <pulsar/ConsumerCryptoFailureAction.h>
22+
23+
static std::string lookupUrl = "pulsar://localhost:6650";
24+
25+
using namespace pulsar;
26+
27+
TEST(EncryptionTests, testEncryptionContext) {
28+
Client client{lookupUrl};
29+
std::string topic = "test-encryption-context-" + std::to_string(time(nullptr));
30+
31+
ProducerConfiguration producerConf;
32+
producerConf.setCompressionType(CompressionLZ4);
33+
producerConf.addEncryptionKey("client-rsa.pem");
34+
producerConf.setCryptoKeyReader(std::make_shared<DefaultCryptoKeyReader>(
35+
TEST_CONF_DIR "/public-key.client-rsa.pem", TEST_CONF_DIR "/private-key.client-rsa.pem"));
36+
// TODO: enable batching
37+
producerConf.setBatchingEnabled(false);
38+
39+
Producer producer;
40+
ASSERT_EQ(ResultOk, client.createProducer(topic, producerConf, producer));
41+
42+
for (int i = 0; i < 5; i++) {
43+
producer.sendAsync(MessageBuilder().setContent("msg-" + std::to_string(i)).build(), nullptr);
44+
}
45+
producer.flush();
46+
producer.send(MessageBuilder().setContent("last-msg").build());
47+
48+
ConsumerConfiguration consumerConf;
49+
consumerConf.setSubscriptionInitialPosition(InitialPositionEarliest);
50+
consumerConf.setCryptoFailureAction(ConsumerCryptoFailureAction::CONSUME);
51+
Consumer consumer;
52+
ASSERT_EQ(ResultOk, client.subscribe(topic, "sub", consumerConf, consumer));
53+
54+
std::vector<Message> messages;
55+
for (int i = 0; i < 6; i++) {
56+
Message msg;
57+
ASSERT_EQ(ResultOk, consumer.receive(msg, 3000));
58+
messages.emplace_back(msg);
59+
}
60+
61+
// TODO: improve it
62+
for (int i = 0; i < 6; i++) {
63+
auto context = messages[i].getEncryptionContext();
64+
if (context.has_value()) {
65+
std::cout << i << " keys: ";
66+
for (auto&& key : context.value()->keys()) {
67+
std::cout << key.first << " => " << key.second.keyValue;
68+
if (!key.second.metadata.empty()) {
69+
std::cout << " (metadata: ";
70+
for (auto&& entry : key.second.metadata) {
71+
std::cout << entry.first << "=" << entry.second << " ";
72+
}
73+
std::cout << ")";
74+
}
75+
std::cout << " ";
76+
}
77+
std::cout << ", algorithm: " << context.value()->algorithm()
78+
<< ", param: " << context.value()->param()
79+
<< ", compressionType: " << context.value()->compressionType()
80+
<< ", uncompressedMessageSize: " << context.value()->uncompressedMessageSize()
81+
<< ", batchSize: " << context.value()->batchSize()
82+
<< ", isDecryptionFailed: " << context.value()->isDecryptionFailed() << "\n";
83+
} else {
84+
std::cerr << i << " no encryption context\n";
85+
}
86+
}
87+
88+
client.close();
89+
}

0 commit comments

Comments
 (0)