Skip to content

Commit 47fb809

Browse files
Use absolute path to find credential files in unit tests (#340)
Fixes #339 ### Motivation Currently the relative path is used in unit tests to specify the token file (`.test-token.txt`) and the credential directory (`./test-conf`). If we don't run the `pulsar-tests` binary in a subdirectory, it won't be able to read these files. ### Modifications Add the macro `-DTOKEN_PATH="..." -DTEST_CONF_DIR="..."` to specify absolute paths according to the `PROJECT_SOURCE_DIR`, which is defined by CMake as the absolute path of the project directory. ### Verifications ```bash cmake -B build cmake --build build -j8 # Run the test in project directory ./build/tests/pulsar-tests --gtest_filter='AuthPlugin*:*testRSAEncryption:*testEncryptionFailure:*DecryptionFailedMessages' # Run the test in a subdirectory cd build ./tests/pulsar-tests --gtest_filter='AuthPlugin*:*testRSAEncryption:*testEncryptionFailure:*DecryptionFailedMessages' ```
1 parent 6f115e7 commit 47fb809

10 files changed

+53
-27
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ if (BUILD_DYNAMIC_LIB)
395395
endif()
396396

397397
if (BUILD_TESTS)
398+
set(TOKEN_PATH "${PROJECT_SOURCE_DIR}/.test-token.txt")
399+
set(TEST_CONF_DIR "${PROJECT_SOURCE_DIR}/test-conf")
398400
add_subdirectory(tests)
399401
endif()
400402

pulsar-test-service-start.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
set -e
2222

23-
SRC_DIR=$(git rev-parse --show-toplevel)
24-
cd $SRC_DIR
23+
cd `dirname $0`
24+
SRC_DIR=$PWD
2525

2626
./pulsar-test-service-stop.sh
2727

pulsar-test-service-stop.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
set -e
2222

23-
SRC_DIR=$(git rev-parse --show-toplevel)
24-
cd $SRC_DIR
23+
cd `dirname $0`
24+
SRC_DIR=$PWD
2525

2626
CONTAINER_ID_PATH=".tests-container-id.txt"
2727

tests/AuthBasicTest.cc

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525

2626
using namespace pulsar;
2727

28+
#ifndef TEST_CONF_DIR
29+
#error "TEST_CONF_DIR is not specified"
30+
#endif
31+
2832
static const std::string serviceUrl = "pulsar://localhost:6650";
2933
static const std::string serviceUrlHttp = "http://localhost:8080";
3034
static const std::string serviceUrlTls = "pulsar+ssl://localhost:6651";
3135
static const std::string serviceUrlHttps = "https://localhost:8443";
32-
static const std::string caPath = "../test-conf/cacert.pem";
33-
static const std::string clientCertificatePath = "../test-conf/client-cert.pem";
34-
static const std::string clientPrivateKeyPath = "../test-conf/client-key.pem";
36+
static const std::string caPath = TEST_CONF_DIR "/cacert.pem";
37+
static const std::string clientCertificatePath = TEST_CONF_DIR "/client-cert.pem";
38+
static const std::string clientPrivateKeyPath = TEST_CONF_DIR "/client-key.pem";
3539

3640
TEST(AuthPluginBasic, testBasic) {
3741
ClientConfiguration config = ClientConfiguration();

tests/AuthPluginTest.cc

+14-6
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ int globalTestTlsMessagesCounter = 0;
3737
static const std::string serviceUrlTls = "pulsar+ssl://localhost:6651";
3838
static const std::string serviceUrlHttps = "https://localhost:8443";
3939

40-
static const std::string caPath = "../test-conf/cacert.pem";
41-
static const std::string clientPublicKeyPath = "../test-conf/client-cert.pem";
42-
static const std::string clientPrivateKeyPath = "../test-conf/client-key.pem";
40+
#ifndef TEST_CONF_DIR
41+
#error "TEST_CONF_DIR is not specified"
42+
#endif
43+
44+
static const std::string caPath = TEST_CONF_DIR "/cacert.pem";
45+
static const std::string clientPublicKeyPath = TEST_CONF_DIR "/client-cert.pem";
46+
static const std::string clientPrivateKeyPath = TEST_CONF_DIR "/client-key.pem";
4347

4448
// Man in middle certificate which tries to act as a broker by sending its own valid certificate
4549
static const std::string mimServiceUrlTls = "pulsar+ssl://localhost:6653";
4650
static const std::string mimServiceUrlHttps = "https://localhost:8444";
4751

48-
static const std::string mimCaPath = "../test-conf/hn-verification/cacert.pem";
52+
static const std::string mimCaPath = TEST_CONF_DIR "/hn-verification/cacert.pem";
4953

5054
static void sendCallBackTls(Result r, const MessageId& msgId) {
5155
ASSERT_EQ(r, ResultOk);
@@ -468,12 +472,16 @@ TEST(AuthPluginTest, testOauth2WrongSecret) {
468472
TEST(AuthPluginTest, testOauth2CredentialFile) {
469473
// test success get token from oauth2 server.
470474
pulsar::AuthenticationDataPtr data;
471-
std::string params = R"({
475+
const char* paramsTemplate = R"({
472476
"type": "client_credentials",
473477
"issuer_url": "https://dev-kt-aa9ne.us.auth0.com",
474-
"private_key": "../test-conf/cpp_credentials_file.json",
478+
"private_key": "%s/cpp_credentials_file.json",
475479
"audience": "https://dev-kt-aa9ne.us.auth0.com/api/v2/"})";
476480

481+
char params[4096];
482+
int numWritten = snprintf(params, sizeof(params), paramsTemplate, TEST_CONF_DIR);
483+
ASSERT_TRUE(numWritten < sizeof(params));
484+
477485
int expectedTokenLength = 3379;
478486
LOG_INFO("PARAMS: " << params);
479487
pulsar::AuthenticationPtr auth = pulsar::AuthOauth2::create(params);

tests/AuthTokenTest.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ using namespace pulsar;
3737
static const std::string serviceUrl = "pulsar://localhost:6650";
3838
static const std::string serviceUrlHttp = "http://localhost:8080";
3939

40-
static const std::string tokenPath = "../.test-token.txt";
40+
#ifndef TOKEN_PATH
41+
#error "TOKEN_PATH is not specified"
42+
#endif
43+
44+
static const std::string tokenPath = TOKEN_PATH;
4145

4246
static std::string getToken() {
4347
std::ifstream file(tokenPath);

tests/BasicEndToEndTest.cc

+10-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ DECLARE_LOG_OBJECT()
5454

5555
using namespace pulsar;
5656

57+
#ifndef TEST_CONF_DIR
58+
#error "TEST_CONF_DIR is not specified"
59+
#endif
60+
5761
std::mutex mutex_;
5862
static int globalCount = 0;
5963
static long globalResendMessageCount = 0;
@@ -1341,9 +1345,9 @@ TEST(BasicEndToEndTest, testRSAEncryption) {
13411345
std::string subName = "my-sub-name";
13421346
Producer producer;
13431347

1344-
std::string PUBLIC_CERT_FILE_PATH = "../test-conf/public-key.client-rsa.pem";
1348+
std::string PUBLIC_CERT_FILE_PATH = TEST_CONF_DIR "/public-key.client-rsa.pem";
13451349

1346-
std::string PRIVATE_CERT_FILE_PATH = "../test-conf//private-key.client-rsa.pem";
1350+
std::string PRIVATE_CERT_FILE_PATH = TEST_CONF_DIR "/private-key.client-rsa.pem";
13471351

13481352
std::shared_ptr<pulsar::DefaultCryptoKeyReader> keyReader =
13491353
std::make_shared<pulsar::DefaultCryptoKeyReader>(PUBLIC_CERT_FILE_PATH, PRIVATE_CERT_FILE_PATH);
@@ -1407,9 +1411,9 @@ TEST(BasicEndToEndTest, testEncryptionFailure) {
14071411
std::string subName = "my-sub-name";
14081412
Producer producer;
14091413

1410-
std::string PUBLIC_CERT_FILE_PATH = "../test-conf/public-key.client-rsa-test.pem";
1414+
std::string PUBLIC_CERT_FILE_PATH = TEST_CONF_DIR "/public-key.client-rsa-test.pem";
14111415

1412-
std::string PRIVATE_CERT_FILE_PATH = "../test-conf//private-key.client-rsa-test.pem";
1416+
std::string PRIVATE_CERT_FILE_PATH = TEST_CONF_DIR "/private-key.client-rsa-test.pem";
14131417

14141418
std::shared_ptr<pulsar::DefaultCryptoKeyReader> keyReader =
14151419
std::make_shared<pulsar::DefaultCryptoKeyReader>(PUBLIC_CERT_FILE_PATH, PRIVATE_CERT_FILE_PATH);
@@ -1449,9 +1453,9 @@ TEST(BasicEndToEndTest, testEncryptionFailure) {
14491453

14501454
// 2. Add valid key
14511455
{
1452-
PUBLIC_CERT_FILE_PATH = "../test-conf/public-key.client-rsa.pem";
1456+
PUBLIC_CERT_FILE_PATH = TEST_CONF_DIR "/public-key.client-rsa.pem";
14531457

1454-
PRIVATE_CERT_FILE_PATH = "../test-conf/private-key.client-rsa.pem";
1458+
PRIVATE_CERT_FILE_PATH = TEST_CONF_DIR "/private-key.client-rsa.pem";
14551459
keyReader =
14561460
std::make_shared<pulsar::DefaultCryptoKeyReader>(PUBLIC_CERT_FILE_PATH, PRIVATE_CERT_FILE_PATH);
14571461
ProducerConfiguration prodConf;

tests/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ file(GLOB TEST_SOURCES *.cc c/*.cc)
5757
add_executable(pulsar-tests ${TEST_SOURCES} ${PROTO_SOURCES})
5858

5959
target_include_directories(pulsar-tests PRIVATE ${AUTOGEN_DIR}/lib)
60-
60+
target_compile_options(pulsar-tests PRIVATE -DTOKEN_PATH="${TOKEN_PATH}" -DTEST_CONF_DIR="${TEST_CONF_DIR}")
6161
target_link_libraries(pulsar-tests ${CLIENT_LIBS} pulsarStatic $<$<CONFIG:Debug>:${GMOCKD_LIBRARY_PATH}> $<$<CONFIG:Debug>:${GTESTD_LIBRARY_PATH}> $<$<NOT:$<CONFIG:Debug>>:${GMOCK_LIBRARY_PATH}> $<$<NOT:$<CONFIG:Debug>>:${GTEST_LIBRARY_PATH}>)
6262

6363
if (UNIX)
@@ -69,7 +69,7 @@ add_executable(BrokerMetadataTest brokermetadata/BrokerMetadataTest.cc)
6969
target_link_libraries(BrokerMetadataTest ${CLIENT_LIBS} pulsarStatic ${GTEST_LIBRARY_PATH})
7070

7171
add_executable(Oauth2Test oauth2/Oauth2Test.cc)
72-
target_compile_options(Oauth2Test PRIVATE "-DTEST_ROOT_PATH=\"${CMAKE_CURRENT_SOURCE_DIR}\"")
72+
target_compile_options(Oauth2Test PRIVATE -DTEST_CONF_DIR="${TEST_CONF_DIR}")
7373
target_link_libraries(Oauth2Test ${CLIENT_LIBS} pulsarStatic ${GTEST_LIBRARY_PATH})
7474

7575
add_executable(ChunkDedupTest chunkdedup/ChunkDedupTest.cc)

tests/ConsumerTest.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ static const std::string adminUrl = "http://localhost:8080/";
5050

5151
DECLARE_LOG_OBJECT()
5252

53+
#ifndef TEST_CONF_DIR
54+
#error "TEST_CONF_DIR is not specified"
55+
#endif
56+
5357
namespace pulsar {
5458

5559
class ConsumerStateEventListener : public ConsumerEventListener {
@@ -960,8 +964,8 @@ TEST(ConsumerTest, testRedeliveryOfDecryptionFailedMessages) {
960964
std::string topicName = "testRedeliveryOfDecryptionFailedMessages" + std::to_string(time(nullptr));
961965
std::string subName = "sub-test";
962966

963-
std::string PUBLIC_CERT_FILE_PATH = "../test-conf/public-key.client-rsa.pem";
964-
std::string PRIVATE_CERT_FILE_PATH = "../test-conf/private-key.client-rsa.pem";
967+
std::string PUBLIC_CERT_FILE_PATH = TEST_CONF_DIR "/public-key.client-rsa.pem";
968+
std::string PRIVATE_CERT_FILE_PATH = TEST_CONF_DIR "/private-key.client-rsa.pem";
965969
std::shared_ptr<pulsar::DefaultCryptoKeyReader> keyReader =
966970
std::make_shared<pulsar::DefaultCryptoKeyReader>(PUBLIC_CERT_FILE_PATH, PRIVATE_CERT_FILE_PATH);
967971

tests/oauth2/Oauth2Test.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727

2828
using namespace pulsar;
2929

30-
#ifndef TEST_ROOT_PATH
31-
#define TEST_ROOT_PATH "."
30+
#ifndef TEST_CONF_DIR
31+
#error "TEST_CONF_DIR is not specified"
3232
#endif
3333

34-
static const std::string gKeyPath = std::string(TEST_ROOT_PATH) + "/../test-conf/cpp_credentials_file.json";
34+
static const std::string gKeyPath = TEST_CONF_DIR "/cpp_credentials_file.json";
3535
static std::string gClientId;
3636
static std::string gClientSecret;
3737
static ParamMap gCommonParams;

0 commit comments

Comments
 (0)