Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Oct 7, 2024
1 parent 1dffefc commit 4aa4634
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 81 deletions.
5 changes: 3 additions & 2 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
Expand Down
132 changes: 53 additions & 79 deletions src/app/tests/TestDefaultTermsAndConditionsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<chip::app::TermsAndConditions> termsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider;
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.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);
}

Expand All @@ -77,14 +75,12 @@ TEST(DefaultTermsAndConditionsProvider, TestNoRequirementsGetRequirementsSuccess

chip::TestPersistentStorageDelegate testPersistentStorageDelegate;
chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate;
chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider;

chip::Optional<chip::app::TermsAndConditions> termsAndConditions = chip::Optional<chip::app::TermsAndConditions>();

err = defaultTermsAndConditionsStorageDelegate.Init(&testPersistentStorageDelegate);
EXPECT_EQ(CHIP_NO_ERROR, err);

err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, termsAndConditions);
chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider;
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>();
err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions);
EXPECT_EQ(CHIP_NO_ERROR, err);

chip::Optional<chip::app::TermsAndConditions> outTermsAndConditions;
Expand All @@ -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<chip::app::TermsAndConditions> termsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider;
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.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<chip::app::TermsAndConditions> outTermsAndConditions;
Expand All @@ -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<chip::app::TermsAndConditions> termsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.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<chip::app::TermsAndConditions> newTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
Expand All @@ -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);
Expand All @@ -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<chip::app::TermsAndConditions> termsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider;
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.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<chip::app::TermsAndConditions> outTermsAndConditions;
Expand All @@ -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<chip::app::TermsAndConditions> termsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider;
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.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<chip::app::TermsAndConditions> acceptedTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
Expand All @@ -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<chip::app::TermsAndConditions> termsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider;
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.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<chip::app::TermsAndConditions> acceptedTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
Expand Down Expand Up @@ -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<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.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);

Expand Down Expand Up @@ -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<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 1,
.version = 1,
});
err = defaultTermsAndConditionsProvider.Init(&defaultTermsAndConditionsStorageDelegate, requiredTermsAndConditions);
EXPECT_EQ(CHIP_NO_ERROR, err);

// Set acceptance
chip::Optional<chip::app::TermsAndConditions> acceptedTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 0b1,
.version = 1,
});

chip::Optional<chip::app::TermsAndConditions> updatedTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 0b11,
.version = 2,
});

chip::Optional<chip::app::TermsAndConditions> 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);

Expand All @@ -350,13 +324,18 @@ TEST(DefaultTermsAndConditionsProvider, TestAcceptanceCommitCheckSetRevertCheckE
EXPECT_EQ(CHIP_NO_ERROR, err);

// Check commit value
chip::Optional<chip::app::TermsAndConditions> 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
chip::Optional<chip::app::TermsAndConditions> updatedTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 0b11,
.version = 2,
});
err = defaultTermsAndConditionsProvider.SetAcceptance(updatedTermsAndConditions);
EXPECT_EQ(CHIP_NO_ERROR, err);

Expand Down Expand Up @@ -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<chip::app::TermsAndConditions> initialTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 1,
.version = 1,
});

chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.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);

Expand All @@ -457,8 +432,6 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAccept
TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAcceptedThenUpdatedUnderFailsafe)
{
CHIP_ERROR err;

chip::app::DefaultTermsAndConditionsProvider defaultTermsAndConditionsProvider;

// Initialize unit under test dependency
chip::Optional<chip::app::TermsAndConditions> initiallyAcceptedTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
Expand All @@ -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);

Expand Down

0 comments on commit 4aa4634

Please sign in to comment.