Skip to content

Commit 1359568

Browse files
ATmobicapull[bot]
authored andcommitted
Set of fixes to unit-tests code (#23071)
* [Fix] Execute TestLogBufferAsHex only if CHIP_PROGRESS_LOGGING TestLogBufferAsHex test validates the LogBufferAsHex function which uses ChipLogProgress(). If progress logging is disable this test failed. Execute it only if CHIP_PROGRESS_LOGGING is defined. Signed-off-by: ATmobica <[email protected]> * [Fix] Fix PeerAddress string conversion test Allow uppercase IPV6 string conversion. Check both cases. Signed-off-by: ATmobica <[email protected]> * [Fix] Fix ArgParser in support library Fix parse args for embedded non posix long_opt implementation. Disable a parse args unit test for non posix long_opt. Fix duplicate id in option sets in ParseArgs unit test. Signed-off-by: ATmobica <[email protected]> * [Fix] Fix warnings of incorrect types in printf formatting src/app/util/mock/attribute-storage.cpp src/controller/tests/data_model/TestRead.cpp src/lib/support/jsontlv/TlvJson.cpp Signed-off-by: ATmobica <[email protected]> * [Fix] Add missing assert header include in TestInetCommonPosix Signed-off-by: ATmobica <[email protected]> * [Fix] Fix TestReadInteraction implementation Remove leftover debug printf. Drain and service IO repeatedly until condition met. Signed-off-by: ATmobica <[email protected]> * [Fix] Fix controller unit tests Change TestReadChunkingTests to TestEventChunkingTests in TestEventChunkingTests.cpp Signed-off-by: ATmobica <[email protected]> Signed-off-by: ATmobica <[email protected]>
1 parent f93f8a7 commit 1359568

File tree

9 files changed

+93
-18
lines changed

9 files changed

+93
-18
lines changed

src/app/tests/TestReadInteraction.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -1565,8 +1565,6 @@ void TestReadInteraction::TestSubscribeRoundtrip(nlTestSuite * apSuite, void * a
15651565
delegate.mGotEventResponse = false;
15661566
delegate.mNumAttributeResponse = 0;
15671567

1568-
printf("HereHere\n");
1569-
15701568
err = engine->GetReportingEngine().SetDirty(dirtyPath1);
15711569
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
15721570
err = engine->GetReportingEngine().SetDirty(dirtyPath2);
@@ -1918,13 +1916,16 @@ void TestReadInteraction::TestSubscribeWildcard(nlTestSuite * apSuite, void * ap
19181916
err = engine->GetReportingEngine().SetDirty(dirtyPath);
19191917
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
19201918

1921-
ctx.DrainAndServiceIO();
1922-
19231919
//
1924-
// Not sure why I had to add this, and didn't have cycles to figure out why.
1925-
// Tracked in Issue #17528.
1920+
// We need to DrainAndServiceIO() until attribute callback will be called.
1921+
// This is not correct behavior and is tracked in Issue #17528.
19261922
//
1927-
ctx.DrainAndServiceIO();
1923+
int last;
1924+
do
1925+
{
1926+
last = delegate.mNumAttributeResponse;
1927+
ctx.DrainAndServiceIO();
1928+
} while (last != delegate.mNumAttributeResponse);
19281929

19291930
NL_TEST_ASSERT(apSuite, delegate.mGotReport);
19301931
// Mock endpoint3 has 13 attributes in total, and we subscribed twice.

src/controller/tests/TestEventChunking.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,10 @@ nlTestSuite sSuite =
542542

543543
} // namespace
544544

545-
int TestReadChunkingTests()
545+
int TestEventChunkingTests()
546546
{
547547
gSuite = &sSuite;
548548
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
549549
}
550550

551-
CHIP_REGISTER_TEST_SUITE(TestReadChunkingTests)
551+
CHIP_REGISTER_TEST_SUITE(TestEventChunkingTests)

src/controller/tests/data_model/TestRead.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2643,8 +2643,8 @@ void TestReadInteraction::TestReadHandler_MultipleSubscriptionsWithDataVersionFi
26432643
numSuccessCalls == (app::InteractionModelEngine::kReadHandlerPoolSize + 1);
26442644
});
26452645

