From 3e4e54bea545bac8458dd41ce4b52ec34c4ccbbe Mon Sep 17 00:00:00 2001 From: Zang MingJie Date: Thu, 21 Apr 2022 06:26:44 +0800 Subject: [PATCH] Fix SimpleSessionResumptionStorage Save/Load index --- .../secure_channel/SimpleSessionResumptionStorage.cpp | 8 ++++++-- .../tests/TestSimpleSessionResumptionStorage.cpp | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/protocols/secure_channel/SimpleSessionResumptionStorage.cpp b/src/protocols/secure_channel/SimpleSessionResumptionStorage.cpp index 5f7ad75e306a48..d20a29db38e7de 100644 --- a/src/protocols/secure_channel/SimpleSessionResumptionStorage.cpp +++ b/src/protocols/secure_channel/SimpleSessionResumptionStorage.cpp @@ -57,7 +57,7 @@ CHIP_ERROR SimpleSessionResumptionStorage::SaveIndex(const SessionIndex & index) TLV::TLVType arrayType; ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Array, arrayType)); - for (size_t i = index.mSize; i < index.mSize; ++i) + for (size_t i = 0; i < index.mSize; ++i) { TLV::TLVType innerType; ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, innerType)); @@ -83,7 +83,11 @@ CHIP_ERROR SimpleSessionResumptionStorage::LoadIndex(SessionIndex & index) uint16_t len = static_cast(buf.size()); DefaultStorageKeyAllocator keyAlloc; - ReturnErrorOnFailure(mStorage->SyncGetKeyValue(keyAlloc.SessionResumptionIndex(), buf.data(), len)); + if (mStorage->SyncGetKeyValue(keyAlloc.SessionResumptionIndex(), buf.data(), len) != CHIP_NO_ERROR) + { + index.mSize = 0; + return CHIP_NO_ERROR; + } TLV::ContiguousBufferTLVReader reader; reader.Init(buf.data(), len); diff --git a/src/protocols/secure_channel/tests/TestSimpleSessionResumptionStorage.cpp b/src/protocols/secure_channel/tests/TestSimpleSessionResumptionStorage.cpp index f967308e73692c..66b0f00c2ace14 100644 --- a/src/protocols/secure_channel/tests/TestSimpleSessionResumptionStorage.cpp +++ b/src/protocols/secure_channel/tests/TestSimpleSessionResumptionStorage.cpp @@ -87,6 +87,10 @@ void TestIndex(nlTestSuite * inSuite, void * inContext) chip::ScopedNodeId node(node1, fabric1); + chip::SessionResumptionStorage::SessionIndex index0o; + NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == sessionStorage.LoadIndex(index0o)); + NL_TEST_ASSERT(inSuite, index0o.mSize == 0); + chip::SessionResumptionStorage::SessionIndex index1; index1.mSize = 0; NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == sessionStorage.SaveIndex(index1)); @@ -116,6 +120,7 @@ static const nlTest sTests[] = { NL_TEST_DEF("TestLink", TestLink), NL_TEST_DEF("TestState", TestState), + NL_TEST_DEF("TestIndex", TestState), NL_TEST_SENTINEL() };