Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Oct 7, 2024
1 parent 2c625b5 commit 1dffefc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/app/server/DefaultTermsAndConditionsProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class TermsAndConditionsStorageDelegate

virtual CHIP_ERROR Delete() = 0;

virtual CHIP_ERROR Get(Optional<TermsAndConditions> & inTermsAndConditions) = 0;
virtual CHIP_ERROR Get(Optional<TermsAndConditions> & outTermsAndConditions) = 0;

virtual CHIP_ERROR Set(const Optional<TermsAndConditions> & outTermsAndConditions) = 0;
virtual CHIP_ERROR Set(const Optional<TermsAndConditions> & inTermsAndConditions) = 0;
};

class DefaultTermsAndConditionsStorageDelegate : public TermsAndConditionsStorageDelegate
Expand Down
94 changes: 84 additions & 10 deletions src/app/tests/TestDefaultTermsAndConditionsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,39 @@

#include "app/server/DefaultTermsAndConditionsProvider.h"
#include "app/server/TermsAndConditionsProvider.h"
#include "pw_unit_test/framework.h"

#include <lib/core/CHIPError.h>
#include <lib/core/StringBuilderAdapters.h>
#include <lib/support/TestPersistentStorageDelegate.h>
#include <pw_unit_test/framework.h>

class TestTermsAndConditionsStorageDelegate : public chip::app::TermsAndConditionsStorageDelegate
{
public:
TestTermsAndConditionsStorageDelegate(chip::Optional<chip::app::TermsAndConditions> &initialTermsAndConditions)
: mTermsAndConditions(initialTermsAndConditions) {
}

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

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) {
mTermsAndConditions = inTermsAndConditions;
return CHIP_NO_ERROR;
}

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

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

chip::Optional<chip::app::TermsAndConditions> outTermsAndConditions;
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.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.
Expand All @@ -417,8 +444,55 @@ TEST(DefaultTermsAndConditionsProvider, TestRevertAcceptenceWhileAccepted)
err = defaultTermsAndConditionsProvider.RevertAcceptance();
EXPECT_EQ(CHIP_NO_ERROR, err);

chip::Optional<chip::app::TermsAndConditions> 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<chip::app::TermsAndConditions> initiallyAcceptedTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 1,
.version = 1,
});
TestTermsAndConditionsStorageDelegate testTermsAndConditionsStorageDelegate(initiallyAcceptedTermsAndConditions);

// Initialize unit under test [Conditions previously accepted]
chip::Optional<chip::app::TermsAndConditions> requiredTermsAndConditions = chip::Optional<chip::app::TermsAndConditions>({
.value = 0b11,
.version = 2,
});
err = defaultTermsAndConditionsProvider.Init(&testTermsAndConditionsStorageDelegate, requiredTermsAndConditions);
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,
});
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<chip::app::TermsAndConditions> 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);
}

0 comments on commit 1dffefc

Please sign in to comment.