2646-
ChipLogError(Zcl, "Success call cnt: %u (expect %u) subscription cnt: %u (expect %u)", numSuccessCalls,
2647-
uint32_t(app::InteractionModelEngine::kReadHandlerPoolSize + 1), numSubscriptionEstablishedCalls,
2646+
ChipLogError(Zcl, "Success call cnt: %" PRIu32 " (expect %" PRIu32 ") subscription cnt: %" PRIu32 " (expect %" PRIu32 ")",
2647+
numSuccessCalls, uint32_t(app::InteractionModelEngine::kReadHandlerPoolSize + 1), numSubscriptionEstablishedCalls,
26482648
uint32_t(app::InteractionModelEngine::kReadHandlerPoolSize + 1));
26492649

26502650
NL_TEST_ASSERT(apSuite, numSuccessCalls == (app::InteractionModelEngine::kReadHandlerPoolSize + 1));

src/inet/tests/TestInetCommonPosix.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "TestInetCommon.h"
3838
#include "TestInetCommonOptions.h"
3939

40+
#include <assert.h>
4041
#include <errno.h>
4142
#include <vector>
4243

src/lib/support/CHIPArgParser.cpp

+64-1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
292292
OptionSet * curOptSet;
293293
OptionDef * curOpt;
294294
bool handlerRes;
295+
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
296+
int lastOptIndex = 0;
297+
int subOptIndex = 0;
298+
int currentOptIndex = 0;
299+
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
295300

296301
// The getopt() functions do not support recursion, so exit immediately with an
297302
// error if called recursively.
@@ -345,7 +350,36 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
345350
// Attempt to match the current option argument (argv[optind]) against the defined long and short options.
346351
optarg = nullptr;
347352
optopt = 0;
348-
id = getopt_long(argc, argv, shortOpts, longOpts, &optIndex);
353+
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
354+
// to check if index has changed
355+
lastOptIndex = currentOptIndex;
356+
// optind will not increment on error, this is why we need to keep track of the current option
357+
// this is for use when getopt_long fails to find the option and we need to print the error
358+
currentOptIndex = optind;
359+
// if it's the first run, optind is not set and we need to find the first option ourselves
360+
if (!currentOptIndex)
361+
{
362+
while (currentOptIndex < argc)
363+
{
364+
currentOptIndex++;
365+
if (*argv[currentOptIndex] == '-')
366+
{
367+
break;
368+
}
369+
}
370+
}
371+
// similarly we need to keep track of short opts index for groups like "-fba"
372+
// if the index has not changed that means we are still analysing the same group
373+
if (lastOptIndex != currentOptIndex)
374+
{
375+
subOptIndex = 0;
376+
}
377+
else
378+
{
379+
subOptIndex++;
380+
}
381+
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
382+
id = getopt_long(argc, argv, shortOpts, longOpts, &optIndex);
349383

350384
// Stop if there are no more options.
351385
if (id == -1)
@@ -356,10 +390,35 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
356390
{
357391
if (ignoreUnknown)
358392
continue;
393+
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
394+
// getopt_long doesn't tell us if the option which failed to match is long or short so check
395+
bool isLongOption = false;
396+
if (strlen(argv[currentOptIndex]) > 2 && argv[currentOptIndex][1] == '-')
397+
{
398+
isLongOption = true;
399+
}
400+
if (optopt == 0 || isLongOption)
401+
{
402+
// getopt_long function incorrectly treats unknown long option as short opt group
403+
if (subOptIndex == 0)
404+
{
405+
PrintArgError("%s: Unknown option: %s\n", progName, argv[currentOptIndex]);
406+
}
407+
}
408+
else if (optopt == '?')
409+
{
410+
PrintArgError("%s: Unknown option: -%c\n", progName, argv[currentOptIndex][subOptIndex + 1]);
411+
}
412+
else
413+
{
414+
PrintArgError("%s: Unknown option: -%c\n", progName, optopt);
415+
}
416+
#else
359417
if (optopt != 0)
360418
PrintArgError("%s: Unknown option: -%c\n", progName, optopt);
361419
else
362420
PrintArgError("%s: Unknown option: %s\n", progName, argv[optind - 1]);
421+
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
363422
goto done;
364423
}
365424

@@ -369,7 +428,11 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
369428
{
370429
// NOTE: with the way getopt_long() works, it is impossible to tell whether the option that
371430
// was missing an argument was a long option or a short option.
431+
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
432+
PrintArgError("%s: Missing argument for %s option\n", progName, argv[currentOptIndex]);
433+
#else
372434
PrintArgError("%s: Missing argument for %s option\n", progName, argv[optind - 1]);
435+
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
373436
goto done;
374437
}
375438

src/lib/support/jsontlv/TlvJson.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void InsertKeyValue(Json::Value & json, const KeyContext & keyContext, T val)
9595
}
9696
else if (keyContext.keyType == KeyContext::kStructField)
9797
{
98-
snprintf(keyBuf, sizeof(keyBuf), "%" PRIu32, keyContext.key);
98+
snprintf(keyBuf, sizeof(keyBuf), "%u", keyContext.key);
9999
json[keyBuf] = val;
100100
}
101101
else

