Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign fabrics to Alice and Bob in MessingContext test class #16214

Merged
merged 2 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/app/tests/TestWriteInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,13 @@ int Test_Setup(void * inContext)

VerifyOrReturnError(TestContext::InitializeAsync(inContext) == SUCCESS, FAILURE);

constexpr chip::FabricIndex kFabricIndex1 = 1;
static const uint8_t kCompressedFabricId[] = { 0x29, 0x06, 0xC9, 0x08, 0xD1, 0x15, 0xD3, 0x62 };
TestContext & ctx = *static_cast<TestContext *>(inContext);
VerifyOrReturnError(CHIP_NO_ERROR == chip::GroupTesting::InitProvider(), FAILURE);
VerifyOrReturnError(CHIP_NO_ERROR == chip::GroupTesting::InitData(kFabricIndex1, chip::ByteSpan(kCompressedFabricId)), FAILURE);

uint8_t buf[sizeof(chip::CompressedFabricId)];
chip::MutableByteSpan span(buf);
VerifyOrReturnError(CHIP_NO_ERROR == ctx.GetBobFabric()->GetCompressedId(span), FAILURE);
VerifyOrReturnError(CHIP_NO_ERROR == chip::GroupTesting::InitData(ctx.GetBobFabricIndex(), span), FAILURE);

return SUCCESS;
}
Expand Down
23 changes: 23 additions & 0 deletions src/credentials/FabricTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,27 @@ CHIP_ERROR formatKey(FabricIndex fabricIndex, MutableCharSpan formattedKey, cons
return err;
}

CHIP_ERROR FabricInfo::TestOnlyBuildFabric(ByteSpan rootCert, ByteSpan icacCert, ByteSpan nocCert, ByteSpan nodePubKey,
ByteSpan nodePrivateKey)
{
Reset();

ReturnErrorOnFailure(SetRootCert(rootCert));
ReturnErrorOnFailure(SetICACert(icacCert));
ReturnErrorOnFailure(SetNOCCert(nocCert));

// NOTE: this requres ENABLE_HSM_CASE_OPS_KEY is not defined
P256SerializedKeypair opKeysSerialized;
memcpy(static_cast<uint8_t *>(opKeysSerialized), nodePubKey.data(), nodePubKey.size());
memcpy(static_cast<uint8_t *>(opKeysSerialized) + nodePubKey.size(), nodePrivateKey.data(), nodePrivateKey.size());
ReturnErrorOnFailure(opKeysSerialized.SetLength(nodePubKey.size() + nodePrivateKey.size()));

P256Keypair opKey;
ReturnErrorOnFailure(opKey.Deserialize(opKeysSerialized));
ReturnErrorOnFailure(SetOperationalKeypair(&opKey));
kghost marked this conversation as resolved.
Show resolved Hide resolved

// NOTE: mVendorId and mFabricLabel are not initialize, because they are not used in tests.
return CHIP_NO_ERROR;
}

} // namespace chip
4 changes: 4 additions & 0 deletions src/credentials/FabricTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ class DLL_EXPORT FabricInfo

friend class FabricTable;

// Test-only, build a fabric using given root cert and NOC
CHIP_ERROR TestOnlyBuildFabric(ByteSpan rootCert, ByteSpan icacCert, ByteSpan nocCert, ByteSpan nodePubKey,
ByteSpan nodePrivateKey);

