Skip to content

Commit

Permalink
restyle
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Oct 7, 2024
1 parent 4aa4634 commit 5891341
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
19 changes: 12 additions & 7 deletions src/app/server/DefaultTermsAndConditionsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,24 @@ constexpr size_t kEstimatedTlvBufferSize = chip::TLV::EstimateStructOverhead(siz
8 * sizeof(uint16_t); // Extra space for rollback compatibility
} // namespace


CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Init(PersistentStorageDelegate * const inPersistentStorageDelegate) {
CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Init(PersistentStorageDelegate * const inPersistentStorageDelegate)
{
VerifyOrReturnError(nullptr != inPersistentStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT);
mPersistentStorageDelegate = inPersistentStorageDelegate;
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Delete() {
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<TermsAndConditions> & outTermsAndConditions) {
CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Get(Optional<TermsAndConditions> & outTermsAndConditions)
{
uint8_t serializationVersion = 0;
uint16_t acknowledgements = 0;
uint16_t acknowledgementsVersion = 0;
Expand Down Expand Up @@ -100,7 +102,8 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Get(Optional<Ter
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Set(const Optional<TermsAndConditions> & inTermsAndConditions) {
CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Set(const Optional<TermsAndConditions> & inTermsAndConditions)
{
uint8_t buffer[kEstimatedTlvBufferSize] = { 0 };
chip::TLV::TLVWriter tlvWriter;
chip::TLV::TLVType tlvContainer;
Expand All @@ -126,11 +129,13 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsStorageDelegate::Set(const Option
}

CHIP_ERROR
chip::app::DefaultTermsAndConditionsProvider::Init(TermsAndConditionsStorageDelegate * const inStorageDelegate, const chip::Optional<chip::app::TermsAndConditions> & inRequiredTermsAndConditions)
chip::app::DefaultTermsAndConditionsProvider::Init(
TermsAndConditionsStorageDelegate * const inStorageDelegate,
const chip::Optional<chip::app::TermsAndConditions> & inRequiredTermsAndConditions)
{
VerifyOrReturnError(nullptr != inStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT);
mTermsAndConditionsStorageDelegate = inStorageDelegate;
mRequiredAcknowledgements = inRequiredTermsAndConditions;
mRequiredAcknowledgements = inRequiredTermsAndConditions;

if (CHIP_NO_ERROR == mTermsAndConditionsStorageDelegate->Get(mLatchedTermsAndConditions))
{
Expand Down
6 changes: 4 additions & 2 deletions src/app/server/DefaultTermsAndConditionsProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider
*
* @param[in] inStorageDelegate Storage delegate dependency.
*/
CHIP_ERROR Init(TermsAndConditionsStorageDelegate * const inStorageDelegate, const chip::Optional<chip::app::TermsAndConditions> & inRequiredTermsAndConditions);
CHIP_ERROR Init(TermsAndConditionsStorageDelegate * const inStorageDelegate,
const chip::Optional<chip::app::TermsAndConditions> & inRequiredTermsAndConditions);

CHIP_ERROR CheckAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions, TermsAndConditionsState & outState) const override;
CHIP_ERROR CheckAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions,
TermsAndConditionsState & outState) const override;

CHIP_ERROR CommitAcceptance() override;

Expand Down
3 changes: 2 additions & 1 deletion src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams
});

ReturnErrorOnFailure(sDefaultTermsAndConditionsStorageDelegateInstance.Init(this->persistentStorageDelegate));
ReturnErrorOnFailure(sDefaultTermsAndConditionsProviderInstance.Init(&sDefaultTermsAndConditionsStorageDelegateInstance, termsAndConditions));
ReturnErrorOnFailure(sDefaultTermsAndConditionsProviderInstance.Init(&sDefaultTermsAndConditionsStorageDelegateInstance,
termsAndConditions));
this->termsAndConditionsProvider = &sDefaultTermsAndConditionsProviderInstance;
}
#endif
Expand Down
45 changes: 25 additions & 20 deletions src/app/tests/TestDefaultTermsAndConditionsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,30 @@
class TestTermsAndConditionsStorageDelegate : public chip::app::TermsAndConditionsStorageDelegate
{
public:
TestTermsAndConditionsStorageDelegate(chip::Optional<chip::app::TermsAndConditions> &initialTermsAndConditions)
: mTermsAndConditions(initialTermsAndConditions) {
}
TestTermsAndConditionsStorageDelegate(chip::Optional<chip::app::TermsAndConditions> & initialTermsAndConditions) :
mTermsAndConditions(initialTermsAndConditions)
{}

CHIP_ERROR Delete() {
CHIP_ERROR Delete()
{
mTermsAndConditions.ClearValue();
return CHIP_NO_ERROR;
}

CHIP_ERROR Get(chip::Optional<chip::app::TermsAndConditions> & outTermsAndConditions) {
CHIP_ERROR Get(chip::Optional<chip::app::TermsAndConditions> & outTermsAndConditions)
{
outTermsAndConditions = mTermsAndConditions;
return CHIP_NO_ERROR;
}

CHIP_ERROR Set(const chip::Optional<chip::app::TermsAndConditions> & inTermsAndConditions) {
CHIP_ERROR Set(const chip::Optional<chip::app::TermsAndConditions> & inTermsAndConditions)
{
mTermsAndConditions = inTermsAndConditions;
return CHIP_NO_ERROR;
}

private:
chip::Optional<chip::app::TermsAndConditions> &mTermsAndConditions;
chip::Optional<chip::app::TermsAndConditions> & mTermsAndConditions;
};

TEST(DefaultTermsAndConditionsProvider, TestInitSuccess)
Expand Down Expand Up @@ -120,7 +123,7 @@ TEST(DefaultTermsAndConditionsProvider, TestTermsAcceptedPersistsSuccess)
chip::app::DefaultTermsAndConditionsStorageDelegate defaultTermsAndConditionsStorageDelegate;
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,
Expand Down Expand Up @@ -400,9 +403,9 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAccept
CHIP_ERROR err;

// 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> initialTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 1,
.version = 1,
});
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 0b11,
Expand Down Expand Up @@ -434,10 +437,11 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAccept
CHIP_ERROR err;

// Initialize unit under test dependency
chip::Optional<chip::app::TermsAndConditions> initiallyAcceptedTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 1,
.version = 1,
});
chip::Optional<chip::app::TermsAndConditions> initiallyAcceptedTermsAndConditions =
chip::Optional<chip::app::TermsAndConditions>({
.value = 1,
.version = 1,
});
TestTermsAndConditionsStorageDelegate testTermsAndConditionsStorageDelegate(initiallyAcceptedTermsAndConditions);

// Initialize unit under test [Conditions previously accepted]
Expand All @@ -450,10 +454,11 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAccept
EXPECT_EQ(CHIP_NO_ERROR, err);

// [Fail-safe started] Acceptance updated.
chip::Optional<chip::app::TermsAndConditions> updatedAcceptedTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 0b111,
.version = 3,
});
chip::Optional<chip::app::TermsAndConditions> updatedAcceptedTermsAndConditions =
chip::Optional<chip::app::TermsAndConditions>({
.value = 0b111,
.version = 3,
});
err = defaultTermsAndConditionsProvider.SetAcceptance(updatedAcceptedTermsAndConditions);
EXPECT_EQ(CHIP_NO_ERROR, err);

Expand All @@ -469,4 +474,4 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhenPreviouslyAccept
EXPECT_TRUE(outTermsAndConditions.HasValue());
EXPECT_EQ(outTermsAndConditions.Value().value, initiallyAcceptedTermsAndConditions.Value().value);
EXPECT_EQ(outTermsAndConditions.Value().version, initiallyAcceptedTermsAndConditions.Value().version);
}
}

0 comments on commit 5891341

Please sign in to comment.