src/lib/support/tests/TestBytesToHex.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,10 @@ const nlTest sTests[] = {
433433
NL_TEST_DEF("TestBytesToHexErrors", TestBytesToHexErrors), //
434434
NL_TEST_DEF("TestBytesToHexUint64", TestBytesToHexUint64), //
435435
NL_TEST_DEF("TestHexToBytesAndUint", TestHexToBytesAndUint), //
436-
NL_TEST_DEF("TestLogBufferAsHex", TestLogBufferAsHex), //
437-
NL_TEST_SENTINEL() //
436+
#ifdef CHIP_PROGRESS_LOGGING
437+
NL_TEST_DEF("TestLogBufferAsHex", TestLogBufferAsHex), //
438+
#endif
439+
NL_TEST_SENTINEL() //
438440
};
439441

440442
} // namespace

src/lib/support/tests/TestCHIPArgParser.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static size_t sCallbackRecordCount = 0;
137137
static OptionDef sOptionSetA_Defs[] =
138138
{
139139
{ "foo", kNoArgument, '1' },
140-
{ "bar", kNoArgument, 1001 },
140+
{ "bar", kNoArgument, 1002 },
141141
{ "baz", kArgumentRequired, 'Z' },
142142
{ }
143143
};
@@ -350,7 +350,7 @@ static void SimpleParseTest_VariousShortAndLongWithArgs()
350350
VerifyHandleOptionCallback(0, __FUNCTION__, &sOptionSetA, '1', "--foo", nullptr);
351351
VerifyHandleOptionCallback(1, __FUNCTION__, &sOptionSetB, 1000, "--run", "run-value");
352352
VerifyHandleOptionCallback(2, __FUNCTION__, &sOptionSetB, 's', "-s", nullptr);
353-
VerifyHandleOptionCallback(3, __FUNCTION__, &sOptionSetA, 1001, "--bar", nullptr);
353+
VerifyHandleOptionCallback(3, __FUNCTION__, &sOptionSetA, 1002, "--bar", nullptr);
354354
VerifyHandleOptionCallback(4, __FUNCTION__, &sOptionSetA, '1', "-1", nullptr);
355355
VerifyHandleOptionCallback(5, __FUNCTION__, &sOptionSetA, 'Z', "-Z", "baz-value");
356356
VerifyHandleOptionCallback(6, __FUNCTION__, &sOptionSetB, 1000, "--run", "run-value-2");
@@ -779,7 +779,12 @@ int TestCHIPArgParser(void)
779779
UnknownOptionTest_UnknownShortOptionBeforeKnown();
780780
UnknownOptionTest_UnknownLongOptionAfterArgs();
781781
UnknownOptionTest_IgnoreUnknownShortOption();
782+
783+
/* Skip this test because the parser successfully captures all the options
784+
but the error reporting is incorrect in this case due to long_opt limitations */
785+
#ifndef CHIP_CONFIG_NON_POSIX_LONG_OPT
782786
UnknownOptionTest_IgnoreUnknownLongOption();
787+
#endif // !CHIP_CONFIG_NON_POSIX_LONG_OPT
783788

784789
MissingValueTest_MissingShortOptionValue();
785790
MissingValueTest_MissingLongOptionValue();

src/transport/raw/tests/TestPeerAddress.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ void TestToString(nlTestSuite * inSuite, void * inContext)
9393
{
9494
IPAddress::FromString("1223::3456:789a", ip);
9595
PeerAddress::UDP(ip, 8080).ToString(buff);
96+
// IPV6 does not specify case
97+
int res1 = strcmp(buff, "UDP:[1223::3456:789a]:8080");
98+
int res2 = strcmp(buff, "UDP:[1223::3456:789A]:8080");
9699

97-
NL_TEST_ASSERT(inSuite, !strcmp(buff, "UDP:[1223::3456:789a]:8080"));
100+
NL_TEST_ASSERT(inSuite, (!res1 || !res2));
98101
}
99102

100103
{

0 commit comments

Comments
 (0)