Skip to content

Commit 5c90231

Browse files
EskoDijkjwhui
authored andcommitted
[tcat] fix unit tests (openthread#12875)
Due to a state retention issue in the unit test platform, TCAT tests were passing in ways they should not. Now with the new settings/flash clearing per openthread#12875 applied, these tests were failing. This fixes TCAT unit tests to pass again and better express the expected behavior also.
1 parent 68ab3a5 commit 5c90231

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

tests/unit/test_tcat.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,17 @@ class UnitTester
345345
const TcatAgent::CertificateAuthorizationField aDeviceAuth,
346346
bool aIsCommissionedAtStart)
347347
{
348-
aAgent->mState = TcatAgent::kStateConnected;
348+
// This mock function mimics the steps in TcatAgent::Connected() without requiring the actual TLS
349+
// session object.
350+
aAgent->ClearCommissionerState();
349351
aAgent->mCommissionerAuthorizationField = aCommAuth;
350352
aAgent->mDeviceAuthorizationField = aDeviceAuth;
351-
aAgent->mPskcVerified = false;
352-
aAgent->mPskdVerified = false;
353-
aAgent->mCommissionerHasExtendedPanId = false;
354-
aAgent->mCommissionerHasNetworkName = false;
355-
aAgent->mCommissionerHasDomainName = false;
356353
aAgent->mIsCommissioned = aIsCommissionedAtStart;
354+
355+
aAgent->mNextState =
356+
(aAgent->mState == TcatAgent::kStateActiveTemporary) ? TcatAgent::kStateStandby : TcatAgent::kStateActive;
357+
aAgent->mState = TcatAgent::kStateConnected;
358+
aAgent->NotifyStateChange();
357359
}
358360

359361
// Mock condition: commissioner has or has not the given Extended Pan ID in its certificate.
@@ -545,6 +547,7 @@ class UnitTester
545547
// Mock TCAT Commissioner 4 connects to the Device - verify it only has access to class General by default.
546548
// The Device is commissioned already at start of the TCAT Link.
547549
// =======================================================================================================
550+
instance->Get<ActiveDatasetManager>().SaveLocal(sFullDataset);
548551
memcpy(&sCommAuth, &kCommCert4AuthField, sizeof(sCommAuth));
549552
MockCommissionerConnected(agent, sCommAuth, sDeviceAuth, true);
550553
VerifyOrQuit(CommandClassesAuthorized(agent, kClassGeneral));
@@ -740,23 +743,42 @@ class UnitTester
740743
VerifyOrQuit(!SetActiveDatasetAuthorized(agent, sFullDataset));
741744
VerifyOrQuit(!SetActiveDatasetAuthorized(agent, sPartialDataset));
742745

743-
// Domain Name match
746+
// Domain Name match - but Commissioning in general is still not authorized, due to missing Active Dataset.
747+
// Hence the Network Name and XPAN ID checks cannot succeed in general. They will succeed now for the
748+
// specific 'Set Active Dataset' command.
744749
MockDomainName(agent, true, &sCommDomainName);
745-
VerifyOrQuit(CommandClassesAuthorized(agent, kClassGeneral | kClassCommissioning));
750+
VerifyOrQuit(CommandClassesAuthorized(agent, kClassGeneral));
746751
VerifyOrQuit(SetActiveDatasetAuthorized(agent, sFullDataset));
752+
753+
// the partial dataset cannot be written, because it lacks the required Network Name and XPAN ID combo.
747754
VerifyOrQuit(!SetActiveDatasetAuthorized(agent, sPartialDataset));
748755

749756
// PSKc proof
750757
agent->mPskcVerified = true;
751-
VerifyOrQuit(CommandClassesAuthorized(agent, kClassGeneral | kClassCommissioning | kClassExtraction |
752-
kClassDecommissioning | kClassApplication));
758+
VerifyOrQuit(CommandClassesAuthorized(agent, kClassGeneral));
753759
VerifyOrQuit(SetActiveDatasetAuthorized(agent, sFullDataset));
754760
VerifyOrQuit(!SetActiveDatasetAuthorized(agent, sPartialDataset));
755761

756-
// Try write a full dataset with differing XPAN ID
762+
// Try write a full dataset with differing XPAN ID - this fails
757763
sFullDataset.mExtendedPanId.m8[2]++;
758764
VerifyOrQuit(!SetActiveDatasetAuthorized(agent, sFullDataset));
759765

766+
// Test an equivalent case to above where the device does have a full dataset stored already, and the
767+
// Commissioner connects. Now it has full access to all classes due to matching Network Name / XPAN ID combo.
768+
sFullDataset = AsCoreType(&kFullDataset);
769+
instance->Get<ActiveDatasetManager>().SaveLocal(sFullDataset);
770+
MockCommissionerConnected(agent, sCommAuth, sDeviceAuth, true);
771+
agent->mPskdVerified = true;
772+
agent->mPskcVerified = true;
773+
MockNetworkName(agent, true, &sCommNetworkName);
774+
MockExtPanId(agent, true, &sCommExtPanId);
775+
MockDomainName(agent, true, &sCommDomainName);
776+
777+
VerifyOrQuit(CommandClassesAuthorized(agent, kClassGeneral | kClassCommissioning | kClassDecommissioning |
778+
kClassExtraction | kClassApplication));
779+
VerifyOrQuit(!SetActiveDatasetAuthorized(agent, sFullDataset));
780+
VerifyOrQuit(!SetActiveDatasetAuthorized(agent, sPartialDataset));
781+
760782
testFreeInstance(instance);
761783
}
762784

0 commit comments

Comments
 (0)