diff --git a/src/app/server/DefaultTermsAndConditionsProvider.h b/src/app/server/DefaultTermsAndConditionsProvider.h index 3b4a8f6f7d008e..bbe22f8aac8af3 100644 --- a/src/app/server/DefaultTermsAndConditionsProvider.h +++ b/src/app/server/DefaultTermsAndConditionsProvider.h @@ -36,9 +36,9 @@ class TermsAndConditionsStorageDelegate virtual CHIP_ERROR Delete() = 0; - virtual CHIP_ERROR Get(Optional & inTermsAndConditions) = 0; + virtual CHIP_ERROR Get(Optional & outTermsAndConditions) = 0; - virtual CHIP_ERROR Set(const Optional & outTermsAndConditions) = 0; + virtual CHIP_ERROR Set(const Optional & inTermsAndConditions) = 0; }; class DefaultTermsAndConditionsStorageDelegate : public TermsAndConditionsStorageDelegate diff --git a/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp b/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp index 8b1170868ca8a3..a125a50f77f212 100644 --- a/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp +++ b/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp @@ -18,12 +18,39 @@ #include "app/server/DefaultTermsAndConditionsProvider.h" #include "app/server/TermsAndConditionsProvider.h" +#include "pw_unit_test/framework.h" #include #include #include #include +class TestTermsAndConditionsStorageDelegate : public chip::app::TermsAndConditionsStorageDelegate +{ +public: + TestTermsAndConditionsStorageDelegate(chip::Optional &initialTermsAndConditions) + : mTermsAndConditions(initialTermsAndConditions) { + } + + CHIP_ERROR Delete() { + mTermsAndConditions.ClearValue(); + return CHIP_NO_ERROR; + } + + CHIP_ERROR Get(chip::Optional & outTermsAndConditions) { + outTermsAndConditions = mTermsAndConditions; + return CHIP_NO_ERROR; + } + + CHIP_ERROR Set(const chip::Optional & inTermsAndConditions) { + mTermsAndConditions = inTermsAndConditions; + return CHIP_NO_ERROR; + } + +private: + chip::Optional &mTermsAndConditions; +}; + TEST(DefaultTermsAndConditionsProvider, TestInitSuccess) { CHIP_ERROR err; @@ -389,26 +416,26 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhileMissing) EXPECT_FALSE(outTermsAndConditions.HasValue()); } -TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhileAccepted) +TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAccepted) { CHIP_ERROR err; - - chip::TestPersistentStorageDelegate testPersistentStorageDelegate; - chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; - chip::Optional requiredTermsAndConditions = chip::Optional({ + chip::Optional initialTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - chip::Optional outTermsAndConditions; + chip::Optional requiredTermsAndConditions = chip::Optional({ + .value = 0b11, + .version = 2, + }); - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); - EXPECT_EQ(CHIP_NO_ERROR, err); + TestTermsAndConditionsStorageDelegate testTermsAndConditionsStorageDelegate(initialTermsAndConditions); // Initialize unit under test [Conditions previously accepted] - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&testTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); // [Fail-safe started] No conditions set during the fail-safe. No commit. @@ -417,8 +444,55 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhileAccepted) err = defaultTermsAndConditionsProvider.RevertAcceptance(); EXPECT_EQ(CHIP_NO_ERROR, err); + chip::Optional outTermsAndConditions; + // [New fail safe started (to retry the commissioning operations)] Confirm acceptance returns previous values (accepted) err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); - EXPECT_FALSE(outTermsAndConditions.HasValue()); + EXPECT_TRUE(outTermsAndConditions.HasValue()); + EXPECT_EQ(outTermsAndConditions.Value().value, 1); + EXPECT_EQ(outTermsAndConditions.Value().version, 1); } + +TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAcceptedThenUpdatedUnderFailsafe) +{ + CHIP_ERROR err; + + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + + // Initialize unit under test dependency + chip::Optional initiallyAcceptedTermsAndConditions = chip::Optional({ + .value = 1, + .version = 1, + }); + TestTermsAndConditionsStorageDelegate testTermsAndConditionsStorageDelegate(initiallyAcceptedTermsAndConditions); + + // Initialize unit under test [Conditions previously accepted] + chip::Optional requiredTermsAndConditions = chip::Optional({ + .value = 0b11, + .version = 2, + }); + err = defaultTermsAndConditionsProvider.Init(&testTermsAndConditionsStorageDelegate, requiredTermsAndConditions); + EXPECT_EQ(CHIP_NO_ERROR, err); + + // [Fail-safe started] Acceptance updated. + chip::Optional updatedAcceptedTermsAndConditions = chip::Optional({ + .value = 0b111, + .version = 3, + }); + err = defaultTermsAndConditionsProvider.SetAcceptance(updatedAcceptedTermsAndConditions); + EXPECT_EQ(CHIP_NO_ERROR, err); + + // [Fail-safe expires] Revert is called. + err = defaultTermsAndConditionsProvider.RevertAcceptance(); + EXPECT_EQ(CHIP_NO_ERROR, err); + + chip::Optional outTermsAndConditions; + + // [New fail safe started (to retry the commissioning operations)] Confirm acceptance returns previous values (accepted) + err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); + EXPECT_EQ(CHIP_NO_ERROR, err); + EXPECT_TRUE(outTermsAndConditions.HasValue()); + EXPECT_EQ(outTermsAndConditions.Value().value, initiallyAcceptedTermsAndConditions.Value().value); + EXPECT_EQ(outTermsAndConditions.Value().version, initiallyAcceptedTermsAndConditions.Value().version); +} \ No newline at end of file