diff --git a/src/app/server/DefaultTermsAndConditionsProvider.cpp b/src/app/server/DefaultTermsAndConditionsProvider.cpp index e1be8dbeb8f9f5..ab63cd6405411a 100644 --- a/src/app/server/DefaultTermsAndConditionsProvider.cpp +++ b/src/app/server/DefaultTermsAndConditionsProvider.cpp @@ -39,18 +39,102 @@ constexpr size_t kEstimatedTlvBufferSize = chip::TLV::EstimateStructOverhead(siz 8 * sizeof(uint16_t); // Extra space for rollback compatibility } // namespace -CHIP_ERROR -chip::app::DefaultTermsAndConditionsProvider::Init(chip::PersistentStorageDelegate * const inPersistentStorageDelegate, - const chip::Optional & inRequiredAcknowledgements) -{ - VerifyOrReturnError(nullptr != inPersistentStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT); +CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Init(PersistentStorageDelegate * const inPersistentStorageDelegate) { + VerifyOrReturnError(nullptr != inPersistentStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT); mPersistentStorageDelegate = inPersistentStorageDelegate; - mRequiredAcknowledgements = inRequiredAcknowledgements; + return CHIP_NO_ERROR; +} + +CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Delete() { + VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED); + + const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance(); + + return mPersistentStorageDelegate->SyncDeleteKeyValue(storageKey.KeyName()); +} + +CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Get(Optional & outTermsAndConditions) { + uint8_t serializationVersion = 0; + uint16_t acknowledgements = 0; + uint16_t acknowledgementsVersion = 0; + + chip::TLV::TLVReader tlvReader; + chip::TLV::TLVType tlvContainer; + + uint8_t buffer[kEstimatedTlvBufferSize] = { 0 }; + uint16_t bufferSize = sizeof(buffer); + + VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED); + + const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance(); + CHIP_ERROR err = mPersistentStorageDelegate->SyncGetKeyValue(storageKey.KeyName(), &buffer, bufferSize); + if (CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND == err) + { + return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND; + } + + VerifyOrReturnError(CHIP_NO_ERROR == err, err); + + tlvReader.Init(buffer, bufferSize); + ReturnErrorOnFailure(tlvReader.Next(chip::TLV::kTLVType_Structure, chip::TLV::AnonymousTag())); + ReturnErrorOnFailure(tlvReader.EnterContainer(tlvContainer)); + ReturnErrorOnFailure(tlvReader.Next(kSerializationVersionTag)); + ReturnErrorOnFailure(tlvReader.Get(serializationVersion)); + ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsTag)); + ReturnErrorOnFailure(tlvReader.Get(acknowledgements)); + ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsVersionTag)); + ReturnErrorOnFailure(tlvReader.Get(acknowledgementsVersion)); + ReturnErrorOnFailure(tlvReader.ExitContainer(tlvContainer)); + + if (kSerializationVersion != serializationVersion) + { + return CHIP_ERROR_VERSION_MISMATCH; + } + + outTermsAndConditions.SetValue({ + .value = acknowledgements, + .version = acknowledgementsVersion, + }); + + return CHIP_NO_ERROR; +} + +CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Set(const Optional & inTermsAndConditions) { + uint8_t buffer[kEstimatedTlvBufferSize] = { 0 }; + chip::TLV::TLVWriter tlvWriter; + chip::TLV::TLVType tlvContainer; + + VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED); + VerifyOrReturnError(inTermsAndConditions.HasValue(), CHIP_ERROR_INCORRECT_STATE); + + tlvWriter.Init(buffer, sizeof(buffer)); + ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, tlvContainer)); + ReturnErrorOnFailure(tlvWriter.Put(kSerializationVersionTag, kSerializationVersion)); + ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsTag, inTermsAndConditions.Value().value)); + ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsVersionTag, inTermsAndConditions.Value().version)); + ReturnErrorOnFailure(tlvWriter.EndContainer(tlvContainer)); + ReturnErrorOnFailure(tlvWriter.Finalize()); + uint32_t lengthWritten = tlvWriter.GetLengthWritten(); + VerifyOrReturnError(CanCastTo(lengthWritten), CHIP_ERROR_BUFFER_TOO_SMALL); + + const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance(); + ReturnErrorOnFailure( + mPersistentStorageDelegate->SyncSetKeyValue(storageKey.KeyName(), buffer, static_cast(lengthWritten))); + + return CHIP_NO_ERROR; +} + +CHIP_ERROR +chip::app::DefaultTermsAndConditionsProvider::Init(TermsAndConditionsStorageDelegate * const inStorageDelegate, const chip::Optional & inRequiredTermsAndConditions) +{ + VerifyOrReturnError(nullptr != inStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT); + mTermsAndConditionsStorageDelegate = inStorageDelegate; + mRequiredAcknowledgements = inRequiredTermsAndConditions; - if (CHIP_NO_ERROR == LoadAcceptance(mLatchedAcceptance)) + if (CHIP_NO_ERROR == mTermsAndConditionsStorageDelegate->Get(mLatchedTermsAndConditions)) { - mTemporalAcceptance.SetValue(mLatchedAcceptance.Value()); + mTemporalAcceptance.SetValue(mLatchedTermsAndConditions.Value()); } return CHIP_NO_ERROR; @@ -104,8 +188,8 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::CommitAcceptance() return CHIP_NO_ERROR; } - mLatchedAcceptance.SetValue(mTemporalAcceptance.Value()); - return StoreAcceptance(mLatchedAcceptance); + mLatchedTermsAndConditions.SetValue(mTemporalAcceptance.Value()); + return mTermsAndConditionsStorageDelegate->Set(mLatchedTermsAndConditions); } CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetAcceptance(Optional & outTermsAndConditions) const @@ -122,12 +206,10 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetRequirements(Optiona CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::ResetAcceptance() { - VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED); - - const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance(); + VerifyOrReturnError(nullptr != mTermsAndConditionsStorageDelegate, CHIP_ERROR_UNINITIALIZED); - ReturnErrorOnFailure(mPersistentStorageDelegate->SyncDeleteKeyValue(storageKey.KeyName())); - mLatchedAcceptance.ClearValue(); + ReturnErrorOnFailure(mTermsAndConditionsStorageDelegate->Delete()); + mLatchedTermsAndConditions.ClearValue(); mTemporalAcceptance.ClearValue(); return CHIP_NO_ERROR; @@ -135,7 +217,7 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::ResetAcceptance() CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::RevertAcceptance() { - mTemporalAcceptance = mLatchedAcceptance; + mTemporalAcceptance = mLatchedTermsAndConditions; return CHIP_NO_ERROR; } @@ -144,76 +226,3 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::SetAcceptance(const Opt mTemporalAcceptance.SetValue(inTermsAndConditions.Value()); return CHIP_NO_ERROR; } - -CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::LoadAcceptance(Optional & outTermsAndConditions) -{ - uint8_t serializationVersion = 0; - uint16_t acknowledgements = 0; - uint16_t acknowledgementsVersion = 0; - - chip::TLV::TLVReader tlvReader; - chip::TLV::TLVType tlvContainer; - - uint8_t buffer[kEstimatedTlvBufferSize] = { 0 }; - uint16_t bufferSize = sizeof(buffer); - - VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED); - - const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance(); - CHIP_ERROR err = mPersistentStorageDelegate->SyncGetKeyValue(storageKey.KeyName(), &buffer, bufferSize); - if (CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND == err) - { - return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND; - } - - VerifyOrReturnError(CHIP_NO_ERROR == err, err); - - tlvReader.Init(buffer, bufferSize); - ReturnErrorOnFailure(tlvReader.Next(chip::TLV::kTLVType_Structure, chip::TLV::AnonymousTag())); - ReturnErrorOnFailure(tlvReader.EnterContainer(tlvContainer)); - ReturnErrorOnFailure(tlvReader.Next(kSerializationVersionTag)); - ReturnErrorOnFailure(tlvReader.Get(serializationVersion)); - ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsTag)); - ReturnErrorOnFailure(tlvReader.Get(acknowledgements)); - ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsVersionTag)); - ReturnErrorOnFailure(tlvReader.Get(acknowledgementsVersion)); - ReturnErrorOnFailure(tlvReader.ExitContainer(tlvContainer)); - - if (kSerializationVersion != serializationVersion) - { - return CHIP_ERROR_VERSION_MISMATCH; - } - - outTermsAndConditions.SetValue({ - .value = acknowledgements, - .version = acknowledgementsVersion, - }); - - return CHIP_NO_ERROR; -} - -CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::StoreAcceptance(const Optional & inTermsAndConditions) -{ - uint8_t buffer[kEstimatedTlvBufferSize] = { 0 }; - chip::TLV::TLVWriter tlvWriter; - chip::TLV::TLVType tlvContainer; - - VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED); - VerifyOrReturnError(inTermsAndConditions.HasValue(), CHIP_ERROR_INCORRECT_STATE); - - tlvWriter.Init(buffer, sizeof(buffer)); - ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, tlvContainer)); - ReturnErrorOnFailure(tlvWriter.Put(kSerializationVersionTag, kSerializationVersion)); - ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsTag, inTermsAndConditions.Value().value)); - ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsVersionTag, inTermsAndConditions.Value().version)); - ReturnErrorOnFailure(tlvWriter.EndContainer(tlvContainer)); - ReturnErrorOnFailure(tlvWriter.Finalize()); - uint32_t lengthWritten = tlvWriter.GetLengthWritten(); - VerifyOrReturnError(CanCastTo(lengthWritten), CHIP_ERROR_BUFFER_TOO_SMALL); - - const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance(); - ReturnErrorOnFailure( - mPersistentStorageDelegate->SyncSetKeyValue(storageKey.KeyName(), buffer, static_cast(lengthWritten))); - - return CHIP_NO_ERROR; -} diff --git a/src/app/server/DefaultTermsAndConditionsProvider.h b/src/app/server/DefaultTermsAndConditionsProvider.h index 1fbde5a7e51418..3b4a8f6f7d008e 100644 --- a/src/app/server/DefaultTermsAndConditionsProvider.h +++ b/src/app/server/DefaultTermsAndConditionsProvider.h @@ -28,19 +28,45 @@ namespace chip { namespace app { + +class TermsAndConditionsStorageDelegate +{ +public: + virtual ~TermsAndConditionsStorageDelegate() = default; + + virtual CHIP_ERROR Delete() = 0; + + virtual CHIP_ERROR Get(Optional & inTermsAndConditions) = 0; + + virtual CHIP_ERROR Set(const Optional & outTermsAndConditions) = 0; +}; + +class DefaultTermsAndConditionsStorageDelegate : public TermsAndConditionsStorageDelegate +{ +public: + CHIP_ERROR Init(PersistentStorageDelegate * const inPersistentStorageDelegate); + + CHIP_ERROR Delete() override; + + CHIP_ERROR Get(Optional & inTermsAndConditions) override; + + CHIP_ERROR Set(const Optional & outTermsAndConditions) override; + +private: + PersistentStorageDelegate * mPersistentStorageDelegate; +}; + class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider { public: /** * @brief Initializes the TermsAndConditionsProvider. * - * @param[in] inPersistentStorageDelegate Persistent storage delegate dependency. + * @param[in] inStorageDelegate Storage delegate dependency. */ - CHIP_ERROR Init(PersistentStorageDelegate * const inPersistentStorageDelegate, - const chip::Optional & inRequiredTermsAndConditions); + CHIP_ERROR Init(TermsAndConditionsStorageDelegate * const inStorageDelegate, const chip::Optional & inRequiredTermsAndConditions); - CHIP_ERROR CheckAcceptance(const Optional & inTermsAndConditions, - TermsAndConditionsState & outState) const override; + CHIP_ERROR CheckAcceptance(const Optional & inTermsAndConditions, TermsAndConditionsState & outState) const override; CHIP_ERROR CommitAcceptance() override; @@ -55,13 +81,8 @@ class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider CHIP_ERROR SetAcceptance(const Optional & inTermsAndConditions) override; private: - CHIP_ERROR LoadAcceptance(Optional & outTermsAndConditions); - - CHIP_ERROR StoreAcceptance(const Optional & inTermsAndConditions); - - PersistentStorageDelegate * mPersistentStorageDelegate; - - Optional mLatchedAcceptance; + TermsAndConditionsStorageDelegate * mTermsAndConditionsStorageDelegate; + Optional mLatchedTermsAndConditions; Optional mTemporalAcceptance; Optional mRequiredAcknowledgements; }; diff --git a/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp b/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp index a39478b8ff8eda..8b1170868ca8a3 100644 --- a/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp +++ b/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp @@ -28,15 +28,19 @@ TEST(DefaultTermsAndConditionsProvider, TestInitSuccess) { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional termsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - err = tncProvider.Init(&storageDelegate, termsAndConditions); + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); } @@ -44,16 +48,20 @@ TEST(DefaultTermsAndConditionsProvider, TestNoRequirementsGetRequirementsSuccess { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional termsAndConditions = chip::Optional(); - err = tncProvider.Init(&storageDelegate, termsAndConditions); + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outTermsAndConditions; - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_FALSE(outTermsAndConditions.HasValue()); } @@ -62,19 +70,23 @@ TEST(DefaultTermsAndConditionsProvider, TestNeverAcceptanceGetAcceptanceSuccess) { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional termsAndConditions = chip::Optional({ .value = 0b1111'1111'1111'1111, .version = 1, }); - err = tncProvider.Init(&storageDelegate, termsAndConditions); + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outTermsAndConditions; - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_FALSE(outTermsAndConditions.HasValue()); } @@ -83,8 +95,9 @@ TEST(DefaultTermsAndConditionsProvider, TestTermsAcceptedPersistsSuccess) { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::app::DefaultTermsAndConditionsProvider anotherTncProvider; chip::Optional termsAndConditions = chip::Optional({ @@ -92,7 +105,10 @@ TEST(DefaultTermsAndConditionsProvider, TestTermsAcceptedPersistsSuccess) .version = 1, }); - err = tncProvider.Init(&storageDelegate, termsAndConditions); + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional newTermsAndConditions = chip::Optional({ @@ -100,22 +116,22 @@ TEST(DefaultTermsAndConditionsProvider, TestTermsAcceptedPersistsSuccess) .version = 1, }); - err = tncProvider.SetAcceptance(newTermsAndConditions); + err = defaultTermsAndConditionsProvider.SetAcceptance(newTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outTermsAndConditions; - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_EQ(1, outTermsAndConditions.Value().value); EXPECT_EQ(1, outTermsAndConditions.Value().version); - err = tncProvider.CommitAcceptance(); - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.CommitAcceptance(); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_EQ(1, outTermsAndConditions.Value().value); EXPECT_EQ(1, outTermsAndConditions.Value().version); - err = anotherTncProvider.Init(&storageDelegate, termsAndConditions); + err = anotherTncProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); err = anotherTncProvider.GetAcceptance(outTermsAndConditions); @@ -128,19 +144,23 @@ TEST(DefaultTermsAndConditionsProvider, TestTermsRequiredGetRequirementsSuccess) { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional termsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - err = tncProvider.Init(&storageDelegate, termsAndConditions); + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outTermsAndConditions; - err = tncProvider.GetRequirements(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetRequirements(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_EQ(1, outTermsAndConditions.Value().value); EXPECT_EQ(1, outTermsAndConditions.Value().version); @@ -150,26 +170,30 @@ TEST(DefaultTermsAndConditionsProvider, TestSetAcceptanceGetAcceptanceSuccess) { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional termsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - err = tncProvider.Init(&storageDelegate, termsAndConditions); + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional acceptedTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - err = tncProvider.SetAcceptance(acceptedTermsAndConditions); + err = defaultTermsAndConditionsProvider.SetAcceptance(acceptedTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outTermsAndConditions; - err = tncProvider.GetRequirements(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetRequirements(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_EQ(1, outTermsAndConditions.Value().value); EXPECT_EQ(1, outTermsAndConditions.Value().version); @@ -179,35 +203,39 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptanceGetAcceptanceSuccess { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional termsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - err = tncProvider.Init(&storageDelegate, termsAndConditions); + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional acceptedTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - err = tncProvider.SetAcceptance(acceptedTermsAndConditions); + err = defaultTermsAndConditionsProvider.SetAcceptance(acceptedTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outTermsAndConditions; - err = tncProvider.GetRequirements(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetRequirements(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_EQ(1, outTermsAndConditions.Value().value); EXPECT_EQ(1, outTermsAndConditions.Value().version); - err = tncProvider.RevertAcceptance(); + err = defaultTermsAndConditionsProvider.RevertAcceptance(); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outAcceptance2; - err = tncProvider.GetAcceptance(outAcceptance2); + err = defaultTermsAndConditionsProvider.GetAcceptance(outAcceptance2); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_FALSE(outAcceptance2.HasValue()); } @@ -216,34 +244,39 @@ TEST(DefaultTermsAndConditionsProvider, TestAcceptanceRequiredTermsMissingFailur { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - err = tncProvider.Init(&storageDelegate, requiredTermsAndConditions); + + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional acceptedTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - err = tncProvider.SetAcceptance(acceptedTermsAndConditions); + err = defaultTermsAndConditionsProvider.SetAcceptance(acceptedTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outAcknowledgementTermsAndConditions; - err = tncProvider.GetAcceptance(outAcknowledgementTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outAcknowledgementTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_EQ(1, outAcknowledgementTermsAndConditions.Value().value); EXPECT_EQ(1, outAcknowledgementTermsAndConditions.Value().version); - err = tncProvider.RevertAcceptance(); + err = defaultTermsAndConditionsProvider.RevertAcceptance(); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outRequiredTermsAndConditions; - err = tncProvider.GetRequirements(outRequiredTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetRequirements(outRequiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_EQ(1, outRequiredTermsAndConditions.Value().value); EXPECT_EQ(1, outRequiredTermsAndConditions.Value().version); @@ -253,8 +286,9 @@ TEST(DefaultTermsAndConditionsProvider, TestAcceptanceCommitCheckSetRevertCheckE { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, @@ -273,42 +307,45 @@ TEST(DefaultTermsAndConditionsProvider, TestAcceptanceCommitCheckSetRevertCheckE chip::Optional outTermsAndConditions; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + // Initialize unit under test - err = tncProvider.Init(&storageDelegate, requiredTermsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); // Set acceptance - err = tncProvider.SetAcceptance(acceptedTermsAndConditions); + err = defaultTermsAndConditionsProvider.SetAcceptance(acceptedTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); // Commit value - err = tncProvider.CommitAcceptance(); + err = defaultTermsAndConditionsProvider.CommitAcceptance(); EXPECT_EQ(CHIP_NO_ERROR, err); // Check commit value - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_TRUE(outTermsAndConditions.HasValue()); EXPECT_EQ(outTermsAndConditions.Value().value, acceptedTermsAndConditions.Value().value); EXPECT_EQ(outTermsAndConditions.Value().version, acceptedTermsAndConditions.Value().version); // Set updated value - err = tncProvider.SetAcceptance(updatedTermsAndConditions); + err = defaultTermsAndConditionsProvider.SetAcceptance(updatedTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); // Check updated value - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_TRUE(outTermsAndConditions.HasValue()); EXPECT_EQ(outTermsAndConditions.Value().value, updatedTermsAndConditions.Value().value); EXPECT_EQ(outTermsAndConditions.Value().version, updatedTermsAndConditions.Value().version); // Revert updated value - err = tncProvider.RevertAcceptance(); + err = defaultTermsAndConditionsProvider.RevertAcceptance(); EXPECT_EQ(CHIP_NO_ERROR, err); // Check committed value - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_TRUE(outTermsAndConditions.HasValue()); EXPECT_EQ(outTermsAndConditions.Value().value, acceptedTermsAndConditions.Value().value); @@ -319,8 +356,9 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhileMissing) { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, @@ -329,21 +367,24 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhileMissing) chip::Optional outTermsAndConditions; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + // Initialize unit under test [No conditions previously accepted] - err = tncProvider.Init(&storageDelegate, requiredTermsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); // [Fail-safe started] No conditions set during the fail-safe. No commit. - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_FALSE(outTermsAndConditions.HasValue()); // [Fail-safe expires] Revert is called. - err = tncProvider.RevertAcceptance(); + err = defaultTermsAndConditionsProvider.RevertAcceptance(); EXPECT_EQ(CHIP_NO_ERROR, err); // [New fail safe started (to retry the commissioning operations)] Confirm acceptance returns previous values (empty) - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_FALSE(outTermsAndConditions.HasValue()); } @@ -352,8 +393,9 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhileAccepted) { CHIP_ERROR err; - chip::TestPersistentStorageDelegate storageDelegate; - chip::app::DefaultTermsAndConditionsProvider tncProvider; + chip::TestPersistentStorageDelegate testPersistentStorageDelegate; + chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, @@ -362,18 +404,21 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhileAccepted) chip::Optional outTermsAndConditions; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + // Initialize unit under test [Conditions previously accepted] - err = tncProvider.Init(&storageDelegate, requiredTermsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); // [Fail-safe started] No conditions set during the fail-safe. No commit. // [Fail-safe expires] Revert is called. - err = tncProvider.RevertAcceptance(); + err = defaultTermsAndConditionsProvider.RevertAcceptance(); EXPECT_EQ(CHIP_NO_ERROR, err); // [New fail safe started (to retry the commissioning operations)] Confirm acceptance returns previous values (accepted) - err = tncProvider.GetAcceptance(outTermsAndConditions); + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_FALSE(outTermsAndConditions.HasValue()); }