Skip to content

Commit 886a7a3

Browse files
committed
Used ASSERT_* in places where tests should be stopped immediately. Refactored to get rid of else branches that did nothing but ASSERT_TRUE(false).
1 parent 82d29b9 commit 886a7a3

File tree

3 files changed

+34
-67
lines changed

3 files changed

+34
-67
lines changed

src/protocols/bdx/tests/BUILD.gn

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
# Copyright (c) 2020 Project CHIP Authors
1+
#Copyright(c) 2020 Project CHIP Authors
22
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
3+
#Licensed under the Apache License, Version 2.0(the "License");
4+
#you may not use this file except in compliance with the License.
5+
#You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
#http: // www.apache.org/licenses/LICENSE-2.0
88
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
9+
#Unless required by applicable law or agreed to in writing, software
10+
#distributed under the License is distributed on an "AS IS" BASIS,
11+
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
#See the License for the specific language governing permissions and
13+
#limitations under the License.
1414

1515
import("//build_overrides/build.gni")
1616
import("//build_overrides/chip.gni")
1717
import("//build_overrides/pigweed.gni")
1818

1919
import("${chip_root}/build/chip/chip_test_suite.gni")
20-
2120
chip_test_suite("tests") {
2221
output_name = "libBDXTests"
2322

src/protocols/bdx/tests/TestBdxMessages.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ void TestHelperWrittenAndParsedMatch(MsgType & testMsg)
2222

2323
size_t msgSize = testMsg.MessageSize();
2424
Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(msgSize));
25-
EXPECT_FALSE(bbuf.IsNull());
25+
ASSERT_FALSE(bbuf.IsNull());
2626

2727
testMsg.WriteToBuffer(bbuf);
2828
EXPECT_TRUE(bbuf.Fit());
2929

3030
System::PacketBufferHandle msgBuf = bbuf.Finalize();
31-
EXPECT_FALSE(msgBuf.IsNull());
31+
ASSERT_FALSE(msgBuf.IsNull());
3232
System::PacketBufferHandle rcvBuf = System::PacketBufferHandle::NewWithData(msgBuf->Start(), msgSize);
33-
EXPECT_FALSE(rcvBuf.IsNull());
33+
ASSERT_FALSE(rcvBuf.IsNull());
3434

3535
MsgType testMsgRcvd;
3636
err = testMsgRcvd.Parse(std::move(rcvBuf));
@@ -43,7 +43,7 @@ struct TestBdxMessages : public ::testing::Test
4343
static void SetUpTestSuite()
4444
{
4545
CHIP_ERROR error = chip::Platform::MemoryInit();
46-
EXPECT_EQ(error, CHIP_NO_ERROR);
46+
ASSERT_EQ(error, CHIP_NO_ERROR);
4747
}
4848

4949
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }

src/protocols/bdx/tests/TestBdxTransferSession.cpp

