Skip to content

Commit

Permalink
Enable TestInetEndPoint and TestInetLayerDNS
Browse files Browse the repository at this point in the history
Problem
Missing TestInetEndPoint and TestInetLayerDNS for Inet layer

Summary of Changes
Add TestInetEndPoint and TestInetLayerDNS

Fix project-chip#329
  • Loading branch information
yunhanw-google committed Sep 14, 2020
1 parent a5f59f9 commit ab605a0
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 45 deletions.
2 changes: 2 additions & 0 deletions src/inet/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ chip_test_suite("tests") {
tests = [
"TestInetAddress",
"TestInetErrorStr",
"TestInetEndPoint",
"TestInetLayerDNS"
]
}
38 changes: 20 additions & 18 deletions src/inet/tests/TestInetEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#define __STDC_LIMIT_MACROS
#endif

#include "TestInetLayer.h"

#include <errno.h>
#include <inttypes.h>
#include <stdint.h>
Expand Down Expand Up @@ -55,10 +57,10 @@ using namespace chip::System;

#define TOOL_NAME "TestInetEndPoint"

static ArgParser::HelpOptions gHelpOptions(TOOL_NAME, "Usage: " TOOL_NAME " [<options...>]\n",
ArgParser::HelpOptions gHelpOptions(TOOL_NAME, "Usage: " TOOL_NAME " [<options...>]\n",
CHIP_VERSION_STRING "\n" CHIP_TOOL_COPYRIGHT);

static ArgParser::OptionSet * gToolOptionSets[] = { &gNetworkOptions, &gFaultInjectionOptions, &gHelpOptions, NULL };
ArgParser::OptionSet * gToolOptionSets[] = { &gNetworkOptions, &gFaultInjectionOptions, &gHelpOptions, NULL };

bool callbackHandlerCalled = false;

Expand All @@ -82,7 +84,7 @@ void HandleTimer(Layer * aLayer, void * aAppState, Error aError)
}

