From 61d54854390ddbf401c3988b6f407077bbdfb04d Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Mon, 12 Jun 2023 12:41:52 -0700 Subject: [PATCH 01/35] Updated SDK commit to use the latest SDK code --- CMakeLists.txt.awssdk | 2 +- source/main.cpp | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt.awssdk b/CMakeLists.txt.awssdk index c1dbc21b..00c05bfb 100644 --- a/CMakeLists.txt.awssdk +++ b/CMakeLists.txt.awssdk @@ -5,7 +5,7 @@ project(aws-iot-device-sdk-cpp-v2-download NONE) include(ExternalProject) ExternalProject_Add(aws-iot-device-sdk-cpp-v2 GIT_REPOSITORY https://github.com/aws/aws-iot-device-sdk-cpp-v2.git - GIT_TAG ac3ba3774b031dde1b988e698880d6064d53b9d9 + GIT_TAG b12656085b9bf1ecff1a0b8a4c7d496acc3f684f SOURCE_DIR "${CMAKE_BINARY_DIR}/aws-iot-device-sdk-cpp-v2-src" BINARY_DIR "${CMAKE_BINARY_DIR}/aws-iot-device-sdk-cpp-v2-build" CONFIGURE_COMMAND "" diff --git a/source/main.cpp b/source/main.cpp index 54b13363..69ceb599 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -289,6 +289,18 @@ namespace Aws int main(int argc, char *argv[]) { + auto listener = std::make_shared(); + + features = make_shared(); + + resourceManager = std::make_shared(); + + if (!resourceManager.get()->initialize(config.config, features)) + { + LOGM_ERROR(TAG, "*** %s: Failed to initialize AWS CRT SDK.", DC_FATAL_ERROR); + deviceClientAbort("Failed to initialize AWS CRT SDK"); + } + CliArgs cliArgs; if (Config::CheckTerminalArgs(argc, argv)) { @@ -321,8 +333,6 @@ int main(int argc, char *argv[]) LOG_WARN(TAG, "Unable to append current working directory to PATH environment variable."); } - features = make_shared(); - LOGM_INFO(TAG, "Now running AWS IoT Device Client version %s", DEVICE_CLIENT_VERSION_FULL); // Register for listening to interrupt signals @@ -333,15 +343,6 @@ int main(int argc, char *argv[]) sigaddset(&sigset, SIGHUP); sigprocmask(SIG_BLOCK, &sigset, nullptr); - auto listener = std::make_shared(); - resourceManager = std::make_shared(); - - if (!resourceManager.get()->initialize(config.config, features)) - { - LOGM_ERROR(TAG, "*** %s: Failed to initialize AWS CRT SDK.", DC_FATAL_ERROR); - deviceClientAbort("Failed to initialize AWS CRT SDK"); - } - #if !defined(EXCLUDE_FP) && !defined(DISABLE_MQTT) if (config.config.fleetProvisioning.enabled && !config.config.fleetProvisioningRuntimeConfig.completedFleetProvisioning) From 50b1f215f09058685076ec5b433b228644929292 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Mon, 19 Jun 2023 16:57:12 -0700 Subject: [PATCH 02/35] Addressed reviewers comments and updated unit tests to support the change --- source/SharedCrtResourceManager.cpp | 14 ++-- source/SharedCrtResourceManager.h | 4 +- source/config/Config.cpp | 5 +- source/config/Config.h | 1 + source/main.cpp | 21 +++-- test/config/TestConfig.cpp | 11 ++- test/config/TestConfigSerialization.cpp | 83 +++++-------------- test/devicedefender/TestDeviceDefender.cpp | 5 +- .../TestFleetProvisioning.cpp | 23 ++++- test/jobs/TestJobDocument.cpp | 35 ++++++-- test/jobs/TestJobsFeature.cpp | 5 ++ test/shadow/TestConfigShadowFeature.cpp | 21 ++++- test/tunneling/TestSecureTunnelingFeature.cpp | 5 +- test/util/TestSharedResourceManager.cpp | 9 ++ test/util/TestStringUtils.cpp | 25 +++++- 15 files changed, 165 insertions(+), 102 deletions(-) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index b74e0640..c8743b6c 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -39,7 +39,6 @@ bool SharedCrtResourceManager::initialize( std::shared_ptr featureRegistry) { features = featureRegistry; - initializeAllocator(config); initialized = buildClient(config) == SharedCrtResourceManager::SUCCESS; return initialized; } @@ -174,22 +173,23 @@ bool SharedCrtResourceManager::setupLogging(const PlainConfig &config) const return true; } -void SharedCrtResourceManager::initializeAllocator(const PlainConfig &config) +void SharedCrtResourceManager::initializeAllocator(const aws_mem_trace_level &configMemTraceLevel) { - allocator = aws_default_allocator(); - memTraceLevel = config.memTraceLevel; + memTraceLevel = configMemTraceLevel; if (memTraceLevel != AWS_MEMTRACE_NONE) { // If memTraceLevel == AWS_MEMTRACE_STACKS(2), then by default 8 frames per stack are used. allocator = aws_mem_tracer_new(allocator, nullptr, memTraceLevel, 0); } -} -int SharedCrtResourceManager::buildClient(const PlainConfig &config) -{ // We MUST declare an instance of the ApiHandle to perform global initialization // of the SDK libraries apiHandle = unique_ptr(new ApiHandle()); + +} + +int SharedCrtResourceManager::buildClient(const PlainConfig &config) +{ if (config.logConfig.sdkLoggingEnabled) { if (!setupLogging(config)) diff --git a/source/SharedCrtResourceManager.h b/source/SharedCrtResourceManager.h index a2ada402..ffa2d3df 100644 --- a/source/SharedCrtResourceManager.h +++ b/source/SharedCrtResourceManager.h @@ -49,8 +49,6 @@ namespace Aws int buildClient(const PlainConfig &config); - void initializeAllocator(const PlainConfig &config); - protected: /** * inheritable for testing @@ -80,6 +78,8 @@ namespace Aws bool initialize(const PlainConfig &config, std::shared_ptr featureRegistry); + void initializeAllocator(const aws_mem_trace_level &config); + void initializeAWSHttpLib(); int establishConnection(const PlainConfig &config); diff --git a/source/config/Config.cpp b/source/config/Config.cpp index 4d637720..5bf003a8 100644 --- a/source/config/Config.cpp +++ b/source/config/Config.cpp @@ -287,7 +287,7 @@ bool PlainConfig::LoadFromCliArgs(const CliArgs &cliArgs) return loadFeatureCliArgs; } -bool PlainConfig::LoadFromEnvironment() +void PlainConfig::LoadMemTraceLevelFromEnvironment() { const char *memTraceLevelStr = std::getenv("AWS_CRT_MEMORY_TRACING"); if (memTraceLevelStr) @@ -306,7 +306,10 @@ bool PlainConfig::LoadFromEnvironment() break; } } +} +bool PlainConfig::LoadFromEnvironment() +{ const char *lockFilePathIn = std::getenv("LOCK_FILE_PATH"); if (lockFilePathIn) { diff --git a/source/config/Config.h b/source/config/Config.h index 56dc9beb..a030d7cb 100644 --- a/source/config/Config.h +++ b/source/config/Config.h @@ -472,6 +472,7 @@ namespace Aws mutable std::vector settings; }; SensorPublish sensorPublish; + void LoadMemTraceLevelFromEnvironment(); }; class Config diff --git a/source/main.cpp b/source/main.cpp index 69ceb599..4f472bff 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -289,17 +289,11 @@ namespace Aws int main(int argc, char *argv[]) { - auto listener = std::make_shared(); - - features = make_shared(); - resourceManager = std::make_shared(); - if (!resourceManager.get()->initialize(config.config, features)) - { - LOGM_ERROR(TAG, "*** %s: Failed to initialize AWS CRT SDK.", DC_FATAL_ERROR); - deviceClientAbort("Failed to initialize AWS CRT SDK"); - } + config.config.LoadMemTraceLevelFromEnvironment(); + + resourceManager.get()->initializeAllocator(config.config.memTraceLevel); CliArgs cliArgs; if (Config::CheckTerminalArgs(argc, argv)) @@ -333,6 +327,8 @@ int main(int argc, char *argv[]) LOG_WARN(TAG, "Unable to append current working directory to PATH environment variable."); } + features = make_shared(); + LOGM_INFO(TAG, "Now running AWS IoT Device Client version %s", DEVICE_CLIENT_VERSION_FULL); // Register for listening to interrupt signals @@ -343,6 +339,13 @@ int main(int argc, char *argv[]) sigaddset(&sigset, SIGHUP); sigprocmask(SIG_BLOCK, &sigset, nullptr); + auto listener = std::make_shared(); + if (!resourceManager.get()->initialize(config.config, features)) + { + LOGM_ERROR(TAG, "*** %s: Failed to initialize AWS CRT SDK.", DC_FATAL_ERROR); + deviceClientAbort("Failed to initialize AWS CRT SDK"); + } + #if !defined(EXCLUDE_FP) && !defined(DISABLE_MQTT) if (config.config.fleetProvisioning.enabled && !config.config.fleetProvisioningRuntimeConfig.completedFleetProvisioning) diff --git a/test/config/TestConfig.cpp b/test/config/TestConfig.cpp index f9ecbdd1..46933ef2 100644 --- a/test/config/TestConfig.cpp +++ b/test/config/TestConfig.cpp @@ -4,6 +4,7 @@ #include "../../source/config/Config.h" #include "../../source/util/FileUtils.h" #include "../../source/util/UniqueString.h" +#include "../../source/SharedCrtResourceManager.h" #include #include @@ -33,6 +34,7 @@ class ConfigTestFixture : public ::testing::Test public: ConfigTestFixture() = default; string outputPath; + shared_ptr resourceManager; void SetUp() override { @@ -61,6 +63,13 @@ class ConfigTestFixture : public ::testing::Test outputPathStream << Config::DEFAULT_SAMPLE_SHADOW_OUTPUT_DIR << Config::DEFAULT_SAMPLE_SHADOW_DOCUMENT_FILE; outputPath = FileUtils::ExtractExpandedPath(outputPathStream.str().c_str()); + + resourceManager = std::make_shared(); + + PlainConfig configuration; + configuration.LoadMemTraceLevelFromEnvironment(); + resourceManager.get()->initializeAllocator(configuration.memTraceLevel); + } void TearDown() override @@ -1729,7 +1738,7 @@ TEST(Config, MemoryTrace) { auto levelstr = std::to_string(level); ::setenv("AWS_CRT_MEMORY_TRACING", levelstr.c_str(), 1); - ASSERT_TRUE(config.LoadFromEnvironment()) << "read AWS_CRT_MEMORY_TRACING=" << level; + config.LoadMemTraceLevelFromEnvironment(); ASSERT_EQ(config.memTraceLevel, level); } } diff --git a/test/config/TestConfigSerialization.cpp b/test/config/TestConfigSerialization.cpp index 9968d625..7325beda 100644 --- a/test/config/TestConfigSerialization.cpp +++ b/test/config/TestConfigSerialization.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "../../source/config/Config.h" +#include "../../source/SharedCrtResourceManager.h" #include "../../source/util/FileUtils.h" #include "../../source/util/UniqueString.h" @@ -20,32 +21,17 @@ using namespace Aws::Crt; using namespace Aws::Iot::DeviceClient; using namespace Aws::Iot::DeviceClient::Util; -class SerializeConfigTestFixture : public ::testing::TestWithParam -{ - protected: - const char *jsonString; -}; -TEST_P(SerializeConfigTestFixture, SerializeConfigTest) +TEST(SerializeConfigTestFixture, SerializeConfigTest) { - const char *jsonString = GetParam(); - PlainConfig config; - JsonObject jsonObject(jsonString); - config.LoadFromJson(jsonObject.View()); - auto inputJsonString = jsonObject.View().WriteCompact(); - - JsonObject serializedConfig; - config.SerializeToObject(serializedConfig); - auto serializedJsonString = serializedConfig.View().WriteCompact(); + shared_ptr resourceManager; + resourceManager = std::make_shared(); - ASSERT_STREQ(inputJsonString.c_str(), serializedJsonString.c_str()); -} + PlainConfig config; + config.LoadMemTraceLevelFromEnvironment(); + resourceManager.get()->initializeAllocator(config.memTraceLevel); -INSTANTIATE_TEST_CASE_P( - SerializationTests, - SerializeConfigTestFixture, - ::testing::Values( - R"( + constexpr char jsonString[] = R"( { "endpoint": "endpoint value", "cert": "/tmp/aws-iot-device-client-test-file", @@ -138,45 +124,14 @@ INSTANTIATE_TEST_CASE_P( } ] } -})", - R"({ - "logging": { - "level": "DEBUG", - "type": "file", - "file": "./aws-iot-device-client.log", - "enable-sdk-logging": false, - "sdk-log-level": "TRACE", - "sdk-log-file": "/var/log/aws-iot-device-client/sdk.log" - }, - "jobs": { - "enabled": true, - "handler-directory": "" - }, - "tunneling": { - "enabled": true - }, - "device-defender": { - "enabled": true, - "interval": 300 - }, - "fleet-provisioning": { - "enabled": true - }, - "runtime-config": { - "completed-fp": false - }, - "samples": { - "pub-sub": { - "enabled": true - } - }, - "config-shadow": { - "enabled": true - }, - "sample-shadow": { - "enabled": true - }, - "secure-element": { - "enabled": true - } -})")); +})"; + JsonObject jsonObject(jsonString); + config.LoadFromJson(jsonObject.View()); + auto inputJsonString = jsonObject.View().WriteCompact(); + + JsonObject serializedConfig; + config.SerializeToObject(serializedConfig); + auto serializedJsonString = serializedConfig.View().WriteCompact(); + + ASSERT_STREQ(inputJsonString.c_str(), serializedJsonString.c_str()); +} \ No newline at end of file diff --git a/test/devicedefender/TestDeviceDefender.cpp b/test/devicedefender/TestDeviceDefender.cpp index ab30c3c1..33f3a20c 100644 --- a/test/devicedefender/TestDeviceDefender.cpp +++ b/test/devicedefender/TestDeviceDefender.cpp @@ -83,8 +83,11 @@ class TestDeviceDefender : public testing::Test public: void SetUp() { - deviceDefender = unique_ptr(new MockDDFeature()); manager = shared_ptr(new SharedCrtResourceManager()); + config.LoadMemTraceLevelFromEnvironment(); + manager.get()->initializeAllocator(config.memTraceLevel); + + deviceDefender = unique_ptr(new MockDDFeature()); notifier = shared_ptr(new MockNotifier()); task = shared_ptr(new MockReportTask()); config = getSimpleDDConfig(); diff --git a/test/fleetprovisioning/TestFleetProvisioning.cpp b/test/fleetprovisioning/TestFleetProvisioning.cpp index c013dcc2..8f04246b 100644 --- a/test/fleetprovisioning/TestFleetProvisioning.cpp +++ b/test/fleetprovisioning/TestFleetProvisioning.cpp @@ -9,7 +9,24 @@ using namespace std; using namespace Aws::Iot::DeviceClient::FleetProvisioningNS; -TEST(FleetProvisioning, EmptyTemplateParameters) +class FleetProvisioningFeature : public ::testing::Test +{ + public: + FleetProvisioningFeature() = default; + shared_ptr resourceManager; + + void SetUp() override + { + resourceManager = std::make_shared(); + + Aws::Iot::DeviceClient::PlainConfig configuration; + configuration.LoadMemTraceLevelFromEnvironment(); + resourceManager.get()->initializeAllocator(configuration.memTraceLevel); + + } +}; + +TEST_F(FleetProvisioningFeature, EmptyTemplateParameters) { Aws::Crt::Optional params; // start with empty value FleetProvisioning fp; @@ -17,7 +34,7 @@ TEST(FleetProvisioning, EmptyTemplateParameters) params = "{}"; // test empty JSON } -TEST(FleetProvisioning, MalformedTemplateParameters) +TEST_F(FleetProvisioningFeature, MalformedTemplateParameters) { Aws::Crt::Optional params("{\"SerialNumber\" \"Device-SN\"}"); // test missing colon FleetProvisioning fp; @@ -28,7 +45,7 @@ TEST(FleetProvisioning, MalformedTemplateParameters) ASSERT_FALSE(fp.MapParameters(params)); } -TEST(FleetProvisioning, ValidTemplateParameters) +TEST_F(FleetProvisioningFeature, ValidTemplateParameters) { Aws::Crt::Optional params("{\"SerialNumber\": \"Device-SN\"}"); // test single JSON property FleetProvisioning fp; diff --git a/test/jobs/TestJobDocument.cpp b/test/jobs/TestJobDocument.cpp index 674fbc20..14b88ffb 100644 --- a/test/jobs/TestJobDocument.cpp +++ b/test/jobs/TestJobDocument.cpp @@ -3,6 +3,8 @@ #include "../../source/jobs/JobDocument.h" #include "../../source/util/UniqueString.h" +#include "../../source/SharedCrtResourceManager.h" + #include "gtest/gtest.h" #include #include @@ -12,6 +14,23 @@ using namespace Aws::Crt; using namespace Aws::Iot::DeviceClient; using namespace Aws::Iot::DeviceClient::Jobs; +class JobDocumentFixture : public ::testing::Test +{ + public: + JobDocumentFixture() = default; + shared_ptr resourceManager; + + void SetUp() override + { + resourceManager = std::make_shared(); + + PlainConfig configuration; + configuration.LoadMemTraceLevelFromEnvironment(); + resourceManager.get()->initializeAllocator(configuration.memTraceLevel); + + } +}; + void AssertVectorEqual(const Optional> &vector1, const Optional> &vector2) { ASSERT_TRUE(vector1->size() == vector2->size()); @@ -75,7 +94,7 @@ void AssertStepEqual(const vector &step1, const vec } } -TEST(JobDocument, SampleJobDocument) +TEST_F(JobDocumentFixture, SampleJobDocument) { constexpr char jsonString[] = R"( { @@ -292,7 +311,7 @@ TEST(JobDocument, SampleJobDocument) ASSERT_TRUE(jobDocument.finalStep->ignoreStepFailure); } -TEST(JobDocument, MissingRequiredFields) +TEST_F(JobDocumentFixture, MissingRequiredFields) { constexpr char jsonString[] = R"( { @@ -385,7 +404,7 @@ TEST(JobDocument, MissingRequiredFields) ASSERT_FALSE(jobDocument.Validate()); } -TEST(JobDocument, MinimumJobDocument) +TEST_F(JobDocumentFixture, MinimumJobDocument) { constexpr char jsonString[] = R"( { @@ -438,7 +457,7 @@ TEST(JobDocument, MinimumJobDocument) ASSERT_TRUE(jobDocument.Validate()); } -TEST(JobDocument, MissingRequiredFieldsValue) +TEST_F(JobDocumentFixture, MissingRequiredFieldsValue) { constexpr char jsonString[] = R"( { @@ -531,7 +550,7 @@ TEST(JobDocument, MissingRequiredFieldsValue) ASSERT_FALSE(jobDocument.Validate()); } -TEST(JobDocument, CommandFieldsIsEmpty) +TEST_F(JobDocumentFixture, CommandFieldsIsEmpty) { constexpr char jsonString[] = R"( { @@ -558,7 +577,7 @@ TEST(JobDocument, CommandFieldsIsEmpty) ASSERT_FALSE(jobDocument.Validate()); } -TEST(JobDocument, CommandContainsSpaceCharacters) +TEST_F(JobDocumentFixture, CommandContainsSpaceCharacters) { constexpr char jsonString[] = R"( { @@ -585,7 +604,7 @@ TEST(JobDocument, CommandContainsSpaceCharacters) ASSERT_TRUE(jobDocument.Validate()); } -TEST(JobDocument, SpaceCharactersContainedWithinFirstWordOfCommand) +TEST_F(JobDocumentFixture, SpaceCharactersContainedWithinFirstWordOfCommand) { constexpr char jsonString[] = R"( { @@ -612,7 +631,7 @@ TEST(JobDocument, SpaceCharactersContainedWithinFirstWordOfCommand) ASSERT_FALSE(jobDocument.Validate()); } -TEST(JobDocument, oldJobDocumentCompatibility) +TEST_F(JobDocumentFixture, oldJobDocumentCompatibility) { constexpr char jsonString[] = R"( { diff --git a/test/jobs/TestJobsFeature.cpp b/test/jobs/TestJobsFeature.cpp index 1f04cc53..6a29fc42 100644 --- a/test/jobs/TestJobsFeature.cpp +++ b/test/jobs/TestJobsFeature.cpp @@ -258,6 +258,10 @@ class TestJobsFeature : public ::testing::Test public: void SetUp() { + resourceManager = std::make_shared(); + config.LoadMemTraceLevelFromEnvironment(); + resourceManager.get()->initializeAllocator(config.memTraceLevel); + ThingName = Aws::Crt::String("thing-name value"); notifier = shared_ptr(new MockNotifier()); config = getSimpleConfig(); @@ -269,6 +273,7 @@ class TestJobsFeature : public ::testing::Test } Aws::Crt::String ThingName; shared_ptr notifier; + shared_ptr resourceManager; PlainConfig config; unique_ptr startNextJobExecutionResponse; unique_ptr jobsMock; diff --git a/test/shadow/TestConfigShadowFeature.cpp b/test/shadow/TestConfigShadowFeature.cpp index a465ec99..95cd7a20 100644 --- a/test/shadow/TestConfigShadowFeature.cpp +++ b/test/shadow/TestConfigShadowFeature.cpp @@ -13,7 +13,24 @@ using namespace Aws::Iot::DeviceClient::Util; using namespace Aws::Iot::DeviceClient::Shadow; using namespace std; -TEST(ConfigShadowFeature, resetClientConfigWithValidJSON) +class ConfigShadowFeature : public ::testing::Test +{ + public: + ConfigShadowFeature() = default; + shared_ptr resourceManager; + + void SetUp() override + { + resourceManager = std::make_shared(); + + Aws::Iot::DeviceClient::PlainConfig configuration; + configuration.LoadMemTraceLevelFromEnvironment(); + resourceManager.get()->initializeAllocator(configuration.memTraceLevel); + + } +}; + +TEST_F(ConfigShadowFeature, resetClientConfigWithValidJSON) { constexpr char oldJsonString[] = R"( { @@ -104,7 +121,7 @@ TEST(ConfigShadowFeature, resetClientConfigWithValidJSON) ASSERT_FALSE(config.jobs.enabled); } -TEST(ConfigShadowFeature, resetClientConfigWithInvalidJSON) +TEST_F(ConfigShadowFeature, resetClientConfigWithInvalidJSON) { constexpr char oldJsonString[] = R"( { diff --git a/test/tunneling/TestSecureTunnelingFeature.cpp b/test/tunneling/TestSecureTunnelingFeature.cpp index 6bae9f81..441f3327 100644 --- a/test/tunneling/TestSecureTunnelingFeature.cpp +++ b/test/tunneling/TestSecureTunnelingFeature.cpp @@ -103,9 +103,12 @@ class TestSecureTunnelingFeature : public testing::Test public: void SetUp() override { + manager = shared_ptr(new SharedCrtResourceManager()); + config.LoadMemTraceLevelFromEnvironment(); + manager.get()->initializeAllocator(config.memTraceLevel); + thingName = Aws::Crt::String("thing-name value"); secureTunnelingFeature = shared_ptr(new MockSecureTunnelingFeature()); - manager = shared_ptr(new SharedCrtResourceManager()); mockClient = shared_ptr(new MockIotSecureTunnelingClient()); notifier = shared_ptr(new MockNotifier()); fakeContext = unique_ptr(new FakeSecureTunnelContext()); diff --git a/test/util/TestSharedResourceManager.cpp b/test/util/TestSharedResourceManager.cpp index 25d1229c..232b7359 100644 --- a/test/util/TestSharedResourceManager.cpp +++ b/test/util/TestSharedResourceManager.cpp @@ -3,6 +3,8 @@ #include "../../source/SharedCrtResourceManager.h" #include "../../source/util/FileUtils.h" +#include "../../source/SharedCrtResourceManager.h" + #include "gtest/gtest.h" #include @@ -54,6 +56,7 @@ class SharedResourceManagerTest : public ::testing::Test { public: SharedResourceManagerTest() = default; + shared_ptr resourceManager; void SetUp() override { @@ -82,6 +85,12 @@ class SharedResourceManagerTest : public ::testing::Test // Ensure invalid files do not exist std::remove(invalidCertFilePath.c_str()); std::remove(invalidKeyFilePath.c_str()); + + resourceManager = std::make_shared(); + + PlainConfig configuration; + configuration.LoadMemTraceLevelFromEnvironment(); + resourceManager.get()->initializeAllocator(configuration.memTraceLevel); } void TearDown() override diff --git a/test/util/TestStringUtils.cpp b/test/util/TestStringUtils.cpp index cc82c399..5ec992cc 100644 --- a/test/util/TestStringUtils.cpp +++ b/test/util/TestStringUtils.cpp @@ -3,6 +3,8 @@ #include "../../source/config/Config.h" #include "../../source/util/StringUtils.h" +#include "../../source/SharedCrtResourceManager.h" + #include "gtest/gtest.h" #include @@ -12,6 +14,23 @@ using namespace Aws::Iot::DeviceClient::Util; constexpr size_t Config::MAX_CONFIG_SIZE; +class StringUtilsFixture : public ::testing::Test +{ + public: + StringUtilsFixture() = default; + shared_ptr resourceManager; + + void SetUp() override + { + resourceManager = std::make_shared(); + + PlainConfig configuration; + configuration.LoadMemTraceLevelFromEnvironment(); + resourceManager.get()->initializeAllocator(configuration.memTraceLevel); + + } +}; + TEST(StringUtils, FormatStringNoArg) { constexpr char expected[] = "Hello world"; @@ -63,7 +82,7 @@ TEST(StringUtils, leavesNewLineAndTabAlone) ASSERT_STREQ(original.c_str(), Sanitize(original).c_str()); } -TEST(StringUtils, maptoString) +TEST_F(StringUtilsFixture, maptoString) { Aws::Crt::Map map; map.insert(std::pair("a", "b")); @@ -75,7 +94,7 @@ TEST(StringUtils, maptoString) ASSERT_STREQ(expected.c_str(), MapToString(map).c_str()); } -TEST(StringUtils, emptyMaptoString) +TEST_F(StringUtilsFixture, emptyMaptoString) { Aws::Crt::Map map; string expected = ""; @@ -124,7 +143,7 @@ TEST(StringUtils, trimMultiChar) ASSERT_EQ("", TrimCopy("", "/")); // Empty string. } -TEST(StringUtils, ParseToStringVector) +TEST_F(StringUtilsFixture, ParseToStringVector) { constexpr char jsonString[] = R"( { From 1516023c6de68e5c4a3eed95a5a7057444943059 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Mon, 19 Jun 2023 17:02:55 -0700 Subject: [PATCH 03/35] Resolved indentation errors --- test/config/TestConfig.cpp | 3 +-- test/config/TestConfigSerialization.cpp | 3 +-- test/fleetprovisioning/TestFleetProvisioning.cpp | 1 - test/jobs/TestJobDocument.cpp | 3 +-- test/shadow/TestConfigShadowFeature.cpp | 1 - test/util/TestSharedResourceManager.cpp | 1 - test/util/TestStringUtils.cpp | 3 +-- 7 files changed, 4 insertions(+), 11 deletions(-) diff --git a/test/config/TestConfig.cpp b/test/config/TestConfig.cpp index 46933ef2..97f4bed4 100644 --- a/test/config/TestConfig.cpp +++ b/test/config/TestConfig.cpp @@ -1,10 +1,10 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +#include "../../source/SharedCrtResourceManager.h" #include "../../source/config/Config.h" #include "../../source/util/FileUtils.h" #include "../../source/util/UniqueString.h" -#include "../../source/SharedCrtResourceManager.h" #include #include @@ -69,7 +69,6 @@ class ConfigTestFixture : public ::testing::Test PlainConfig configuration; configuration.LoadMemTraceLevelFromEnvironment(); resourceManager.get()->initializeAllocator(configuration.memTraceLevel); - } void TearDown() override diff --git a/test/config/TestConfigSerialization.cpp b/test/config/TestConfigSerialization.cpp index 7325beda..a92a9b1b 100644 --- a/test/config/TestConfigSerialization.cpp +++ b/test/config/TestConfigSerialization.cpp @@ -1,8 +1,8 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -#include "../../source/config/Config.h" #include "../../source/SharedCrtResourceManager.h" +#include "../../source/config/Config.h" #include "../../source/util/FileUtils.h" #include "../../source/util/UniqueString.h" @@ -21,7 +21,6 @@ using namespace Aws::Crt; using namespace Aws::Iot::DeviceClient; using namespace Aws::Iot::DeviceClient::Util; - TEST(SerializeConfigTestFixture, SerializeConfigTest) { shared_ptr resourceManager; diff --git a/test/fleetprovisioning/TestFleetProvisioning.cpp b/test/fleetprovisioning/TestFleetProvisioning.cpp index 8f04246b..ec917753 100644 --- a/test/fleetprovisioning/TestFleetProvisioning.cpp +++ b/test/fleetprovisioning/TestFleetProvisioning.cpp @@ -22,7 +22,6 @@ class FleetProvisioningFeature : public ::testing::Test Aws::Iot::DeviceClient::PlainConfig configuration; configuration.LoadMemTraceLevelFromEnvironment(); resourceManager.get()->initializeAllocator(configuration.memTraceLevel); - } }; diff --git a/test/jobs/TestJobDocument.cpp b/test/jobs/TestJobDocument.cpp index 14b88ffb..abdd272d 100644 --- a/test/jobs/TestJobDocument.cpp +++ b/test/jobs/TestJobDocument.cpp @@ -1,9 +1,9 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +#include "../../source/SharedCrtResourceManager.h" #include "../../source/jobs/JobDocument.h" #include "../../source/util/UniqueString.h" -#include "../../source/SharedCrtResourceManager.h" #include "gtest/gtest.h" #include @@ -27,7 +27,6 @@ class JobDocumentFixture : public ::testing::Test PlainConfig configuration; configuration.LoadMemTraceLevelFromEnvironment(); resourceManager.get()->initializeAllocator(configuration.memTraceLevel); - } }; diff --git a/test/shadow/TestConfigShadowFeature.cpp b/test/shadow/TestConfigShadowFeature.cpp index 95cd7a20..b0ab76ae 100644 --- a/test/shadow/TestConfigShadowFeature.cpp +++ b/test/shadow/TestConfigShadowFeature.cpp @@ -26,7 +26,6 @@ class ConfigShadowFeature : public ::testing::Test Aws::Iot::DeviceClient::PlainConfig configuration; configuration.LoadMemTraceLevelFromEnvironment(); resourceManager.get()->initializeAllocator(configuration.memTraceLevel); - } }; diff --git a/test/util/TestSharedResourceManager.cpp b/test/util/TestSharedResourceManager.cpp index 232b7359..e63937a1 100644 --- a/test/util/TestSharedResourceManager.cpp +++ b/test/util/TestSharedResourceManager.cpp @@ -3,7 +3,6 @@ #include "../../source/SharedCrtResourceManager.h" #include "../../source/util/FileUtils.h" -#include "../../source/SharedCrtResourceManager.h" #include "gtest/gtest.h" diff --git a/test/util/TestStringUtils.cpp b/test/util/TestStringUtils.cpp index 5ec992cc..08c1aa08 100644 --- a/test/util/TestStringUtils.cpp +++ b/test/util/TestStringUtils.cpp @@ -1,9 +1,9 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +#include "../../source/SharedCrtResourceManager.h" #include "../../source/config/Config.h" #include "../../source/util/StringUtils.h" -#include "../../source/SharedCrtResourceManager.h" #include "gtest/gtest.h" #include @@ -27,7 +27,6 @@ class StringUtilsFixture : public ::testing::Test PlainConfig configuration; configuration.LoadMemTraceLevelFromEnvironment(); resourceManager.get()->initializeAllocator(configuration.memTraceLevel); - } }; From 09af0fb5436ce58741b1c765b20eeb92d445a206 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Mon, 19 Jun 2023 17:05:47 -0700 Subject: [PATCH 04/35] Resolved indentation errors --- source/SharedCrtResourceManager.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index c8743b6c..5b3c6909 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -185,7 +185,6 @@ void SharedCrtResourceManager::initializeAllocator(const aws_mem_trace_level &co // We MUST declare an instance of the ApiHandle to perform global initialization // of the SDK libraries apiHandle = unique_ptr(new ApiHandle()); - } int SharedCrtResourceManager::buildClient(const PlainConfig &config) @@ -371,7 +370,8 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) * This will execute when an mqtt connect has completed or failed. */ auto onConnectionCompleted = [this, &connectionCompletedPromise]( - const Mqtt::MqttConnection &, int errorCode, Mqtt::ReturnCode returnCode, bool) { + const Mqtt::MqttConnection &, int errorCode, Mqtt::ReturnCode returnCode, bool) + { if (errorCode) { LOGM_ERROR(TAG, "MQTT Connection failed with error: %s", ErrorDebugString(errorCode)); @@ -395,7 +395,8 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) /* * Invoked when a disconnect message has completed. */ - auto onDisconnect = [this](const Mqtt::MqttConnection & /*conn*/) { + auto onDisconnect = [this](const Mqtt::MqttConnection & /*conn*/) + { { LOG_INFO(TAG, "MQTT Connection is now disconnected"); connectionClosedPromise.set_value(); @@ -405,7 +406,8 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) /* * Invoked when connection is interrupted. */ - auto OnConnectionInterrupted = [this](const Mqtt::MqttConnection &, int errorCode) { + auto OnConnectionInterrupted = [this](const Mqtt::MqttConnection &, int errorCode) + { { if (errorCode) { @@ -421,7 +423,8 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) /* * Invoked when connection is resumed. */ - auto OnConnectionResumed = [this](const Mqtt::MqttConnection &, int returnCode, bool) { + auto OnConnectionResumed = [this](const Mqtt::MqttConnection &, int returnCode, bool) + { { LOGM_INFO(TAG, "MQTT connection resumed with return code: %d", returnCode); } From d196c07a1d2fb0628f6db242c645bae03d16591a Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Mon, 19 Jun 2023 17:10:20 -0700 Subject: [PATCH 05/35] Resolved indentation errors --- source/SharedCrtResourceManager.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index 5b3c6909..9d2241a1 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -185,6 +185,7 @@ void SharedCrtResourceManager::initializeAllocator(const aws_mem_trace_level &co // We MUST declare an instance of the ApiHandle to perform global initialization // of the SDK libraries apiHandle = unique_ptr(new ApiHandle()); + } int SharedCrtResourceManager::buildClient(const PlainConfig &config) From b4358ef4d74235bd2660d10a25e22c80961c96a9 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Mon, 19 Jun 2023 17:11:45 -0700 Subject: [PATCH 06/35] Resolved indentation errors --- source/SharedCrtResourceManager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index 9d2241a1..5b3c6909 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -185,7 +185,6 @@ void SharedCrtResourceManager::initializeAllocator(const aws_mem_trace_level &co // We MUST declare an instance of the ApiHandle to perform global initialization // of the SDK libraries apiHandle = unique_ptr(new ApiHandle()); - } int SharedCrtResourceManager::buildClient(const PlainConfig &config) From 866712cd29f99d0886fcc02ff35ba56e478f0c9a Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Mon, 19 Jun 2023 17:22:35 -0700 Subject: [PATCH 07/35] Resolved indentation errors --- source/SharedCrtResourceManager.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index 5b3c6909..d5fa27fe 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -370,8 +370,7 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) * This will execute when an mqtt connect has completed or failed. */ auto onConnectionCompleted = [this, &connectionCompletedPromise]( - const Mqtt::MqttConnection &, int errorCode, Mqtt::ReturnCode returnCode, bool) - { + const Mqtt::MqttConnection &, int errorCode, Mqtt::ReturnCode returnCode, bool) { if (errorCode) { LOGM_ERROR(TAG, "MQTT Connection failed with error: %s", ErrorDebugString(errorCode)); @@ -395,8 +394,7 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) /* * Invoked when a disconnect message has completed. */ - auto onDisconnect = [this](const Mqtt::MqttConnection & /*conn*/) - { + auto onDisconnect = [this](const Mqtt::MqttConnection & /*conn*/) { { LOG_INFO(TAG, "MQTT Connection is now disconnected"); connectionClosedPromise.set_value(); @@ -406,8 +404,7 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) /* * Invoked when connection is interrupted. */ - auto OnConnectionInterrupted = [this](const Mqtt::MqttConnection &, int errorCode) - { + auto OnConnectionInterrupted = [this](const Mqtt::MqttConnection &, int errorCode) { { if (errorCode) { @@ -423,8 +420,7 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) /* * Invoked when connection is resumed. */ - auto OnConnectionResumed = [this](const Mqtt::MqttConnection &, int returnCode, bool) - { + auto OnConnectionResumed = [this](const Mqtt::MqttConnection &, int returnCode, bool) { { LOGM_INFO(TAG, "MQTT connection resumed with return code: %d", returnCode); } From d0c2eb9c6977e02bbd49df0efd2c72f6342c2af5 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Tue, 20 Jun 2023 09:17:19 -0700 Subject: [PATCH 08/35] update device mqtt connection logic --- source/SharedCrtResourceManager.cpp | 6 ++++++ source/main.cpp | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index d5fa27fe..bcc326b4 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -515,6 +515,12 @@ Aws::Crt::Io::ClientBootstrap *SharedCrtResourceManager::getClientBootstrap() void SharedCrtResourceManager::disconnect() { LOG_DEBUG(TAG, "Attempting to disconnect MQTT connection"); + if (connection == NULL) + { + LOG_WARN(TAG, "Tried to disconnect MQTT connection but the MQTT connection has not yet been established!"); + return; + } + if (connection->Disconnect()) { if (connectionClosedPromise.get_future().wait_for(std::chrono::seconds(DEFAULT_WAIT_TIME_SECONDS)) == diff --git a/source/main.cpp b/source/main.cpp index 4f472bff..0fb2ec8e 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -289,6 +289,13 @@ namespace Aws int main(int argc, char *argv[]) { + + if (Config::CheckTerminalArgs(argc, argv)) + { + LoggerFactory::getLoggerInstance()->shutdown(); + return 0; + } + resourceManager = std::make_shared(); config.config.LoadMemTraceLevelFromEnvironment(); @@ -296,11 +303,6 @@ int main(int argc, char *argv[]) resourceManager.get()->initializeAllocator(config.config.memTraceLevel); CliArgs cliArgs; - if (Config::CheckTerminalArgs(argc, argv)) - { - LoggerFactory::getLoggerInstance()->shutdown(); - return 0; - } if (!Config::ParseCliArgs(argc, argv, cliArgs) || !config.init(cliArgs)) { LOGM_ERROR( From a90659d1ed4bf6fef08947db521fc3841d7a9b2d Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Tue, 20 Jun 2023 12:29:46 -0700 Subject: [PATCH 09/35] removing DC initialization code to create multiple lock files --- source/main.cpp | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 0fb2ec8e..b460938a 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -329,6 +329,19 @@ int main(int argc, char *argv[]) LOG_WARN(TAG, "Unable to append current working directory to PATH environment variable."); } +#if !defined(DISABLE_MQTT) + /** + * init() is currently responsible for making sure only 1 instance of Device Client is running at a given time. + * In the future, we may want to move other Device Client startup logic into this function. + * returns false if an exception is thrown + */ + if (!init(argc, argv)) + { + LOGM_ERROR(TAG, "*** %s: An instance of Device Client is already running.", DC_FATAL_ERROR); + deviceClientAbort("An instance of Device Client is already running."); + } +#endif + features = make_shared(); LOGM_INFO(TAG, "Now running AWS IoT Device Client version %s", DEVICE_CLIENT_VERSION_FULL); @@ -352,16 +365,6 @@ int main(int argc, char *argv[]) if (config.config.fleetProvisioning.enabled && !config.config.fleetProvisioningRuntimeConfig.completedFleetProvisioning) { - /** - * init() is currently responsible for making sure only 1 instance of Device Client is running at a given time. - * In the future, we may want to move other Device Client startup logic into this function. - * returns false if an exception is thrown - */ - if (!init(argc, argv)) - { - LOGM_ERROR(TAG, "*** %s: An instance of Device Client is already running.", DC_FATAL_ERROR); - deviceClientAbort("An instance of Device Client is already running."); - } /* * Establish MQTT connection using claim certificates and private key to provision the device/thing. */ @@ -400,16 +403,6 @@ int main(int argc, char *argv[]) #endif #if !defined(DISABLE_MQTT) - /** - * init() is currently responsible for making sure only 1 instance of Device Client is running at a given time. - * In the future, we may want to move other Device Client startup logic into this function. - * returns false if an exception is thrown - */ - if (!init(argc, argv)) - { - LOGM_ERROR(TAG, "*** %s: An instance of Device Client is already running.", DC_FATAL_ERROR); - deviceClientAbort("An instance of Device Client is already running."); - } /* * Establish MQTT connection using permanent certificate and private key to start and run AWS IoT Device Client * features. From 730d83934fbe229b5984fa54d6f803558872b448 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Tue, 27 Jun 2023 01:53:43 -0700 Subject: [PATCH 10/35] addressed reviewers comments to resolve test failures --- source/SharedCrtResourceManager.cpp | 27 +++++++++++++++++++++++++-- source/SharedCrtResourceManager.h | 6 ++++-- source/config/Config.cpp | 21 --------------------- source/config/Config.h | 2 -- source/main.cpp | 4 ---- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index bcc326b4..8744e95e 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -43,6 +43,27 @@ bool SharedCrtResourceManager::initialize( return initialized; } +void SharedCrtResourceManager::loadMemTraceLevelFromEnvironment() +{ + const char *memTraceLevelStr = std::getenv("AWS_CRT_MEMORY_TRACING"); + if (memTraceLevelStr) + { + switch (atoi(memTraceLevelStr)) + { + case AWS_MEMTRACE_BYTES: + LOG_DEBUG(Config::TAG, "Set AWS_CRT_MEMORY_TRACING=AWS_MEMTRACE_BYTES"); + memTraceLevel = AWS_MEMTRACE_BYTES; + break; + case AWS_MEMTRACE_STACKS: + LOG_DEBUG(Config::TAG, "Set AWS_CRT_MEMORY_TRACING=AWS_MEMTRACE_STACKS"); + memTraceLevel = AWS_MEMTRACE_STACKS; + break; + default: + break; + } + } +} + bool SharedCrtResourceManager::locateCredentials(const PlainConfig &config) const { struct stat fileInfo; @@ -173,9 +194,11 @@ bool SharedCrtResourceManager::setupLogging(const PlainConfig &config) const return true; } -void SharedCrtResourceManager::initializeAllocator(const aws_mem_trace_level &configMemTraceLevel) +void SharedCrtResourceManager::initializeAllocator() { - memTraceLevel = configMemTraceLevel; + loadMemTraceLevelFromEnvironment(); + allocator = aws_default_allocator(); + if (memTraceLevel != AWS_MEMTRACE_NONE) { // If memTraceLevel == AWS_MEMTRACE_STACKS(2), then by default 8 frames per stack are used. diff --git a/source/SharedCrtResourceManager.h b/source/SharedCrtResourceManager.h index ffa2d3df..f791f7ed 100644 --- a/source/SharedCrtResourceManager.h +++ b/source/SharedCrtResourceManager.h @@ -49,6 +49,8 @@ namespace Aws int buildClient(const PlainConfig &config); + void loadMemTraceLevelFromEnvironment(); + protected: /** * inheritable for testing @@ -56,7 +58,7 @@ namespace Aws bool locateCredentials(const PlainConfig &config) const; public: - SharedCrtResourceManager() = default; + SharedCrtResourceManager() { initializeAllocator(); } virtual ~SharedCrtResourceManager(); @@ -78,7 +80,7 @@ namespace Aws bool initialize(const PlainConfig &config, std::shared_ptr featureRegistry); - void initializeAllocator(const aws_mem_trace_level &config); + void initializeAllocator(); void initializeAWSHttpLib(); diff --git a/source/config/Config.cpp b/source/config/Config.cpp index 5bf003a8..d7feac83 100644 --- a/source/config/Config.cpp +++ b/source/config/Config.cpp @@ -287,27 +287,6 @@ bool PlainConfig::LoadFromCliArgs(const CliArgs &cliArgs) return loadFeatureCliArgs; } -void PlainConfig::LoadMemTraceLevelFromEnvironment() -{ - const char *memTraceLevelStr = std::getenv("AWS_CRT_MEMORY_TRACING"); - if (memTraceLevelStr) - { - switch (atoi(memTraceLevelStr)) - { - case AWS_MEMTRACE_BYTES: - LOG_DEBUG(Config::TAG, "Set AWS_CRT_MEMORY_TRACING=AWS_MEMTRACE_BYTES"); - memTraceLevel = AWS_MEMTRACE_BYTES; - break; - case AWS_MEMTRACE_STACKS: - LOG_DEBUG(Config::TAG, "Set AWS_CRT_MEMORY_TRACING=AWS_MEMTRACE_STACKS"); - memTraceLevel = AWS_MEMTRACE_STACKS; - break; - default: - break; - } - } -} - bool PlainConfig::LoadFromEnvironment() { const char *lockFilePathIn = std::getenv("LOCK_FILE_PATH"); diff --git a/source/config/Config.h b/source/config/Config.h index a030d7cb..5d13b468 100644 --- a/source/config/Config.h +++ b/source/config/Config.h @@ -108,7 +108,6 @@ namespace Aws Aws::Crt::Optional rootCa; Aws::Crt::Optional thingName; - aws_mem_trace_level memTraceLevel{AWS_MEMTRACE_NONE}; std::string lockFilePath{DEFAULT_LOCK_FILE_PATH}; struct LogConfig : public LoadableFromJsonAndCliAndEnvironment @@ -472,7 +471,6 @@ namespace Aws mutable std::vector settings; }; SensorPublish sensorPublish; - void LoadMemTraceLevelFromEnvironment(); }; class Config diff --git a/source/main.cpp b/source/main.cpp index b460938a..09bcc9b0 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -298,10 +298,6 @@ int main(int argc, char *argv[]) resourceManager = std::make_shared(); - config.config.LoadMemTraceLevelFromEnvironment(); - - resourceManager.get()->initializeAllocator(config.config.memTraceLevel); - CliArgs cliArgs; if (!Config::ParseCliArgs(argc, argv, cliArgs) || !config.init(cliArgs)) { From 3897903d06553e09d7fcc9eda9f22511ba9bbb2f Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Tue, 27 Jun 2023 03:00:58 -0700 Subject: [PATCH 11/35] resolved unit test failure --- test/config/TestConfig.cpp | 30 ++---------- test/config/TestConfigSerialization.cpp | 10 ++-- test/devicedefender/TestDeviceDefender.cpp | 5 +- .../TestFleetProvisioning.cpp | 25 +++------- test/jobs/TestJobDocument.cpp | 48 +++++++++---------- test/jobs/TestJobsFeature.cpp | 6 +-- test/shadow/TestConfigShadowFeature.cpp | 22 ++------- test/tunneling/TestSecureTunnelingFeature.cpp | 3 -- test/util/TestSharedResourceManager.cpp | 29 +++++++---- test/util/TestStringUtils.cpp | 26 +++------- 10 files changed, 71 insertions(+), 133 deletions(-) diff --git a/test/config/TestConfig.cpp b/test/config/TestConfig.cpp index 97f4bed4..1a40323d 100644 --- a/test/config/TestConfig.cpp +++ b/test/config/TestConfig.cpp @@ -34,7 +34,7 @@ class ConfigTestFixture : public ::testing::Test public: ConfigTestFixture() = default; string outputPath; - shared_ptr resourceManager; + SharedCrtResourceManager resourceManager; void SetUp() override { @@ -64,11 +64,7 @@ class ConfigTestFixture : public ::testing::Test outputPath = FileUtils::ExtractExpandedPath(outputPathStream.str().c_str()); - resourceManager = std::make_shared(); - - PlainConfig configuration; - configuration.LoadMemTraceLevelFromEnvironment(); - resourceManager.get()->initializeAllocator(configuration.memTraceLevel); +// resourceManager = std::make_shared(); } void TearDown() override @@ -162,6 +158,8 @@ TEST_F(ConfigTestFixture, AllFeaturesEnabled) "secure-element-token-label": "token-label" } })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -1721,23 +1719,3 @@ TEST_F(ConfigTestFixture, HTTPProxyConfigNoAuth) ASSERT_FALSE(httpProxyConfig.httpProxyAuthEnabled); ASSERT_STREQ("None", httpProxyConfig.proxyAuthMethod->c_str()); } - -TEST(Config, MemoryTrace) -{ - PlainConfig config; - - // Test all permutations of memory trace set through the environment. - vector levels{ - AWS_MEMTRACE_NONE, - AWS_MEMTRACE_BYTES, - AWS_MEMTRACE_STACKS, - }; - - for (const auto &level : levels) - { - auto levelstr = std::to_string(level); - ::setenv("AWS_CRT_MEMORY_TRACING", levelstr.c_str(), 1); - config.LoadMemTraceLevelFromEnvironment(); - ASSERT_EQ(config.memTraceLevel, level); - } -} diff --git a/test/config/TestConfigSerialization.cpp b/test/config/TestConfigSerialization.cpp index a92a9b1b..0bc83760 100644 --- a/test/config/TestConfigSerialization.cpp +++ b/test/config/TestConfigSerialization.cpp @@ -23,13 +23,6 @@ using namespace Aws::Iot::DeviceClient::Util; TEST(SerializeConfigTestFixture, SerializeConfigTest) { - shared_ptr resourceManager; - resourceManager = std::make_shared(); - - PlainConfig config; - config.LoadMemTraceLevelFromEnvironment(); - resourceManager.get()->initializeAllocator(config.memTraceLevel); - constexpr char jsonString[] = R"( { "endpoint": "endpoint value", @@ -124,6 +117,9 @@ TEST(SerializeConfigTestFixture, SerializeConfigTest) ] } })"; + SharedCrtResourceManager resourceManager; + PlainConfig config; + JsonObject jsonObject(jsonString); config.LoadFromJson(jsonObject.View()); auto inputJsonString = jsonObject.View().WriteCompact(); diff --git a/test/devicedefender/TestDeviceDefender.cpp b/test/devicedefender/TestDeviceDefender.cpp index 33f3a20c..ab30c3c1 100644 --- a/test/devicedefender/TestDeviceDefender.cpp +++ b/test/devicedefender/TestDeviceDefender.cpp @@ -83,11 +83,8 @@ class TestDeviceDefender : public testing::Test public: void SetUp() { - manager = shared_ptr(new SharedCrtResourceManager()); - config.LoadMemTraceLevelFromEnvironment(); - manager.get()->initializeAllocator(config.memTraceLevel); - deviceDefender = unique_ptr(new MockDDFeature()); + manager = shared_ptr(new SharedCrtResourceManager()); notifier = shared_ptr(new MockNotifier()); task = shared_ptr(new MockReportTask()); config = getSimpleDDConfig(); diff --git a/test/fleetprovisioning/TestFleetProvisioning.cpp b/test/fleetprovisioning/TestFleetProvisioning.cpp index ec917753..d5475737 100644 --- a/test/fleetprovisioning/TestFleetProvisioning.cpp +++ b/test/fleetprovisioning/TestFleetProvisioning.cpp @@ -9,32 +9,18 @@ using namespace std; using namespace Aws::Iot::DeviceClient::FleetProvisioningNS; -class FleetProvisioningFeature : public ::testing::Test -{ - public: - FleetProvisioningFeature() = default; - shared_ptr resourceManager; - - void SetUp() override - { - resourceManager = std::make_shared(); - - Aws::Iot::DeviceClient::PlainConfig configuration; - configuration.LoadMemTraceLevelFromEnvironment(); - resourceManager.get()->initializeAllocator(configuration.memTraceLevel); - } -}; - -TEST_F(FleetProvisioningFeature, EmptyTemplateParameters) +TEST(FleetProvisioning, EmptyTemplateParameters) { + Aws::Iot::DeviceClient::SharedCrtResourceManager resourceManager; Aws::Crt::Optional params; // start with empty value FleetProvisioning fp; ASSERT_TRUE(fp.MapParameters(params)); params = "{}"; // test empty JSON } -TEST_F(FleetProvisioningFeature, MalformedTemplateParameters) +TEST(FleetProvisioning, MalformedTemplateParameters) { + Aws::Iot::DeviceClient::SharedCrtResourceManager resourceManager; Aws::Crt::Optional params("{\"SerialNumber\" \"Device-SN\"}"); // test missing colon FleetProvisioning fp; ASSERT_FALSE(fp.MapParameters(params)); @@ -44,8 +30,9 @@ TEST_F(FleetProvisioningFeature, MalformedTemplateParameters) ASSERT_FALSE(fp.MapParameters(params)); } -TEST_F(FleetProvisioningFeature, ValidTemplateParameters) +TEST(FleetProvisioning, ValidTemplateParameters) { + Aws::Iot::DeviceClient::SharedCrtResourceManager resourceManager; Aws::Crt::Optional params("{\"SerialNumber\": \"Device-SN\"}"); // test single JSON property FleetProvisioning fp; ASSERT_TRUE(fp.MapParameters(params)); diff --git a/test/jobs/TestJobDocument.cpp b/test/jobs/TestJobDocument.cpp index abdd272d..98ca1f35 100644 --- a/test/jobs/TestJobDocument.cpp +++ b/test/jobs/TestJobDocument.cpp @@ -14,22 +14,6 @@ using namespace Aws::Crt; using namespace Aws::Iot::DeviceClient; using namespace Aws::Iot::DeviceClient::Jobs; -class JobDocumentFixture : public ::testing::Test -{ - public: - JobDocumentFixture() = default; - shared_ptr resourceManager; - - void SetUp() override - { - resourceManager = std::make_shared(); - - PlainConfig configuration; - configuration.LoadMemTraceLevelFromEnvironment(); - resourceManager.get()->initializeAllocator(configuration.memTraceLevel); - } -}; - void AssertVectorEqual(const Optional> &vector1, const Optional> &vector2) { ASSERT_TRUE(vector1->size() == vector2->size()); @@ -93,7 +77,7 @@ void AssertStepEqual(const vector &step1, const vec } } -TEST_F(JobDocumentFixture, SampleJobDocument) +TEST(JobDocument, SampleJobDocument) { constexpr char jsonString[] = R"( { @@ -189,6 +173,8 @@ TEST_F(JobDocumentFixture, SampleJobDocument) } })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -310,7 +296,7 @@ TEST_F(JobDocumentFixture, SampleJobDocument) ASSERT_TRUE(jobDocument.finalStep->ignoreStepFailure); } -TEST_F(JobDocumentFixture, MissingRequiredFields) +TEST(JobDocument, MissingRequiredFields) { constexpr char jsonString[] = R"( { @@ -394,6 +380,8 @@ TEST_F(JobDocumentFixture, MissingRequiredFields) } })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -403,7 +391,7 @@ TEST_F(JobDocumentFixture, MissingRequiredFields) ASSERT_FALSE(jobDocument.Validate()); } -TEST_F(JobDocumentFixture, MinimumJobDocument) +TEST(JobDocument, MinimumJobDocument) { constexpr char jsonString[] = R"( { @@ -447,6 +435,8 @@ TEST_F(JobDocumentFixture, MinimumJobDocument) ] })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -456,7 +446,7 @@ TEST_F(JobDocumentFixture, MinimumJobDocument) ASSERT_TRUE(jobDocument.Validate()); } -TEST_F(JobDocumentFixture, MissingRequiredFieldsValue) +TEST(JobDocument, MissingRequiredFieldsValue) { constexpr char jsonString[] = R"( { @@ -540,6 +530,8 @@ TEST_F(JobDocumentFixture, MissingRequiredFieldsValue) } })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -549,7 +541,7 @@ TEST_F(JobDocumentFixture, MissingRequiredFieldsValue) ASSERT_FALSE(jobDocument.Validate()); } -TEST_F(JobDocumentFixture, CommandFieldsIsEmpty) +TEST(JobDocument, CommandFieldsIsEmpty) { constexpr char jsonString[] = R"( { @@ -567,6 +559,8 @@ TEST_F(JobDocumentFixture, CommandFieldsIsEmpty) ] })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -576,7 +570,7 @@ TEST_F(JobDocumentFixture, CommandFieldsIsEmpty) ASSERT_FALSE(jobDocument.Validate()); } -TEST_F(JobDocumentFixture, CommandContainsSpaceCharacters) +TEST(JobDocument, CommandContainsSpaceCharacters) { constexpr char jsonString[] = R"( { @@ -594,6 +588,8 @@ TEST_F(JobDocumentFixture, CommandContainsSpaceCharacters) ] })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -603,7 +599,7 @@ TEST_F(JobDocumentFixture, CommandContainsSpaceCharacters) ASSERT_TRUE(jobDocument.Validate()); } -TEST_F(JobDocumentFixture, SpaceCharactersContainedWithinFirstWordOfCommand) +TEST(JobDocument, SpaceCharactersContainedWithinFirstWordOfCommand) { constexpr char jsonString[] = R"( { @@ -621,6 +617,8 @@ TEST_F(JobDocumentFixture, SpaceCharactersContainedWithinFirstWordOfCommand) ] })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -630,7 +628,7 @@ TEST_F(JobDocumentFixture, SpaceCharactersContainedWithinFirstWordOfCommand) ASSERT_FALSE(jobDocument.Validate()); } -TEST_F(JobDocumentFixture, oldJobDocumentCompatibility) +TEST(JobDocument, oldJobDocumentCompatibility) { constexpr char jsonString[] = R"( { @@ -639,6 +637,8 @@ TEST_F(JobDocumentFixture, oldJobDocumentCompatibility) "path": "default" })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); diff --git a/test/jobs/TestJobsFeature.cpp b/test/jobs/TestJobsFeature.cpp index 6a29fc42..dfb92819 100644 --- a/test/jobs/TestJobsFeature.cpp +++ b/test/jobs/TestJobsFeature.cpp @@ -258,10 +258,6 @@ class TestJobsFeature : public ::testing::Test public: void SetUp() { - resourceManager = std::make_shared(); - config.LoadMemTraceLevelFromEnvironment(); - resourceManager.get()->initializeAllocator(config.memTraceLevel); - ThingName = Aws::Crt::String("thing-name value"); notifier = shared_ptr(new MockNotifier()); config = getSimpleConfig(); @@ -273,7 +269,7 @@ class TestJobsFeature : public ::testing::Test } Aws::Crt::String ThingName; shared_ptr notifier; - shared_ptr resourceManager; + SharedCrtResourceManager resourceManager; PlainConfig config; unique_ptr startNextJobExecutionResponse; unique_ptr jobsMock; diff --git a/test/shadow/TestConfigShadowFeature.cpp b/test/shadow/TestConfigShadowFeature.cpp index b0ab76ae..cc7ccaeb 100644 --- a/test/shadow/TestConfigShadowFeature.cpp +++ b/test/shadow/TestConfigShadowFeature.cpp @@ -13,23 +13,7 @@ using namespace Aws::Iot::DeviceClient::Util; using namespace Aws::Iot::DeviceClient::Shadow; using namespace std; -class ConfigShadowFeature : public ::testing::Test -{ - public: - ConfigShadowFeature() = default; - shared_ptr resourceManager; - - void SetUp() override - { - resourceManager = std::make_shared(); - - Aws::Iot::DeviceClient::PlainConfig configuration; - configuration.LoadMemTraceLevelFromEnvironment(); - resourceManager.get()->initializeAllocator(configuration.memTraceLevel); - } -}; - -TEST_F(ConfigShadowFeature, resetClientConfigWithValidJSON) +TEST(ConfigShadowFeature, resetClientConfigWithValidJSON) { constexpr char oldJsonString[] = R"( { @@ -75,6 +59,7 @@ TEST_F(ConfigShadowFeature, resetClientConfigWithValidJSON) } })"; + SharedCrtResourceManager resourceManager; JsonObject oldJsonObject(oldJsonString); JsonView jsonView = oldJsonObject.View(); PlainConfig config; @@ -120,7 +105,7 @@ TEST_F(ConfigShadowFeature, resetClientConfigWithValidJSON) ASSERT_FALSE(config.jobs.enabled); } -TEST_F(ConfigShadowFeature, resetClientConfigWithInvalidJSON) +TEST(ConfigShadowFeature, resetClientConfigWithInvalidJSON) { constexpr char oldJsonString[] = R"( { @@ -165,6 +150,7 @@ TEST_F(ConfigShadowFeature, resetClientConfigWithInvalidJSON) "shadow-output-file": "" } })"; + SharedCrtResourceManager resourceManager; JsonObject oldJsonObject(oldJsonString); JsonView jsonView = oldJsonObject.View(); diff --git a/test/tunneling/TestSecureTunnelingFeature.cpp b/test/tunneling/TestSecureTunnelingFeature.cpp index 441f3327..4535d0d2 100644 --- a/test/tunneling/TestSecureTunnelingFeature.cpp +++ b/test/tunneling/TestSecureTunnelingFeature.cpp @@ -104,9 +104,6 @@ class TestSecureTunnelingFeature : public testing::Test void SetUp() override { manager = shared_ptr(new SharedCrtResourceManager()); - config.LoadMemTraceLevelFromEnvironment(); - manager.get()->initializeAllocator(config.memTraceLevel); - thingName = Aws::Crt::String("thing-name value"); secureTunnelingFeature = shared_ptr(new MockSecureTunnelingFeature()); mockClient = shared_ptr(new MockIotSecureTunnelingClient()); diff --git a/test/util/TestSharedResourceManager.cpp b/test/util/TestSharedResourceManager.cpp index e63937a1..a1ad9939 100644 --- a/test/util/TestSharedResourceManager.cpp +++ b/test/util/TestSharedResourceManager.cpp @@ -3,7 +3,6 @@ #include "../../source/SharedCrtResourceManager.h" #include "../../source/util/FileUtils.h" - #include "gtest/gtest.h" #include @@ -55,7 +54,6 @@ class SharedResourceManagerTest : public ::testing::Test { public: SharedResourceManagerTest() = default; - shared_ptr resourceManager; void SetUp() override { @@ -84,12 +82,6 @@ class SharedResourceManagerTest : public ::testing::Test // Ensure invalid files do not exist std::remove(invalidCertFilePath.c_str()); std::remove(invalidKeyFilePath.c_str()); - - resourceManager = std::make_shared(); - - PlainConfig configuration; - configuration.LoadMemTraceLevelFromEnvironment(); - resourceManager.get()->initializeAllocator(configuration.memTraceLevel); } void TearDown() override @@ -159,3 +151,24 @@ TEST_F(SharedResourceManagerTest, badPermissionsDirectory) ASSERT_FALSE(manager.locateCredentialsWrapper(config)); } + +//TEST(SharedResourceManagerTest, MemoryTrace) +//{ +// shared_ptr resourceManager; +// resourceManager = std::make_shared(); +// +// // Test all permutations of memory trace set through the environment. +// vector levels{ +// AWS_MEMTRACE_NONE, +// AWS_MEMTRACE_BYTES, +// AWS_MEMTRACE_STACKS, +// }; +// +// for (const auto &level : levels) +// { +// auto levelstr = std::to_string(level); +// ::setenv("AWS_CRT_MEMORY_TRACING", levelstr.c_str(), 1); +// ASSERT_TRUE(config.LoadFromEnvironment()) << "read AWS_CRT_MEMORY_TRACING=" << level; +// ASSERT_EQ(config.memTraceLevel, level); +// } +//} \ No newline at end of file diff --git a/test/util/TestStringUtils.cpp b/test/util/TestStringUtils.cpp index 08c1aa08..9ee59e0b 100644 --- a/test/util/TestStringUtils.cpp +++ b/test/util/TestStringUtils.cpp @@ -14,22 +14,6 @@ using namespace Aws::Iot::DeviceClient::Util; constexpr size_t Config::MAX_CONFIG_SIZE; -class StringUtilsFixture : public ::testing::Test -{ - public: - StringUtilsFixture() = default; - shared_ptr resourceManager; - - void SetUp() override - { - resourceManager = std::make_shared(); - - PlainConfig configuration; - configuration.LoadMemTraceLevelFromEnvironment(); - resourceManager.get()->initializeAllocator(configuration.memTraceLevel); - } -}; - TEST(StringUtils, FormatStringNoArg) { constexpr char expected[] = "Hello world"; @@ -81,8 +65,10 @@ TEST(StringUtils, leavesNewLineAndTabAlone) ASSERT_STREQ(original.c_str(), Sanitize(original).c_str()); } -TEST_F(StringUtilsFixture, maptoString) +TEST(StringUtils, maptoString) { + SharedCrtResourceManager resourceManager; + Aws::Crt::Map map; map.insert(std::pair("a", "b")); map.insert(std::pair("c", "d")); @@ -93,7 +79,7 @@ TEST_F(StringUtilsFixture, maptoString) ASSERT_STREQ(expected.c_str(), MapToString(map).c_str()); } -TEST_F(StringUtilsFixture, emptyMaptoString) +TEST(StringUtils, emptyMaptoString) { Aws::Crt::Map map; string expected = ""; @@ -142,12 +128,14 @@ TEST(StringUtils, trimMultiChar) ASSERT_EQ("", TrimCopy("", "/")); // Empty string. } -TEST_F(StringUtilsFixture, ParseToStringVector) +TEST(StringUtils, ParseToStringVector) { constexpr char jsonString[] = R"( { "args": ["hello", "world"] })"; + SharedCrtResourceManager resourceManager; + JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); From 8e1ce2c6b29a328bfefa56545affa991cb7aaf70 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Tue, 27 Jun 2023 03:05:22 -0700 Subject: [PATCH 12/35] remove MemoryTrace unit test --- test/util/TestSharedResourceManager.cpp | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/test/util/TestSharedResourceManager.cpp b/test/util/TestSharedResourceManager.cpp index a1ad9939..156da1ef 100644 --- a/test/util/TestSharedResourceManager.cpp +++ b/test/util/TestSharedResourceManager.cpp @@ -150,25 +150,4 @@ TEST_F(SharedResourceManagerTest, badPermissionsDirectory) chmod(certDir.c_str(), 0777); ASSERT_FALSE(manager.locateCredentialsWrapper(config)); -} - -//TEST(SharedResourceManagerTest, MemoryTrace) -//{ -// shared_ptr resourceManager; -// resourceManager = std::make_shared(); -// -// // Test all permutations of memory trace set through the environment. -// vector levels{ -// AWS_MEMTRACE_NONE, -// AWS_MEMTRACE_BYTES, -// AWS_MEMTRACE_STACKS, -// }; -// -// for (const auto &level : levels) -// { -// auto levelstr = std::to_string(level); -// ::setenv("AWS_CRT_MEMORY_TRACING", levelstr.c_str(), 1); -// ASSERT_TRUE(config.LoadFromEnvironment()) << "read AWS_CRT_MEMORY_TRACING=" << level; -// ASSERT_EQ(config.memTraceLevel, level); -// } -//} \ No newline at end of file +} \ No newline at end of file From 4d4a60d572657a273952b42794351b65dde64cc9 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Tue, 27 Jun 2023 03:08:35 -0700 Subject: [PATCH 13/35] resolved Lint errors --- test/config/TestConfig.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/config/TestConfig.cpp b/test/config/TestConfig.cpp index 1a40323d..5e1b23d5 100644 --- a/test/config/TestConfig.cpp +++ b/test/config/TestConfig.cpp @@ -63,8 +63,6 @@ class ConfigTestFixture : public ::testing::Test outputPathStream << Config::DEFAULT_SAMPLE_SHADOW_OUTPUT_DIR << Config::DEFAULT_SAMPLE_SHADOW_DOCUMENT_FILE; outputPath = FileUtils::ExtractExpandedPath(outputPathStream.str().c_str()); - -// resourceManager = std::make_shared(); } void TearDown() override From 6eb723b9d09012c50782ec9bda905349217875a3 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Wed, 5 Jul 2023 13:43:14 -0700 Subject: [PATCH 14/35] explicitly calling the code in SharedCrtManager to initialize allocator and api handle. This will resolve the Sensor pullish unit tests which manually creates and initializes allocator and eventloop. --- source/SharedCrtResourceManager.h | 2 +- source/main.cpp | 1 + test/config/TestConfig.cpp | 4 +++- test/config/TestConfigSerialization.cpp | 2 ++ test/devicedefender/TestDeviceDefender.cpp | 3 +++ test/fleetprovisioning/TestFleetProvisioning.cpp | 6 ++++++ test/jobs/TestJobDocument.cpp | 16 ++++++++++++++++ test/jobs/TestJobsFeature.cpp | 3 +++ test/shadow/TestConfigShadowFeature.cpp | 5 +++++ test/tunneling/TestSecureTunnelingFeature.cpp | 3 +++ test/util/TestSharedResourceManager.cpp | 3 +++ test/util/TestStringUtils.cpp | 4 ++++ 12 files changed, 50 insertions(+), 2 deletions(-) diff --git a/source/SharedCrtResourceManager.h b/source/SharedCrtResourceManager.h index f791f7ed..00298497 100644 --- a/source/SharedCrtResourceManager.h +++ b/source/SharedCrtResourceManager.h @@ -58,7 +58,7 @@ namespace Aws bool locateCredentials(const PlainConfig &config) const; public: - SharedCrtResourceManager() { initializeAllocator(); } + SharedCrtResourceManager() { } virtual ~SharedCrtResourceManager(); diff --git a/source/main.cpp b/source/main.cpp index 09bcc9b0..9fc8cecd 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -297,6 +297,7 @@ int main(int argc, char *argv[]) } resourceManager = std::make_shared(); + resourceManager->initializeAllocator(); CliArgs cliArgs; if (!Config::ParseCliArgs(argc, argv, cliArgs) || !config.init(cliArgs)) diff --git a/test/config/TestConfig.cpp b/test/config/TestConfig.cpp index 5e1b23d5..0822bb32 100644 --- a/test/config/TestConfig.cpp +++ b/test/config/TestConfig.cpp @@ -38,6 +38,9 @@ class ConfigTestFixture : public ::testing::Test void SetUp() override { + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + resourceManager.initializeAllocator(); + // Config::Validate will check that cert, key, and root-ca files exist. // Create a temporary file to use as a placeholder for this purpose. ofstream file(filePath, std::fstream::app); @@ -156,7 +159,6 @@ TEST_F(ConfigTestFixture, AllFeaturesEnabled) "secure-element-token-label": "token-label" } })"; - SharedCrtResourceManager resourceManager; JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); diff --git a/test/config/TestConfigSerialization.cpp b/test/config/TestConfigSerialization.cpp index 0bc83760..cae70d8d 100644 --- a/test/config/TestConfigSerialization.cpp +++ b/test/config/TestConfigSerialization.cpp @@ -117,7 +117,9 @@ TEST(SerializeConfigTestFixture, SerializeConfigTest) ] } })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); PlainConfig config; JsonObject jsonObject(jsonString); diff --git a/test/devicedefender/TestDeviceDefender.cpp b/test/devicedefender/TestDeviceDefender.cpp index ab30c3c1..11cfcea8 100644 --- a/test/devicedefender/TestDeviceDefender.cpp +++ b/test/devicedefender/TestDeviceDefender.cpp @@ -85,6 +85,9 @@ class TestDeviceDefender : public testing::Test { deviceDefender = unique_ptr(new MockDDFeature()); manager = shared_ptr(new SharedCrtResourceManager()); + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + manager->initializeAllocator(); + notifier = shared_ptr(new MockNotifier()); task = shared_ptr(new MockReportTask()); config = getSimpleDDConfig(); diff --git a/test/fleetprovisioning/TestFleetProvisioning.cpp b/test/fleetprovisioning/TestFleetProvisioning.cpp index d5475737..34ba6e63 100644 --- a/test/fleetprovisioning/TestFleetProvisioning.cpp +++ b/test/fleetprovisioning/TestFleetProvisioning.cpp @@ -21,6 +21,9 @@ TEST(FleetProvisioning, EmptyTemplateParameters) TEST(FleetProvisioning, MalformedTemplateParameters) { Aws::Iot::DeviceClient::SharedCrtResourceManager resourceManager; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + resourceManager.initializeAllocator(); + Aws::Crt::Optional params("{\"SerialNumber\" \"Device-SN\"}"); // test missing colon FleetProvisioning fp; ASSERT_FALSE(fp.MapParameters(params)); @@ -33,6 +36,9 @@ TEST(FleetProvisioning, MalformedTemplateParameters) TEST(FleetProvisioning, ValidTemplateParameters) { Aws::Iot::DeviceClient::SharedCrtResourceManager resourceManager; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + resourceManager.initializeAllocator(); + Aws::Crt::Optional params("{\"SerialNumber\": \"Device-SN\"}"); // test single JSON property FleetProvisioning fp; ASSERT_TRUE(fp.MapParameters(params)); diff --git a/test/jobs/TestJobDocument.cpp b/test/jobs/TestJobDocument.cpp index 98ca1f35..d6855f49 100644 --- a/test/jobs/TestJobDocument.cpp +++ b/test/jobs/TestJobDocument.cpp @@ -173,7 +173,9 @@ TEST(JobDocument, SampleJobDocument) } })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -380,7 +382,9 @@ TEST(JobDocument, MissingRequiredFields) } })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -435,7 +439,9 @@ TEST(JobDocument, MinimumJobDocument) ] })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -530,7 +536,9 @@ TEST(JobDocument, MissingRequiredFieldsValue) } })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -559,7 +567,9 @@ TEST(JobDocument, CommandFieldsIsEmpty) ] })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -588,7 +598,9 @@ TEST(JobDocument, CommandContainsSpaceCharacters) ] })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -617,7 +629,9 @@ TEST(JobDocument, SpaceCharactersContainedWithinFirstWordOfCommand) ] })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); @@ -637,7 +651,9 @@ TEST(JobDocument, oldJobDocumentCompatibility) "path": "default" })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); diff --git a/test/jobs/TestJobsFeature.cpp b/test/jobs/TestJobsFeature.cpp index dfb92819..fb489549 100644 --- a/test/jobs/TestJobsFeature.cpp +++ b/test/jobs/TestJobsFeature.cpp @@ -258,6 +258,9 @@ class TestJobsFeature : public ::testing::Test public: void SetUp() { + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + resourceManager.initializeAllocator(); + ThingName = Aws::Crt::String("thing-name value"); notifier = shared_ptr(new MockNotifier()); config = getSimpleConfig(); diff --git a/test/shadow/TestConfigShadowFeature.cpp b/test/shadow/TestConfigShadowFeature.cpp index cc7ccaeb..c936daea 100644 --- a/test/shadow/TestConfigShadowFeature.cpp +++ b/test/shadow/TestConfigShadowFeature.cpp @@ -60,6 +60,9 @@ TEST(ConfigShadowFeature, resetClientConfigWithValidJSON) })"; SharedCrtResourceManager resourceManager; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + resourceManager.initializeAllocator(); + JsonObject oldJsonObject(oldJsonString); JsonView jsonView = oldJsonObject.View(); PlainConfig config; @@ -151,6 +154,8 @@ TEST(ConfigShadowFeature, resetClientConfigWithInvalidJSON) } })"; SharedCrtResourceManager resourceManager; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + resourceManager.initializeAllocator(); JsonObject oldJsonObject(oldJsonString); JsonView jsonView = oldJsonObject.View(); diff --git a/test/tunneling/TestSecureTunnelingFeature.cpp b/test/tunneling/TestSecureTunnelingFeature.cpp index 4535d0d2..e6eec948 100644 --- a/test/tunneling/TestSecureTunnelingFeature.cpp +++ b/test/tunneling/TestSecureTunnelingFeature.cpp @@ -104,6 +104,9 @@ class TestSecureTunnelingFeature : public testing::Test void SetUp() override { manager = shared_ptr(new SharedCrtResourceManager()); + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + manager->initializeAllocator(); + thingName = Aws::Crt::String("thing-name value"); secureTunnelingFeature = shared_ptr(new MockSecureTunnelingFeature()); mockClient = shared_ptr(new MockIotSecureTunnelingClient()); diff --git a/test/util/TestSharedResourceManager.cpp b/test/util/TestSharedResourceManager.cpp index 156da1ef..c9403f7b 100644 --- a/test/util/TestSharedResourceManager.cpp +++ b/test/util/TestSharedResourceManager.cpp @@ -57,6 +57,9 @@ class SharedResourceManagerTest : public ::testing::Test void SetUp() override { + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + manager.initializeAllocator(); + // SharedCrtResourceManager::locateCredentials will check that cert, key files // have valid permissions. Create a temporary file to use as a placeholder for this purpose. FileUtils::CreateDirectoryWithPermissions(certDir.c_str(), 0700); diff --git a/test/util/TestStringUtils.cpp b/test/util/TestStringUtils.cpp index 9ee59e0b..ac5007c9 100644 --- a/test/util/TestStringUtils.cpp +++ b/test/util/TestStringUtils.cpp @@ -67,7 +67,9 @@ TEST(StringUtils, leavesNewLineAndTabAlone) TEST(StringUtils, maptoString) { + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); Aws::Crt::Map map; map.insert(std::pair("a", "b")); @@ -134,7 +136,9 @@ TEST(StringUtils, ParseToStringVector) { "args": ["hello", "world"] })"; + //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); JsonObject jsonObject(jsonString); JsonView jsonView = jsonObject.View(); From 926147bf027b81373d1e0eda8ed459170301612b Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Wed, 5 Jul 2023 13:50:35 -0700 Subject: [PATCH 15/35] addressed lint errors --- test/config/TestConfig.cpp | 2 +- test/config/TestConfigSerialization.cpp | 2 +- test/devicedefender/TestDeviceDefender.cpp | 2 +- test/fleetprovisioning/TestFleetProvisioning.cpp | 4 ++-- test/jobs/TestJobDocument.cpp | 16 ++++++++-------- test/jobs/TestJobsFeature.cpp | 2 +- test/shadow/TestConfigShadowFeature.cpp | 4 ++-- test/tunneling/TestSecureTunnelingFeature.cpp | 2 +- test/util/TestSharedResourceManager.cpp | 2 +- test/util/TestStringUtils.cpp | 4 ++-- 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/test/config/TestConfig.cpp b/test/config/TestConfig.cpp index 0822bb32..8b8d5c81 100644 --- a/test/config/TestConfig.cpp +++ b/test/config/TestConfig.cpp @@ -38,7 +38,7 @@ class ConfigTestFixture : public ::testing::Test void SetUp() override { - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. resourceManager.initializeAllocator(); // Config::Validate will check that cert, key, and root-ca files exist. diff --git a/test/config/TestConfigSerialization.cpp b/test/config/TestConfigSerialization.cpp index cae70d8d..b9f4f08f 100644 --- a/test/config/TestConfigSerialization.cpp +++ b/test/config/TestConfigSerialization.cpp @@ -117,7 +117,7 @@ TEST(SerializeConfigTestFixture, SerializeConfigTest) ] } })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); PlainConfig config; diff --git a/test/devicedefender/TestDeviceDefender.cpp b/test/devicedefender/TestDeviceDefender.cpp index 11cfcea8..e81d1086 100644 --- a/test/devicedefender/TestDeviceDefender.cpp +++ b/test/devicedefender/TestDeviceDefender.cpp @@ -85,7 +85,7 @@ class TestDeviceDefender : public testing::Test { deviceDefender = unique_ptr(new MockDDFeature()); manager = shared_ptr(new SharedCrtResourceManager()); - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. manager->initializeAllocator(); notifier = shared_ptr(new MockNotifier()); diff --git a/test/fleetprovisioning/TestFleetProvisioning.cpp b/test/fleetprovisioning/TestFleetProvisioning.cpp index 34ba6e63..74f7033c 100644 --- a/test/fleetprovisioning/TestFleetProvisioning.cpp +++ b/test/fleetprovisioning/TestFleetProvisioning.cpp @@ -21,7 +21,7 @@ TEST(FleetProvisioning, EmptyTemplateParameters) TEST(FleetProvisioning, MalformedTemplateParameters) { Aws::Iot::DeviceClient::SharedCrtResourceManager resourceManager; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. resourceManager.initializeAllocator(); Aws::Crt::Optional params("{\"SerialNumber\" \"Device-SN\"}"); // test missing colon @@ -36,7 +36,7 @@ TEST(FleetProvisioning, MalformedTemplateParameters) TEST(FleetProvisioning, ValidTemplateParameters) { Aws::Iot::DeviceClient::SharedCrtResourceManager resourceManager; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. resourceManager.initializeAllocator(); Aws::Crt::Optional params("{\"SerialNumber\": \"Device-SN\"}"); // test single JSON property diff --git a/test/jobs/TestJobDocument.cpp b/test/jobs/TestJobDocument.cpp index d6855f49..4f12353d 100644 --- a/test/jobs/TestJobDocument.cpp +++ b/test/jobs/TestJobDocument.cpp @@ -173,7 +173,7 @@ TEST(JobDocument, SampleJobDocument) } })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); @@ -382,7 +382,7 @@ TEST(JobDocument, MissingRequiredFields) } })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); @@ -439,7 +439,7 @@ TEST(JobDocument, MinimumJobDocument) ] })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); @@ -536,7 +536,7 @@ TEST(JobDocument, MissingRequiredFieldsValue) } })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); @@ -567,7 +567,7 @@ TEST(JobDocument, CommandFieldsIsEmpty) ] })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); @@ -598,7 +598,7 @@ TEST(JobDocument, CommandContainsSpaceCharacters) ] })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); @@ -629,7 +629,7 @@ TEST(JobDocument, SpaceCharactersContainedWithinFirstWordOfCommand) ] })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); @@ -651,7 +651,7 @@ TEST(JobDocument, oldJobDocumentCompatibility) "path": "default" })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); diff --git a/test/jobs/TestJobsFeature.cpp b/test/jobs/TestJobsFeature.cpp index fb489549..38bdc95e 100644 --- a/test/jobs/TestJobsFeature.cpp +++ b/test/jobs/TestJobsFeature.cpp @@ -258,7 +258,7 @@ class TestJobsFeature : public ::testing::Test public: void SetUp() { - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. resourceManager.initializeAllocator(); ThingName = Aws::Crt::String("thing-name value"); diff --git a/test/shadow/TestConfigShadowFeature.cpp b/test/shadow/TestConfigShadowFeature.cpp index c936daea..740f05d5 100644 --- a/test/shadow/TestConfigShadowFeature.cpp +++ b/test/shadow/TestConfigShadowFeature.cpp @@ -60,7 +60,7 @@ TEST(ConfigShadowFeature, resetClientConfigWithValidJSON) })"; SharedCrtResourceManager resourceManager; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. resourceManager.initializeAllocator(); JsonObject oldJsonObject(oldJsonString); @@ -154,7 +154,7 @@ TEST(ConfigShadowFeature, resetClientConfigWithInvalidJSON) } })"; SharedCrtResourceManager resourceManager; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. resourceManager.initializeAllocator(); JsonObject oldJsonObject(oldJsonString); diff --git a/test/tunneling/TestSecureTunnelingFeature.cpp b/test/tunneling/TestSecureTunnelingFeature.cpp index e6eec948..6aeaf880 100644 --- a/test/tunneling/TestSecureTunnelingFeature.cpp +++ b/test/tunneling/TestSecureTunnelingFeature.cpp @@ -104,7 +104,7 @@ class TestSecureTunnelingFeature : public testing::Test void SetUp() override { manager = shared_ptr(new SharedCrtResourceManager()); - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. manager->initializeAllocator(); thingName = Aws::Crt::String("thing-name value"); diff --git a/test/util/TestSharedResourceManager.cpp b/test/util/TestSharedResourceManager.cpp index c9403f7b..a5d3215b 100644 --- a/test/util/TestSharedResourceManager.cpp +++ b/test/util/TestSharedResourceManager.cpp @@ -57,7 +57,7 @@ class SharedResourceManagerTest : public ::testing::Test void SetUp() override { - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. manager.initializeAllocator(); // SharedCrtResourceManager::locateCredentials will check that cert, key files diff --git a/test/util/TestStringUtils.cpp b/test/util/TestStringUtils.cpp index ac5007c9..ca6a70a8 100644 --- a/test/util/TestStringUtils.cpp +++ b/test/util/TestStringUtils.cpp @@ -67,7 +67,7 @@ TEST(StringUtils, leavesNewLineAndTabAlone) TEST(StringUtils, maptoString) { - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); @@ -136,7 +136,7 @@ TEST(StringUtils, ParseToStringVector) { "args": ["hello", "world"] })"; - //Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. SharedCrtResourceManager resourceManager; resourceManager.initializeAllocator(); From f9c50fefca44630508d5233f7bc94210b2ebca04 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Wed, 5 Jul 2023 15:27:34 -0700 Subject: [PATCH 16/35] addressed lint errors --- source/SharedCrtResourceManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/SharedCrtResourceManager.h b/source/SharedCrtResourceManager.h index 00298497..4b1f1df0 100644 --- a/source/SharedCrtResourceManager.h +++ b/source/SharedCrtResourceManager.h @@ -58,7 +58,7 @@ namespace Aws bool locateCredentials(const PlainConfig &config) const; public: - SharedCrtResourceManager() { } + SharedCrtResourceManager() {} virtual ~SharedCrtResourceManager(); From 0597270952b674a8b5c9e0b54b03ba3b13811c31 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Wed, 5 Jul 2023 17:25:24 -0700 Subject: [PATCH 17/35] addressed reviewers comment and added the config serialization test back --- test/config/TestConfigSerialization.cpp | 64 ++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/test/config/TestConfigSerialization.cpp b/test/config/TestConfigSerialization.cpp index b9f4f08f..c8e6b4fd 100644 --- a/test/config/TestConfigSerialization.cpp +++ b/test/config/TestConfigSerialization.cpp @@ -21,7 +21,7 @@ using namespace Aws::Crt; using namespace Aws::Iot::DeviceClient; using namespace Aws::Iot::DeviceClient::Util; -TEST(SerializeConfigTestFixture, SerializeConfigTest) +TEST(SerializeConfigTestFixture, SerializeCompleteConfigTest) { constexpr char jsonString[] = R"( { @@ -39,7 +39,7 @@ TEST(SerializeConfigTestFixture, SerializeConfigTest) }, "jobs": { "enabled": true, - "handler-directory": "directory" + "handler-directory": "directory" }, "tunneling": { "enabled": true @@ -130,5 +130,65 @@ TEST(SerializeConfigTestFixture, SerializeConfigTest) config.SerializeToObject(serializedConfig); auto serializedJsonString = serializedConfig.View().WriteCompact(); + ASSERT_STREQ(inputJsonString.c_str(), serializedJsonString.c_str()); +} + +TEST(SerializeConfigTestFixture, SerializeBasicConfigTest) +{ + constexpr char jsonString[] = R"( +{ + "logging": { + "level": "DEBUG", + "type": "file", + "file": "./aws-iot-device-client.log", + "enable-sdk-logging": false, + "sdk-log-level": "TRACE", + "sdk-log-file": "/var/log/aws-iot-device-client/sdk.log" + }, + "jobs": { + "enabled": true, + "handler-directory": "" + }, + "tunneling": { + "enabled": true + }, + "device-defender": { + "enabled": true, + "interval": 300 + }, + "fleet-provisioning": { + "enabled": true + }, + "runtime-config": { + "completed-fp": false + }, + "samples": { + "pub-sub": { + "enabled": true + } + }, + "config-shadow": { + "enabled": true + }, + "sample-shadow": { + "enabled": true + }, + "secure-element": { + "enabled": true + } +})"; + // Initializing allocator, so we can use CJSON lib from SDK in our unit tests. + SharedCrtResourceManager resourceManager; + resourceManager.initializeAllocator(); + PlainConfig config; + + JsonObject jsonObject(jsonString); + config.LoadFromJson(jsonObject.View()); + auto inputJsonString = jsonObject.View().WriteCompact(); + + JsonObject serializedConfig; + config.SerializeToObject(serializedConfig); + auto serializedJsonString = serializedConfig.View().WriteCompact(); + ASSERT_STREQ(inputJsonString.c_str(), serializedJsonString.c_str()); } \ No newline at end of file From 676882cd5c24062bc9a5db8b70eafeb03113af77 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi <63674702+HarshGandhi-AWS@users.noreply.github.com> Date: Wed, 12 Jul 2023 18:25:49 -0700 Subject: [PATCH 18/35] Update GTestMain.cpp to not cleanup resources after testing is completed This change will be reverted later. Added this to investigate DD integ test failure --- integration-tests/source/GTestMain.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration-tests/source/GTestMain.cpp b/integration-tests/source/GTestMain.cpp index ac4709ab..a67f6f51 100644 --- a/integration-tests/source/GTestMain.cpp +++ b/integration-tests/source/GTestMain.cpp @@ -104,7 +104,7 @@ bool parseCliArgs(int argc, char **argv) } else if (currentArg == CLI_CLEAN_UP) { - CLEAN_UP = true; + CLEAN_UP = false; } else { @@ -185,4 +185,4 @@ int main(int argc, char **argv) printf("Tests Complete!\n"); Aws::ShutdownAPI(options); return rc; -} \ No newline at end of file +} From f357df681a33510956ad782b3ac536d341882c13 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi <63674702+HarshGandhi-AWS@users.noreply.github.com> Date: Thu, 13 Jul 2023 18:30:21 -0700 Subject: [PATCH 19/35] Update e2e-tests-ubutu-x86 test to skip secure tunneling test --- .github/workflows/e2e-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/e2e-ci.yml b/.github/workflows/e2e-ci.yml index ad937655..d7467e9c 100644 --- a/.github/workflows/e2e-ci.yml +++ b/.github/workflows/e2e-ci.yml @@ -370,7 +370,7 @@ jobs: DEVICE_KEY_SECRET: ${{ secrets.FP_DEVICE_KEY_SECRET }} AMAZON_ROOT_CA: ${{ secrets.AMAZON_ROOT_CA }} run: | - docker run -e AWS_ACCESS_KEY_ID="$(echo ${{ secrets.INTEG_USER_KEY_ID }})" -e AWS_SECRET_ACCESS_KEY="$(echo ${{ secrets.INTEG_USER_KEY_SECRET }})" -e IOT_ENDPOINT="$(echo $IOT_ENDPOINT)" -e CERTIFICATE="$(echo $CERTIFICATE)" -e DEVICE_KEY_SECRET="$(echo $DEVICE_KEY_SECRET)" -e AMAZON_ROOT_CA="$(echo $AMAZON_ROOT_CA)" -e THING_NAME=fleetprovisioning ${{ steps.build-test-runner.outputs.imageid }} --clean-up + docker run -e AWS_ACCESS_KEY_ID="$(echo ${{ secrets.INTEG_USER_KEY_ID }})" -e AWS_SECRET_ACCESS_KEY="$(echo ${{ secrets.INTEG_USER_KEY_SECRET }})" -e IOT_ENDPOINT="$(echo $IOT_ENDPOINT)" -e CERTIFICATE="$(echo $CERTIFICATE)" -e DEVICE_KEY_SECRET="$(echo $DEVICE_KEY_SECRET)" -e AMAZON_ROOT_CA="$(echo $AMAZON_ROOT_CA)" -e THING_NAME=fleetprovisioning ${{ steps.build-test-runner.outputs.imageid }} --skip-st --clean-up e2e-tests-ubuntu-aarch64: runs-on: ubuntu-latest if: ${{ false }} # Disabled for now. aarch64 local proxy build takes too long From 95db8c94d96758f974e88716cc8b2e2bd849a6d2 Mon Sep 17 00:00:00 2001 From: marco morais <93558171+marcoemorais-aws@users.noreply.github.com> Date: Wed, 2 Aug 2023 17:02:16 -0700 Subject: [PATCH 20/35] Compile device client binaries as Release build type (#422) --- .github/docker-images/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/docker-images/Dockerfile b/.github/docker-images/Dockerfile index 754b7d14..bd9a1dce 100644 --- a/.github/docker-images/Dockerfile +++ b/.github/docker-images/Dockerfile @@ -9,7 +9,7 @@ FROM ${BASE_IMAGE} AS deploy COPY . /root/aws-iot-device-client RUN mkdir -p /root/aws-iot-device-client/build \ && cd /root/aws-iot-device-client/build \ - && cmake .. \ + && cmake -DCMAKE_BUILD_TYPE=Release .. \ && cmake --build . --target aws-iot-device-client ENTRYPOINT ["/root/aws-iot-device-client/build/aws-iot-device-client"] From f50db7051f2dc4c6ab49130796a7c81dd5ec3664 Mon Sep 17 00:00:00 2001 From: RogerZhongAWS Date: Mon, 7 Aug 2023 23:10:28 +0000 Subject: [PATCH 21/35] update sdk commit again --- CMakeLists.txt.awssdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt.awssdk b/CMakeLists.txt.awssdk index 00c05bfb..01b27310 100644 --- a/CMakeLists.txt.awssdk +++ b/CMakeLists.txt.awssdk @@ -5,7 +5,7 @@ project(aws-iot-device-sdk-cpp-v2-download NONE) include(ExternalProject) ExternalProject_Add(aws-iot-device-sdk-cpp-v2 GIT_REPOSITORY https://github.com/aws/aws-iot-device-sdk-cpp-v2.git - GIT_TAG b12656085b9bf1ecff1a0b8a4c7d496acc3f684f + GIT_TAG e065cff69ca9b52644347efdaf1e671eaec63c9c SOURCE_DIR "${CMAKE_BINARY_DIR}/aws-iot-device-sdk-cpp-v2-src" BINARY_DIR "${CMAKE_BINARY_DIR}/aws-iot-device-sdk-cpp-v2-build" CONFIGURE_COMMAND "" From 129f6629c1de57f7873bfba9818dc201c30bd1f3 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi <63674702+HarshGandhi-AWS@users.noreply.github.com> Date: Fri, 29 Sep 2023 11:01:47 -0700 Subject: [PATCH 22/35] Updated code to shutdown on tunnel close callback and updated zlib dependency version(#426) * updated code to shutdown on tunnel close callback * addressed lint errors * addressed lint errors * addressed lint errors * updated zlib version * updated zlib version * updated comment --- .github/docker-images/integration-tests/ubuntu/Dockerfile | 6 +++--- source/main.cpp | 4 ++++ source/tunneling/SecureTunnelingFeature.cpp | 5 ++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/docker-images/integration-tests/ubuntu/Dockerfile b/.github/docker-images/integration-tests/ubuntu/Dockerfile index 85c2e789..23fc2538 100644 --- a/.github/docker-images/integration-tests/ubuntu/Dockerfile +++ b/.github/docker-images/integration-tests/ubuntu/Dockerfile @@ -14,9 +14,9 @@ RUN apt update && apt upgrade -y && \ RUN mkdir /home/dependencies WORKDIR /home/dependencies -RUN wget https://www.zlib.net/zlib-1.2.13.tar.gz -O /tmp/zlib-1.2.13.tar.gz && \ - tar xzvf /tmp/zlib-1.2.13.tar.gz && \ - cd zlib-1.2.13 && \ +RUN wget https://www.zlib.net/zlib-1.3.tar.gz -O /tmp/zlib-1.3.tar.gz && \ + tar xzvf /tmp/zlib-1.3.tar.gz && \ + cd zlib-1.3 && \ ./configure && \ make && \ make install && \ diff --git a/source/main.cpp b/source/main.cpp index 25b736c6..59c085c7 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -237,6 +237,10 @@ namespace Aws case ClientBaseEventNotification::FEATURE_STOPPED: { LOGM_INFO(TAG, "%s has stopped", feature->getName().c_str()); +// Stopping DC instance if secure tunnel is closed for a ST component +#if defined(DISABLE_MQTT) + shutdown(); +#endif break; } default: diff --git a/source/tunneling/SecureTunnelingFeature.cpp b/source/tunneling/SecureTunnelingFeature.cpp index 8aef2341..dbbdbf3c 100644 --- a/source/tunneling/SecureTunnelingFeature.cpp +++ b/source/tunneling/SecureTunnelingFeature.cpp @@ -272,12 +272,15 @@ namespace Aws void SecureTunnelingFeature::OnConnectionShutdown(SecureTunnelingContext *contextToRemove) { LOG_DEBUG(TAG, "SecureTunnelingFeature::OnConnectionShutdown"); - +#if defined(DISABLE_MQTT) + this->stop(); +#else auto it = find_if(mContexts.begin(), mContexts.end(), [&](const unique_ptr &c) { return c.get() == contextToRemove; }); mContexts.erase(std::remove(mContexts.begin(), mContexts.end(), *it)); +#endif } } // namespace SecureTunneling From d0e773dd9eca229467f59ff4c1cd69d0c3efe957 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi <63674702+HarshGandhi-AWS@users.noreply.github.com> Date: Tue, 3 Oct 2023 10:08:57 -0700 Subject: [PATCH 23/35] Updated Secure Tunneling close logic for Secure Tunneling Component (#427) * Updated tunnel shutdown logic for Secure Tunneling component * removed extra log statement --- source/main.cpp | 70 ++++++++++++--------- source/tunneling/SecureTunnelingFeature.cpp | 8 ++- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 59c085c7..8d808048 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -145,16 +145,19 @@ void shutdown() LOG_DEBUG(TAG, "Calling stop all"); features->stopAll(); } - resourceManager->dumpMemTrace(); LOG_INFO(TAG, "All features have stopped"); // terminate program #if !defined(DISABLE_MQTT) - resourceManager->disconnect(); + if (resourceManager != NULL) + { + resourceManager->dumpMemTrace(); + resourceManager->disconnect(); + resourceManager.reset(); + } #endif - LoggerFactory::getLoggerInstance().get()->shutdown(); - resourceManager.reset(); - exit(0); + LoggerFactory::getLoggerInstance()->shutdown(); + exit(EXIT_SUCCESS); } /** @@ -162,7 +165,7 @@ void shutdown() * * @param reason the reason why the abort is happening */ -void deviceClientAbort(const string &reason) +void deviceClientAbort(const string &reason, int exitCode) { if (resourceManager != NULL) { @@ -172,7 +175,7 @@ void deviceClientAbort(const string &reason) LoggerFactory::getLoggerInstance()->shutdown(); cout << "AWS IoT Device Client must abort execution, reason: " << reason << endl; cout << "Please check the AWS IoT Device Client logs for more information" << endl; - exit(EXIT_FAILURE); + exit(exitCode); } void attemptConnection() @@ -190,7 +193,8 @@ void attemptConnection() "IoT credentials, " "configuration and/or certificate policy. ***", DC_FATAL_ERROR); - deviceClientAbort("Failed to establish MQTT connection due to credential/configuration error"); + deviceClientAbort( + "Failed to establish MQTT connection due to credential/configuration error", EXIT_FAILURE); return true; } else if (SharedCrtResourceManager::SUCCESS == connectionStatus) @@ -209,7 +213,7 @@ void attemptConnection() catch (const std::exception &e) { LOGM_ERROR(TAG, "Error attempting to connect: %s", e.what()); - deviceClientAbort("Failure from attemptConnection"); + deviceClientAbort("Failure from attemptConnection", EXIT_FAILURE); } } @@ -237,10 +241,6 @@ namespace Aws case ClientBaseEventNotification::FEATURE_STOPPED: { LOGM_INFO(TAG, "%s has stopped", feature->getName().c_str()); -// Stopping DC instance if secure tunnel is closed for a ST component -#if defined(DISABLE_MQTT) - shutdown(); -#endif break; } default: @@ -283,7 +283,7 @@ namespace Aws TAG, "*** %s: Aborting program due to unrecoverable feature error! ***", DeviceClient::DC_FATAL_ERROR); - deviceClientAbort(feature->getName() + " encountered an error"); + deviceClientAbort(feature->getName() + " encountered an error", EXIT_FAILURE); #endif } }; @@ -305,7 +305,7 @@ int main(int argc, char *argv[]) TAG, "*** %s: AWS IoT Device Client must abort execution, reason: Invalid configuration ***", DC_FATAL_ERROR); - deviceClientAbort("Invalid configuration"); + deviceClientAbort("Invalid configuration", EXIT_FAILURE); } if (!LoggerFactory::reconfigure(config.config) && @@ -334,7 +334,7 @@ int main(int argc, char *argv[]) if (!init(argc, argv)) { LOGM_ERROR(TAG, "*** %s: An instance of Device Client is already running.", DC_FATAL_ERROR); - deviceClientAbort("An instance of Device Client is already running."); + deviceClientAbort("An instance of Device Client is already running.", EXIT_FAILURE); } #endif @@ -356,7 +356,7 @@ int main(int argc, char *argv[]) if (!resourceManager.get()->initialize(config.config, features)) { LOGM_ERROR(TAG, "*** %s: Failed to initialize AWS CRT SDK.", DC_FATAL_ERROR); - deviceClientAbort("Failed to initialize AWS CRT SDK"); + deviceClientAbort("Failed to initialize AWS CRT SDK", EXIT_FAILURE); } #if !defined(EXCLUDE_FP) && !defined(DISABLE_MQTT) @@ -384,7 +384,7 @@ int main(int argc, char *argv[]) "Please verify your AWS IoT credentials, " "configuration, Fleet Provisioning Template, claim certificate and policy used. ***", DC_FATAL_ERROR); - deviceClientAbort("Fleet provisioning failed"); + deviceClientAbort("Fleet provisioning failed", EXIT_FAILURE); } resourceManager->disconnect(); } @@ -395,8 +395,10 @@ int main(int argc, char *argv[]) TAG, "*** %s: Fleet Provisioning configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); - deviceClientAbort("Invalid configuration. Fleet Provisioning configuration is enabled but feature is not " - "compiled into binary."); + deviceClientAbort( + "Invalid configuration. Fleet Provisioning configuration is enabled but feature is not " + "compiled into binary.", + EXIT_FAILURE); } #endif @@ -415,7 +417,7 @@ int main(int argc, char *argv[]) TAG, "*** %s: Secure Element configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); - deviceClientAbort("Invalid configuration"); + deviceClientAbort("Invalid configuration", EXIT_FAILURE); } else { @@ -429,7 +431,8 @@ int main(int argc, char *argv[]) "*** %s: Secure Element configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); deviceClientAbort( - "Invalid configuration. Secure Element configuration is enabled but feature is not compiled into binary."); + "Invalid configuration. Secure Element configuration is enabled but feature is not compiled into binary.", + EXIT_FAILURE); } #endif @@ -453,7 +456,7 @@ int main(int argc, char *argv[]) TAG, "*** %s: Config Shadow configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); - deviceClientAbort("Invalid configuration"); + deviceClientAbort("Invalid configuration", EXIT_FAILURE); } #endif @@ -477,7 +480,8 @@ int main(int argc, char *argv[]) LOGM_ERROR( TAG, "*** %s: Jobs configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); deviceClientAbort( - "Invalid configuration. Config Shadow configuration is enabled but feature is not compiled into binary."); + "Invalid configuration. Config Shadow configuration is enabled but feature is not compiled into binary.", + EXIT_FAILURE); } #endif @@ -502,8 +506,10 @@ int main(int argc, char *argv[]) TAG, "*** %s: Secure Tunneling configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); - deviceClientAbort("Invalid configuration. Secure Tunneling configuration is enabled but feature is not " - "compiled into binary."); + deviceClientAbort( + "Invalid configuration. Secure Tunneling configuration is enabled but feature is not " + "compiled into binary.", + EXIT_FAILURE); } #endif @@ -529,7 +535,8 @@ int main(int argc, char *argv[]) "*** %s: Device Defender configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); deviceClientAbort( - "Invalid configuration. Device Defender configuration is enabled but feature is not compiled into binary."); + "Invalid configuration. Device Defender configuration is enabled but feature is not compiled into binary.", + EXIT_FAILURE); } #endif @@ -555,7 +562,8 @@ int main(int argc, char *argv[]) "*** %s: Sample Shadow configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); deviceClientAbort( - "Invalid configuration. Sample Shadow configuration is enabled but feature is not compiled into binary."); + "Invalid configuration. Sample Shadow configuration is enabled but feature is not compiled into binary.", + EXIT_FAILURE); } #endif @@ -581,7 +589,8 @@ int main(int argc, char *argv[]) "*** %s: PubSub sample configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); deviceClientAbort( - "Invalid configuration. PubSub sample configuration is enabled but feature is not compiled into binary."); + "Invalid configuration. PubSub sample configuration is enabled but feature is not compiled into binary.", + EXIT_FAILURE); } #endif @@ -607,7 +616,8 @@ int main(int argc, char *argv[]) "*** %s: Sensor Publish configuration is enabled but feature is not compiled into binary.", DC_FATAL_ERROR); deviceClientAbort( - "Invalid configuration. Sensor Publish configuration is enabled but feature is not compiled into binary."); + "Invalid configuration. Sensor Publish configuration is enabled but feature is not compiled into binary.", + EXIT_FAILURE); } #endif diff --git a/source/tunneling/SecureTunnelingFeature.cpp b/source/tunneling/SecureTunnelingFeature.cpp index dbbdbf3c..a77ab697 100644 --- a/source/tunneling/SecureTunnelingFeature.cpp +++ b/source/tunneling/SecureTunnelingFeature.cpp @@ -7,6 +7,7 @@ #include "TcpForward.h" #include #include +#include #include #include #include @@ -272,14 +273,15 @@ namespace Aws void SecureTunnelingFeature::OnConnectionShutdown(SecureTunnelingContext *contextToRemove) { LOG_DEBUG(TAG, "SecureTunnelingFeature::OnConnectionShutdown"); -#if defined(DISABLE_MQTT) - this->stop(); -#else auto it = find_if(mContexts.begin(), mContexts.end(), [&](const unique_ptr &c) { return c.get() == contextToRemove; }); mContexts.erase(std::remove(mContexts.begin(), mContexts.end(), *it)); + +#if defined(DISABLE_MQTT) + LOG_INFO(TAG, "Secure Tunnel closed, component cleaning up open thread"); + raise(SIGTERM); #endif } From 6ee7ff9a9d8dfe9f79382082641191ba24eb945e Mon Sep 17 00:00:00 2001 From: Harsh Gandhi <63674702+HarshGandhi-AWS@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:14:42 -0700 Subject: [PATCH 24/35] Added null check before closing connection (#429) --- source/SharedCrtResourceManager.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index b74e0640..e0f232a7 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -516,6 +516,10 @@ Aws::Crt::Io::ClientBootstrap *SharedCrtResourceManager::getClientBootstrap() void SharedCrtResourceManager::disconnect() { LOG_DEBUG(TAG, "Attempting to disconnect MQTT connection"); + if (connection == NULL) + { + return; + } if (connection->Disconnect()) { if (connectionClosedPromise.get_future().wait_for(std::chrono::seconds(DEFAULT_WAIT_TIME_SECONDS)) == From f207eafc31b723492e33d5941fe1d05ba46c59b8 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi <63674702+HarshGandhi-AWS@users.noreply.github.com> Date: Thu, 12 Oct 2023 10:14:46 -0700 Subject: [PATCH 25/35] fixed bug to read SIGTERM signal when sent form kernal (#430) --- source/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/main.cpp b/source/main.cpp index 8d808048..9080d9c7 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -348,6 +348,7 @@ int main(int argc, char *argv[]) int received_signal; sigaddset(&sigset, SIGINT); sigaddset(&sigset, SIGHUP); + sigaddset(&sigset, SIGTERM); sigprocmask(SIG_BLOCK, &sigset, nullptr); auto listener = std::make_shared(); From af69d3368f65ace997e8fccd25d46a4d20584ce5 Mon Sep 17 00:00:00 2001 From: RogerZhongAWS <100961047+RogerZhongAWS@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:57:21 +0000 Subject: [PATCH 26/35] Fix docker-build.sh (#423) There is an additional level of indirection required for the script to be able to build the container. Co-authored-by: Miguel Cano --- docker-build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-build.sh b/docker-build.sh index 6ebcd55f..2c8e40ed 100755 --- a/docker-build.sh +++ b/docker-build.sh @@ -2,4 +2,4 @@ # Defaults to ubuntu (18.04). Other options are amazonlinux and ubi8 os=${1:-"ubuntu"} -docker build . --file .github/docker-images/base-images/"$os"/Dockerfile \ No newline at end of file +docker build . --file .github/docker-images/base-images/device-client/"$os"/Dockerfile From 2efead687fd4d7a1172e5ea7e193406cc51f59c7 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi <63674702+HarshGandhi-AWS@users.noreply.github.com> Date: Fri, 13 Oct 2023 18:17:51 -0700 Subject: [PATCH 27/35] Updated google-test lib version to v1.12.0 (#431) --- .../base-images/device-client/amazonlinux/Dockerfile | 6 +++--- .../docker-images/base-images/device-client/ubi8/Dockerfile | 6 +++--- .../base-images/device-client/ubuntu/Dockerfile | 6 +++--- .../build-from-source-package-licenses-ubi8.txt | 2 +- .../build-from-source-package-licenses.txt | 2 +- CMakeLists.txt.gtest | 2 +- integration-tests/CMakeLists.txt.gtest | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/docker-images/base-images/device-client/amazonlinux/Dockerfile b/.github/docker-images/base-images/device-client/amazonlinux/Dockerfile index f2ebc7c5..466f5197 100644 --- a/.github/docker-images/base-images/device-client/amazonlinux/Dockerfile +++ b/.github/docker-images/base-images/device-client/amazonlinux/Dockerfile @@ -46,9 +46,9 @@ RUN curl -sSL https://github.com/Kitware/CMake/releases/download/v3.24.4/cmake-3 # Clone and build Google Test ############################################################################### WORKDIR /tmp -RUN curl -sSL https://github.com/google/googletest/archive/release-1.11.0.tar.gz -o release-1.11.0.tar.gz \ - && tar xf release-1.11.0.tar.gz \ - && cd googletest-release-1.11.0 \ +RUN curl -sSL https://github.com/google/googletest/archive/release-1.12.0.tar.gz -o release-1.12.0.tar.gz \ + && tar xf release-1.12.0.tar.gz \ + && cd googletest-release-1.12.0 \ && cmake -DBUILD_SHARED_LIBS=ON . \ && make \ && cp -a googletest/include/gtest /usr/include/ \ diff --git a/.github/docker-images/base-images/device-client/ubi8/Dockerfile b/.github/docker-images/base-images/device-client/ubi8/Dockerfile index ea238c10..71c6e377 100644 --- a/.github/docker-images/base-images/device-client/ubi8/Dockerfile +++ b/.github/docker-images/base-images/device-client/ubi8/Dockerfile @@ -59,9 +59,9 @@ RUN wget https://dist.opendnssec.org/source/softhsm-2.3.0.tar.gz \ # Clone and build Google Test ############################################################################### WORKDIR /tmp -RUN curl -sSL https://github.com/google/googletest/archive/release-1.11.0.tar.gz -o release-1.11.0.tar.gz \ - && tar xf release-1.11.0.tar.gz \ - && cd googletest-release-1.11.0 \ +RUN curl -sSL https://github.com/google/googletest/archive/release-1.12.0.tar.gz -o release-1.12.0.tar.gz \ + && tar xf release-1.12.0.tar.gz \ + && cd googletest-release-1.12.0 \ && cmake -DBUILD_SHARED_LIBS=ON . \ && make \ && cp -a googletest/include/gtest /usr/include/ \ diff --git a/.github/docker-images/base-images/device-client/ubuntu/Dockerfile b/.github/docker-images/base-images/device-client/ubuntu/Dockerfile index abf3df6a..96710cec 100644 --- a/.github/docker-images/base-images/device-client/ubuntu/Dockerfile +++ b/.github/docker-images/base-images/device-client/ubuntu/Dockerfile @@ -46,9 +46,9 @@ RUN curl -sSL https://github.com/Kitware/CMake/releases/download/v3.10.0/cmake-3 # Clone and build Google Test ############################################################################### WORKDIR /tmp -RUN wget --ca-certificate=/etc/ssl/certs/ca-certificates.crt https://github.com/google/googletest/archive/release-1.11.0.tar.gz \ - && tar xf release-1.11.0.tar.gz \ - && cd googletest-release-1.11.0 \ +RUN wget --ca-certificate=/etc/ssl/certs/ca-certificates.crt https://github.com/google/googletest/archive/release-1.12.0.tar.gz \ + && tar xf release-1.12.0.tar.gz \ + && cd googletest-release-1.12.0 \ && cmake -DBUILD_SHARED_LIBS=ON . \ && make \ && cp -a googletest/include/gtest /usr/include/ \ diff --git a/.github/docker-images/oss-compliance/build-from-source-packages/build-from-source-package-licenses-ubi8.txt b/.github/docker-images/oss-compliance/build-from-source-packages/build-from-source-package-licenses-ubi8.txt index ed47595f..0cc64d0b 100644 --- a/.github/docker-images/oss-compliance/build-from-source-packages/build-from-source-package-licenses-ubi8.txt +++ b/.github/docker-images/oss-compliance/build-from-source-packages/build-from-source-package-licenses-ubi8.txt @@ -377,7 +377,7 @@ Public License instead of this License. ------ -** Google Test 1.10.0; version 1.10.0 -- https://github.com/google/googletest/blob/v1.10.x/LICENSE +** Google Test 1.12.0; version 1.12.0 -- https://github.com/google/googletest/blob/v1.12.x/LICENSE Copyright 2008, Google Inc. All rights reserved. diff --git a/.github/docker-images/oss-compliance/build-from-source-packages/build-from-source-package-licenses.txt b/.github/docker-images/oss-compliance/build-from-source-packages/build-from-source-package-licenses.txt index 750f986c..d1457f17 100644 --- a/.github/docker-images/oss-compliance/build-from-source-packages/build-from-source-package-licenses.txt +++ b/.github/docker-images/oss-compliance/build-from-source-packages/build-from-source-package-licenses.txt @@ -343,7 +343,7 @@ Public License instead of this License. ------ -** Google Test 1.10.0; version 1.10.0 -- https://github.com/google/googletest/blob/v1.10.x/LICENSE +** Google Test 1.12.0; version 1.12.0 -- https://github.com/google/googletest/blob/v1.12.x/LICENSE Copyright 2008, Google Inc. All rights reserved. diff --git a/CMakeLists.txt.gtest b/CMakeLists.txt.gtest index cae95844..341249e8 100644 --- a/CMakeLists.txt.gtest +++ b/CMakeLists.txt.gtest @@ -5,7 +5,7 @@ project(googletest-download NONE) include(ExternalProject) ExternalProject_Add(googletest GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.10.0 + GIT_TAG release-1.12.0 SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" CONFIGURE_COMMAND "" diff --git a/integration-tests/CMakeLists.txt.gtest b/integration-tests/CMakeLists.txt.gtest index 1e630783..becd6bd6 100644 --- a/integration-tests/CMakeLists.txt.gtest +++ b/integration-tests/CMakeLists.txt.gtest @@ -5,7 +5,7 @@ project(googletest-download NONE) include(ExternalProject) ExternalProject_Add(googletest GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.11.0 + GIT_TAG release-1.12.0 SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" CMAKE_ARGS -DCMAKE_BUILD_TYPE=Debug From b75dc3ac03ee601d30cfff3d7596913a7aca15d9 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi <63674702+HarshGandhi-AWS@users.noreply.github.com> Date: Tue, 7 Nov 2023 14:57:28 -0800 Subject: [PATCH 28/35] disabled jobs and secure tunneling if they are not compiled into binary (#436) --- source/config/Config.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/source/config/Config.cpp b/source/config/Config.cpp index 4d637720..c010e416 100644 --- a/source/config/Config.cpp +++ b/source/config/Config.cpp @@ -17,6 +17,7 @@ #endif +#include "../SharedCrtResourceManager.h" #include "../util/FileUtils.h" #include "../util/MqttUtils.h" #include "../util/ProxyUtils.h" @@ -2641,6 +2642,14 @@ bool Config::ParseCliArgs(int argc, char **argv, CliArgs &cliArgs) bool Config::init(const CliArgs &cliArgs) { +#if defined(EXCLUDE_JOBS) + config.jobs.enabled = false; +#endif + +#if defined(EXCLUDE_ST) + config.tunneling.enabled = false; +#endif + try { string filename = Config::DEFAULT_CONFIG_FILE; @@ -3171,4 +3180,4 @@ string Config::ExpandDefaultConfigDir(bool removeTrailingSeparator) return Util::TrimRightCopy(expandedConfigDir, string{Config::PATH_DIRECTORY_SEPARATOR}); } return expandedConfigDir; -} \ No newline at end of file +} From 0f913802b9c4ca327b35ecfad2423c46da1ddc7d Mon Sep 17 00:00:00 2001 From: RogerZhongAWS <100961047+RogerZhongAWS@users.noreply.github.com> Date: Wed, 8 Nov 2023 00:33:22 +0000 Subject: [PATCH 29/35] Update Dockerfile OpenSSL version (#435) * Update Dockerfile * Update README.md --- .../device-client/amazonlinux/Dockerfile | 6 +++--- .../base-images/device-client/ubi8/Dockerfile | 6 +++--- .../base-images/device-client/ubuntu/Dockerfile | 6 +++--- .../integration-tests/ubuntu/Dockerfile | 14 +++----------- README.md | 2 +- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/.github/docker-images/base-images/device-client/amazonlinux/Dockerfile b/.github/docker-images/base-images/device-client/amazonlinux/Dockerfile index 466f5197..6a90821b 100644 --- a/.github/docker-images/base-images/device-client/amazonlinux/Dockerfile +++ b/.github/docker-images/base-images/device-client/amazonlinux/Dockerfile @@ -1,6 +1,6 @@ FROM amazonlinux:2.0.20230307.0 as base -ARG OPENSSL_VERSION=1.1.1n +ARG OPENSSL_VERSION=3.0.8 ############################################################################### # Install prereqs @@ -22,7 +22,7 @@ RUN yum -y update \ && rm -rf /var/cache/yum ############################################################################### -# Install OpenSSL 1.1.1 +# Install OpenSSL 3.0.8 ############################################################################### WORKDIR /tmp RUN wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz \ @@ -117,4 +117,4 @@ RUN mkdir -p /root/aws-iot-device-client/build \ && cmake .. \ && cmake --build . --target aws-iot-device-client -ENTRYPOINT ["/root/aws-iot-device-client/build/aws-iot-device-client"] \ No newline at end of file +ENTRYPOINT ["/root/aws-iot-device-client/build/aws-iot-device-client"] diff --git a/.github/docker-images/base-images/device-client/ubi8/Dockerfile b/.github/docker-images/base-images/device-client/ubi8/Dockerfile index 71c6e377..97a69b9f 100644 --- a/.github/docker-images/base-images/device-client/ubi8/Dockerfile +++ b/.github/docker-images/base-images/device-client/ubi8/Dockerfile @@ -2,7 +2,7 @@ #https://access.redhat.com/RegistryAuthentication FROM registry.access.redhat.com/ubi8/ubi AS base -ARG OPENSSL_VERSION=1.1.1n +ARG OPENSSL_VERSION=3.0.8 ############################################################################### # Install prereqs @@ -33,7 +33,7 @@ RUN curl -sSL https://github.com/Kitware/CMake/releases/download/v3.10.0/cmake-3 && make install ############################################################################### -# Install OpenSSL 1.1.1 +# Install OpenSSL 3.0.8 ############################################################################### WORKDIR /tmp RUN wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz \ @@ -130,4 +130,4 @@ RUN mkdir -p /root/aws-iot-device-client/build \ && cmake .. \ && cmake --build . --target aws-iot-device-client -ENTRYPOINT ["/root/aws-iot-device-client/build/aws-iot-device-client"] \ No newline at end of file +ENTRYPOINT ["/root/aws-iot-device-client/build/aws-iot-device-client"] diff --git a/.github/docker-images/base-images/device-client/ubuntu/Dockerfile b/.github/docker-images/base-images/device-client/ubuntu/Dockerfile index 96710cec..a0e371f5 100644 --- a/.github/docker-images/base-images/device-client/ubuntu/Dockerfile +++ b/.github/docker-images/base-images/device-client/ubuntu/Dockerfile @@ -2,7 +2,7 @@ FROM ubuntu:18.04 AS base ENV DEBIAN_FRONTEND=noninteractive -ARG OPENSSL_VERSION=1.1.1n +ARG OPENSSL_VERSION=3.0.8 ############################################################################### # Install prereqs @@ -20,7 +20,7 @@ RUN apt-get update -qq \ && apt-get clean ############################################################################### -# Install OpenSSL 1.1.1 +# Install OpenSSL 3.0.8 ############################################################################### WORKDIR /tmp RUN wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz \ @@ -116,4 +116,4 @@ RUN mkdir -p /root/aws-iot-device-client/build \ && cmake .. \ && cmake --build . --target aws-iot-device-client -ENTRYPOINT ["/root/aws-iot-device-client/build/aws-iot-device-client"] \ No newline at end of file +ENTRYPOINT ["/root/aws-iot-device-client/build/aws-iot-device-client"] diff --git a/.github/docker-images/integration-tests/ubuntu/Dockerfile b/.github/docker-images/integration-tests/ubuntu/Dockerfile index 23fc2538..b72fb3e4 100644 --- a/.github/docker-images/integration-tests/ubuntu/Dockerfile +++ b/.github/docker-images/integration-tests/ubuntu/Dockerfile @@ -22,9 +22,9 @@ RUN wget https://www.zlib.net/zlib-1.3.tar.gz -O /tmp/zlib-1.3.tar.gz && \ make install && \ cd /home/dependencies -RUN wget https://boostorg.jfrog.io/artifactory/main/release/1.79.0/source/boost_1_79_0.tar.gz -O /tmp/boost.tar.gz && \ +RUN wget https://boostorg.jfrog.io/artifactory/main/release/1.81.0/source/boost_1_81_0.tar.gz -O /tmp/boost.tar.gz && \ tar xzvf /tmp/boost.tar.gz && \ - cd boost_1_79_0 && \ + cd boost_1_81_0 && \ ./bootstrap.sh && \ ./b2 install link=static && \ cd /home/dependencies @@ -39,14 +39,6 @@ RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/p make install && \ cd /home/dependencies -RUN git clone https://github.com/openssl/openssl.git && \ - cd openssl && \ - git checkout OpenSSL_1_1_1-stable && \ - ./config && \ - make depend && \ - make all && \ - cd /home/dependencies - RUN git clone --branch v2.13.6 https://github.com/catchorg/Catch2.git && \ cd Catch2 && \ mkdir build && \ @@ -58,7 +50,7 @@ RUN git clone --branch v2.13.6 https://github.com/catchorg/Catch2.git && \ RUN git clone https://github.com/aws-samples/aws-iot-securetunneling-localproxy && \ cd aws-iot-securetunneling-localproxy && \ - git checkout 851de4cc2b48861b835327d005000c7d0c81d11b && \ + git checkout d3150e0ebc4ef022939deb1ab43de005254f5751 && \ mkdir build && \ cd build && \ cmake ../ && \ diff --git a/README.md b/README.md index 70e3be68..dd7ead70 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ The AWS IoT Device Client is currently compatible with x86_64, aarch64, armv7l, * C++ 11 or higher * [CMake](https://cmake.org/) 3.10+ -* OpenSSL 1.1.1 +* OpenSSL 3.0.0+ * [aws-iot-device-sdk-cpp-v2](https://github.com/aws/aws-iot-device-sdk-cpp-v2) commit hash located in `CMakeLists.txt.awssdk` *Note:* The TLS stack, and the version of the SDK mentioned above is what our CI uses. You could potentially use a different TLS stack for example, we just don't actively test or support this. From 00f68ce4e67e8c0eec6b7274655f21c49a719964 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi <63674702+HarshGandhi-AWS@users.noreply.github.com> Date: Mon, 27 Nov 2023 12:55:11 -0800 Subject: [PATCH 30/35] Removed number of slash validation for MQTT topics (#438) --- source/util/MqttUtils.cpp | 11 ----------- source/util/MqttUtils.h | 5 ----- test/config/TestConfig.cpp | 35 ----------------------------------- test/util/TestMqttUtils.cpp | 15 --------------- 4 files changed, 66 deletions(-) diff --git a/source/util/MqttUtils.cpp b/source/util/MqttUtils.cpp index b1baf6bb..57d3799d 100644 --- a/source/util/MqttUtils.cpp +++ b/source/util/MqttUtils.cpp @@ -24,17 +24,6 @@ bool MqttUtils::ValidateAwsIotMqttTopicName(std::string topic) topic = reserved_topic.suffix(); } - const std::size_t count_slashes = std::count(topic.cbegin(), topic.cend(), '/'); - if (count_slashes > MAX_NUMBER_OF_FORWARD_SLASHES) - { - LOGM_ERROR( - TAG, - "Number of forward slashes in topic (%lu) exceeds maximum (%lu)", - count_slashes, - MAX_NUMBER_OF_FORWARD_SLASHES); - return false; - } - // Since std::string is based on char, the size of the string and number of UTF8 chars is same. if (topic.size() > MAX_LENGTH_OF_TOPIC) { diff --git a/source/util/MqttUtils.h b/source/util/MqttUtils.h index 9e1e90fd..d1a354a6 100644 --- a/source/util/MqttUtils.h +++ b/source/util/MqttUtils.h @@ -18,15 +18,10 @@ namespace Aws // // https://docs.aws.amazon.com/general/latest/gr/iot-core.html#message-broker-limits // - // A topic in a publish or subscribe request can have no more than 7 forward slashes (/). - // This excludes the first 3 slashes in the mandatory segments for - // Basic Ingest topics ($AWS/rules/rule-name/). - // // The topic passed to AWS IoT Core when sending a publish request can be no larger // than 256 bytes of UTF-8 encoded characters. This excludes the first 3 mandatory // segments for Basic Ingest topics ($AWS/rules/rule-name/). // - static constexpr std::size_t MAX_NUMBER_OF_FORWARD_SLASHES{7}; static constexpr std::size_t MAX_LENGTH_OF_TOPIC{256}; /** diff --git a/test/config/TestConfig.cpp b/test/config/TestConfig.cpp index f9ecbdd1..d17b7075 100644 --- a/test/config/TestConfig.cpp +++ b/test/config/TestConfig.cpp @@ -1310,41 +1310,6 @@ TEST_F(ConfigTestFixture, SensorPublishInvalidConfigMqttTopicEmpty) ASSERT_FALSE(settings.enabled); } -TEST_F(ConfigTestFixture, SensorPublishInvalidConfigMqttTopic) -{ - constexpr char jsonString[] = R"( -{ - "endpoint": "endpoint value", - "cert": "/tmp/aws-iot-device-client-test-file", - "root-ca": "/tmp/aws-iot-device-client-test/AmazonRootCA1.pem", - "key": "/tmp/aws-iot-device-client-test-file", - "thing-name": "thing-name value", - "sensor-publish": { - "sensors": [ - { - "addr": "/tmp/sensors/my-sensor-server", - "eom_delimiter": "[\r\n]+", - "mqtt_topic": "////////my-sensor-data" - } - ] - } -})"; - JsonObject jsonObject(jsonString); - JsonView jsonView = jsonObject.View(); - - PlainConfig config; - config.LoadFromJson(jsonView); - -#if defined(EXCLUDE_SENSOR_PUBLISH) - GTEST_SKIP(); -#endif - ASSERT_FALSE(config.Validate()); // Invalid mqtt_topic. - ASSERT_TRUE(config.sensorPublish.enabled); - ASSERT_EQ(config.sensorPublish.settings.size(), 1); - const auto &settings = config.sensorPublish.settings[0]; - ASSERT_FALSE(settings.enabled); -} - TEST_F(ConfigTestFixture, SensorPublishInvalidConfigEomDelimiter) { constexpr char jsonString[] = R"( diff --git a/test/util/TestMqttUtils.cpp b/test/util/TestMqttUtils.cpp index 5b0d6800..caacf9a7 100644 --- a/test/util/TestMqttUtils.cpp +++ b/test/util/TestMqttUtils.cpp @@ -27,21 +27,6 @@ TEST(MqttUtils, ReservedTopicValid) ASSERT_TRUE(MqttUtils::ValidateAwsIotMqttTopicName(topic)); } -TEST(MqttUtils, TopicNotValidExceedsMaxSlashes) -{ - std::string topic(MqttUtils::MAX_NUMBER_OF_FORWARD_SLASHES + 1, '/'); - - ASSERT_FALSE(MqttUtils::ValidateAwsIotMqttTopicName(topic)); -} - -TEST(MqttUtils, TopicValidExceedsMaxSlashesWithReservedTopic) -{ - std::string topic(RESERVED_TOPIC); - std::fill_n(std::back_inserter(topic), MqttUtils::MAX_NUMBER_OF_FORWARD_SLASHES, '/'); - - ASSERT_TRUE(MqttUtils::ValidateAwsIotMqttTopicName(topic)); -} - TEST(MqttUtils, TopicNotValidExceedsMaxLength) { std::string topic(MqttUtils::MAX_LENGTH_OF_TOPIC + 1, 'A'); From d11c527491c7fd3bd7aca7662f6ffa2cbeec11c6 Mon Sep 17 00:00:00 2001 From: Harsh Gandhi Date: Tue, 5 Dec 2023 14:19:51 -0800 Subject: [PATCH 31/35] conflict fix --- source/SharedCrtResourceManager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index 0fde9133..9f0f1138 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -543,7 +543,6 @@ void SharedCrtResourceManager::disconnect() return; } ->>>>>>> updateSDKVersion if (connection->Disconnect()) { if (connectionClosedPromise.get_future().wait_for(std::chrono::seconds(DEFAULT_WAIT_TIME_SECONDS)) == From fb58d1fa049ed1bbcb935307b1ee3c34e4edc7a6 Mon Sep 17 00:00:00 2001 From: RogerZhongAWS Date: Mon, 11 Dec 2023 00:13:58 +0000 Subject: [PATCH 32/35] add http proxy support for secure tunneling --- source/SharedCrtResourceManager.cpp | 3 +- source/tunneling/SecureTunnelWrapper.cpp | 37 +++++++++++ source/tunneling/SecureTunnelWrapper.h | 24 ++++++- source/tunneling/SecureTunnelingContext.cpp | 69 ++++++++++++++++----- source/tunneling/SecureTunnelingContext.h | 19 ++++++ source/tunneling/SecureTunnelingFeature.cpp | 48 +++++++++++--- source/tunneling/SecureTunnelingFeature.h | 6 ++ 7 files changed, 181 insertions(+), 25 deletions(-) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index 9f0f1138..25e9f2e4 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -340,11 +340,12 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) { proxyOptions.HostName = proxyConfig.proxyHost->c_str(); proxyOptions.Port = proxyConfig.proxyPort.value(); + proxyOptions.ProxyConnectionType = Aws::Crt::Http::AwsHttpProxyConnectionType::Tunneling; LOGM_INFO( TAG, "Attempting to establish MQTT connection with proxy: %s:%u", - proxyConfig.proxyHost->c_str(), + proxyOptions.HostName.c_str(), proxyConfig.proxyPort.value()); if (proxyConfig.httpProxyAuthEnabled) diff --git a/source/tunneling/SecureTunnelWrapper.cpp b/source/tunneling/SecureTunnelWrapper.cpp index a7c8b3bc..606f05ae 100644 --- a/source/tunneling/SecureTunnelWrapper.cpp +++ b/source/tunneling/SecureTunnelWrapper.cpp @@ -40,6 +40,43 @@ SecureTunnelWrapper::SecureTunnelWrapper( onSessionReset)) { } + +SecureTunnelWrapper::SecureTunnelWrapper( + Aws::Crt::Allocator *allocator, + Aws::Crt::Io::ClientBootstrap *bootstrap, + const Aws::Crt::Io::SocketOptions &socketOptions, + const Aws::Crt::Http::HttpClientConnectionProxyOptions &proxyOptions, + const std::string &accessToken, + aws_secure_tunneling_local_proxy_mode localProxyMode, + const std::string &endpoint, + const std::string &rootCa, + const Aws::Iotsecuretunneling::OnConnectionComplete &onConnectionComplete, + const Aws::Iotsecuretunneling::OnConnectionShutdown &onConnectionShutdown, + const Aws::Iotsecuretunneling::OnSendDataComplete &onSendDataComplete, + const Aws::Iotsecuretunneling::OnDataReceive &onDataReceive, + const Aws::Iotsecuretunneling::OnStreamStart &onStreamStart, + const Aws::Iotsecuretunneling::OnStreamReset &onStreamReset, + const Aws::Iotsecuretunneling::OnSessionReset &onSessionReset) + : secureTunnel((Aws::Iotsecuretunneling::SecureTunnelBuilder( + allocator, + *bootstrap, + socketOptions, + accessToken, + localProxyMode, + endpoint)) + .WithHttpClientConnectionProxyOptions(proxyOptions) + .WithRootCa(rootCa) + .WithOnConnectionComplete(onConnectionComplete) + .WithOnConnectionShutdown(onConnectionShutdown) + .WithOnSendDataComplete(onSendDataComplete) + .WithOnDataReceive(onDataReceive) + .WithOnStreamStart(onStreamStart) + .WithOnStreamReset(onSessionReset) + .WithOnSessionReset(onSessionReset) + .Build()) +{ +} + int SecureTunnelWrapper::Connect() { return secureTunnel->Connect(); diff --git a/source/tunneling/SecureTunnelWrapper.h b/source/tunneling/SecureTunnelWrapper.h index af07c071..babf0124 100644 --- a/source/tunneling/SecureTunnelWrapper.h +++ b/source/tunneling/SecureTunnelWrapper.h @@ -19,10 +19,32 @@ namespace Aws public: SecureTunnelWrapper() = default; virtual ~SecureTunnelWrapper() = default; + + // Without HTTP Proxy + SecureTunnelWrapper( + Aws::Crt::Allocator *allocator, + Aws::Crt::Io::ClientBootstrap *clientBootstrap, + const Aws::Crt::Io::SocketOptions &socketOptions, + + const std::string &accessToken, + aws_secure_tunneling_local_proxy_mode localProxyMode, + const std::string &endpointHost, + const std::string &rootCa, + + const Aws::Iotsecuretunneling::OnConnectionComplete &onConnectionComplete, + const Aws::Iotsecuretunneling::OnConnectionShutdown &onConnectionShutdown, + const Aws::Iotsecuretunneling::OnSendDataComplete &onSendDataComplete, + const Aws::Iotsecuretunneling::OnDataReceive &onDataReceive, + const Aws::Iotsecuretunneling::OnStreamStart &onStreamStart, + const Aws::Iotsecuretunneling::OnStreamReset &onStreamReset, + const Aws::Iotsecuretunneling::OnSessionReset &onSessionReset); + + // With HTTP Proxy SecureTunnelWrapper( Aws::Crt::Allocator *allocator, Aws::Crt::Io::ClientBootstrap *clientBootstrap, const Aws::Crt::Io::SocketOptions &socketOptions, + const Aws::Crt::Http::HttpClientConnectionProxyOptions &proxyOptions, const std::string &accessToken, aws_secure_tunneling_local_proxy_mode localProxyMode, @@ -47,7 +69,7 @@ namespace Aws virtual bool IsValid(); - std::unique_ptr secureTunnel; + std::shared_ptr secureTunnel; private: /** diff --git a/source/tunneling/SecureTunnelingContext.cpp b/source/tunneling/SecureTunnelingContext.cpp index 15f91a83..9ee4dccb 100644 --- a/source/tunneling/SecureTunnelingContext.cpp +++ b/source/tunneling/SecureTunnelingContext.cpp @@ -28,7 +28,21 @@ namespace Aws const int port, const OnConnectionShutdownFn &onConnectionShutdown) : mSharedCrtResourceManager(manager), mRootCa(rootCa.has_value() ? rootCa.value() : ""), - mAccessToken(accessToken), mEndpoint(endpoint), mPort(port), + mAccessToken(accessToken), mEndpoint(endpoint), mPort(port), isHTTPProxyEnabled(false), + mOnConnectionShutdown(onConnectionShutdown) + { + } + + SecureTunnelingContext::SecureTunnelingContext( + shared_ptr manager, + const Aws::Crt::Http::HttpClientConnectionProxyOptions &proxyOptions, + const Aws::Crt::Optional &rootCa, + const string &accessToken, + const string &endpoint, + const int port, + const OnConnectionShutdownFn &onConnectionShutdown) + : mSharedCrtResourceManager(manager), mProxyOptions(proxyOptions), mRootCa(rootCa.has_value() ? rootCa.value() : ""), + mAccessToken(accessToken), mEndpoint(endpoint), mPort(port), isHTTPProxyEnabled(true), mOnConnectionShutdown(onConnectionShutdown) { } @@ -191,21 +205,44 @@ namespace Aws const Aws::Iotsecuretunneling::OnStreamReset &onStreamReset, const Aws::Iotsecuretunneling::OnSessionReset &onSessionReset) { - return std::make_shared( - mSharedCrtResourceManager->getAllocator(), - mSharedCrtResourceManager->getClientBootstrap(), - Crt::Io::SocketOptions(), - mAccessToken, - AWS_SECURE_TUNNELING_DESTINATION_MODE, - mEndpoint, - mRootCa, - onConnectionComplete, - onConnectionShutdown, - onSendDataComplete, - onDataReceive, - onStreamStart, - onStreamReset, - onSessionReset); + // const Aws::Crt::Http::HttpClientConnectionProxyOptions& proxyOptions = mProxyOptions; + + if (isHTTPProxyEnabled) { + LOGM_INFO(TAG, "Creating Secure Tunneling with proxy to: %s", mProxyOptions.HostName.c_str()); + return std::make_shared( + mSharedCrtResourceManager->getAllocator(), + mSharedCrtResourceManager->getClientBootstrap(), + Crt::Io::SocketOptions(), + mProxyOptions, + mAccessToken, + AWS_SECURE_TUNNELING_DESTINATION_MODE, + mEndpoint, + mRootCa, + onConnectionComplete, + nullptr, // TODO: long term fix needed for onConnectionShutdown callback + onSendDataComplete, + onDataReceive, + onStreamStart, + onStreamReset, + onSessionReset); + } + else { + return std::make_shared( + mSharedCrtResourceManager->getAllocator(), + mSharedCrtResourceManager->getClientBootstrap(), + Crt::Io::SocketOptions(), + mAccessToken, + AWS_SECURE_TUNNELING_DESTINATION_MODE, + mEndpoint, + mRootCa, + onConnectionComplete, + nullptr, // TODO: long term fix needed for onConnectionShutdown callback + onSendDataComplete, + onDataReceive, + onStreamStart, + onStreamReset, + onSessionReset); + } } std::shared_ptr SecureTunnelingContext::CreateTcpForward() diff --git a/source/tunneling/SecureTunnelingContext.h b/source/tunneling/SecureTunnelingContext.h index d0aed1ef..abc4518c 100644 --- a/source/tunneling/SecureTunnelingContext.h +++ b/source/tunneling/SecureTunnelingContext.h @@ -47,6 +47,15 @@ namespace Aws const int port, const OnConnectionShutdownFn &onConnectionShutdown); + SecureTunnelingContext( + std::shared_ptr manager, + const Aws::Crt::Http::HttpClientConnectionProxyOptions &proxyOptions, + const Aws::Crt::Optional &rootCa, + const std::string &accessToken, + const std::string &endpoint, + const int port, + const OnConnectionShutdownFn &onConnectionShutdown); + /** * \brief Constructor */ @@ -178,6 +187,11 @@ namespace Aws */ std::shared_ptr mSharedCrtResourceManager; + /** + * \brief HTTP proxy strategy and auth config + */ + Aws::Crt::Http::HttpClientConnectionProxyOptions mProxyOptions; + /** * \brief Path to the Amazon root CA */ @@ -198,6 +212,11 @@ namespace Aws */ uint16_t mPort{22}; + /** + * \brief boolean for HTTP proxy enablement + */ + bool isHTTPProxyEnabled; + /** * \brief Callback when the secure tunnel is shutdown */ diff --git a/source/tunneling/SecureTunnelingFeature.cpp b/source/tunneling/SecureTunnelingFeature.cpp index a77ab697..97972cc9 100644 --- a/source/tunneling/SecureTunnelingFeature.cpp +++ b/source/tunneling/SecureTunnelingFeature.cpp @@ -94,6 +94,38 @@ namespace Aws void SecureTunnelingFeature::LoadFromConfig(const PlainConfig &config) { + // Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions; + PlainConfig::HttpProxyConfig proxyConfig = config.httpProxyConfig; + + if (proxyConfig.httpProxyEnabled) + { + proxyOptions.HostName = proxyConfig.proxyHost->c_str(); + proxyOptions.Port = proxyConfig.proxyPort.value(); + proxyOptions.ProxyConnectionType = Aws::Crt::Http::AwsHttpProxyConnectionType::Tunneling; + + LOGM_INFO( + TAG, + "Attempting to establish tunneling connection with proxy: %s:%u", + proxyOptions.HostName.c_str(), + proxyOptions.Port); + + if (proxyConfig.httpProxyAuthEnabled) + { + LOG_INFO(TAG, "Proxy Authentication is enabled"); + Aws::Crt::Http::HttpProxyStrategyBasicAuthConfig basicAuthConfig; + basicAuthConfig.ConnectionType = Aws::Crt::Http::AwsHttpProxyConnectionType::Tunneling; + proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::Basic; + basicAuthConfig.Username = proxyConfig.proxyUsername->c_str(); + basicAuthConfig.Password = proxyConfig.proxyPassword->c_str(); + proxyOptions.ProxyStrategy = + Aws::Crt::Http::HttpProxyStrategy::CreateBasicHttpProxyStrategy(basicAuthConfig, Aws::Crt::g_allocator); + } + else + { + LOG_INFO(TAG, "Proxy Authentication is disabled"); + proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None; + } + } mThingName = *config.thingName; mRootCa = config.rootCa; mSubscribeNotification = config.tunneling.subscribeNotification; @@ -101,13 +133,14 @@ namespace Aws if (!config.tunneling.subscribeNotification) { - auto context = unique_ptr(new SecureTunnelingContext( - mSharedCrtResourceManager, - mRootCa, - *config.tunneling.destinationAccessToken, - GetEndpoint(*config.tunneling.region), - static_cast(config.tunneling.port.value()), - bind(&SecureTunnelingFeature::OnConnectionShutdown, this, placeholders::_1))); + auto context = createContext(*config.tunneling.destinationAccessToken, *config.tunneling.region, static_cast(config.tunneling.port.value())); + // auto context = unique_ptr(new SecureTunnelingContext( + // mSharedCrtResourceManager, + // mRootCa, + // *config.tunneling.destinationAccessToken, + // GetEndpoint(*config.tunneling.region), + // static_cast(config.tunneling.port.value()), + // bind(&SecureTunnelingFeature::OnConnectionShutdown, this, placeholders::_1))); mContexts.push_back(std::move(context)); } } @@ -257,6 +290,7 @@ namespace Aws { return std::unique_ptr(new SecureTunnelingContext( mSharedCrtResourceManager, + proxyOptions, mRootCa, accessToken, GetEndpoint(region), diff --git a/source/tunneling/SecureTunnelingFeature.h b/source/tunneling/SecureTunnelingFeature.h index e7b85554..f07642c7 100644 --- a/source/tunneling/SecureTunnelingFeature.h +++ b/source/tunneling/SecureTunnelingFeature.h @@ -9,6 +9,7 @@ #include "../SharedCrtResourceManager.h" #include "IotSecureTunnelingClientWrapper.h" #include "SecureTunnelingContext.h" +#include "aws/crt/http/HttpProxyStrategy.h" #include #include @@ -177,6 +178,11 @@ namespace Aws */ std::shared_ptr mClientBaseNotifier; + /** + * \brief HTTP proxy strategy and auth config + */ + Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions; + /** * \brief The ThingName to use */ From 1beb8326d55f11055e8c9ca4e44dccf3cc4a84c2 Mon Sep 17 00:00:00 2001 From: RogerZhongAWS Date: Tue, 12 Dec 2023 03:15:34 +0000 Subject: [PATCH 33/35] cleanup http proxy changes --- source/SharedCrtResourceManager.cpp | 2 +- source/tunneling/SecureTunnelingContext.cpp | 8 +++----- source/tunneling/SecureTunnelingContext.h | 5 ----- source/tunneling/SecureTunnelingFeature.cpp | 7 ------- 4 files changed, 4 insertions(+), 18 deletions(-) diff --git a/source/SharedCrtResourceManager.cpp b/source/SharedCrtResourceManager.cpp index 25e9f2e4..115a1863 100644 --- a/source/SharedCrtResourceManager.cpp +++ b/source/SharedCrtResourceManager.cpp @@ -346,7 +346,7 @@ int SharedCrtResourceManager::establishConnection(const PlainConfig &config) TAG, "Attempting to establish MQTT connection with proxy: %s:%u", proxyOptions.HostName.c_str(), - proxyConfig.proxyPort.value()); + proxyOptions.Port); if (proxyConfig.httpProxyAuthEnabled) { diff --git a/source/tunneling/SecureTunnelingContext.cpp b/source/tunneling/SecureTunnelingContext.cpp index 9ee4dccb..1d5e48c9 100644 --- a/source/tunneling/SecureTunnelingContext.cpp +++ b/source/tunneling/SecureTunnelingContext.cpp @@ -28,7 +28,7 @@ namespace Aws const int port, const OnConnectionShutdownFn &onConnectionShutdown) : mSharedCrtResourceManager(manager), mRootCa(rootCa.has_value() ? rootCa.value() : ""), - mAccessToken(accessToken), mEndpoint(endpoint), mPort(port), isHTTPProxyEnabled(false), + mAccessToken(accessToken), mEndpoint(endpoint), mPort(port), mOnConnectionShutdown(onConnectionShutdown) { } @@ -42,7 +42,7 @@ namespace Aws const int port, const OnConnectionShutdownFn &onConnectionShutdown) : mSharedCrtResourceManager(manager), mProxyOptions(proxyOptions), mRootCa(rootCa.has_value() ? rootCa.value() : ""), - mAccessToken(accessToken), mEndpoint(endpoint), mPort(port), isHTTPProxyEnabled(true), + mAccessToken(accessToken), mEndpoint(endpoint), mPort(port), mOnConnectionShutdown(onConnectionShutdown) { } @@ -205,9 +205,7 @@ namespace Aws const Aws::Iotsecuretunneling::OnStreamReset &onStreamReset, const Aws::Iotsecuretunneling::OnSessionReset &onSessionReset) { - // const Aws::Crt::Http::HttpClientConnectionProxyOptions& proxyOptions = mProxyOptions; - - if (isHTTPProxyEnabled) { + if (mProxyOptions.HostName.length() > 0) { LOGM_INFO(TAG, "Creating Secure Tunneling with proxy to: %s", mProxyOptions.HostName.c_str()); return std::make_shared( mSharedCrtResourceManager->getAllocator(), diff --git a/source/tunneling/SecureTunnelingContext.h b/source/tunneling/SecureTunnelingContext.h index abc4518c..dab946c0 100644 --- a/source/tunneling/SecureTunnelingContext.h +++ b/source/tunneling/SecureTunnelingContext.h @@ -212,11 +212,6 @@ namespace Aws */ uint16_t mPort{22}; - /** - * \brief boolean for HTTP proxy enablement - */ - bool isHTTPProxyEnabled; - /** * \brief Callback when the secure tunnel is shutdown */ diff --git a/source/tunneling/SecureTunnelingFeature.cpp b/source/tunneling/SecureTunnelingFeature.cpp index 97972cc9..1d496675 100644 --- a/source/tunneling/SecureTunnelingFeature.cpp +++ b/source/tunneling/SecureTunnelingFeature.cpp @@ -134,13 +134,6 @@ namespace Aws if (!config.tunneling.subscribeNotification) { auto context = createContext(*config.tunneling.destinationAccessToken, *config.tunneling.region, static_cast(config.tunneling.port.value())); - // auto context = unique_ptr(new SecureTunnelingContext( - // mSharedCrtResourceManager, - // mRootCa, - // *config.tunneling.destinationAccessToken, - // GetEndpoint(*config.tunneling.region), - // static_cast(config.tunneling.port.value()), - // bind(&SecureTunnelingFeature::OnConnectionShutdown, this, placeholders::_1))); mContexts.push_back(std::move(context)); } } From 2aff02a081886935b470b0c8a298e62cf226245c Mon Sep 17 00:00:00 2001 From: RogerZhongAWS Date: Tue, 12 Dec 2023 22:30:35 +0000 Subject: [PATCH 34/35] fix format --- source/tunneling/SecureTunnelWrapper.cpp | 32 ++++++++++----------- source/tunneling/SecureTunnelingContext.cpp | 12 ++++---- source/tunneling/SecureTunnelingFeature.cpp | 4 +-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/source/tunneling/SecureTunnelWrapper.cpp b/source/tunneling/SecureTunnelWrapper.cpp index 606f05ae..25ec1af2 100644 --- a/source/tunneling/SecureTunnelWrapper.cpp +++ b/source/tunneling/SecureTunnelWrapper.cpp @@ -58,22 +58,22 @@ SecureTunnelWrapper::SecureTunnelWrapper( const Aws::Iotsecuretunneling::OnStreamReset &onStreamReset, const Aws::Iotsecuretunneling::OnSessionReset &onSessionReset) : secureTunnel((Aws::Iotsecuretunneling::SecureTunnelBuilder( - allocator, - *bootstrap, - socketOptions, - accessToken, - localProxyMode, - endpoint)) - .WithHttpClientConnectionProxyOptions(proxyOptions) - .WithRootCa(rootCa) - .WithOnConnectionComplete(onConnectionComplete) - .WithOnConnectionShutdown(onConnectionShutdown) - .WithOnSendDataComplete(onSendDataComplete) - .WithOnDataReceive(onDataReceive) - .WithOnStreamStart(onStreamStart) - .WithOnStreamReset(onSessionReset) - .WithOnSessionReset(onSessionReset) - .Build()) + allocator, + *bootstrap, + socketOptions, + accessToken, + localProxyMode, + endpoint)) + .WithHttpClientConnectionProxyOptions(proxyOptions) + .WithRootCa(rootCa) + .WithOnConnectionComplete(onConnectionComplete) + .WithOnConnectionShutdown(onConnectionShutdown) + .WithOnSendDataComplete(onSendDataComplete) + .WithOnDataReceive(onDataReceive) + .WithOnStreamStart(onStreamStart) + .WithOnStreamReset(onSessionReset) + .WithOnSessionReset(onSessionReset) + .Build()) { } diff --git a/source/tunneling/SecureTunnelingContext.cpp b/source/tunneling/SecureTunnelingContext.cpp index 1d5e48c9..234e0f2e 100644 --- a/source/tunneling/SecureTunnelingContext.cpp +++ b/source/tunneling/SecureTunnelingContext.cpp @@ -41,9 +41,9 @@ namespace Aws const string &endpoint, const int port, const OnConnectionShutdownFn &onConnectionShutdown) - : mSharedCrtResourceManager(manager), mProxyOptions(proxyOptions), mRootCa(rootCa.has_value() ? rootCa.value() : ""), - mAccessToken(accessToken), mEndpoint(endpoint), mPort(port), - mOnConnectionShutdown(onConnectionShutdown) + : mSharedCrtResourceManager(manager), mProxyOptions(proxyOptions), + mRootCa(rootCa.has_value() ? rootCa.value() : ""), mAccessToken(accessToken), mEndpoint(endpoint), + mPort(port), mOnConnectionShutdown(onConnectionShutdown) { } @@ -205,7 +205,8 @@ namespace Aws const Aws::Iotsecuretunneling::OnStreamReset &onStreamReset, const Aws::Iotsecuretunneling::OnSessionReset &onSessionReset) { - if (mProxyOptions.HostName.length() > 0) { + if (mProxyOptions.HostName.length() > 0) + { LOGM_INFO(TAG, "Creating Secure Tunneling with proxy to: %s", mProxyOptions.HostName.c_str()); return std::make_shared( mSharedCrtResourceManager->getAllocator(), @@ -224,7 +225,8 @@ namespace Aws onStreamReset, onSessionReset); } - else { + else + { return std::make_shared( mSharedCrtResourceManager->getAllocator(), mSharedCrtResourceManager->getClientBootstrap(), diff --git a/source/tunneling/SecureTunnelingFeature.cpp b/source/tunneling/SecureTunnelingFeature.cpp index 1d496675..b8b0d96e 100644 --- a/source/tunneling/SecureTunnelingFeature.cpp +++ b/source/tunneling/SecureTunnelingFeature.cpp @@ -94,7 +94,6 @@ namespace Aws void SecureTunnelingFeature::LoadFromConfig(const PlainConfig &config) { - // Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions; PlainConfig::HttpProxyConfig proxyConfig = config.httpProxyConfig; if (proxyConfig.httpProxyEnabled) @@ -118,7 +117,8 @@ namespace Aws basicAuthConfig.Username = proxyConfig.proxyUsername->c_str(); basicAuthConfig.Password = proxyConfig.proxyPassword->c_str(); proxyOptions.ProxyStrategy = - Aws::Crt::Http::HttpProxyStrategy::CreateBasicHttpProxyStrategy(basicAuthConfig, Aws::Crt::g_allocator); + Aws::Crt::Http::HttpProxyStrategy::CreateBasicHttpProxyStrategy( + basicAuthConfig, Aws::Crt::g_allocator); } else { From 0a3172b4caf95c5a8e98671eefa4e3711eef4c4d Mon Sep 17 00:00:00 2001 From: RogerZhongAWS Date: Tue, 12 Dec 2023 22:37:59 +0000 Subject: [PATCH 35/35] fix format --- source/tunneling/SecureTunnelingFeature.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/tunneling/SecureTunnelingFeature.cpp b/source/tunneling/SecureTunnelingFeature.cpp index b8b0d96e..6aaf5792 100644 --- a/source/tunneling/SecureTunnelingFeature.cpp +++ b/source/tunneling/SecureTunnelingFeature.cpp @@ -133,7 +133,10 @@ namespace Aws if (!config.tunneling.subscribeNotification) { - auto context = createContext(*config.tunneling.destinationAccessToken, *config.tunneling.region, static_cast(config.tunneling.port.value())); + auto context = createContext( + *config.tunneling.destinationAccessToken, + *config.tunneling.region, + static_cast(config.tunneling.port.value())); mContexts.push_back(std::move(context)); } }