6
6
#include " aws/crt/Types.h"
7
7
#include < aws/crt/Api.h>
8
8
9
+ #include < aws/crt/mqtt/Mqtt5Client.h>
10
+
9
11
#include < aws/iotdevicecommon/IotDevice.h>
10
12
#include < aws/iotdevicedefender/DeviceDefender.h>
11
13
#include < aws/testing/aws_test_harness.h>
@@ -32,9 +34,8 @@ static int s_TestDeviceDefenderCustomMetricSuccess(Aws::Crt::Allocator *allocato
32
34
Aws::Crt::Io::ClientBootstrap clientBootstrap (eventLoopGroup, defaultHostResolver, allocator);
33
35
clientBootstrap.EnableBlockingShutdown ();
34
36
Aws::Crt::Mqtt::MqttClient mqttClient (clientBootstrap, allocator);
35
- Aws::Crt::Mqtt::MqttClient mqttClientMoved = std::move (mqttClient);
36
37
37
- auto mqttConnection = mqttClientMoved .NewConnection (" www.example.com" , 443 , socketOptions, tlsContext);
38
+ auto mqttConnection = mqttClient .NewConnection (" www.example.com" , 443 , socketOptions, tlsContext);
38
39
const Aws::Crt::String thingName (" TestThing" );
39
40
bool callbackSuccess = false ;
40
41
@@ -114,12 +115,6 @@ static int s_TestDeviceDefenderCustomMetricSuccess(Aws::Crt::Allocator *allocato
114
115
}
115
116
116
117
ASSERT_TRUE (callbackSuccess);
117
-
118
- mqttConnection->Disconnect ();
119
- ASSERT_TRUE (mqttConnection);
120
-
121
- ASSERT_FALSE (mqttClient);
122
-
123
118
ASSERT_INT_EQUALS ((int )Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int )task->GetStatus ());
124
119
}
125
120
@@ -142,9 +137,8 @@ static int s_TestDeviceDefenderCustomMetricFail(Aws::Crt::Allocator *allocator,
142
137
Aws::Crt::Io::ClientBootstrap clientBootstrap (eventLoopGroup, defaultHostResolver, allocator);
143
138
clientBootstrap.EnableBlockingShutdown ();
144
139
Aws::Crt::Mqtt::MqttClient mqttClient (clientBootstrap, allocator);
145
- Aws::Crt::Mqtt::MqttClient mqttClientMoved = std::move (mqttClient);
146
140
147
- auto mqttConnection = mqttClientMoved .NewConnection (" www.example.com" , 443 , socketOptions, tlsContext);
141
+ auto mqttConnection = mqttClient .NewConnection (" www.example.com" , 443 , socketOptions, tlsContext);
148
142
const Aws::Crt::String thingName (" TestThing" );
149
143
bool callbackSuccess = false ;
150
144
@@ -187,13 +181,195 @@ static int s_TestDeviceDefenderCustomMetricFail(Aws::Crt::Allocator *allocator,
187
181
188
182
ASSERT_TRUE (callbackSuccess);
189
183
190
- mqttConnection->Disconnect ();
191
- ASSERT_TRUE (mqttConnection);
184
+ ASSERT_INT_EQUALS ((int )Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int )task->GetStatus ());
185
+ }
186
+ return AWS_ERROR_SUCCESS;
187
+ }
188
+ AWS_TEST_CASE (DeviceDefenderCustomMetricFail, s_TestDeviceDefenderCustomMetricFail)
189
+
190
+ static int s_TestMqtt5DeviceDefenderCustomMetricSuccess(Aws::Crt::Allocator *allocator, void *ctx)
191
+ {
192
+ (void )ctx;
193
+ {
194
+ Aws::Crt::ApiHandle apiHandle (allocator);
195
+ Aws::Iotdevicecommon::DeviceApiHandle deviceApiHandle (allocator);
196
+ Aws::Crt::Io::TlsContextOptions tlsCtxOptions = Aws::Crt::Io::TlsContextOptions::InitDefaultClient ();
197
+ Aws::Crt::Io::TlsContext tlsContext (tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT, allocator);
198
+ Aws::Crt::Io::SocketOptions socketOptions;
199
+ socketOptions.SetConnectTimeoutMs (3000 );
200
+ Aws::Crt::Io::EventLoopGroup eventLoopGroup (0 , allocator);
201
+ Aws::Crt::Io::DefaultHostResolver defaultHostResolver (eventLoopGroup, 8 , 30 , allocator);
202
+ Aws::Crt::Io::ClientBootstrap clientBootstrap (eventLoopGroup, defaultHostResolver, allocator);
203
+ clientBootstrap.EnableBlockingShutdown ();
204
+
205
+ Aws::Crt::Mqtt5::Mqtt5ClientOptions mqtt5Options (allocator);
206
+ mqtt5Options.WithBootstrap (&clientBootstrap);
207
+ mqtt5Options.WithSocketOptions (socketOptions);
208
+ mqtt5Options.WithTlsConnectionOptions (tlsContext.NewConnectionOptions ());
209
+ mqtt5Options.WithHostName (" www.example.come" );
210
+ mqtt5Options.WithPort (443 );
211
+
212
+ std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> mqtt5Client =
213
+ Aws::Crt::Mqtt5::Mqtt5Client::NewMqtt5Client (mqtt5Options, allocator);
214
+ ASSERT_TRUE (mqtt5Client);
215
+
216
+ const Aws::Crt::String thingName (" TestThing" );
217
+ bool callbackSuccess = false ;
218
+
219
+ std::mutex mutex;
220
+ std::condition_variable cv;
221
+ bool taskStopped = false ;
222
+
223
+ auto onCancelled = [&](void *a) -> void {
224
+ auto *data = reinterpret_cast <bool *>(a);
225
+ *data = true ;
226
+ taskStopped = true ;
227
+ cv.notify_one ();
228
+ };
229
+
230
+ Aws::Iotdevicedefenderv1::ReportTaskBuilder taskBuilder (allocator, mqtt5Client, eventLoopGroup, thingName);
231
+ taskBuilder.WithTaskPeriodSeconds ((uint32_t )1UL )
232
+ .WithNetworkConnectionSamplePeriodSeconds ((uint32_t )1UL )
233
+ .WithTaskCancelledHandler (onCancelled)
234
+ .WithTaskCancellationUserData (&callbackSuccess);
235
+
236
+ std::shared_ptr<Aws::Iotdevicedefenderv1::ReportTask> task = taskBuilder.Build ();
237
+
238
+ // ================
239
+ // Add the custom metrics
240
+ std::function<int (double *)> local_metric_number_func = [](double *output) {
241
+ *output = 10 ;
242
+ return AWS_OP_SUCCESS;
243
+ };
244
+ task->RegisterCustomMetricNumber (" CustomNumberOne" , std::move (local_metric_number_func));
245
+ std::function<int (double *)> global_metric_number_func_ref = global_metric_number_func;
246
+ task->RegisterCustomMetricNumber (" CustomNumberTwo" , std::move (global_metric_number_func_ref));
247
+
248
+ std::function<int (Aws::Crt::Vector<double > *)> local_metric_number_list_func =
249
+ [](Aws::Crt::Vector<double > *output) {
250
+ output->push_back (101 );
251
+ output->push_back (102 );
252
+ output->push_back (103 );
253
+ return AWS_OP_SUCCESS;
254
+ };
255
+ task->RegisterCustomMetricNumberList (" CustomNumberList" , std::move (local_metric_number_list_func));
256
+
257
+ std::function<int (Aws::Crt::Vector<Aws::Crt::String> *)> local_metric_str_list_func =
258
+ [](Aws::Crt::Vector<Aws::Crt::String> *output) {
259
+ output->push_back (" One Fish" );
260
+ output->push_back (" Two Fish" );
261
+ output->push_back (" Red Fish" );
262
+ output->push_back (" Blue Fish" );
263
+ return AWS_OP_SUCCESS;
264
+ };
265
+ task->RegisterCustomMetricStringList (" CustomStringList" , std::move (local_metric_str_list_func));
192
266
193
- ASSERT_FALSE (mqttClient);
267
+ std::function<int (Aws::Crt::Vector<Aws::Crt::String> *)> local_metric_ip_list_func =
268
+ [](Aws::Crt::Vector<Aws::Crt::String> *output) {
269
+ output->push_back (" 192.0.2.0" );
270
+ output->push_back (" 198.51.100.0" );
271
+ output->push_back (" 203.0.113.0" );
272
+ output->push_back (" 233.252.0.0" );
273
+ return AWS_OP_SUCCESS;
274
+ };
275
+ task->RegisterCustomMetricIpAddressList (" CustomIPList" , std::move (local_metric_ip_list_func));
276
+
277
+ // ================
278
+
279
+ ASSERT_INT_EQUALS ((int )Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int )task->GetStatus ());
280
+
281
+ ASSERT_SUCCESS (task->StartTask ());
282
+ ASSERT_INT_EQUALS ((int )Aws::Iotdevicedefenderv1::ReportTaskStatus::Running, (int )task->GetStatus ());
283
+ ASSERT_FAILS (task->StartTask ());
284
+ ASSERT_TRUE (aws_last_error () == AWS_ERROR_INVALID_STATE);
285
+ task->StopTask ();
286
+
287
+ ASSERT_TRUE (task->GetStatus () == Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped);
288
+
289
+ {
290
+ std::unique_lock<std::mutex> lock (mutex);
291
+ cv.wait (lock, [&]() { return taskStopped; });
292
+ }
293
+
294
+ ASSERT_TRUE (callbackSuccess);
194
295
195
296
ASSERT_INT_EQUALS ((int )Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int )task->GetStatus ());
196
297
}
298
+
197
299
return AWS_ERROR_SUCCESS;
198
300
}
199
- AWS_TEST_CASE (DeviceDefenderCustomMetricFail, s_TestDeviceDefenderCustomMetricFail)
301
+ AWS_TEST_CASE (Mqtt5DeviceDefenderCustomMetricSuccess, s_TestMqtt5DeviceDefenderCustomMetricSuccess)
302
+
303
+ static int s_TestMqtt5DeviceDefenderCustomMetricFail(Aws::Crt::Allocator *allocator, void *ctx)
304
+ {
305
+ (void )ctx;
306
+ {
307
+ Aws::Crt::ApiHandle apiHandle (allocator);
308
+ Aws::Iotdevicecommon::DeviceApiHandle deviceApiHandle (allocator);
309
+ Aws::Crt::Io::TlsContextOptions tlsCtxOptions = Aws::Crt::Io::TlsContextOptions::InitDefaultClient ();
310
+ Aws::Crt::Io::TlsContext tlsContext (tlsCtxOptions, Aws::Crt::Io::TlsMode::CLIENT, allocator);
311
+ Aws::Crt::Io::SocketOptions socketOptions;
312
+ socketOptions.SetConnectTimeoutMs (3000 );
313
+ Aws::Crt::Io::EventLoopGroup eventLoopGroup (0 , allocator);
314
+ Aws::Crt::Io::DefaultHostResolver defaultHostResolver (eventLoopGroup, 8 , 30 , allocator);
315
+ Aws::Crt::Io::ClientBootstrap clientBootstrap (eventLoopGroup, defaultHostResolver, allocator);
316
+ clientBootstrap.EnableBlockingShutdown ();
317
+
318
+ Aws::Crt::Mqtt5::Mqtt5ClientOptions mqtt5Options (allocator);
319
+ mqtt5Options.WithBootstrap (&clientBootstrap);
320
+ mqtt5Options.WithSocketOptions (socketOptions);
321
+ mqtt5Options.WithTlsConnectionOptions (tlsContext.NewConnectionOptions ());
322
+ mqtt5Options.WithHostName (" www.example.come" );
323
+ mqtt5Options.WithPort (443 );
324
+
325
+ std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> mqtt5Client =
326
+ Aws::Crt::Mqtt5::Mqtt5Client::NewMqtt5Client (mqtt5Options, allocator);
327
+ ASSERT_TRUE (mqtt5Client);
328
+
329
+ const Aws::Crt::String thingName (" TestThing" );
330
+ bool callbackSuccess = false ;
331
+
332
+ std::mutex mutex;
333
+ std::condition_variable cv;
334
+ bool taskStopped = false ;
335
+
336
+ auto onCancelled = [&](void *a) -> void {
337
+ auto *data = reinterpret_cast <bool *>(a);
338
+ *data = true ;
339
+ taskStopped = true ;
340
+ cv.notify_one ();
341
+ };
342
+
343
+ Aws::Iotdevicedefenderv1::ReportTaskBuilder taskBuilder (allocator, mqtt5Client, eventLoopGroup, thingName);
344
+ taskBuilder.WithTaskPeriodSeconds ((uint32_t )1UL )
345
+ .WithNetworkConnectionSamplePeriodSeconds ((uint32_t )1UL )
346
+ .WithTaskCancelledHandler (onCancelled)
347
+ .WithTaskCancellationUserData (&callbackSuccess);
348
+
349
+ std::shared_ptr<Aws::Iotdevicedefenderv1::ReportTask> task = taskBuilder.Build ();
350
+
351
+ // Add the error custom metric
352
+ std::function<int (double *)> number_metric_func = [](double *output) {
353
+ *output = 10 ;
354
+ return AWS_OP_ERR;
355
+ };
356
+ task->RegisterCustomMetricNumber (" CustomNumberOne" , std::move (number_metric_func));
357
+
358
+ ASSERT_INT_EQUALS ((int )Aws::Iotdevicedefenderv1::ReportTaskStatus::Ready, (int )task->GetStatus ());
359
+
360
+ task->StartTask ();
361
+ task->StopTask ();
362
+
363
+ ASSERT_TRUE (task->GetStatus () == Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped);
364
+ {
365
+ std::unique_lock<std::mutex> lock (mutex);
366
+ cv.wait (lock, [&]() { return taskStopped; });
367
+ }
368
+
369
+ ASSERT_TRUE (callbackSuccess);
370
+
371
+ ASSERT_INT_EQUALS ((int )Aws::Iotdevicedefenderv1::ReportTaskStatus::Stopped, (int )task->GetStatus ());
372
+ }
373
+ return AWS_ERROR_SUCCESS;
374
+ }
375
+ AWS_TEST_CASE (Mqtt5DeviceDefenderCustomMetricFail, s_TestMqtt5DeviceDefenderCustomMetricFail)
0 commit comments