// Test before init network, Inet is not initialized
static void TestInetPre(nlTestSuite * inSuite, void * inContext)
void TestInetPre(nlTestSuite * inSuite, void * inContext)
{
#if INET_CONFIG_ENABLE_RAW_ENDPOINT
RawEndPoint * testRawEP = NULL;
Expand Down Expand Up @@ -127,9 +129,9 @@ static void TestInetPre(nlTestSuite * inSuite, void * inContext)

#if INET_CONFIG_ENABLE_DNS_RESOLVER
// Test Inet ResolveHostAddress functionality
static void TestResolveHostAddress(nlTestSuite * inSuite, void * inContext)
void TestResolveHostAddress(nlTestSuite * inSuite, void * inContext)
{
char testHostName1[20] = "www.nest.com";
char testHostName1[20] = "www.google.com";
char testHostName2[20] = "127.0.0.1";
char testHostName3[20] = "";
char testHostName4[260];
Expand Down Expand Up @@ -188,10 +190,10 @@ static void TestResolveHostAddress(nlTestSuite * inSuite, void * inContext)
#endif // INET_CONFIG_ENABLE_DNS_RESOLVER

// Test Inet ParseHostPortAndInterface
static void TestParseHost(nlTestSuite * inSuite, void * inContext)
void TestParseHost(nlTestSuite * inSuite, void * inContext)
{
char correctHostName[7][30] = {
"10.0.0.1", "10.0.0.1:3000", "www.nest.com", "www.nest.com:3000", "[fd00:0:1:1::1]:3000", "[fd00:0:1:1::1]:300%wpan0",
"10.0.0.1", "10.0.0.1:3000", "www.google.com", "www.google.com:3000", "[fd00:0:1:1::1]:3000", "[fd00:0:1:1::1]:300%wpan0",
"%wpan0"
};
char invalidHostName[4][30] = { "[fd00::1]5", "[fd00:0:1:1::1:3000", "10.0.0.1:1234567", "10.0.0.1:er31" };
Expand All @@ -214,7 +216,7 @@ static void TestParseHost(nlTestSuite * inSuite, void * inContext)
}
}

static void TestInetError(nlTestSuite * inSuite, void * inContext)
void TestInetError(nlTestSuite * inSuite, void * inContext)
{
INET_ERROR err = INET_NO_ERROR;

Expand All @@ -223,7 +225,7 @@ static void TestInetError(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, IsErrorPOSIX(err));
}

static void TestInetInterface(nlTestSuite * inSuite, void * inContext)
void TestInetInterface(nlTestSuite * inSuite, void * inContext)
{
InterfaceIterator intIterator;
InterfaceAddressIterator addrIterator;
Expand Down Expand Up @@ -296,7 +298,7 @@ static void TestInetInterface(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, !addrIterator.HasBroadcastAddress());
}

static void TestInetEndPoint(nlTestSuite * inSuite, void * inContext)
void TestInetEndPointFunct(nlTestSuite * inSuite, void * inContext)
{
INET_ERROR err;
IPAddress addr_any = IPAddress::Any;
Expand Down Expand Up @@ -478,7 +480,7 @@ static void TestInetEndPoint(nlTestSuite * inSuite, void * inContext)
}

// Test the InetLayer resource limitation
static void TestInetEndPointLimit(nlTestSuite * inSuite, void * inContext)
void TestInetEndPointLimit(nlTestSuite * inSuite, void * inContext)
{
RawEndPoint * testRawEP = NULL;
UDPEndPoint * testUDPEP = NULL;
Expand Down Expand Up @@ -525,7 +527,7 @@ static const nlTest sTests[] = { NL_TEST_DEF("InetEndPoint::PreTest", TestInetPr
NL_TEST_DEF("InetEndPoint::TestParseHost", TestParseHost),
NL_TEST_DEF("InetEndPoint::TestInetError", TestInetError),
NL_TEST_DEF("InetEndPoint::TestInetInterface", TestInetInterface),
NL_TEST_DEF("InetEndPoint::TestInetEndPoint", TestInetEndPoint),
NL_TEST_DEF("InetEndPoint::TestInetEndPoint", TestInetEndPointFunct),
NL_TEST_DEF("InetEndPoint::TestEndPointLimit", TestInetEndPointLimit),
NL_TEST_SENTINEL() };

Expand All @@ -535,7 +537,7 @@ static const nlTest sTests[] = { NL_TEST_DEF("InetEndPoint::PreTest", TestInetPr
* This is a work-around to initiate PacketBuffer protected class instance's
* data and set it to a known state, before an instance is created.
*/
static int TestSetup(void * inContext)
int TestSetup(void * inContext)
{
return (SUCCESS);
}
Expand All @@ -544,13 +546,13 @@ static int TestSetup(void * inContext)
* Tear down the test suite.
* Free memory reserved at TestSetup.
*/
static int TestTeardown(void * inContext)
int TestTeardown(void * inContext)
{
return (SUCCESS);
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

int TestInetEndPoint(void)
int TestInetEndPointFunct(void)
{
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
// clang-format off
Expand All @@ -574,10 +576,10 @@ int TestInetEndPoint(void)

static void __attribute__((constructor)) TestCHIPInetEndpointCtor(void)
{
VerifyOrDie(RegisterUnitTests(&TestInetEndPoint) == CHIP_NO_ERROR);
VerifyOrDie(RegisterUnitTests(&TestInetEndPointFunct) == CHIP_NO_ERROR);
}

int main(int argc, char * argv[])
int TestInetEndPoint(int argc, char * argv[])
{
SetSIGUSR1Handler();

Expand All @@ -589,5 +591,5 @@ int main(int argc, char * argv[])
// Generate machine-readable, comma-separated value (CSV) output.
nlTestSetOutputStyle(OUTPUT_CSV);

return (TestInetEndPoint());
return (TestInetEndPointFunct());
}
36 changes: 36 additions & 0 deletions src/inet/tests/TestInetEndPointDriver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* This file implements a standalone/native program executable
* test driver for the CHIP Internet (inet) library address unit
* tests.
*
*/

#include "TestInetLayer.h"

#include <nlunit-test.h>

int main(int argc, char * argv[])
{
// Generate machine-readable, comma-separated value (CSV) output.
nlTestSetOutputStyle(OUTPUT_CSV);

return (TestInetEndPoint(argc, argv));
}
3 changes: 2 additions & 1 deletion src/inet/tests/TestInetLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ int TestInetAddress(void);
int TestInetBuffer(void);
int TestInetErrorStr(void);
int TestInetTimer(void);

int TestInetEndPoint(int argc, char * argv[]);
int TestInetLayerDNS(int argc, char * argv[]);
#ifdef __cplusplus
}
#endif
Expand Down
53 changes: 27 additions & 26 deletions src/inet/tests/TestInetLayerDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#define __STDC_LIMIT_MACROS
#endif

#include "TestInetLayer.h"

#include <inttypes.h>
#include <stdint.h>
#include <string.h>
Expand Down Expand Up @@ -76,18 +78,18 @@ struct DNSResolutionTestContext
IPAddress resultsBuf[kMaxResults];
};

static void RunTestCase(nlTestSuite * testSuite, const DNSResolutionTestCase & testCase);
static void StartTestCase(DNSResolutionTestContext & testContext);
static void HandleResolutionComplete(void * appState, INET_ERROR err, uint8_t addrCount, IPAddress * addrArray);
static void ServiceNetworkUntilDone(uint32_t timeoutMS);
static void HandleSIGUSR1(int sig);
void RunTestCase(nlTestSuite * testSuite, const DNSResolutionTestCase & testCase);
void StartTestCase(DNSResolutionTestContext & testContext);
void HandleResolutionComplete(void * appState, INET_ERROR err, uint8_t addrCount, IPAddress * addrArray);
void ServiceNetworkUntilDone(uint32_t timeoutMS);
void HandleSIGUSR1(int sig);

// clang-format off
static ArgParser::HelpOptions gHelpOptions(TOOL_NAME,
ArgParser::HelpOptions gHelpOptions(TOOL_NAME,
"Usage: " TOOL_NAME " [<options...>]\n",
CHIP_VERSION_STRING "\n" CHIP_TOOL_COPYRIGHT);

static ArgParser::OptionSet * gToolOptionSets[] =
ArgParser::OptionSet * gToolOptionSets[] =
{
&gNetworkOptions,
&gFaultInjectionOptions,
Expand All @@ -99,7 +101,7 @@ static ArgParser::OptionSet * gToolOptionSets[] =
/**
* Test basic name resolution functionality.
*/
static void TestDNSResolution_Basic(nlTestSuite * testSuite, void * testContext)
void TestDNSResolution_Basic(nlTestSuite * testSuite, void * testContext)
{
// clang-format off

Expand Down Expand Up @@ -147,7 +149,7 @@ static void TestDNSResolution_Basic(nlTestSuite * testSuite, void * testContext)
/**
* Test resolving a name using various address type options.
*/
static void TestDNSResolution_AddressTypeOption(nlTestSuite * testSuite, void * testContext)
void TestDNSResolution_AddressTypeOption(nlTestSuite * testSuite, void * testContext)
{
// clang-format off

Expand Down Expand Up @@ -212,7 +214,7 @@ static void TestDNSResolution_AddressTypeOption(nlTestSuite * testSuite, void *
/**
* Test resolving a name with a limited number of results.
*/
static void TestDNSResolution_RestrictedResults(nlTestSuite * testSuite, void * testContext)
void TestDNSResolution_RestrictedResults(nlTestSuite * testSuite, void * testContext)
{
// clang-format off

Expand Down Expand Up @@ -279,7 +281,7 @@ static void TestDNSResolution_RestrictedResults(nlTestSuite * testSuite, void *
/**
* Test resolving a non-existant name.
*/
static void TestDNSResolution_NoRecord(nlTestSuite * testSuite, void * testContext)
void TestDNSResolution_NoRecord(nlTestSuite * testSuite, void * testContext)
{
// clang-format off
RunTestCase(testSuite,
Expand All @@ -299,7 +301,7 @@ static void TestDNSResolution_NoRecord(nlTestSuite * testSuite, void * testConte
/**
* Test resolving a name where the resultant DNS entry lacks an A or AAAA record.
*/
static void TestDNSResolution_NoHostRecord(nlTestSuite * testSuite, void * testContext)
void TestDNSResolution_NoHostRecord(nlTestSuite * testSuite, void * testContext)
{
// clang-format off

Expand Down Expand Up @@ -349,7 +351,7 @@ static void TestDNSResolution_NoHostRecord(nlTestSuite * testSuite, void * testC
/**
* Test resolving text form IP addresses.
*/
static void TestDNSResolution_TextForm(nlTestSuite * testSuite, void * testContext)
void TestDNSResolution_TextForm(nlTestSuite * testSuite, void * testContext)
{
// clang-format off
RunTestCase(testSuite,
Expand Down Expand Up @@ -405,7 +407,7 @@ static void TestDNSResolution_TextForm(nlTestSuite * testSuite, void * testConte
// clang-format on
}

static void TestDNSResolution_Cancel(nlTestSuite * testSuite, void * inContext)
void TestDNSResolution_Cancel(nlTestSuite * testSuite, void * inContext)
{
DNSResolutionTestContext testContext{
testSuite, DNSResolutionTestCase{ "www.google.com", kDNSOption_Default, kMaxResults, INET_NO_ERROR, true, false }
Expand Down Expand Up @@ -433,7 +435,7 @@ static void TestDNSResolution_Cancel(nlTestSuite * testSuite, void * inContext)
sNumResInProgress = 0;
}

static void TestDNSResolution_Simultaneous(nlTestSuite * testSuite, void * inContext)
void TestDNSResolution_Simultaneous(nlTestSuite * testSuite, void * inContext)
{
// clang-format off
DNSResolutionTestContext tests[] =
Expand Down Expand Up @@ -505,7 +507,7 @@ static void TestDNSResolution_Simultaneous(nlTestSuite * testSuite, void * inCon
NL_TEST_ASSERT(testSuite, sNumResInProgress == 0);
}

static void RunTestCase(nlTestSuite * testSuite, const DNSResolutionTestCase & testCase)
void RunTestCase(nlTestSuite * testSuite, const DNSResolutionTestCase & testCase)
{
DNSResolutionTestContext testContext{ testSuite, testCase };

Expand All @@ -522,7 +524,7 @@ static void RunTestCase(nlTestSuite * testSuite, const DNSResolutionTestCase & t
NL_TEST_ASSERT(testSuite, sNumResInProgress == 0);
}

static void StartTestCase(DNSResolutionTestContext & testContext)
void StartTestCase(DNSResolutionTestContext & testContext)
{
INET_ERROR err = INET_NO_ERROR;
DNSResolutionTestCase & testCase = testContext.testCase;
Expand Down Expand Up @@ -553,7 +555,7 @@ static void StartTestCase(DNSResolutionTestContext & testContext)
}
}

static void HandleResolutionComplete(void * appState, INET_ERROR err, uint8_t addrCount, IPAddress * addrArray)
void HandleResolutionComplete(void * appState, INET_ERROR err, uint8_t addrCount, IPAddress * addrArray)
{
DNSResolutionTestContext & testContext = *static_cast<DNSResolutionTestContext *>(appState);
DNSResolutionTestCase & testCase = testContext.testCase;
Expand Down Expand Up @@ -653,7 +655,7 @@ static void HandleResolutionComplete(void * appState, INET_ERROR err, uint8_t ad
}
}

static void ServiceNetworkUntilDone(uint32_t timeoutMS)
void ServiceNetworkUntilDone(uint32_t timeoutMS)
{
uint64_t timeoutTimeMS = System::Layer::GetClock_MonotonicMS() + timeoutMS;
struct timeval sleepTime;
Expand All @@ -671,14 +673,14 @@ static void ServiceNetworkUntilDone(uint32_t timeoutMS)
}
}

static void HandleSIGUSR1(int sig)
void HandleSIGUSR1(int sig)
{
gInet.Shutdown();

exit(0);
}

int TestInetLayerDNS(void)
int TestInetLayerDNSFunct(void)
{
// clang-format off
const nlTest DNSTests[] =
Expand Down Expand Up @@ -719,12 +721,11 @@ int TestInetLayerDNS(void)

static void __attribute__((constructor)) TestCHIPInetLayerDNSCtor(void)
{
VerifyOrDie(RegisterUnitTests(&TestInetLayerDNS) == CHIP_NO_ERROR);
VerifyOrDie(RegisterUnitTests(&TestInetLayerDNSFunct) == CHIP_NO_ERROR);
}

#else // !INET_CONFIG_ENABLE_DNS_RESOLVER

int TestInetLayerDNS(void)
int TestInetLayerDNSFunct(void)
{
fprintf(stderr, "Please assert INET_CONFIG_ENABLE_DNS_RESOLVER to use this test.\n");

Expand All @@ -733,7 +734,7 @@ int TestInetLayerDNS(void)

#endif // !INET_CONFIG_ENABLE_DNS_RESOLVER

int main(int argc, char * argv[])
int TestInetLayerDNS(int argc, char * argv[])
{
SetupFaultInjectionContext(argc, argv);

Expand All @@ -747,5 +748,5 @@ int main(int argc, char * argv[])

nlTestSetOutputStyle(OUTPUT_CSV);

return (TestInetLayerDNS());
return (TestInetLayerDNSFunct());
}
Loading

0 comments on commit ab605a0

Please sign in to comment.