Skip to content

Commit df32595

Browse files
committed
Fix tests
1 parent 332672d commit df32595

7 files changed

+54
-74
lines changed

src/platform/tests/TestConfigurationMgr.cpp

+13-15
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <gtest/gtest.h>
3232
#include <lib/support/CHIPMem.h>
3333
#include <lib/support/CodeUtils.h>
34+
3435
#include <platform/BuildTime.h>
3536
#include <platform/CHIPDeviceLayer.h>
3637
#include <platform/DeviceInstanceInfoProvider.h>
@@ -50,6 +51,7 @@ struct TestConfigurationMgr : ::testing::Test
5051
{
5152
static void SetUpTestSuite()
5253
{
54+
// ConfigurationManager is initialized from PlatformManager indirectly
5355
CHIP_ERROR err = chip::Platform::MemoryInit();
5456
EXPECT_EQ(err, CHIP_NO_ERROR);
5557
err = PlatformMgr().InitChipStack();
@@ -89,7 +91,7 @@ TEST_F(TestConfigurationMgr, SerialNumber)
8991
EXPECT_EQ(err, CHIP_NO_ERROR);
9092

9193
EXPECT_EQ(strlen(buf), 12u);
92-
ASSERT_STREQ(buf, serialNumber);
94+
EXPECT_STREQ(buf, serialNumber);
9395

9496
err = ConfigurationMgr().StoreSerialNumber(serialNumber, 5);
9597
EXPECT_EQ(err, CHIP_NO_ERROR);
@@ -98,7 +100,7 @@ TEST_F(TestConfigurationMgr, SerialNumber)
98100
EXPECT_EQ(err, CHIP_NO_ERROR);
99101

100102
EXPECT_EQ(strlen(buf), 5u);
101-
ASSERT_STREQ(buf, "89051");
103+
EXPECT_STREQ(buf, "89051");
102104
}
103105

104106
TEST_F(TestConfigurationMgr, UniqueId)
@@ -115,7 +117,7 @@ TEST_F(TestConfigurationMgr, UniqueId)
115117
EXPECT_EQ(err, CHIP_NO_ERROR);
116118

117119
EXPECT_EQ(strlen(buf), 14u);
118-
ASSERT_STREQ(buf, uniqueId);
120+
EXPECT_STREQ(buf, uniqueId);
119121

120122
err = ConfigurationMgr().StoreUniqueId(uniqueId, 7);
121123
EXPECT_EQ(err, CHIP_NO_ERROR);
@@ -124,7 +126,7 @@ TEST_F(TestConfigurationMgr, UniqueId)
124126
EXPECT_EQ(err, CHIP_NO_ERROR);
125127

126128
EXPECT_EQ(strlen(buf), 7u);
127-
ASSERT_STREQ(buf, "67MXAZ0");
129+
EXPECT_STREQ(buf, "67MXAZ0");
128130
}
129131

130132
TEST_F(TestConfigurationMgr, ManufacturingDate)
@@ -265,8 +267,8 @@ TEST_F(TestConfigurationMgr, FirmwareBuildTime)
265267
const char * timeOfDay = CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME;
266268

267269
// Check that strings look good.
268-
EXPECT_TRUE(!BUILD_DATE_IS_BAD(date));
269-
EXPECT_TRUE(!BUILD_TIME_IS_BAD(timeOfDay));
270+
EXPECT_FALSE(BUILD_DATE_IS_BAD(date));
271+
EXPECT_FALSE(BUILD_TIME_IS_BAD(timeOfDay));
270272
if (BUILD_DATE_IS_BAD(date) || BUILD_TIME_IS_BAD(timeOfDay))
271273
{
272274
break;
@@ -299,8 +301,8 @@ TEST_F(TestConfigurationMgr, FirmwareBuildTime)
299301
}
300302

301303
// Verify match.
302-
ASSERT_STREQ(date, parsedDate);
303-
ASSERT_STREQ(timeOfDay, parsedTimeOfDay);
304+
EXPECT_STREQ(date, parsedDate);
305+
EXPECT_STREQ(timeOfDay, parsedTimeOfDay);
304306
} while (false);
305307

