From 10b224fec7f10b53282c0f2af199774f97d8b746 Mon Sep 17 00:00:00 2001 From: Zhihui Xia Date: Wed, 25 Oct 2023 13:36:47 -0700 Subject: [PATCH] update cr --- crt/aws-crt-cpp | 2 +- devicedefender/source/DeviceDefender.cpp | 2 +- devicedefender/tests/DeviceDefenderTest.cpp | 178 +++++++++--------- .../mqtt5_basic_report/README.md | 4 +- 4 files changed, 91 insertions(+), 95 deletions(-) diff --git a/crt/aws-crt-cpp b/crt/aws-crt-cpp index 1201e4cd6..8fe495c42 160000 --- a/crt/aws-crt-cpp +++ b/crt/aws-crt-cpp @@ -1 +1 @@ -Subproject commit 1201e4cd6a8408da2f0609a9775858c2e76912a6 +Subproject commit 8fe495c4238ad1cf3a91e2ec7c7b8349a7cd4bd5 diff --git a/devicedefender/source/DeviceDefender.cpp b/devicedefender/source/DeviceDefender.cpp index 50023e78d..03eb3c1cb 100644 --- a/devicedefender/source/DeviceDefender.cpp +++ b/devicedefender/source/DeviceDefender.cpp @@ -307,7 +307,7 @@ namespace Aws const Crt::String &thingName) : ReportTaskBuilder( allocator, - Crt::Mqtt::MqttConnection::NewConnectionFromMqtt5Client(mqtt5Client), + Crt::Mqtt::MqttConnection::NewConnectionFromMqtt5Client(std::move(mqtt5Client)), eventLoopGroup, thingName) { diff --git a/devicedefender/tests/DeviceDefenderTest.cpp b/devicedefender/tests/DeviceDefenderTest.cpp index 26a5f8898..cb75c418c 100644 --- a/devicedefender/tests/DeviceDefenderTest.cpp +++ b/devicedefender/tests/DeviceDefenderTest.cpp @@ -140,78 +140,76 @@ AWS_TEST_CASE(DeviceDefenderFailedTest, s_TestDeviceDefenderFailedTest) static int s_TestMqtt5DeviceDefenderResourceSafety(Aws::Crt::Allocator *allocator, void *ctx) { (void)ctx; - { - Aws::Crt::ApiHandle apiHandle(allocator); - Aws::Iotdevicecommon::DeviceApiHandle deviceApiHandle(allocator); - Aws::Crt::Io::TlsContextOptions tlsCtxOptions = Aws::Crt::Io::TlsContextOptions::InitDefaultClient(); + Aws::Crt::ApiHandle apiHandle(allocator); + Aws::Iotdevicecommon::DeviceApiHandle deviceApiHandle(allocator); + Aws::Crt::Io::TlsContextOptions tlsCtxOptions = Aws::Crt::Io::TlsContextOptions::InitDefaultClient(); - Aws::Crt::Io::TlsContext tlsContext(tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT, allocator); - ASSERT_TRUE(tlsContext); + Aws::Crt::Io::TlsContext tlsContext(tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT, allocator); + ASSERT_TRUE(tlsContext); - Aws::Crt::Io::SocketOptions socketOptions; - socketOptions.SetConnectTimeoutMs(3000); + Aws::Crt::Io::SocketOptions socketOptions; + socketOptions.SetConnectTimeoutMs(3000); - Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator); - ASSERT_TRUE(eventLoopGroup); + Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator); + ASSERT_TRUE(eventLoopGroup); - Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8, 30, allocator); - ASSERT_TRUE(defaultHostResolver); + Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8, 30, allocator); + ASSERT_TRUE(defaultHostResolver); - Aws::Crt::Io::ClientBootstrap clientBootstrap(eventLoopGroup, defaultHostResolver, allocator); - ASSERT_TRUE(allocator); - clientBootstrap.EnableBlockingShutdown(); + Aws::Crt::Io::ClientBootstrap clientBootstrap(eventLoopGroup, defaultHostResolver, allocator); + ASSERT_TRUE(allocator); + clientBootstrap.EnableBlockingShutdown(); - Aws::Crt::Mqtt5::Mqtt5ClientOptions mqtt5Options(allocator); - mqtt5Options.WithBootstrap(&clientBootstrap); - mqtt5Options.WithSocketOptions(socketOptions); - mqtt5Options.WithTlsConnectionOptions(tlsContext.NewConnectionOptions()); - mqtt5Options.WithHostName("www.example.come"); - mqtt5Options.WithPort(443); - std::shared_ptr mqtt5Client = - Aws::Crt::Mqtt5::Mqtt5Client::NewMqtt5Client(mqtt5Options, allocator); - ASSERT_TRUE(mqtt5Client); + Aws::Crt::Mqtt5::Mqtt5ClientOptions mqtt5Options(allocator); + mqtt5Options.WithBootstrap(&clientBootstrap); + mqtt5Options.WithSocketOptions(socketOptions); + mqtt5Options.WithTlsConnectionOptions(tlsContext.NewConnectionOptions()); + mqtt5Options.WithHostName("www.example.come"); + mqtt5Options.WithPort(443); + std::shared_ptr mqtt5Client = + Aws::Crt::Mqtt5::Mqtt5Client::NewMqtt5Client(mqtt5Options, allocator); + ASSERT_TRUE(mqtt5Client); - const Aws::Crt::String thingName("TestThing"); - bool callbackSuccess = false; + const Aws::Crt::String thingName("TestThing"); + bool callbackSuccess = false; - std::mutex mutex; - std::condition_variable cv; - bool taskStopped = false; + std::mutex mutex; + std::condition_variable cv; + bool taskStopped = false; - auto onCancelled = [&](void *a) -> void { - auto *data = reinterpret_cast(a); - *data = true; - taskStopped = true; - cv.notify_one(); - }; + auto onCancelled = [&](void *a) -> void { + auto *data = reinterpret_cast(a); + *data = true; + taskStopped = true; + cv.notify_one(); + }; - Aws::Iotdevicedefenderv1::ReportTaskBuilder taskBuilder(allocator, mqtt5Client, eventLoopGroup, thingName); - taskBuilder.WithTaskPeriodSeconds((uint32_t)1UL) - .WithNetworkConnectionSamplePeriodSeconds((uint32_t)1UL) - .WithTaskCancelledHandler(onCancelled) - .WithTaskCancellationUserData(&callbackSuccess); + Aws::Iotdevicedefenderv1::ReportTaskBuilder taskBuilder(allocator, mqtt5Client, eventLoopGroup, thingName); + taskBuilder.WithTaskPeriodSeconds((uint32_t)1UL) + .WithNetworkConnectionSamplePeriodSeconds((uint32_t)1UL) + .WithTaskCancelledHandler(onCancelled) + .WithTaskCancellationUserData(&callbackSuccess); - std::shared_ptr task = taskBuilder.Build(); + std::shared_ptr task = taskBuilder.Build(); - ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int)task->GetStatus()); + ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int)task->GetStatus()); - ASSERT_SUCCESS(task->StartTask()); - ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Running, (int)task->GetStatus()); - ASSERT_FAILS(task->StartTask()); - ASSERT_TRUE(aws_last_error() == AWS_ERROR_INVALID_STATE); - task->StopTask(); + ASSERT_SUCCESS(task->StartTask()); + ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Running, (int)task->GetStatus()); + ASSERT_FAILS(task->StartTask()); + ASSERT_TRUE(aws_last_error() == AWS_ERROR_INVALID_STATE); + task->StopTask(); - ASSERT_TRUE(task->GetStatus() == Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped); + ASSERT_TRUE(task->GetStatus() == Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped); - { - std::unique_lock lock(mutex); - cv.wait(lock, [&]() { return taskStopped; }); - } + { + std::unique_lock lock(mutex); + cv.wait(lock, [&]() { return taskStopped; }); + } - ASSERT_TRUE(callbackSuccess); + ASSERT_TRUE(callbackSuccess); - ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int)task->GetStatus()); - } + ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int)task->GetStatus()); return AWS_ERROR_SUCCESS; } @@ -221,54 +219,52 @@ AWS_TEST_CASE(Mqtt5DeviceDefenderResourceSafety, s_TestMqtt5DeviceDefenderResour static int s_TestMqtt5DeviceDefenderFailedTest(Aws::Crt::Allocator *allocator, void *ctx) { (void)ctx; - { - Aws::Crt::ApiHandle apiHandle(allocator); - Aws::Iotdevicecommon::DeviceApiHandle deviceApiHandle(allocator); - Aws::Crt::Io::TlsContextOptions tlsCtxOptions = Aws::Crt::Io::TlsContextOptions::InitDefaultClient(); + Aws::Crt::ApiHandle apiHandle(allocator); + Aws::Iotdevicecommon::DeviceApiHandle deviceApiHandle(allocator); + Aws::Crt::Io::TlsContextOptions tlsCtxOptions = Aws::Crt::Io::TlsContextOptions::InitDefaultClient(); - Aws::Crt::Io::TlsContext tlsContext(tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT, allocator); - ASSERT_TRUE(tlsContext); + Aws::Crt::Io::TlsContext tlsContext(tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT, allocator); + ASSERT_TRUE(tlsContext); - Aws::Crt::Io::SocketOptions socketOptions; - socketOptions.SetConnectTimeoutMs(3000); + Aws::Crt::Io::SocketOptions socketOptions; + socketOptions.SetConnectTimeoutMs(3000); - Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator); - ASSERT_TRUE(eventLoopGroup); + Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator); + ASSERT_TRUE(eventLoopGroup); - Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8, 30, allocator); - ASSERT_TRUE(defaultHostResolver); + Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8, 30, allocator); + ASSERT_TRUE(defaultHostResolver); - Aws::Crt::Io::ClientBootstrap clientBootstrap(eventLoopGroup, defaultHostResolver, allocator); - ASSERT_TRUE(allocator); - clientBootstrap.EnableBlockingShutdown(); + Aws::Crt::Io::ClientBootstrap clientBootstrap(eventLoopGroup, defaultHostResolver, allocator); + ASSERT_TRUE(allocator); + clientBootstrap.EnableBlockingShutdown(); - Aws::Crt::Mqtt5::Mqtt5ClientOptions mqtt5Options(allocator); - mqtt5Options.WithBootstrap(&clientBootstrap); - mqtt5Options.WithSocketOptions(socketOptions); - mqtt5Options.WithTlsConnectionOptions(tlsContext.NewConnectionOptions()); - mqtt5Options.WithHostName("www.example.come"); - mqtt5Options.WithPort(443); + Aws::Crt::Mqtt5::Mqtt5ClientOptions mqtt5Options(allocator); + mqtt5Options.WithBootstrap(&clientBootstrap); + mqtt5Options.WithSocketOptions(socketOptions); + mqtt5Options.WithTlsConnectionOptions(tlsContext.NewConnectionOptions()); + mqtt5Options.WithHostName("www.example.come"); + mqtt5Options.WithPort(443); - std::shared_ptr mqtt5Client = - Aws::Crt::Mqtt5::Mqtt5Client::NewMqtt5Client(mqtt5Options, allocator); - ASSERT_TRUE(mqtt5Client); + std::shared_ptr mqtt5Client = + Aws::Crt::Mqtt5::Mqtt5Client::NewMqtt5Client(mqtt5Options, allocator); + ASSERT_TRUE(mqtt5Client); - const Aws::Crt::String thingName("TestThing"); - Aws::Crt::String data("TestData"); + const Aws::Crt::String thingName("TestThing"); + Aws::Crt::String data("TestData"); - Aws::Iotdevicedefenderv1::ReportTaskBuilder taskBuilder(allocator, mqtt5Client, eventLoopGroup, thingName); - taskBuilder.WithTaskPeriodSeconds((uint32_t)1UL) - .WithTaskPeriodSeconds((uint32_t)1UL) - .WithReportFormat(Aws::Iotdevicedefenderv1::ReportFormat::AWS_IDDRF_SHORT_JSON); + Aws::Iotdevicedefenderv1::ReportTaskBuilder taskBuilder(allocator, mqtt5Client, eventLoopGroup, thingName); + taskBuilder.WithTaskPeriodSeconds((uint32_t)1UL) + .WithTaskPeriodSeconds((uint32_t)1UL) + .WithReportFormat(Aws::Iotdevicedefenderv1::ReportFormat::AWS_IDDRF_SHORT_JSON); - std::shared_ptr task = taskBuilder.Build(); + std::shared_ptr task = taskBuilder.Build(); - ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int)task->GetStatus()); + ASSERT_INT_EQUALS((int)Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int)task->GetStatus()); - ASSERT_INT_EQUALS(AWS_ERROR_IOTDEVICE_DEFENDER_UNSUPPORTED_REPORT_FORMAT, task->LastError()); - ASSERT_FAILS(task->StartTask()); - ASSERT_TRUE(aws_last_error() == AWS_ERROR_INVALID_STATE); - } + ASSERT_INT_EQUALS(AWS_ERROR_IOTDEVICE_DEFENDER_UNSUPPORTED_REPORT_FORMAT, task->LastError()); + ASSERT_FAILS(task->StartTask()); + ASSERT_TRUE(aws_last_error() == AWS_ERROR_INVALID_STATE); return AWS_ERROR_SUCCESS; } diff --git a/samples/device_defender/mqtt5_basic_report/README.md b/samples/device_defender/mqtt5_basic_report/README.md index f280b5ea4..f22df1f9c 100644 --- a/samples/device_defender/mqtt5_basic_report/README.md +++ b/samples/device_defender/mqtt5_basic_report/README.md @@ -66,13 +66,13 @@ This sample expects and requires the following custom metrics: To run the Device Defender sample, use the following command: ``` sh -./basic-report --endpoint --cert --key --thing_name --ca_file +./mqtt5-basic-report --endpoint --cert --key --thing_name --ca_file ``` You can also pass a Certificate Authority file (CA) if your certificate and key combination requires it: ``` sh -./basic-report --endpoint --cert --key --thing_name --ca_file +./mqtt5-basic-report --endpoint --cert --key --thing_name --ca_file ``` ### Device Defender Data Requirements