Skip to content

Commit

Permalink
update cr
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazhvera committed Oct 25, 2023
1 parent 83bf93b commit 10b224f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 95 deletions.
2 changes: 1 addition & 1 deletion crt/aws-crt-cpp
2 changes: 1 addition & 1 deletion devicedefender/source/DeviceDefender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
178 changes: 87 additions & 91 deletions devicedefender/tests/DeviceDefenderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Aws::Crt::Mqtt5::Mqtt5Client> 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<Aws::Crt::Mqtt5::Mqtt5Client> 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<bool *>(a);
*data = true;
taskStopped = true;
cv.notify_one();
};
auto onCancelled = [&](void *a) -> void {
auto *data = reinterpret_cast<bool *>(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<Aws::Iotdevicedefenderv1::ReportTask> task = taskBuilder.Build();
std::shared_ptr<Aws::Iotdevicedefenderv1::ReportTask> 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<std::mutex> lock(mutex);
cv.wait(lock, [&]() { return taskStopped; });
}
{
std::unique_lock<std::mutex> 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;
}
Expand All @@ -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<Aws::Crt::Mqtt5::Mqtt5Client> mqtt5Client =
Aws::Crt::Mqtt5::Mqtt5Client::NewMqtt5Client(mqtt5Options, allocator);
ASSERT_TRUE(mqtt5Client);
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> 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<Aws::Iotdevicedefenderv1::ReportTask> task = taskBuilder.Build();
std::shared_ptr<Aws::Iotdevicedefenderv1::ReportTask> 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;
}
Expand Down
4 changes: 2 additions & 2 deletions samples/device_defender/mqtt5_basic_report/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <endpoint> --cert <path to the certificate> --key <path to the private key> --thing_name <thing name> --ca_file <path to root CA>
./mqtt5-basic-report --endpoint <endpoint> --cert <path to the certificate> --key <path to the private key> --thing_name <thing name> --ca_file <path to root CA>
```

You can also pass a Certificate Authority file (CA) if your certificate and key combination requires it:

``` sh
./basic-report --endpoint <endpoint> --cert <path to the certificate> --key <path to the private key> --thing_name <thing name> --ca_file <path to root CA>
./mqtt5-basic-report --endpoint <endpoint> --cert <path to the certificate> --key <path to the private key> --thing_name <thing name> --ca_file <path to root CA>
```

### Device Defender Data Requirements
Expand Down

0 comments on commit 10b224f

Please sign in to comment.