|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +#include "mat/config.h" |
| 6 | + |
| 7 | +// These tests only apply to the curl HTTP client path (Linux, non-Apple, non-Android) |
| 8 | +#if defined(MATSDK_PAL_CPP11) && !defined(_MSC_VER) && defined(HAVE_MAT_DEFAULT_HTTP_CLIENT) \ |
| 9 | + && !defined(__APPLE__) && !defined(ANDROID) |
| 10 | + |
| 11 | +#include "common/Common.hpp" |
| 12 | +#include "http/HttpClient_Curl.hpp" |
| 13 | +#include "config/RuntimeConfig_Default.hpp" |
| 14 | + |
| 15 | +using namespace testing; |
| 16 | +using namespace MAT; |
| 17 | + |
| 18 | +class HttpClientCurlTests : public ::testing::Test |
| 19 | +{ |
| 20 | +protected: |
| 21 | + HttpClient_Curl m_client; |
| 22 | +}; |
| 23 | + |
| 24 | +// --- SetSslVerification wiring --- |
| 25 | + |
| 26 | +TEST_F(HttpClientCurlTests, SslVerification_DefaultsToTrue) |
| 27 | +{ |
| 28 | + CurlHttpOperation op("GET", "https://example.com", nullptr); |
| 29 | + ASSERT_NE(op.GetHandle(), nullptr); |
| 30 | +} |
| 31 | + |
| 32 | +TEST_F(HttpClientCurlTests, CurlHttpOperation_ConstructsWithVerifyTrue) |
| 33 | +{ |
| 34 | + CurlHttpOperation op("GET", "https://example.com", nullptr, |
| 35 | + std::map<std::string, std::string>(), std::vector<uint8_t>(), |
| 36 | + false, 5, true, ""); |
| 37 | + ASSERT_NE(op.GetHandle(), nullptr); |
| 38 | +} |
| 39 | + |
| 40 | +TEST_F(HttpClientCurlTests, CurlHttpOperation_ConstructsWithVerifyFalse) |
| 41 | +{ |
| 42 | + CurlHttpOperation op("GET", "https://example.com", nullptr, |
| 43 | + std::map<std::string, std::string>(), std::vector<uint8_t>(), |
| 44 | + false, 5, false, ""); |
| 45 | + ASSERT_NE(op.GetHandle(), nullptr); |
| 46 | +} |
| 47 | + |
| 48 | +TEST_F(HttpClientCurlTests, CurlHttpOperation_ConstructsWithCaInfo) |
| 49 | +{ |
| 50 | + CurlHttpOperation op("GET", "https://example.com", nullptr, |
| 51 | + std::map<std::string, std::string>(), std::vector<uint8_t>(), |
| 52 | + false, 5, true, "/etc/ssl/certs/ca-certificates.crt"); |
| 53 | + ASSERT_NE(op.GetHandle(), nullptr); |
| 54 | +} |
| 55 | + |
| 56 | +// --- ILogConfiguration integration --- |
| 57 | + |
| 58 | +TEST(HttpClientCurlConfigTests, LogConfiguration_SslVerify_DefaultIsTrue) |
| 59 | +{ |
| 60 | + // defaultRuntimeConfig from RuntimeConfig_Default.hpp has the defaults |
| 61 | + bool sslVerify = defaultRuntimeConfig[CFG_MAP_HTTP][CFG_BOOL_HTTP_SSL_VERIFY]; |
| 62 | + EXPECT_TRUE(sslVerify); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(HttpClientCurlConfigTests, LogConfiguration_SslCaInfo_DefaultIsEmpty) |
| 66 | +{ |
| 67 | + const char* caInfo = defaultRuntimeConfig[CFG_MAP_HTTP][CFG_STR_HTTP_SSL_CAINFO]; |
| 68 | + EXPECT_STREQ(caInfo, ""); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(HttpClientCurlConfigTests, LogConfiguration_SslVerify_CanBeDisabled) |
| 72 | +{ |
| 73 | + ILogConfiguration config; |
| 74 | + config[CFG_MAP_HTTP][CFG_BOOL_HTTP_SSL_VERIFY] = false; |
| 75 | + bool sslVerify = config[CFG_MAP_HTTP][CFG_BOOL_HTTP_SSL_VERIFY]; |
| 76 | + EXPECT_FALSE(sslVerify); |
| 77 | +} |
| 78 | + |
| 79 | +TEST(HttpClientCurlConfigTests, LogConfiguration_SslCaInfo_CanBeSet) |
| 80 | +{ |
| 81 | + ILogConfiguration config; |
| 82 | + config[CFG_MAP_HTTP][CFG_STR_HTTP_SSL_CAINFO] = "/custom/ca-bundle.crt"; |
| 83 | + const char* caInfo = config[CFG_MAP_HTTP][CFG_STR_HTTP_SSL_CAINFO]; |
| 84 | + EXPECT_STREQ(caInfo, "/custom/ca-bundle.crt"); |
| 85 | +} |
| 86 | + |
| 87 | +// --- ApplySettings integration --- |
| 88 | + |
| 89 | +TEST_F(HttpClientCurlTests, ApplySettings_ReadsSslConfigFromLogConfiguration) |
| 90 | +{ |
| 91 | + ILogConfiguration config; |
| 92 | + config[CFG_MAP_HTTP][CFG_BOOL_HTTP_SSL_VERIFY] = false; |
| 93 | + config[CFG_MAP_HTTP][CFG_STR_HTTP_SSL_CAINFO] = "/custom/ca.pem"; |
| 94 | + m_client.ApplySettings(config); |
| 95 | + // Verify indirectly -- constructing an operation should not fail |
| 96 | + SUCCEED(); |
| 97 | +} |
| 98 | + |
| 99 | +TEST_F(HttpClientCurlTests, ApplySettings_DefaultConfigEnablesVerification) |
| 100 | +{ |
| 101 | + ILogConfiguration config; |
| 102 | + m_client.ApplySettings(config); |
| 103 | + SUCCEED(); |
| 104 | +} |
| 105 | + |
| 106 | +// --- Thread safety: SetSslVerification concurrent with reads --- |
| 107 | + |
| 108 | +TEST_F(HttpClientCurlTests, SetSslVerification_ConcurrentCallsNoRace) |
| 109 | +{ |
| 110 | + // Exercise the atomic + mutex path under contention. |
| 111 | + // No assertions on output -- this is a sanitizer/TSAN target. |
| 112 | + std::vector<std::future<void>> futures; |
| 113 | + for (int i = 0; i < 10; ++i) |
| 114 | + { |
| 115 | + futures.push_back(std::async(std::launch::async, [this, i]() { |
| 116 | + m_client.SetSslVerification(i % 2 == 0, (i % 2 == 0) ? "/some/path" : ""); |
| 117 | + })); |
| 118 | + } |
| 119 | + for (auto& f : futures) |
| 120 | + { |
| 121 | + f.get(); |
| 122 | + } |
| 123 | + SUCCEED(); |
| 124 | +} |
| 125 | + |
| 126 | +#endif // MATSDK_PAL_CPP11 && !_MSC_VER && HAVE_MAT_DEFAULT_HTTP_CLIENT |
0 commit comments