3 * Copyright (c) 2020-2021 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file implements unit tests for the ExchangeManager implementation.
24 #include "TestMessagingLayer.h"
26 #include <core/CHIPCore.h>
27 #include <messaging/ExchangeContext.h>
28 #include <messaging/ExchangeMgr.h>
29 #include <messaging/Flags.h>
30 #include <messaging/tests/MessagingContext.h>
31 #include <protocols/Protocols.h>
32 #include <support/CHIPMem.h>
33 #include <support/CodeUtils.h>
34 #include <transport/SecureSessionMgr.h>
35 #include <transport/TransportMgr.h>
37 #include <nlbyteorder.h>
38 #include <nlunit-test.h>
46 using namespace chip::Inet;
47 using namespace chip::Transport;
48 using namespace chip::Messaging;
50 using TestContext = chip::Test::MessagingContext;
54 class LoopbackTransport : public Transport::Base
57 /// Transports are required to have a constructor that takes exactly one argument
58 CHIP_ERROR Init(const char * unused) { return CHIP_NO_ERROR; }
60 CHIP_ERROR SendMessage(const PacketHeader & header, const PeerAddress & address, System::PacketBufferHandle msgBuf) override
62 HandleMessageReceived(header, address, std::move(msgBuf));
66 bool CanSendToPeer(const PeerAddress & address) override { return true; }
69 TransportMgr<LoopbackTransport> gTransportMgr;
71 class MockAppDelegate : public ExchangeDelegate
74 void OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
75 System::PacketBufferHandle buffer) override
77 IsOnMessageReceivedCalled = true;
80 void OnResponseTimeout(ExchangeContext * ec) override {}
82 bool IsOnMessageReceivedCalled = false;
85 void CheckNewContextTest(nlTestSuite * inSuite, void * inContext)
87 TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);
89 MockAppDelegate mockAppDelegate;
90 ExchangeContext * ec1 = ctx.NewExchangeToLocal(&mockAppDelegate);
91 NL_TEST_ASSERT(inSuite, ec1 != nullptr);
92 NL_TEST_ASSERT(inSuite, ec1->IsInitiator() == true);
93 NL_TEST_ASSERT(inSuite, ec1->GetExchangeId() != 0);
94 auto sessionPeerToLocal = ctx.GetSecureSessionManager().GetPeerConnectionState(ec1->GetSecureSession());
95 NL_TEST_ASSERT(inSuite, sessionPeerToLocal->GetPeerNodeId() == ctx.GetSourceNodeId());
96 NL_TEST_ASSERT(inSuite, sessionPeerToLocal->GetPeerKeyID() == ctx.GetLocalKeyId());
97 NL_TEST_ASSERT(inSuite, ec1->GetDelegate() == &mockAppDelegate);
99 ExchangeContext * ec2 = ctx.NewExchangeToPeer(&mockAppDelegate);
100 NL_TEST_ASSERT(inSuite, ec2 != nullptr);
101 NL_TEST_ASSERT(inSuite, ec2->GetExchangeId() > ec1->GetExchangeId());
102 auto sessionLocalToPeer = ctx.GetSecureSessionManager().GetPeerConnectionState(ec2->GetSecureSession());
103 NL_TEST_ASSERT(inSuite, sessionLocalToPeer->GetPeerNodeId() == ctx.GetDestinationNodeId());
104 NL_TEST_ASSERT(inSuite, sessionLocalToPeer->GetPeerKeyID() == ctx.GetPeerKeyId());
107 void CheckUmhRegistrationTest(nlTestSuite * inSuite, void * inContext)
109 TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);
112 MockAppDelegate mockAppDelegate;
114 err = ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id(VendorId::Common, 0x0001),
116 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
118 err = ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType(Protocols::Id(VendorId::Common, 0x0002), 0x0001,
120 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
122 err = ctx.GetExchangeManager().UnregisterUnsolicitedMessageHandlerForProtocol(Protocols::Id(VendorId::Common, 0x0001));
123 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
125 err = ctx.GetExchangeManager().UnregisterUnsolicitedMessageHandlerForProtocol(Protocols::Id(VendorId::Common, 0x0002));
126 NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
128 err = ctx.GetExchangeManager().UnregisterUnsolicitedMessageHandlerForType(Protocols::Id(VendorId::Common, 0x0002), 0x0001);
129 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
131 err = ctx.GetExchangeManager().UnregisterUnsolicitedMessageHandlerForType(Protocols::Id(VendorId::Common, 0x0002), 0x0002);
132 NL_TEST_ASSERT(inSuite, err != CHIP_NO_ERROR);
135 void CheckExchangeMessages(nlTestSuite * inSuite, void * inContext)
137 TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);
141 // create solicited exchange
142 MockAppDelegate mockSolicitedAppDelegate;
143 ExchangeContext * ec1 = ctx.NewExchangeToPeer(&mockSolicitedAppDelegate);
145 // create unsolicited exchange
146 MockAppDelegate mockUnsolicitedAppDelegate;
147 err = ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType(Protocols::Id(VendorId::Common, 0x0001), 0x0001,
148 &mockUnsolicitedAppDelegate);
149 NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);
151 // send a malicious packet
152 // TODO: https://github.com/project-chip/connectedhomeip/issues/4635
153 // ec1->SendMessage(0x0001, 0x0002, System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize),
154 // SendFlags(Messaging::SendMessageFlags::kNone));
155 NL_TEST_ASSERT(inSuite, !mockUnsolicitedAppDelegate.IsOnMessageReceivedCalled);
157 // send a good packet
158 ec1->SendMessage(Protocols::Id(VendorId::Common, 0x0001), 0x0001,
159 System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize),
160 SendFlags(Messaging::SendMessageFlags::kNone));
161 NL_TEST_ASSERT(inSuite, mockUnsolicitedAppDelegate.IsOnMessageReceivedCalled);
167 * Test Suite that lists all the test functions.
170 const nlTest sTests[] =
172 NL_TEST_DEF("Test ExchangeMgr::NewContext", CheckNewContextTest),
173 NL_TEST_DEF("Test ExchangeMgr::CheckUmhRegistrationTest", CheckUmhRegistrationTest),
174 NL_TEST_DEF("Test ExchangeMgr::CheckExchangeMessages", CheckExchangeMessages),
180 int Initialize(void * aContext);
181 int Finalize(void * aContext);
186 "Test-CHIP-ExchangeManager",
194 * Initialize the test suite.
196 int Initialize(void * aContext)
198 CHIP_ERROR err = chip::Platform::MemoryInit();
199 if (err != CHIP_NO_ERROR)
202 err = gTransportMgr.Init("LOOPBACK");
203 if (err != CHIP_NO_ERROR)
206 err = reinterpret_cast<TestContext *>(aContext)->Init(&sSuite, &gTransportMgr);
207 return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE;
211 * Finalize the test suite.
213 int Finalize(void * aContext)
215 CHIP_ERROR err = reinterpret_cast<TestContext *>(aContext)->Shutdown();
216 chip::Platform::MemoryShutdown();
217 return (err == CHIP_NO_ERROR) ? SUCCESS : FAILURE;
225 int TestExchangeMgr()
227 // Run test suit against one context
228 nlTestRunner(&sSuite, &sContext);
230 return (nlTestRunnerStats(&sSuite));