forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestInteractionModelEngine.cpp
760 lines (593 loc) · 33.7 KB
/
TestInteractionModelEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <app/AppConfig.h>
#include <app/InteractionModelEngine.h>
#include <app/icd/server/ICDServerConfig.h>
#include <app/reporting/tests/MockReportScheduler.h>
#include <app/tests/AppTestContext.h>
#include <app/util/mock/Constants.h>
#include <app/util/mock/Functions.h>
#include <lib/core/CASEAuthTag.h>
#include <lib/core/ErrorStr.h>
#include <lib/core/StringBuilderAdapters.h>
#include <lib/core/TLV.h>
#include <lib/core/TLVDebug.h>
#include <lib/core/TLVUtilities.h>
#include <lib/support/tests/ExtraPwTestMacros.h>
#include <messaging/ExchangeContext.h>
#include <messaging/Flags.h>
#include <platform/CHIPDeviceLayer.h>
#include <pw_unit_test/framework.h>
#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
#include <app/SimpleSubscriptionResumptionStorage.h>
#include <lib/support/TestPersistentStorageDelegate.h>
#endif // CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
namespace {
using TestContext = chip::Test::AppContext;
class NullReadHandlerCallback : public chip::app::ReadHandler::ManagementCallback
{
public:
void OnDone(chip::app::ReadHandler & apReadHandlerObj) override {}
chip::app::ReadHandler::ApplicationCallback * GetAppCallback() override { return nullptr; }
chip::app::InteractionModelEngine * GetInteractionModelEngine() override
{
return chip::app::InteractionModelEngine::GetInstance();
}
};
} // namespace
namespace chip {
namespace app {
class TestInteractionModelEngine : public ::testing::Test
{
public:
static void SetUpTestSuite()
{
mpTestContext = new chip::Test::AppContext;
mpTestContext->SetUpTestSuite();
}
static void TearDownTestSuite()
{
mpTestContext->TearDownTestSuite();
if (mpTestContext != nullptr)
{
delete mpTestContext;
}
}
void SetUp() override
{
if (mpTestContext != nullptr)
{
mpTestContext->SetUp();
}
}
void TearDown() override
{
if (mpTestContext != nullptr)
{
mpTestContext->TearDown();
}
}
void TestSubjectHasActiveSubscriptionSingleSubOneEntry();
void TestSubjectHasActiveSubscriptionSingleSubMultipleEntries();
void TestSubjectHasActiveSubscriptionMultipleSubsSingleEntry();
void TestSubjectHasActiveSubscriptionMultipleSubsMultipleEntries();
void TestSubjectHasActiveSubscriptionSubWithCAT();
void TestSubscriptionResumptionTimer();
void TestDecrementNumSubscriptionsToResume();
static int GetAttributePathListLength(SingleLinkedListNode<AttributePathParams> * apattributePathParamsList);
static chip::Test::AppContext * mpTestContext;
};
chip::Test::AppContext * TestInteractionModelEngine::mpTestContext = nullptr;
int TestInteractionModelEngine::GetAttributePathListLength(SingleLinkedListNode<AttributePathParams> * apAttributePathParamsList)
{
int length = 0;
SingleLinkedListNode<AttributePathParams> * runner = apAttributePathParamsList;
while (runner != nullptr)
{
runner = runner->mpNext;
length++;
}
return length;
}
TEST_F(TestInteractionModelEngine, TestAttributePathParamsPushRelease)
{
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
EXPECT_EQ(engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
app::reporting::GetDefaultReportScheduler()),
CHIP_NO_ERROR);
SingleLinkedListNode<AttributePathParams> * attributePathParamsList = nullptr;
AttributePathParams attributePathParams1;
AttributePathParams attributePathParams2;
AttributePathParams attributePathParams3;
attributePathParams1.mEndpointId = 1;
attributePathParams2.mEndpointId = 2;
attributePathParams3.mEndpointId = 3;
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams1);
ASSERT_NE(attributePathParamsList, nullptr);
EXPECT_EQ(attributePathParams1.mEndpointId, attributePathParamsList->mValue.mEndpointId);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 1);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams2);
ASSERT_NE(attributePathParamsList, nullptr);
EXPECT_EQ(attributePathParams2.mEndpointId, attributePathParamsList->mValue.mEndpointId);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 2);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams3);
ASSERT_NE(attributePathParamsList, nullptr);
EXPECT_EQ(attributePathParams3.mEndpointId, attributePathParamsList->mValue.mEndpointId);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 3);
engine->ReleaseAttributePathList(attributePathParamsList);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 0);
}
TEST_F(TestInteractionModelEngine, TestRemoveDuplicateConcreteAttribute)
{
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
EXPECT_EQ(CHIP_NO_ERROR,
engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
app::reporting::GetDefaultReportScheduler()));
SingleLinkedListNode<AttributePathParams> * attributePathParamsList = nullptr;
AttributePathParams attributePathParams1;
AttributePathParams attributePathParams2;
AttributePathParams attributePathParams3;
// Three concrete paths, no duplicates
attributePathParams1.mEndpointId = chip::Test::kMockEndpoint3;
attributePathParams1.mClusterId = chip::Test::MockClusterId(2);
attributePathParams1.mAttributeId = chip::Test::MockAttributeId(1);
attributePathParams2.mEndpointId = chip::Test::kMockEndpoint3;
attributePathParams2.mClusterId = chip::Test::MockClusterId(2);
attributePathParams2.mAttributeId = chip::Test::MockAttributeId(2);
attributePathParams3.mEndpointId = chip::Test::kMockEndpoint3;
attributePathParams3.mClusterId = chip::Test::MockClusterId(2);
attributePathParams3.mAttributeId = chip::Test::MockAttributeId(3);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams1);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams2);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams3);
engine->RemoveDuplicateConcreteAttributePath(attributePathParamsList);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 3);
engine->ReleaseAttributePathList(attributePathParamsList);
attributePathParams1.mEndpointId = kInvalidEndpointId;
attributePathParams1.mClusterId = kInvalidClusterId;
attributePathParams1.mAttributeId = kInvalidAttributeId;
attributePathParams2.mEndpointId = chip::Test::kMockEndpoint3;
attributePathParams2.mClusterId = chip::Test::MockClusterId(2);
attributePathParams2.mAttributeId = chip::Test::MockAttributeId(2);
attributePathParams3.mEndpointId = chip::Test::kMockEndpoint3;
attributePathParams3.mClusterId = chip::Test::MockClusterId(2);
attributePathParams3.mAttributeId = chip::Test::MockAttributeId(3);
// 1st path is wildcard endpoint, 2nd, 3rd paths are concrete paths, the concrete ones would be removed.
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams1);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams2);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams3);
engine->RemoveDuplicateConcreteAttributePath(attributePathParamsList);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 1);
engine->ReleaseAttributePathList(attributePathParamsList);
// 2nd path is wildcard endpoint, 1st, 3rd paths are concrete paths, the latter two would be removed.
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams2);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams1);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams3);
engine->RemoveDuplicateConcreteAttributePath(attributePathParamsList);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 1);
engine->ReleaseAttributePathList(attributePathParamsList);
// 3nd path is wildcard endpoint, 1st, 2nd paths are concrete paths, the latter two would be removed.
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams2);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams3);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams1);
engine->RemoveDuplicateConcreteAttributePath(attributePathParamsList);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 1);
engine->ReleaseAttributePathList(attributePathParamsList);
attributePathParams1.mEndpointId = chip::Test::kMockEndpoint3;
attributePathParams1.mClusterId = chip::Test::MockClusterId(2);
attributePathParams1.mAttributeId = kInvalidAttributeId;
attributePathParams2.mEndpointId = chip::Test::kMockEndpoint2;
attributePathParams2.mClusterId = chip::Test::MockClusterId(2);
attributePathParams2.mAttributeId = chip::Test::MockAttributeId(2);
attributePathParams3.mEndpointId = chip::Test::kMockEndpoint2;
attributePathParams3.mClusterId = chip::Test::MockClusterId(2);
attributePathParams3.mAttributeId = chip::Test::MockAttributeId(3);
// 1st is wildcard one, but not intersect with the latter two concrete paths, so the paths in total are 3 finally
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams1);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams2);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams3);
engine->RemoveDuplicateConcreteAttributePath(attributePathParamsList);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 3);
engine->ReleaseAttributePathList(attributePathParamsList);
attributePathParams1.mEndpointId = kInvalidEndpointId;
attributePathParams1.mClusterId = kInvalidClusterId;
attributePathParams1.mAttributeId = kInvalidAttributeId;
attributePathParams2.mEndpointId = chip::Test::kMockEndpoint3;
attributePathParams2.mClusterId = kInvalidClusterId;
attributePathParams2.mAttributeId = kInvalidAttributeId;
attributePathParams3.mEndpointId = kInvalidEndpointId;
attributePathParams3.mClusterId = kInvalidClusterId;
attributePathParams3.mAttributeId = chip::Test::MockAttributeId(3);
// Wildcards cannot be deduplicated.
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams1);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams2);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams3);
engine->RemoveDuplicateConcreteAttributePath(attributePathParamsList);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 3);
engine->ReleaseAttributePathList(attributePathParamsList);
attributePathParams1.mEndpointId = kInvalidEndpointId;
attributePathParams1.mClusterId = chip::Test::MockClusterId(2);
attributePathParams1.mAttributeId = chip::Test::MockAttributeId(10);
attributePathParams2.mEndpointId = chip::Test::kMockEndpoint3;
attributePathParams2.mClusterId = chip::Test::MockClusterId(2);
attributePathParams2.mAttributeId = chip::Test::MockAttributeId(10);
// 1st path is wildcard endpoint, 2nd path is invalid attribute
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams1);
engine->PushFrontAttributePathList(attributePathParamsList, attributePathParams2);
engine->RemoveDuplicateConcreteAttributePath(attributePathParamsList);
EXPECT_EQ(GetAttributePathListLength(attributePathParamsList), 2);
engine->ReleaseAttributePathList(attributePathParamsList);
}
/**
* @brief Test verifies the SubjectHasActiveSubscription with a single subscription with a single entry
*/
TEST_F_FROM_FIXTURE(TestInteractionModelEngine, TestSubjectHasActiveSubscriptionSingleSubOneEntry)
{
NullReadHandlerCallback nullCallback;
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
NodeId bobNodeId = 0x12344321ull;
FabricIndex bobFabricIndex = 1;
// Create ExchangeContext
Messaging::ExchangeContext * exchangeCtx1 = mpTestContext->NewExchangeToBob(nullptr, false);
ASSERT_TRUE(exchangeCtx1);
// InteractionModelEngine init
EXPECT_EQ(CHIP_NO_ERROR,
engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
reporting::GetDefaultReportScheduler()));
// Verify that there are no active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Create and setup readHandler 1
ReadHandler * readHandler1 = engine->GetReadHandlerPool().CreateObject(
nullCallback, exchangeCtx1, ReadHandler::InteractionType::Subscribe, reporting::GetDefaultReportScheduler());
// Verify that Bob still doesn't have an active subscription
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Set readHandler1 to active
readHandler1->SetStateFlag(ReadHandler::ReadHandlerFlags::ActiveSubscription, true);
// Verify that Bob still has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Clean up read handlers
engine->GetReadHandlerPool().ReleaseAll();
// Verify that there are no active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
}
/**
* @brief Test verifies that the SubjectHasActiveSubscription will continue iterating till it fines at least one valid active
* subscription
*/
TEST_F_FROM_FIXTURE(TestInteractionModelEngine, TestSubjectHasActiveSubscriptionSingleSubMultipleEntries)
{
NullReadHandlerCallback nullCallback;
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
NodeId bobNodeId = 0x12344321ull;
FabricIndex bobFabricIndex = 1;
// Create ExchangeContexts
Messaging::ExchangeContext * exchangeCtx1 = mpTestContext->NewExchangeToBob(nullptr, false);
ASSERT_TRUE(exchangeCtx1);
Messaging::ExchangeContext * exchangeCtx2 = mpTestContext->NewExchangeToBob(nullptr, false);
ASSERT_TRUE(exchangeCtx1);
// InteractionModelEngine init
EXPECT_EQ(CHIP_NO_ERROR,
engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
reporting::GetDefaultReportScheduler()));
// Verify that both Alice and Bob have no active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Create readHandler 1
engine->GetReadHandlerPool().CreateObject(nullCallback, exchangeCtx1, ReadHandler::InteractionType::Subscribe,
reporting::GetDefaultReportScheduler());
// Verify that Bob still doesn't have an active subscription
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Create and setup readHandler 2
ReadHandler * readHandler2 = engine->GetReadHandlerPool().CreateObject(
nullCallback, exchangeCtx2, ReadHandler::InteractionType::Subscribe, reporting::GetDefaultReportScheduler());
// Verify that Bob still doesn't have an active subscription
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Set readHandler2 to active
readHandler2->SetStateFlag(ReadHandler::ReadHandlerFlags::ActiveSubscription, true);
// Verify that Bob has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Release active ReadHandler
engine->GetReadHandlerPool().ReleaseObject(readHandler2);
// Verify that there are no active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Clean up read handlers
engine->GetReadHandlerPool().ReleaseAll();
}
/**
* @brief Test validates that the SubjectHasActiveSubscription can support multiple subscriptions from different clients
*/
TEST_F_FROM_FIXTURE(TestInteractionModelEngine, TestSubjectHasActiveSubscriptionMultipleSubsSingleEntry)
{
NullReadHandlerCallback nullCallback;
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
NodeId bobNodeId = 0x12344321ull;
FabricIndex bobFabricIndex = 1;
NodeId aliceNodeId = 0x11223344ull;
FabricIndex aliceFabricIndex = 2;
// Create ExchangeContexts
Messaging::ExchangeContext * exchangeCtx1 = mpTestContext->NewExchangeToBob(nullptr, false);
ASSERT_TRUE(exchangeCtx1);
Messaging::ExchangeContext * exchangeCtx2 = mpTestContext->NewExchangeToAlice(nullptr, false);
ASSERT_TRUE(exchangeCtx2);
// InteractionModelEngine init
EXPECT_EQ(CHIP_NO_ERROR,
engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
reporting::GetDefaultReportScheduler()));
// Verify that both Alice and Bob have no active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
EXPECT_FALSE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
// Create and setup readHandler 1
ReadHandler * readHandler1 = engine->GetReadHandlerPool().CreateObject(
nullCallback, exchangeCtx1, ReadHandler::InteractionType::Subscribe, reporting::GetDefaultReportScheduler());
// Create and setup readHandler 2
ReadHandler * readHandler2 = engine->GetReadHandlerPool().CreateObject(
nullCallback, exchangeCtx2, ReadHandler::InteractionType::Subscribe, reporting::GetDefaultReportScheduler());
// Verify that Bob still doesn't have an active subscription
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Set readHandler1 to active
readHandler1->SetStateFlag(ReadHandler::ReadHandlerFlags::ActiveSubscription, true);
// Verify that Bob has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Verify that Alice still doesn't have an active subscription
EXPECT_FALSE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
// Set readHandler2 to active
readHandler2->SetStateFlag(ReadHandler::ReadHandlerFlags::ActiveSubscription, true);
// Verify that Bob has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Verify that Alice has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
// Set readHandler1 to inactive
readHandler1->SetStateFlag(ReadHandler::ReadHandlerFlags::ActiveSubscription, false);
// Verify that Bob doesn't have an active subscription
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Verify that Alice still has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
// Clean up read handlers
engine->GetReadHandlerPool().ReleaseAll();
// Verify that both Alice and Bob have no active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
EXPECT_FALSE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
}
/**
* @brief Test validates that the SubjectHasActiveSubscription can find the active subscription even if there are multiple
* subscriptions for each client
*/
TEST_F_FROM_FIXTURE(TestInteractionModelEngine, TestSubjectHasActiveSubscriptionMultipleSubsMultipleEntries)
{
NullReadHandlerCallback nullCallback;
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
NodeId bobNodeId = 0x12344321ull;
FabricIndex bobFabricIndex = 1;
NodeId aliceNodeId = 0x11223344ull;
FabricIndex aliceFabricIndex = 2;
// Create ExchangeContexts
Messaging::ExchangeContext * exchangeCtx11 = mpTestContext->NewExchangeToBob(nullptr, false);
ASSERT_TRUE(exchangeCtx11);
Messaging::ExchangeContext * exchangeCtx12 = mpTestContext->NewExchangeToBob(nullptr, false);
ASSERT_TRUE(exchangeCtx12);
Messaging::ExchangeContext * exchangeCtx21 = mpTestContext->NewExchangeToAlice(nullptr, false);
ASSERT_TRUE(exchangeCtx21);
Messaging::ExchangeContext * exchangeCtx22 = mpTestContext->NewExchangeToAlice(nullptr, false);
ASSERT_TRUE(exchangeCtx22);
// InteractionModelEngine init
EXPECT_EQ(CHIP_NO_ERROR,
engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
reporting::GetDefaultReportScheduler()));
// Verify that both Alice and Bob have no active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
EXPECT_FALSE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
// Create and setup readHandler 1-1
engine->GetReadHandlerPool().CreateObject(nullCallback, exchangeCtx11, ReadHandler::InteractionType::Subscribe,
reporting::GetDefaultReportScheduler());
// Create and setup readHandler 1-2
ReadHandler * readHandler12 = engine->GetReadHandlerPool().CreateObject(
nullCallback, exchangeCtx12, ReadHandler::InteractionType::Subscribe, reporting::GetDefaultReportScheduler());
// Create and setup readHandler 2-1
engine->GetReadHandlerPool().CreateObject(nullCallback, exchangeCtx21, ReadHandler::InteractionType::Subscribe,
reporting::GetDefaultReportScheduler());
// Create and setup readHandler 2-2
ReadHandler * readHandler22 = engine->GetReadHandlerPool().CreateObject(
nullCallback, exchangeCtx22, ReadHandler::InteractionType::Subscribe, reporting::GetDefaultReportScheduler());
// Verify that both Alice and Bob have no active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
EXPECT_FALSE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
// Set readHandler 1-2 to active
readHandler12->SetStateFlag(ReadHandler::ReadHandlerFlags::ActiveSubscription, true);
// Verify that Bob has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Verify that Alice still doesn't have an active subscription
EXPECT_FALSE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
// Set readHandler 2-2 to active
readHandler22->SetStateFlag(ReadHandler::ReadHandlerFlags::ActiveSubscription, true);
// Verify that Bob has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Verify that Alice has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
// Set readHandler1 to inactive
readHandler12->SetStateFlag(ReadHandler::ReadHandlerFlags::ActiveSubscription, false);
// Verify that Bob doesn't have an active subscription
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
// Verify that Alice still has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
// Clean up read handlers
engine->GetReadHandlerPool().ReleaseAll();
// Verify that both Alice and Bob have no active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, bobNodeId));
EXPECT_FALSE(engine->SubjectHasActiveSubscription(aliceFabricIndex, aliceNodeId));
}
/**
* @brief Verifies that SubjectHasActiveSubscription support CATs as a subject-id
*/
TEST_F_FROM_FIXTURE(TestInteractionModelEngine, TestSubjectHasActiveSubscriptionSubWithCAT)
{
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
NullReadHandlerCallback nullCallback;
CASEAuthTag cat = 0x1111'0001;
CASEAuthTag invalidCAT = 0x1112'0001;
CATValues cats = CATValues{ { cat } };
NodeId valideSubjectId = NodeIdFromCASEAuthTag(cat);
NodeId invalideSubjectId = NodeIdFromCASEAuthTag(invalidCAT);
FabricIndex bobFabricIndex = 1;
// InteractionModelEngine init
EXPECT_EQ(CHIP_NO_ERROR,
engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
reporting::GetDefaultReportScheduler()));
// Make sure we are using CASE sessions, because there is no defunct-marking for PASE.
mpTestContext->ExpireSessionBobToAlice();
mpTestContext->ExpireSessionAliceToBob();
EXPECT_EQ(CHIP_NO_ERROR, mpTestContext->CreateCASESessionBobToAlice(cats));
EXPECT_EQ(CHIP_NO_ERROR, mpTestContext->CreateCASESessionAliceToBob(cats));
// Create ExchangeContexts
Messaging::ExchangeContext * exchangeCtx = mpTestContext->NewExchangeToBob(nullptr, false);
ASSERT_TRUE(exchangeCtx);
// Create readHandler
ReadHandler * readHandler = engine->GetReadHandlerPool().CreateObject(
nullCallback, exchangeCtx, ReadHandler::InteractionType::Subscribe, reporting::GetDefaultReportScheduler());
// Verify there are not active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, valideSubjectId));
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, invalideSubjectId));
// Set readHandler to active
readHandler->SetStateFlag(ReadHandler::ReadHandlerFlags::ActiveSubscription, true);
// Verify tthat valid subjectID has an active subscription
EXPECT_TRUE(engine->SubjectHasActiveSubscription(bobFabricIndex, valideSubjectId));
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, invalideSubjectId));
// Clean up read handlers
engine->GetReadHandlerPool().ReleaseAll();
// Verify there are not active subscriptions
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, valideSubjectId));
EXPECT_FALSE(engine->SubjectHasActiveSubscription(bobFabricIndex, invalideSubjectId));
}
#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
/**
* @brief Test verifies the SubjectHasPersistedSubscription with single and multiple persisted subscriptions.
*/
TEST_F(TestInteractionModelEngine, TestSubjectHasPersistedSubscription)
{
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
chip::TestPersistentStorageDelegate storage;
chip::app::SimpleSubscriptionResumptionStorage subscriptionStorage;
EXPECT_EQ(subscriptionStorage.Init(&storage), CHIP_NO_ERROR);
EXPECT_EQ(CHIP_NO_ERROR,
engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
app::reporting::GetDefaultReportScheduler(), nullptr, &subscriptionStorage));
NodeId nodeId1 = 1;
FabricIndex fabric1 = 1;
SubscriptionId sub1 = 1;
NodeId nodeId2 = 2;
FabricIndex fabric2 = 2;
SubscriptionId sub2 = 2;
SubscriptionResumptionStorage::SubscriptionInfo info1 = { .mNodeId = nodeId1,
.mFabricIndex = fabric1,
.mSubscriptionId = sub1 };
SubscriptionResumptionStorage::SubscriptionInfo info2 = { .mNodeId = nodeId2,
.mFabricIndex = fabric2,
.mSubscriptionId = sub2 };
// Test with no persisted subscriptions - Should return false
EXPECT_FALSE(engine->SubjectHasPersistedSubscription(fabric1, nodeId1));
// Add one entry
EXPECT_EQ(CHIP_NO_ERROR, subscriptionStorage.Save(info1));
// Verify that entry matches - Should return true
EXPECT_TRUE(engine->SubjectHasPersistedSubscription(fabric1, nodeId1));
// Test with absent subscription - Should return false
EXPECT_FALSE(engine->SubjectHasPersistedSubscription(fabric2, nodeId2));
// Add second entry
EXPECT_EQ(CHIP_NO_ERROR, subscriptionStorage.Save(info2));
// Verify that entry matches - Should return true
EXPECT_TRUE(engine->SubjectHasPersistedSubscription(fabric2, nodeId2));
EXPECT_TRUE(engine->SubjectHasPersistedSubscription(fabric1, nodeId1));
// Remove an entry
EXPECT_EQ(CHIP_NO_ERROR, subscriptionStorage.Delete(nodeId1, fabric1, sub1));
// Test with absent subscription - Should return false
EXPECT_FALSE(engine->SubjectHasPersistedSubscription(fabric1, nodeId1));
// Clean Up entries
subscriptionStorage.DeleteAll(fabric1);
subscriptionStorage.DeleteAll(fabric2);
}
#if CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
TEST_F_FROM_FIXTURE(TestInteractionModelEngine, TestSubscriptionResumptionTimer)
{
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
EXPECT_EQ(engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
app::reporting::GetDefaultReportScheduler()),
CHIP_NO_ERROR);
uint32_t timeTillNextResubscriptionMs;
engine->mNumSubscriptionResumptionRetries = 0;
timeTillNextResubscriptionMs = engine->ComputeTimeSecondsTillNextSubscriptionResumption();
EXPECT_EQ(timeTillNextResubscriptionMs, (unsigned) CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION_MIN_RETRY_INTERVAL_SECS);
uint32_t lastTimeTillNextResubscriptionMs = timeTillNextResubscriptionMs;
for (engine->mNumSubscriptionResumptionRetries = 1;
engine->mNumSubscriptionResumptionRetries <= CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION_MAX_FIBONACCI_STEP_INDEX;
engine->mNumSubscriptionResumptionRetries++)
{
timeTillNextResubscriptionMs = engine->ComputeTimeSecondsTillNextSubscriptionResumption();
EXPECT_GE(timeTillNextResubscriptionMs, lastTimeTillNextResubscriptionMs);
EXPECT_LT(timeTillNextResubscriptionMs, (unsigned) CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION_MAX_RETRY_INTERVAL_SECS);
lastTimeTillNextResubscriptionMs = timeTillNextResubscriptionMs;
}
engine->mNumSubscriptionResumptionRetries = 2000;
timeTillNextResubscriptionMs = engine->ComputeTimeSecondsTillNextSubscriptionResumption();
EXPECT_EQ(timeTillNextResubscriptionMs, (unsigned) CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION_MAX_RETRY_INTERVAL_SECS);
}
#endif // CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
TEST_F_FROM_FIXTURE(TestInteractionModelEngine, TestDecrementNumSubscriptionsToResume)
{
InteractionModelEngine * engine = InteractionModelEngine::GetInstance();
constexpr uint8_t kNumberOfSubsToResume = 5;
uint8_t numberOfSubsRemaining = kNumberOfSubsToResume;
EXPECT_EQ(engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
app::reporting::GetDefaultReportScheduler()),
CHIP_NO_ERROR);
#if CHIP_CONFIG_ENABLE_ICD_CIP && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
ICDManager manager;
engine->SetICDManager(&manager);
#endif // CHIP_CONFIG_ENABLE_ICD_CIP && CHIP_CONFIG_PERSIST_SUBSCRIPTIONS && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
// Set number of subs
engine->mNumOfSubscriptionsToResume = kNumberOfSubsToResume;
#if CHIP_CONFIG_ENABLE_ICD_CIP && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
// Verify mIsBootUpResumeSubscriptionExecuted has not been set
EXPECT_FALSE(manager.GetIsBootUpResumeSubscriptionExecuted());
#endif // CHIP_CONFIG_ENABLE_ICD_CIP && CHIP_CONFIG_PERSIST_SUBSCRIPTIONS && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
// Decrease number of subs by 1
numberOfSubsRemaining--;
engine->DecrementNumSubscriptionsToResume();
EXPECT_EQ(numberOfSubsRemaining, engine->mNumOfSubscriptionsToResume);
// Decrease to 0 subs remaining
while (numberOfSubsRemaining > 0)
{
#if CHIP_CONFIG_ENABLE_ICD_CIP && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
// Verify mIsBootUpResumeSubscriptionExecuted has not been set
EXPECT_FALSE(manager.GetIsBootUpResumeSubscriptionExecuted());
#endif // CHIP_CONFIG_ENABLE_ICD_CIP && CHIP_CONFIG_PERSIST_SUBSCRIPTIONS && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
numberOfSubsRemaining--;
engine->DecrementNumSubscriptionsToResume();
EXPECT_EQ(numberOfSubsRemaining, engine->mNumOfSubscriptionsToResume);
}
#if CHIP_CONFIG_ENABLE_ICD_CIP && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
// Verify mIsBootUpResumeSubscriptionExecuted has been set
EXPECT_TRUE(manager.GetIsBootUpResumeSubscriptionExecuted());
#endif // CHIP_CONFIG_ENABLE_ICD_CIP && CHIP_CONFIG_PERSIST_SUBSCRIPTIONS && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
// Make sure we don't rollover / go negative
engine->DecrementNumSubscriptionsToResume();
EXPECT_EQ(numberOfSubsRemaining, engine->mNumOfSubscriptionsToResume);
// Clean up
#if CHIP_CONFIG_ENABLE_ICD_CIP && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
engine->SetICDManager(nullptr);
#endif // CHIP_CONFIG_ENABLE_ICD_CIP && CHIP_CONFIG_PERSIST_SUBSCRIPTIONS && !CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
}
#endif // CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
} // namespace app
} // namespace chip