private:
static constexpr size_t MetadataTLVMaxSize()
{
Expand Down
1 change: 1 addition & 0 deletions src/messaging/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static_library("helpers") {
cflags = [ "-Wconversion" ]

deps = [
"${chip_root}/src/credentials/tests:cert_test_vectors",
"${chip_root}/src/messaging",
"${chip_root}/src/protocols",
"${chip_root}/src/transport",
Expand Down
41 changes: 33 additions & 8 deletions src/messaging/tests/MessagingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "MessagingContext.h"

#include <credentials/tests/CHIPCert_test_vectors.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/ErrorStr.h>

Expand All @@ -38,9 +39,31 @@ CHIP_ERROR MessagingContext::Init(TransportMgrBase * transport, IOContext * ioCo
ReturnErrorOnFailure(mExchangeManager.Init(&mSessionManager));
ReturnErrorOnFailure(mMessageCounterManager.Init(&mExchangeManager));

ReturnErrorOnFailure(CreateSessionBobToAlice());
ReturnErrorOnFailure(CreateSessionAliceToBob());
ReturnErrorOnFailure(CreateSessionBobToFriends());
if (mInitializeNodes)
{
FabricInfo aliceFabric;
FabricInfo bobFabric;

aliceFabric.TestOnlyBuildFabric(
ByteSpan(TestCerts::sTestCert_Root01_Chip, TestCerts::sTestCert_Root01_Chip_Len),
ByteSpan(TestCerts::sTestCert_ICA01_Chip, TestCerts::sTestCert_ICA01_Chip_Len),
ByteSpan(TestCerts::sTestCert_Node01_01_Chip, TestCerts::sTestCert_Node01_01_Chip_Len),
ByteSpan(TestCerts::sTestCert_Node01_01_PublicKey, TestCerts::sTestCert_Node01_01_PublicKey_Len),
ByteSpan(TestCerts::sTestCert_Node01_01_PrivateKey, TestCerts::sTestCert_Node01_01_PrivateKey_Len));
ReturnErrorOnFailure(mFabricTable.AddNewFabric(aliceFabric, &mAliceFabricIndex));

bobFabric.TestOnlyBuildFabric(
ByteSpan(TestCerts::sTestCert_Root02_Chip, TestCerts::sTestCert_Root02_Chip_Len),
ByteSpan(TestCerts::sTestCert_ICA02_Chip, TestCerts::sTestCert_ICA02_Chip_Len),
ByteSpan(TestCerts::sTestCert_Node02_01_Chip, TestCerts::sTestCert_Node02_01_Chip_Len),
ByteSpan(TestCerts::sTestCert_Node02_01_PublicKey, TestCerts::sTestCert_Node02_01_PublicKey_Len),
ByteSpan(TestCerts::sTestCert_Node02_01_PrivateKey, TestCerts::sTestCert_Node02_01_PrivateKey_Len));
ReturnErrorOnFailure(mFabricTable.AddNewFabric(bobFabric, &mBobFabricIndex));

ReturnErrorOnFailure(CreateSessionBobToAlice());
ReturnErrorOnFailure(CreateSessionAliceToBob());
ReturnErrorOnFailure(CreateSessionBobToFriends());
}

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -72,19 +95,21 @@ CHIP_ERROR MessagingContext::ShutdownAndRestoreExisting(MessagingContext & exist

CHIP_ERROR MessagingContext::CreateSessionBobToAlice()
{
return mSessionManager.NewPairing(mSessionBobToAlice, Optional<Transport::PeerAddress>::Value(mAliceAddress), GetAliceNodeId(),
&mPairingBobToAlice, CryptoContext::SessionRole::kInitiator, mSrcFabricIndex);
return mSessionManager.NewPairing(mSessionBobToAlice, Optional<Transport::PeerAddress>::Value(mAliceAddress),
GetAliceFabric()->GetNodeId(), &mPairingBobToAlice, CryptoContext::SessionRole::kInitiator,
mBobFabricIndex);
}

CHIP_ERROR MessagingContext::CreateSessionAliceToBob()
{
return mSessionManager.NewPairing(mSessionAliceToBob, Optional<Transport::PeerAddress>::Value(mBobAddress), GetBobNodeId(),
&mPairingAliceToBob, CryptoContext::SessionRole::kResponder, mDestFabricIndex);
return mSessionManager.NewPairing(mSessionAliceToBob, Optional<Transport::PeerAddress>::Value(mBobAddress),
GetBobFabric()->GetNodeId(), &mPairingAliceToBob, CryptoContext::SessionRole::kResponder,
mAliceFabricIndex);
}

CHIP_ERROR MessagingContext::CreateSessionBobToFriends()
{
mSessionBobToFriends.Emplace(GetFriendsGroupId(), mSrcFabricIndex, GetBobNodeId());
mSessionBobToFriends.Emplace(GetFriendsGroupId(), mBobFabricIndex, GetBobFabric()->GetNodeId());
return CHIP_NO_ERROR;
}

Expand Down
44 changes: 18 additions & 26 deletions src/messaging/tests/MessagingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ class MessagingContext : public PlatformMemoryUser
public:
MessagingContext() :
mInitialized(false), mAliceAddress(Transport::PeerAddress::UDP(GetAddress(), CHIP_PORT + 1)),
mBobAddress(Transport::PeerAddress::UDP(GetAddress(), CHIP_PORT)), mPairingAliceToBob(GetBobKeyId(), GetAliceKeyId()),
mPairingBobToAlice(GetAliceKeyId(), GetBobKeyId())
mBobAddress(Transport::PeerAddress::UDP(GetAddress(), CHIP_PORT)), mPairingAliceToBob(kBobKeyId, kAliceKeyId),
mPairingBobToAlice(kAliceKeyId, kBobKeyId)
{}
~MessagingContext() { VerifyOrDie(mInitialized == false); }

// Whether Alice and Bob are initialized, must be called before Init
void ConfigInitializeNodes(bool initializeNodes) { mInitializeNodes = initializeNodes; }

/// Initialize the underlying layers and test suite pointer
CHIP_ERROR Init(TransportMgrBase * transport, IOContext * io);

Expand All @@ -96,30 +99,22 @@ class MessagingContext : public PlatformMemoryUser
Inet::IPAddress::FromString("::1", addr);
return addr;
}
NodeId GetBobNodeId() const { return mBobNodeId; }
NodeId GetAliceNodeId() const { return mAliceNodeId; }

void SetBobNodeId(NodeId nodeId) { mBobNodeId = nodeId; }
void SetAliceNodeId(NodeId nodeId) { mAliceNodeId = nodeId; }

uint16_t GetBobKeyId() const { return mBobKeyId; }
uint16_t GetAliceKeyId() const { return mAliceKeyId; }
static const uint16_t kBobKeyId = 1;
static const uint16_t kAliceKeyId = 2;
NodeId GetBobNodeId() const;
NodeId GetAliceNodeId() const;
GroupId GetFriendsGroupId() const { return mFriendsGroupId; }

void SetBobKeyId(uint16_t id) { mBobKeyId = id; }
void SetAliceKeyId(uint16_t id) { mAliceKeyId = id; }

FabricIndex GetFabricIndex() const { return mSrcFabricIndex; }
void SetFabricIndex(FabricIndex id)
{
mSrcFabricIndex = id;
mDestFabricIndex = id;
}

SessionManager & GetSecureSessionManager() { return mSessionManager; }
Messaging::ExchangeManager & GetExchangeManager() { return mExchangeManager; }
secure_channel::MessageCounterManager & GetMessageCounterManager() { return mMessageCounterManager; }

FabricIndex GetAliceFabricIndex() { return mAliceFabricIndex; }
FabricIndex GetBobFabricIndex() { return mBobFabricIndex; }
FabricInfo * GetAliceFabric() { return mFabricTable.FindFabricWithIndex(mAliceFabricIndex); }
FabricInfo * GetBobFabric() { return mFabricTable.FindFabricWithIndex(mBobFabricIndex); }

CHIP_ERROR CreateSessionBobToAlice();
CHIP_ERROR CreateSessionAliceToBob();
CHIP_ERROR CreateSessionBobToFriends();
Expand All @@ -141,6 +136,7 @@ class MessagingContext : public PlatformMemoryUser
System::Layer & GetSystemLayer() { return mIOContext->GetSystemLayer(); }

private:
bool mInitializeNodes = true;
bool mInitialized;
FabricTable mFabricTable;
SessionManager mSessionManager;
Expand All @@ -150,20 +146,16 @@ class MessagingContext : public PlatformMemoryUser
TransportMgrBase * mTransport; // Only needed for InitFromExisting.
chip::TestPersistentStorageDelegate mStorage; // for SessionManagerInit

NodeId mBobNodeId = 123654;
NodeId mAliceNodeId = 111222333;
uint16_t mBobKeyId = 1;
uint16_t mAliceKeyId = 2;
GroupId mFriendsGroupId = 0x0101;
FabricIndex mAliceFabricIndex = kUndefinedFabricIndex;
FabricIndex mBobFabricIndex = kUndefinedFabricIndex;
GroupId mFriendsGroupId = 0x0101;
Transport::PeerAddress mAliceAddress;
Transport::PeerAddress mBobAddress;
SecurePairingUsingTestSecret mPairingAliceToBob;
SecurePairingUsingTestSecret mPairingBobToAlice;
SessionHolder mSessionAliceToBob;
SessionHolder mSessionBobToAlice;
Optional<Transport::OutgoingGroupSession> mSessionBobToFriends;
FabricIndex mSrcFabricIndex = 1;
FabricIndex mDestFabricIndex = 1;
};

template <typename Transport = LoopbackTransport>
Expand Down
8 changes: 2 additions & 6 deletions src/messaging/tests/TestExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,13 @@ void CheckNewContextTest(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, ec1 != nullptr);
NL_TEST_ASSERT(inSuite, ec1->IsInitiator() == true);
NL_TEST_ASSERT(inSuite, ec1->GetExchangeId() != 0);
auto sessionPeerToLocal = ec1->GetSessionHandle()->AsSecureSession();
NL_TEST_ASSERT(inSuite, sessionPeerToLocal->GetPeerNodeId() == ctx.GetBobNodeId());
NL_TEST_ASSERT(inSuite, sessionPeerToLocal->GetPeerSessionId() == ctx.GetBobKeyId());
NL_TEST_ASSERT(inSuite, ec1->GetSessionHandle() == ctx.GetSessionAliceToBob());
NL_TEST_ASSERT(inSuite, ec1->GetDelegate() == &mockAppDelegate);

ExchangeContext * ec2 = ctx.NewExchangeToAlice(&mockAppDelegate);
NL_TEST_ASSERT(inSuite, ec2 != nullptr);
NL_TEST_ASSERT(inSuite, ec2->GetExchangeId() > ec1->GetExchangeId());
auto sessionLocalToPeer = ec2->GetSessionHandle()->AsSecureSession();
NL_TEST_ASSERT(inSuite, sessionLocalToPeer->GetPeerNodeId() == ctx.GetAliceNodeId());
NL_TEST_ASSERT(inSuite, sessionLocalToPeer->GetPeerSessionId() == ctx.GetAliceKeyId());
NL_TEST_ASSERT(inSuite, ec2->GetSessionHandle() == ctx.GetSessionBobToAlice());

ec1->Close();
ec2->Close();
Expand Down
7 changes: 1 addition & 6 deletions src/protocols/secure_channel/tests/TestCASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,15 +656,10 @@ CHIP_ERROR CASETestSecurePairingSetup(void * inContext)
{
TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);

ctx.ConfigInitializeNodes(false);
ReturnErrorOnFailure(ctx.Init());
ctx.EnableAsyncDispatch();

ctx.SetBobNodeId(kPlaceholderNodeId);
ctx.SetAliceNodeId(kPlaceholderNodeId);
ctx.SetBobKeyId(0);
ctx.SetAliceKeyId(0);
ctx.SetFabricIndex(kUndefinedFabricIndex);

gCommissionerFabrics.Init(&gCommissionerStorageDelegate);
gDeviceFabrics.Init(&gDeviceStorageDelegate);

Expand Down
10 changes: 3 additions & 7 deletions src/protocols/secure_channel/tests/TestPASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,16 +496,12 @@ static nlTestSuite sSuite =
*/
int TestSecurePairing_Setup(void * inContext)
{
auto & ctx = *static_cast<TestContext *>(inContext);

// Initialize System memory and resources
ctx.ConfigInitializeNodes(false);
VerifyOrReturnError(TestContext::InitializeAsync(inContext) == SUCCESS, FAILURE);

auto & ctx = *static_cast<TestContext *>(inContext);
ctx.SetBobNodeId(kPlaceholderNodeId);
ctx.SetAliceNodeId(kPlaceholderNodeId);
ctx.SetBobKeyId(0);
ctx.SetAliceKeyId(0);
ctx.SetFabricIndex(kUndefinedFabricIndex);

return SUCCESS;
}

Expand Down