306308
// Generate random chip epoch times and verify that our BuildTime.h parser
@@ -333,8 +335,8 @@ TEST_F(TestConfigurationMgr, FirmwareBuildTime)
333335
}
334336

335337
// Check that strings look good.
336-
EXPECT_TRUE(!BUILD_DATE_IS_BAD(date));
337-
EXPECT_TRUE(!BUILD_TIME_IS_BAD(timeOfDay));
338+
EXPECT_FALSE(BUILD_DATE_IS_BAD(date));
339+
EXPECT_FALSE(BUILD_TIME_IS_BAD(timeOfDay));
338340
if (BUILD_DATE_IS_BAD(date) || BUILD_TIME_IS_BAD(timeOfDay))
339341
{
340342
continue;
@@ -374,7 +376,7 @@ TEST_F(TestConfigurationMgr, CountryCode)
374376
EXPECT_EQ(err, CHIP_NO_ERROR);
375377

376378
EXPECT_EQ(countryCodeLen, strlen(countryCode));
377-
ASSERT_STREQ(buf, countryCode);
379+
EXPECT_STREQ(buf, countryCode);
378380
}
379381

380382
TEST_F(TestConfigurationMgr, GetPrimaryMACAddress)
@@ -464,7 +466,3 @@ TEST_F(TestConfigurationMgr, GetProductId)
464466
}
465467

466468
} // namespace
467-
468-
/**
469-
* Main
470-
*/

src/platform/tests/TestConnectivityMgr.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ struct TestConnectivityMgr : public ::testing::Test
5050

5151
static void SetUpTestSuite()
5252
{
53-
// ConfigurationManager is initialized from PlatformManager indirectly
54-
CHIP_ERROR err = PlatformMgr().InitChipStack();
53+
auto err = chip::Platform::MemoryInit();
5554
EXPECT_EQ(err, CHIP_NO_ERROR);
56-
err = chip::Platform::MemoryInit();
57-
ASSERT_EQ(err, CHIP_NO_ERROR);
5855
}
5956

6057
static void TearDownTestSuite()
@@ -63,6 +60,12 @@ struct TestConnectivityMgr : public ::testing::Test
6360
chip::DeviceLayer::PlatformMgr().Shutdown();
6461
}
6562
};
63+
TEST_F(TestConnectivityMgr, Init)
64+
{
65+
// ConfigurationManager is initialized from PlatformManager indirectly
66+
CHIP_ERROR err = PlatformMgr().InitChipStack();
67+
EXPECT_EQ(err, CHIP_NO_ERROR);
68+
}
6669

6770
TEST_F(TestConnectivityMgr, GetNetworkInterfaces)
6871
{
@@ -72,7 +75,7 @@ TEST_F(TestConnectivityMgr, GetNetworkInterfaces)
7275

7376
err = GetDiagnosticDataProvider().GetNetworkInterfaces(&netifs);
7477
EXPECT_EQ(err, CHIP_NO_ERROR);
75-
ASSERT_NE(netifs, nullptr);
78+
EXPECT_NE(netifs, nullptr);
7679

7780
GetDiagnosticDataProvider().ReleaseNetworkInterfaces(netifs);
7881
}

src/platform/tests/TestDnssd.cpp

+25-36
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@ using chip::Dnssd::TextEntry;
4141

4242
namespace {
4343

44-
struct DnssdContext
45-
{
46-
std::atomic<bool> mTimeoutExpired{ false };
47-
48-
intptr_t mBrowseIdentifier = 0;
49-
50-
unsigned int mBrowsedServicesCount = 0;
51-
unsigned int mResolvedServicesCount = 0;
52-
bool mEndOfInput = false;
53-
};
54-
5544
class TestDnssdResolveServerDelegate : public mdns::Minimal::ServerDelegate, public mdns::Minimal::ParserDelegate
5645
{
5746
public:
@@ -96,11 +85,19 @@ class TestDnssd : public ::testing::Test
9685
chip::DeviceLayer::PlatformMgr().Shutdown();
9786
chip::Platform::MemoryShutdown();
9887
}
88+
89+
std::atomic<bool> mTimeoutExpired{ false };
90+
91+
intptr_t mBrowseIdentifier = 0;
92+
93+
unsigned int mBrowsedServicesCount = 0;
94+
unsigned int mResolvedServicesCount = 0;
95+
bool mEndOfInput = false;
9996
};
10097