+20-52
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,10 @@ void VerifyStatusReport(const System::PacketBufferHandle & msg, StatusCode expec
104104
{
105105
CHIP_ERROR err = CHIP_NO_ERROR;
106106

107-
if (msg.IsNull())
108-
{
109-
EXPECT_TRUE(false);
110-
return;
111-
}
107+
ASSERT_FALSE(msg.IsNull())
112108

113109
System::PacketBufferHandle msgCopy = msg.CloneData();
114-
if (msgCopy.IsNull())
115-
{
116-
EXPECT_TRUE(false);
117-
return;
118-
}
110+
ASSERT_FALSE(msgCopy.IsNull())
119111

120112
SecureChannel::StatusReport report;
121113
err = report.Parse(std::move(msgCopy));
@@ -175,22 +167,14 @@ void SendAndVerifyTransferInit(TransferSession::OutputEvent & outEvent, System::
175167
}
176168
if (outEvent.transferInitData.Metadata != nullptr)
177169
{
178-
EXPECT_EQ(outEvent.transferInitData.MetadataLength, initData.MetadataLength);
179-
if (outEvent.transferInitData.MetadataLength == initData.MetadataLength)
180-
{
181-
// Even if initData.MetadataLength is 0, it is still technically undefined behaviour to call memcmp with a null
182-
bool isNullAndLengthZero = initData.Metadata == nullptr && initData.MetadataLength == 0;
183-
if (!isNullAndLengthZero)
184-
{
185-
// Only check that metadata buffers match. The OutputEvent can still be inspected when this function returns to
186-
// parse the metadata and verify that it matches.
187-
EXPECT_EQ(0,
188-
memcmp(initData.Metadata, outEvent.transferInitData.Metadata, outEvent.transferInitData.MetadataLength));
189-
}
190-
}
191-
else
170+
ASSERT_EQ(outEvent.transferInitData.MetadataLength, initData.MetadataLength);
171+
// Even if initData.MetadataLength is 0, it is still technically undefined behaviour to call memcmp with a null
172+
bool isNullAndLengthZero = initData.Metadata == nullptr && initData.MetadataLength == 0;
173+
if (!isNullAndLengthZero)
192174
{
193-
EXPECT_TRUE(false); // Metadata length mismatch
175+
// Only check that metadata buffers match. The OutputEvent can still be inspected when this function returns to
176+
// parse the metadata and verify that it matches.
177+
EXPECT_EQ(0, memcmp(initData.Metadata, outEvent.transferInitData.Metadata, outEvent.transferInitData.MetadataLength));
194178
}
195179
}
196180
}
@@ -235,23 +219,15 @@ void SendAndVerifyAcceptMsg(TransferSession::OutputEvent & outEvent, TransferSes
235219
EXPECT_EQ(outEvent.transferAcceptData.Length, acceptData.Length);
236220
if (outEvent.transferAcceptData.Metadata != nullptr)
237221
{
238-
EXPECT_EQ(outEvent.transferAcceptData.MetadataLength, acceptData.MetadataLength);
239-
if (outEvent.transferAcceptData.MetadataLength == acceptData.MetadataLength)
222+
ASSERT_EQ(outEvent.transferAcceptData.MetadataLength, acceptData.MetadataLength);
223+
// Even if acceptData.MetadataLength is 0, it is still technically undefined behaviour to call memcmp with a null
224+
bool isNullAndLengthZero = acceptData.Metadata == nullptr && acceptData.MetadataLength == 0;
225+
if (!isNullAndLengthZero)
240226
{
241-
// Even if acceptData.MetadataLength is 0, it is still technically undefined behaviour to call memcmp with a null
242-
bool isNullAndLengthZero = acceptData.Metadata == nullptr && acceptData.MetadataLength == 0;
243-
if (!isNullAndLengthZero)
244-
{
245-
// Only check that metadata buffers match. The OutputEvent can still be inspected when this function returns to
246-
// parse the metadata and verify that it matches.
247-
EXPECT_EQ(
248-
0,
249-
memcmp(acceptData.Metadata, outEvent.transferAcceptData.Metadata, outEvent.transferAcceptData.MetadataLength));
250-
}
251-
}
252-
else
253-
{
254-
EXPECT_TRUE(false); // Metadata length mismatch
227+
// Only check that metadata buffers match. The OutputEvent can still be inspected when this function returns to
228+
// parse the metadata and verify that it matches.
229+
EXPECT_EQ(
230+
0, memcmp(acceptData.Metadata, outEvent.transferAcceptData.Metadata, outEvent.transferAcceptData.MetadataLength));
255231
}
256232
}
257233

@@ -289,11 +265,7 @@ void SendAndVerifyArbitraryBlock(TransferSession & sender, TransferSession & rec
289265

290266
EXPECT_GT(maxBlockSize, 0);
291267
System::PacketBufferHandle fakeDataBuf = System::PacketBufferHandle::New(maxBlockSize);
292-
if (fakeDataBuf.IsNull())
293-
{
294-
EXPECT_TRUE(false);
295-
return;
296-
}
268+
ASSERT_FALSE(fakeDataBuf.IsNull());
297269

298270
uint8_t * fakeBlockData = fakeDataBuf->Start();
299271
fakeBlockData[0] = dataCount++;
@@ -356,7 +328,7 @@ struct TestBdxTransferSession : public ::testing::Test
356328
static void SetUpTestSuite()
357329
{
358330
CHIP_ERROR error = chip::Platform::MemoryInit();
359-
EXPECT_EQ(error, CHIP_NO_ERROR);
331+
ASSERT_EQ(error, CHIP_NO_ERROR);
360332
}
361333

362334
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
@@ -434,11 +406,7 @@ TEST_F(TestBdxTransferSession, TestInitiatingReceiverReceiverDrive)
434406
// Test only one block can be prepared at a time, without receiving a response to the first
435407
System::PacketBufferHandle fakeBuf = System::PacketBufferHandle::New(testSmallerBlockSize);
436408
TransferSession::BlockData prematureBlock;
437-
if (fakeBuf.IsNull())
438-
{
439-
EXPECT_TRUE(false);
440-
return;
441-
}
409+
ASSERT_FALSE(fakeBuf.IsNull());
442410
prematureBlock.Data = fakeBuf->Start();
443411
prematureBlock.Length = testSmallerBlockSize;
444412
prematureBlock.IsEof = false;

0 commit comments

Comments
 (0)