From 4aa463414faf9eec5ee380d70b64bb06a76cbcf4 Mon Sep 17 00:00:00 2001 From: James Swan <122404367+swan-amazon@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:53:18 +0000 Subject: [PATCH] wip --- src/app/server/Server.h | 5 +- .../TestDefaultTermsAndConditionsProvider.cpp | 132 +++++++----------- 2 files changed, 56 insertions(+), 81 deletions(-) diff --git a/src/app/server/Server.h b/src/app/server/Server.h index 4a428ddc1f1af8..95d77fa90ffab9 100644 --- a/src/app/server/Server.h +++ b/src/app/server/Server.h @@ -319,6 +319,7 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams #if CHIP_CONFIG_TC_REQUIRED static app::DefaultTermsAndConditionsProvider sDefaultTermsAndConditionsProviderInstance; + static app::DefaultTermsAndConditionsStorageDelegate sDefaultTermsAndConditionsStorageDelegateInstance; if (this->termsAndConditionsProvider == nullptr) { @@ -327,8 +328,8 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams .version = CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION, }); - ReturnErrorOnFailure( - sDefaultTermsAndConditionsProviderInstance.Init(this->persistentStorageDelegate, termsAndConditions)); + ReturnErrorOnFailure(sDefaultTermsAndConditionsStorageDelegateInstance.Init(this->persistentStorageDelegate)); + ReturnErrorOnFailure(sDefaultTermsAndConditionsProviderInstance.Init(&sDefaultTermsAndConditionsStorageDelegateInstance, termsAndConditions)); this->termsAndConditionsProvider = &sDefaultTermsAndConditionsProviderInstance; } #endif diff --git a/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp b/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp index a125a50f77f212..3bdfa8963358a2 100644 --- a/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp +++ b/src/app/tests/TestDefaultTermsAndConditionsProvider.cpp @@ -57,17 +57,15 @@ TEST(DefaultTermsAndConditionsProvider, TestInitSuccess) chip::TestPersistentStorageDelegate testPersistentStorageDelegate; chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); - chip::Optional termsAndConditions = chip::Optional({ + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); - EXPECT_EQ(CHIP_NO_ERROR, err); - - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); } @@ -77,14 +75,12 @@ TEST(DefaultTermsAndConditionsProvider, TestNoRequirementsGetRequirementsSuccess chip::TestPersistentStorageDelegate testPersistentStorageDelegate; chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; - - chip::Optional termsAndConditions = chip::Optional(); - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); EXPECT_EQ(CHIP_NO_ERROR, err); - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + chip::Optional requiredTermsAndConditions = chip::Optional(); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outTermsAndConditions; @@ -99,17 +95,15 @@ TEST(DefaultTermsAndConditionsProvider, TestNeverAcceptanceGetAcceptanceSuccess) chip::TestPersistentStorageDelegate testPersistentStorageDelegate; chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); - chip::Optional termsAndConditions = chip::Optional({ + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 0b1111'1111'1111'1111, .version = 1, }); - - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); - EXPECT_EQ(CHIP_NO_ERROR, err); - - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outTermsAndConditions; @@ -124,18 +118,15 @@ TEST(DefaultTermsAndConditionsProvider, TestTermsAcceptedPersistsSuccess) chip::TestPersistentStorageDelegate testPersistentStorageDelegate; chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; - chip::app::DefaultTermsAndConditionsProvider anotherTncProvider; - - chip::Optional termsAndConditions = chip::Optional({ + chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); - EXPECT_EQ(CHIP_NO_ERROR, err); - - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional newTermsAndConditions = chip::Optional({ @@ -158,7 +149,8 @@ TEST(DefaultTermsAndConditionsProvider, TestTermsAcceptedPersistsSuccess) EXPECT_EQ(1, outTermsAndConditions.Value().value); EXPECT_EQ(1, outTermsAndConditions.Value().version); - err = anotherTncProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); + chip::app::DefaultTermsAndConditionsProvider anotherTncProvider; + err = anotherTncProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); err = anotherTncProvider.GetAcceptance(outTermsAndConditions); @@ -173,17 +165,15 @@ TEST(DefaultTermsAndConditionsProvider, TestTermsRequiredGetRequirementsSuccess) chip::TestPersistentStorageDelegate testPersistentStorageDelegate; chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); - chip::Optional termsAndConditions = chip::Optional({ + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); - EXPECT_EQ(CHIP_NO_ERROR, err); - - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional outTermsAndConditions; @@ -199,17 +189,15 @@ TEST(DefaultTermsAndConditionsProvider, TestSetAcceptanceGetAcceptanceSuccess) chip::TestPersistentStorageDelegate testPersistentStorageDelegate; chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); - chip::Optional termsAndConditions = chip::Optional({ + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); - EXPECT_EQ(CHIP_NO_ERROR, err); - - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional acceptedTermsAndConditions = chip::Optional({ @@ -232,17 +220,15 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptanceGetAcceptanceSuccess chip::TestPersistentStorageDelegate testPersistentStorageDelegate; chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); - chip::Optional termsAndConditions = chip::Optional({ + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); - EXPECT_EQ(CHIP_NO_ERROR, err); - - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); chip::Optional acceptedTermsAndConditions = chip::Optional({ @@ -273,16 +259,14 @@ TEST(DefaultTermsAndConditionsProvider, TestAcceptanceRequiredTermsMissingFailur chip::TestPersistentStorageDelegate testPersistentStorageDelegate; chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); - EXPECT_EQ(CHIP_NO_ERROR, err); - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); @@ -315,33 +299,23 @@ TEST(DefaultTermsAndConditionsProvider, TestAcceptanceCommitCheckSetRevertCheckE chip::TestPersistentStorageDelegate testPersistentStorageDelegate; chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate; - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); + EXPECT_EQ(CHIP_NO_ERROR, err); + // Initialize unit under test + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); + err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); + EXPECT_EQ(CHIP_NO_ERROR, err); + // Set acceptance chip::Optional acceptedTermsAndConditions = chip::Optional({ .value = 0b1, .version = 1, }); - - chip::Optional updatedTermsAndConditions = chip::Optional({ - .value = 0b11, - .version = 2, - }); - - chip::Optional outTermsAndConditions; - - err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate); - EXPECT_EQ(CHIP_NO_ERROR, err); - - // Initialize unit under test - err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions); - EXPECT_EQ(CHIP_NO_ERROR, err); - - // Set acceptance err = defaultTermsAndConditionsProvider.SetAcceptance(acceptedTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); @@ -350,6 +324,7 @@ TEST(DefaultTermsAndConditionsProvider, TestAcceptanceCommitCheckSetRevertCheckE EXPECT_EQ(CHIP_NO_ERROR, err); // Check commit value + chip::Optional outTermsAndConditions; err = defaultTermsAndConditionsProvider.GetAcceptance(outTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); EXPECT_TRUE(outTermsAndConditions.HasValue()); @@ -357,6 +332,10 @@ TEST(DefaultTermsAndConditionsProvider, TestAcceptanceCommitCheckSetRevertCheckE EXPECT_EQ(outTermsAndConditions.Value().version, acceptedTermsAndConditions.Value().version); // Set updated value + chip::Optional updatedTermsAndConditions = chip::Optional({ + .value = 0b11, + .version = 2, + }); err = defaultTermsAndConditionsProvider.SetAcceptance(updatedTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); @@ -419,22 +398,18 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhileMissing) TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAccepted) { CHIP_ERROR err; - - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; + // Initialize unit under test [Conditions previously accepted] chip::Optional initialTermsAndConditions = chip::Optional({ .value = 1, .version = 1, }); - chip::Optional requiredTermsAndConditions = chip::Optional({ .value = 0b11, .version = 2, }); - TestTermsAndConditionsStorageDelegate testTermsAndConditionsStorageDelegate(initialTermsAndConditions); - - // Initialize unit under test [Conditions previously accepted] + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; err = defaultTermsAndConditionsProvider.Init(&testTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err); @@ -457,8 +432,6 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAccept TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAcceptedThenUpdatedUnderFailsafe) { CHIP_ERROR err; - - chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; // Initialize unit under test dependency chip::Optional initiallyAcceptedTermsAndConditions = chip::Optional({ @@ -472,6 +445,7 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAccept .value = 0b11, .version = 2, }); + chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider; err = defaultTermsAndConditionsProvider.Init(&testTermsAndConditionsStorageDelegate, requiredTermsAndConditions); EXPECT_EQ(CHIP_NO_ERROR, err);