10198
static void Timeout(chip::System::Layer * systemLayer, void * context)
10299
{
103-
auto * ctx = static_cast<DnssdContext *>(context);
100+
auto * ctx = static_cast<TestDnssd *>(context);
104101
ChipLogError(DeviceLayer, "mDNS test timeout, is avahi daemon running?");
105102
ctx->mTimeoutExpired = true;
106103
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
@@ -109,16 +106,12 @@ static void Timeout(chip::System::Layer * systemLayer, void * context)
109106
static void HandleResolve(void * context, DnssdService * result, const chip::Span<chip::Inet::IPAddress> & addresses,
110107
CHIP_ERROR error)
111108
{
112-
auto * ctx = static_cast<DnssdContext *>(context);
109+
auto * ctx = static_cast<TestDnssd *>(context);
113110
char addrBuf[100];
114111

115-
ASSERT_NE(result, nullptr);
112+
EXPECT_NE(result, nullptr);
116113
EXPECT_EQ(error, CHIP_NO_ERROR);
117114

118-
// The NL_TEST_ASSERT above will not abort the test, so we need to
119-
// explicitly abort it here to avoid dereferencing a null pointer.
120-
VerifyOrReturn(result != nullptr, );
121-
122115
if (!addresses.empty())
123116
{
124117
addresses.data()[0].ToString(addrBuf, sizeof(addrBuf));
@@ -147,7 +140,7 @@ static void HandleResolve(void * context, DnssdService * result, const chip::Spa
147140

148141
static void HandleBrowse(void * context, DnssdService * services, size_t servicesSize, bool finalBrowse, CHIP_ERROR error)
149142
{
150-
auto * ctx = static_cast<DnssdContext *>(context);
143+
auto * ctx = static_cast<TestDnssd *>(context);
151144

152145
// Make sure that we will not be called again after end-of-input is set
153146
EXPECT_EQ(ctx->mEndOfInput, false);
@@ -177,7 +170,7 @@ static void DnssdErrorCallback(void * context, CHIP_ERROR error)
177170

178171
void TestDnssdBrowse_DnssdInitCallback(void * context, CHIP_ERROR error)
179172
{
180-
auto * ctx = static_cast<DnssdContext *>(context);
173+
auto * ctx = static_cast<TestDnssd *>(context);
181174
EXPECT_EQ(error, CHIP_NO_ERROR);
182175

183176
EXPECT_EQ(ChipDnssdBrowse("_mock", DnssdServiceProtocol::kDnssdProtocolUdp, chip::Inet::IPAddressType::kAny,
@@ -194,8 +187,6 @@ void TestDnssdBrowse_DnssdInitCallback(void * context, CHIP_ERROR error)
194187
// services by querying for all of these records separately.
195188
TEST_F(TestDnssd, TestDnssdBrowse)
196189
{
197-
DnssdContext context;
198-
199190
mdns::Minimal::SetDefaultAddressPolicy();
200191

201192
mdns::Minimal::Server<10> server;
@@ -236,18 +227,18 @@ TEST_F(TestDnssd, TestDnssdBrowse)
236227
auto endpoints = mdns::Minimal::GetAddressPolicy()->GetListenEndpoints();
237228
EXPECT_EQ(server.Listen(chip::DeviceLayer::UDPEndPointManager(), endpoints.get(), 5353), CHIP_NO_ERROR);
238229

239-
EXPECT_EQ(chip::Dnssd::ChipDnssdInit(TestDnssdBrowse_DnssdInitCallback, DnssdErrorCallback, &context), CHIP_NO_ERROR);
240-
EXPECT_EQ(chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(5), Timeout, &context), CHIP_NO_ERROR);
230+
EXPECT_EQ(chip::Dnssd::ChipDnssdInit(TestDnssdBrowse_DnssdInitCallback, DnssdErrorCallback, this), CHIP_NO_ERROR);
231+
EXPECT_EQ(chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(5), Timeout, this), CHIP_NO_ERROR);
241232

242233
ChipLogProgress(DeviceLayer, "Start EventLoop");
243234
chip::DeviceLayer::PlatformMgr().RunEventLoop();
244235
ChipLogProgress(DeviceLayer, "End EventLoop");
245236

246-
EXPECT_TRUE(context.mResolvedServicesCount > 0);
247-
EXPECT_TRUE(!context.mTimeoutExpired);
237+
EXPECT_GT(mResolvedServicesCount, 0);
238+
EXPECT_FALSE(mTimeoutExpired);
248239

249240
// Stop browsing so we can safely shutdown DNS-SD
250-
chip::Dnssd::ChipDnssdStopBrowse(context.mBrowseIdentifier);
241+
chip::Dnssd::ChipDnssdStopBrowse(mBrowseIdentifier);
251242

252243
chip::Dnssd::ChipDnssdShutdown();
253244
}
@@ -259,7 +250,7 @@ static void HandlePublish(void * context, const char * type, const char * instan
259250

260251
static void TestDnssdPublishService_DnssdInitCallback(void * context, CHIP_ERROR error)
261252
{
262-
auto * ctx = static_cast<DnssdContext *>(context);
253+
auto * ctx = static_cast<TestDnssd *>(context);
263254
EXPECT_EQ(error, CHIP_NO_ERROR);
264255

265256
DnssdService service{};
@@ -277,7 +268,7 @@ static void TestDnssdPublishService_DnssdInitCallback(void * context, CHIP_ERROR
277268
service.mSubTypes = nullptr;
278269
service.mSubTypeSize = 0;
279270

280-
EXPECT_EQ(ChipDnssdPublishService(&service, HandlePublish, context), CHIP_NO_ERROR);
271+
EXPECT_EQ(ChipDnssdPublishService(&service, HandlePublish, nullptr), CHIP_NO_ERROR);
281272

282273
EXPECT_EQ(ChipDnssdBrowse("_mock", DnssdServiceProtocol::kDnssdProtocolTcp, chip::Inet::IPAddressType::kAny,
283274
chip::Inet::InterfaceId::Null(), HandleBrowse, context, &ctx->mBrowseIdentifier),
@@ -291,20 +282,18 @@ static void TestDnssdPublishService_DnssdInitCallback(void * context, CHIP_ERROR
291282
// here we only verify that the server implementation can publish services.
292283
TEST_F(TestDnssd, TestDnssdPublishService)
293284
{
294-
DnssdContext context;
295-
296-
EXPECT_EQ(chip::Dnssd::ChipDnssdInit(TestDnssdPublishService_DnssdInitCallback, DnssdErrorCallback, &context), CHIP_NO_ERROR);
297-
EXPECT_EQ(chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(5), Timeout, &context), CHIP_NO_ERROR);
285+
EXPECT_EQ(chip::Dnssd::ChipDnssdInit(TestDnssdPublishService_DnssdInitCallback, DnssdErrorCallback, this), CHIP_NO_ERROR);
286+
EXPECT_EQ(chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(5), Timeout, this), CHIP_NO_ERROR);
298287

299288
ChipLogProgress(DeviceLayer, "Start EventLoop");
300289
chip::DeviceLayer::PlatformMgr().RunEventLoop();
301290
ChipLogProgress(DeviceLayer, "End EventLoop");
302291

303-
EXPECT_TRUE(context.mResolvedServicesCount > 0);
304-
EXPECT_TRUE(!context.mTimeoutExpired);
292+
EXPECT_GT(mResolvedServicesCount, 0);
293+
EXPECT_FALSE(mTimeoutExpired);
305294

306295
// Stop browsing so we can safely shutdown DNS-SD
307-
chip::Dnssd::ChipDnssdStopBrowse(context.mBrowseIdentifier);
296+
chip::Dnssd::ChipDnssdStopBrowse(mBrowseIdentifier);
308297

309298
chip::Dnssd::ChipDnssdShutdown();
310299
}

src/platform/tests/TestKeyValueStoreMgr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct TestKeyValueStoreMgr : public ::testing::Test
3939
static void SetUpTestSuite()
4040
{
4141
CHIP_ERROR err = chip::Platform::MemoryInit();
42-
ASSERT_EQ(err, CHIP_NO_ERROR);
42+
EXPECT_EQ(err, CHIP_NO_ERROR);
4343
}
4444

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

src/platform/tests/TestPlatformMgr.cpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,15 @@ class TestPlatformMgr : public ::testing::Test
5353
static void SetUpTestSuite()
5454
{
5555
CHIP_ERROR error = chip::Platform::MemoryInit();
56-
if (error != CHIP_NO_ERROR)
57-
FAIL() << "Failed to initialize memory";
56+
EXPECT_EQ(error, CHIP_NO_ERROR);
5857

5958
// Setup a fake commissionable data provider since required by internals of several
6059
// Device/SystemLayer components.
6160
static chip::DeviceLayer::TestOnlyCommissionableDataProvider commissionable_data_provider;
6261
chip::DeviceLayer::SetCommissionableDataProvider(&commissionable_data_provider);
6362
}
6463

65-
static void TearDownTestSuite()
66-
{
67-
chip::Platform::MemoryShutdown();
68-
SUCCEED();
69-
}
64+
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
7065
};
7166

7267
TEST_F(TestPlatformMgr, InitShutdown)
@@ -199,8 +194,8 @@ TEST_F(TestPlatformMgr, RunEventLoopStopBeforeSleep)
199194
SleepSome(arg);
200195
});
201196

202-
EXPECT_TRUE(!stopRan);
203-
EXPECT_TRUE(!sleepRan);
197+
EXPECT_FALSE(stopRan);
198+
EXPECT_FALSE(sleepRan);
204199
PlatformMgr().RunEventLoop();
205200
EXPECT_TRUE(stopRan);
206201
EXPECT_TRUE(sleepRan);

src/platform/tests/TestPlatformTime.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <string.h>
3030

3131
#include <lib/support/CodeUtils.h>
32-
3332
#include <gtest/gtest.h>
3433
#include <lib/support/UnitTestUtils.h>
3534
#include <system/SystemClock.h>
@@ -76,7 +75,7 @@ TEST(TestDevice, GetMonotonicMicroseconds)
7675
// verify that timers don't fire early
7776
EXPECT_GT(Tdelta, (Tdelay - margin));
7877
// verify they're not too late
79-
// NL_TEST_ASSERT(inSuite, Tdelta < (Tdelay + margin));
78+
// EXPECT_LT(Tdelta, (Tdelay + margin));
8079
numOfTestsRan++;
8180
}
8281
EXPECT_GT(numOfTestsRan, 0);
@@ -110,12 +109,8 @@ TEST(TestDevice, GetMonotonicMilliseconds)
110109
// verify that timers don't fire early
111110
EXPECT_GT(Tdelta, (Tdelay - margin));
112111
// verify they're not too late
113-
// NL_TEST_ASSERT(inSuite, Tdelta < (Tdelay + margin));
112+
// EXPECT_LT(Tdelta, (Tdelay + margin));
114113
numOfTestsRan++;
115114
}
116115
EXPECT_GT(numOfTestsRan, 0);
117116
}
118-
119-
/**
120-
* Test Suite. It lists all the test functions.
121-
*/

src/platform/tests/TestThreadStackMgr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,5 @@ TEST(TestThreadStackManager, TestThreadStackManager)
7979
chip::DeviceLayer::PlatformMgrImpl().RunEventLoop();
8080
chip::Platform::MemoryShutdown();
8181

82-
ASSERT_TRUE(eventReceived);
82+
EXPECT_TRUE(eventReceived);
8383
}

0 commit comments

Comments
 (0)