diff --git a/.gitignore b/.gitignore index 537cf8ba..ed9f0ed7 100644 --- a/.gitignore +++ b/.gitignore @@ -48,6 +48,9 @@ Makefile.in /build cmake-build-* +# Test +/tmp + #tools .vscode .idea diff --git a/src/app/border_agent_functions_mock.cpp b/src/app/border_agent_functions_mock.cpp index 30c217c7..8aaa9a13 100644 --- a/src/app/border_agent_functions_mock.cpp +++ b/src/app/border_agent_functions_mock.cpp @@ -43,9 +43,9 @@ void ClearBorderAgentFunctionsMock() gBorderAgentFunctionsMock = nullptr; } -Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout) +Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout, const std::string &aNetIf) { - return gBorderAgentFunctionsMock->DiscoverBorderAgent(aBorderAgentHandler, aTimeout); + return gBorderAgentFunctionsMock->DiscoverBorderAgent(aBorderAgentHandler, aTimeout, aNetIf); } } // namespace commissioner diff --git a/src/app/border_agent_functions_mock.hpp b/src/app/border_agent_functions_mock.hpp index 6d488012..e775af79 100644 --- a/src/app/border_agent_functions_mock.hpp +++ b/src/app/border_agent_functions_mock.hpp @@ -45,7 +45,7 @@ class BorderAgentFunctionsMock public: virtual ~BorderAgentFunctionsMock() = default; - MOCK_METHOD(Error, DiscoverBorderAgent, (BorderAgentHandler, size_t)); + MOCK_METHOD(Error, DiscoverBorderAgent, (BorderAgentHandler, size_t, const std::string &)); }; void SetBorderAgentFunctionsMock(ot::commissioner::BorderAgentFunctionsMock *ptr); diff --git a/src/app/br_discover.cpp b/src/app/br_discover.cpp index f0928756..0d6128d7 100644 --- a/src/app/br_discover.cpp +++ b/src/app/br_discover.cpp @@ -29,6 +29,11 @@ #include "br_discover.hpp" #include +#ifdef __linux__ +#include +#else // __NetBSD__ || __FreeBSD__ || __APPLE__ +#include +#endif #include #include "common/error_macros.hpp" @@ -38,7 +43,7 @@ namespace ot { namespace commissioner { -Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout) +Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout, const std::string &aNetIf) { static constexpr size_t kDefaultBufferSize = 1024 * 16; static constexpr mdns_record_type_t kMdnsQueryType = MDNS_RECORDTYPE_PTR; @@ -46,12 +51,24 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou Error error; uint8_t buf[kDefaultBufferSize]; + int rval = 0; auto begin = std::chrono::system_clock::now(); int socket = mdns_socket_open_ipv4(); VerifyOrExit(socket >= 0, error = ERROR_IO_ERROR("failed to open mDNS IPv4 socket")); + if (!aNetIf.empty()) + { +#ifdef __linux__ + rval = setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, aNetIf.c_str(), aNetIf.size()); +#else // __NetBSD__ || __FreeBSD__ || __APPLE__ + rval = setsockopt(socket, IPPROTO_IPV6, IP_BOUND_IF, aNetIf.c_str(), aNetIf.size()); +#endif // __linux__ + VerifyOrExit(rval == 0, + error = ERROR_INVALID_ARGS("failed to bind network interface {}: {}", aNetIf, strerror(errno))); + } + if (mdns_query_send(socket, kMdnsQueryType, kServiceName, strlen(kServiceName), buf, sizeof(buf)) != 0) { ExitNow(error = ERROR_IO_ERROR("failed to send mDNS query")); diff --git a/src/app/br_discover.hpp b/src/app/br_discover.hpp index 91273b95..7304d0ee 100644 --- a/src/app/br_discover.hpp +++ b/src/app/br_discover.hpp @@ -56,9 +56,11 @@ using BorderAgentHandler = std::function help borderagent usage: -borderagent discover [] +borderagent discover [] [] borderagent get locator [done] > diff --git a/src/app/cli/interpreter.cpp b/src/app/cli/interpreter.cpp index dce7ef82..e0549f81 100644 --- a/src/app/cli/interpreter.cpp +++ b/src/app/cli/interpreter.cpp @@ -45,7 +45,11 @@ #include #include +#ifdef __linux__ #include +#else // __NetBSD__ || __FreeBSD__ || __APPLE__ +#include +#endif #include #include @@ -112,8 +116,6 @@ #define WARN_NETWORK_SELECTION_DROPPED "Network selection was dropped by the command" #define WARN_NETWORK_SELECTION_CHANGED "Network selection was changed by the command" -#define SO_BINDTODEVICE 25 - #define COLOR_ALIAS_FAILED Console::Color::kYellow namespace ot { @@ -1420,6 +1422,7 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr) std::vector borderAgents; nlohmann::json baJson; char mdnsSendBuffer[kMdnsBufferSize]; + int rval = 0; auto it = std::find(mContext.mCommandKeys.begin(), mContext.mCommandKeys.end(), "--timeout"); if (it != mContext.mCommandKeys.end()) @@ -1451,9 +1454,15 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr) mdnsSocket = mdns_socket_open_ipv4(); VerifyOrExit(mdnsSocket >= 0, value = ERROR_IO_ERROR("failed to open mDNS IPv4 socket")); - if (!netIf.empty() && setsockopt(mdnsSocket, SOL_SOCKET, SO_BINDTODEVICE, netIf.c_str(), netIf.size()) < 0) + if (!netIf.empty()) { - ExitNow(value = ERROR_INVALID_ARGS("failed to bind network interface {}: {}", netIf, strerror(errno))); +#ifdef __linux__ + rval = setsockopt(mdnsSocket, SOL_SOCKET, SO_BINDTODEVICE, netIf.c_str(), netIf.size()); +#else // __NetBSD__ || __FreeBSD__ || __APPLE__ + rval = setsockopt(mdnsSocket, IPPROTO_IPV6, IP_BOUND_IF, netIf.c_str(), netIf.size()); +#endif // __linux__ + VerifyOrExit(rval == 0, + value = ERROR_INVALID_ARGS("failed to bind network interface {}: {}", netIf, strerror(errno))); } fdgMdnsSocket.mFD = mdnsSocket; @@ -1796,14 +1805,20 @@ Interpreter::Value Interpreter::ProcessBorderAgent(const Expression &aExpr) if (CaseInsensitiveEqual(aExpr[1], "discover")) { - uint64_t timeout = 4000; + uint64_t timeout = 4000; + std::string netIf = ""; if (aExpr.size() >= 3) { SuccessOrExit(value = ParseInteger(timeout, aExpr[2])); } - SuccessOrExit(value = DiscoverBorderAgent(BorderAgentHandler, static_cast(timeout))); + if (aExpr.size() == 4) + { + netIf = aExpr[3]; + } + + SuccessOrExit(value = DiscoverBorderAgent(BorderAgentHandler, static_cast(timeout), netIf)); } else if (CaseInsensitiveEqual(aExpr[1], "get")) { diff --git a/src/app/cli/interpreter_test.cpp b/src/app/cli/interpreter_test.cpp index aef3342b..39602721 100644 --- a/src/app/cli/interpreter_test.cpp +++ b/src/app/cli/interpreter_test.cpp @@ -99,8 +99,8 @@ class InterpreterTestSuite : public testing::Test void InitContext(TestContext &ctx) { // Minimum test setup: create config file - const std::string configFile = "./config"; - auto error = WriteFile("{\"ThreadSMRoot\": \"./\"}", configFile); + const std::string configFile = "./tmp/config"; + auto error = WriteFile("{\"ThreadSMRoot\": \"./tmp\"}", configFile); ASSERT_EQ(error.GetCode(), ErrorCode::kNone); ASSERT_NE(ctx.mDefaultCommissionerObject, nullptr); @@ -110,7 +110,7 @@ class InterpreterTestSuite : public testing::Test DoAll(WithArg<0>([&](std::shared_ptr &a) { a = ctx.mDefaultCommissionerObject; }), Return(Error{}))); - auto result = ctx.mInterpreter.Init("./config", ""); + auto result = ctx.mInterpreter.Init("./tmp/config", ""); ASSERT_EQ(result.GetCode(), ErrorCode::kNone); ctx.mRegistry = ctx.mInterpreter.mRegistry.get(); @@ -118,6 +118,12 @@ class InterpreterTestSuite : public testing::Test // Add formal default PSKc ctx.mInterpreter.mJobManager->mDefaultConf.mPSKc = {'1', '0'}; } + + void SetUp() override + { + ASSERT_TRUE(system("rm -rf tmp") == 0); + ASSERT_TRUE(system("mkdir -p tmp") == 0); + } }; TEST_F(InterpreterTestSuite, TestInit) @@ -662,10 +668,10 @@ TEST_F(InterpreterTestSuite, IESV_SingleImportFileMustPass) }\n\ }"; - EXPECT_EQ(WriteFile(jsonStr, "./json.json").GetCode(), ErrorCode::kNone); + EXPECT_EQ(WriteFile(jsonStr, "./tmp/json.json").GetCode(), ErrorCode::kNone); EXPECT_CALL(*commissionerAppMock, SetActiveDataset(_)).WillOnce(Return(Error{})); - expr = ctx.mInterpreter.ParseExpression("opdataset set active --import ./json.json"); + expr = ctx.mInterpreter.ParseExpression("opdataset set active --import ./tmp/json.json"); auto value = ctx.mInterpreter.Eval(expr); EXPECT_TRUE(value.HasNoError()); } @@ -1115,10 +1121,10 @@ TEST_F(InterpreterTestSuite, PC_Token) value = ctx.mInterpreter.Eval(expr); EXPECT_TRUE(value.HasNoError()); - EXPECT_EQ(WriteFile("123aef", "./token").GetCode(), ErrorCode::kNone); + EXPECT_EQ(WriteFile("123aef", "./tmp/token").GetCode(), ErrorCode::kNone); EXPECT_CALL(*ctx.mDefaultCommissionerObject, SetToken(_)).WillOnce(Return(Error{})); EXPECT_CALL(*ctx.mDefaultCommissionerObject, GetToken()).WillOnce(ReturnRef(token)); - expr = ctx.mInterpreter.ParseExpression("token set ./token"); + expr = ctx.mInterpreter.ParseExpression("token set ./tmp/token"); value = ctx.mInterpreter.Eval(expr); EXPECT_TRUE(value.HasNoError()); } @@ -1129,10 +1135,11 @@ TEST_F(InterpreterTestSuite, PC_TokenWithCCM) InitContext(ctx); // prepare CCM network record - const std::string kDomainName = "domain1"; - const std::string kNetworkName = "net1"; - const std::string kSmPath = "./dom/" + kDomainName + "/"; - uint64_t xpanCcm = 1; + const std::string kDomainName = "domain1"; + const std::string kNetworkName = "net1"; + const std::string kSmPath = "./tmp/dom/" + kDomainName + "/"; + uint64_t xpanCcm = 1; + BorderAgent::State baStateCcm{BorderAgent::State::ConnectionMode::kX509Connection, 0, 0, 0, 0}; ASSERT_EQ(ctx.mRegistry->Add(BorderAgent{"127.0.0.1", 20001, ByteArray{}, "1.1", baStateCcm, kNetworkName, xpanCcm, @@ -1165,15 +1172,15 @@ TEST_F(InterpreterTestSuite, PC_TokenWithCCM) EXPECT_EQ(ctx.mInterpreter.mRegistry->SetCurrentNetwork(xpanCcm), RegistryStatus::kSuccess); expr = ctx.mInterpreter.ParseExpression("token request 127.0.0.1 2001"); value = ctx.mInterpreter.Eval(expr); - EXPECT_TRUE(value.HasNoError()); + EXPECT_TRUE(value.HasNoError()) << "value is " << value.ToString(); const ByteArray token = {'1', '2', '3', 'a', 'e', 'f'}; EXPECT_CALL(*pcaMock, SetToken(_)).WillOnce(Return(Error{})); // note: again, we do not expect GetToken() here, same reason - EXPECT_EQ(WriteFile("123aef", "./token").GetCode(), ErrorCode::kNone); - expr = ctx.mInterpreter.ParseExpression("token set ./token"); + EXPECT_EQ(WriteFile("123aef", "./tmp/token").GetCode(), ErrorCode::kNone); + expr = ctx.mInterpreter.ParseExpression("token set ./tmp/token"); value = ctx.mInterpreter.Eval(expr); EXPECT_TRUE(value.HasNoError()); } @@ -1184,10 +1191,11 @@ TEST_F(InterpreterTestSuite, PC_TokenWithNonCCM) InitContext(ctx); // prepare non-CCM network record - const std::string kDomainName = "DefaultDomain"; - const std::string kNetworkName = "net2"; - const std::string kSmPath = "./nwk/" + kNetworkName + "/"; - uint64_t xpanNonCcm = 2; + const std::string kDomainName = "DefaultDomain"; + const std::string kNetworkName = "net2"; + const std::string kSmPath = "./tmp/nwk/" + kNetworkName + "/"; + uint64_t xpanNonCcm = 2; + BorderAgent::State baStateNonCcm{BorderAgent::State::ConnectionMode::kPSKcConnection, 0, 0, 0, 0}; ASSERT_EQ( @@ -1224,8 +1232,8 @@ TEST_F(InterpreterTestSuite, PC_TokenWithNonCCM) // note: we do not expect GetToken() here as no default config // update to happen with a network selected - EXPECT_EQ(WriteFile("123aef", "./token").GetCode(), ErrorCode::kNone); - expr = ctx.mInterpreter.ParseExpression("token set ./token"); + EXPECT_EQ(WriteFile("123aef", "./tmp/token").GetCode(), ErrorCode::kNone); + expr = ctx.mInterpreter.ParseExpression("token set ./tmp/token"); value = ctx.mInterpreter.Eval(expr); EXPECT_TRUE(value.HasNoError()); } @@ -1256,8 +1264,8 @@ TEST_F(InterpreterTestSuite, PC_TokenWithNone) // GetToken() to be called on default commissioner instance EXPECT_CALL(*ctx.mDefaultCommissionerObject, GetToken()).WillOnce(ReturnRef(token)); - EXPECT_EQ(WriteFile("123aef", "./token").GetCode(), ErrorCode::kNone); - expr = ctx.mInterpreter.ParseExpression("token set ./token"); + EXPECT_EQ(WriteFile("123aef", "./tmp/token").GetCode(), ErrorCode::kNone); + expr = ctx.mInterpreter.ParseExpression("token set ./tmp/token"); value = ctx.mInterpreter.Eval(expr); EXPECT_TRUE(value.HasNoError()); } @@ -1630,7 +1638,7 @@ TEST_F(InterpreterTestSuite, PC_BorderagentDiscover) BorderAgentFunctionsMock bafm; SetBorderAgentFunctionsMock(&bafm); - EXPECT_CALL(bafm, DiscoverBorderAgent(_, _)).WillOnce(Return(Error{})); + EXPECT_CALL(bafm, DiscoverBorderAgent(_, _, _)).WillOnce(Return(Error{})); Interpreter::Expression expr; Interpreter::Value value; @@ -1825,17 +1833,14 @@ TEST_F(InterpreterTestSuite, PC_OpdatasetGetActive) EXPECT_EQ(ctx.mRegistry->mStorage->Get(nwk_id, nwk), PersistentStorage::Status::kSuccess); EXPECT_EQ(nwk.mPan, 0x0001); - EXPECT_EQ(system("rm -f ./aods.json"), 0); - EXPECT_NE(PathExists("./aods.json").GetCode(), ErrorCode::kNone); - - expr = ctx.mInterpreter.ParseExpression("opdataset get active --export ./aods.json"); + expr = ctx.mInterpreter.ParseExpression("opdataset get active --export ./tmp/aods.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_TRUE(value.HasNoError()); ctx.mInterpreter.PrintOrExport(value); - EXPECT_EQ(PathExists("./aods.json").GetCode(), ErrorCode::kNone); + EXPECT_EQ(PathExists("./tmp/aods.json").GetCode(), ErrorCode::kNone); std::string jsonStr; - EXPECT_EQ(ReadFile(jsonStr, "./aods.json").GetCode(), ErrorCode::kNone); + EXPECT_EQ(ReadFile(jsonStr, "./tmp/aods.json").GetCode(), ErrorCode::kNone); nlohmann::json json = nlohmann::json::parse(jsonStr); EXPECT_TRUE(json.contains("PanId")); EXPECT_STREQ("0x0001", json.at("PanId").get().c_str()); @@ -2111,7 +2116,7 @@ TEST_F(InterpreterTestSuite, PC_BrScanExport) Interpreter::Expression expr; Interpreter::Value value; - std::string jsonFileName = "./br-list.json"; + std::string jsonFileName = "./tmp/br-list.json"; EXPECT_EQ(system(fmt::format("rm -rf {}", jsonFileName).c_str()), 0); EXPECT_NE(PathExists(jsonFileName).GetCode(), ErrorCode::kNone); @@ -2141,8 +2146,7 @@ TEST_F(InterpreterTestSuite, PC_BrScanExportDirAbsent) Interpreter::Expression expr; Interpreter::Value value; - std::string jsonFileName = "./tmpdir/br-list.json"; - ASSERT_EQ(system("rm -rf ./tmpdir"), 0); + std::string jsonFileName = "./tmp/br-list.json"; expr = ctx.mInterpreter.ParseExpression(std::string("br scan --timeout 1 --export ") + jsonFileName); value = ctx.mInterpreter.Eval(expr); EXPECT_TRUE(value.HasNoError()); @@ -2200,23 +2204,23 @@ TEST_F(InterpreterTestSuite, PC_BrAddNoMandatoryFail) Interpreter::Expression expr; Interpreter::Value value; - EXPECT_EQ(WriteFile(brJsonNoAddr, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonNoAddr, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); - EXPECT_EQ(WriteFile(brJsonNoPort, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonNoPort, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); - EXPECT_EQ(WriteFile(brJsonNoVersion, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonNoVersion, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); - EXPECT_EQ(WriteFile(brJsonNoState, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonNoState, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); } @@ -2269,23 +2273,23 @@ n]"; Interpreter::Expression expr; Interpreter::Value value; - EXPECT_EQ(WriteFile(brJsonNwkName, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonNwkName, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); - EXPECT_EQ(WriteFile(brJsonNwkNameZeroXPan, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonNwkNameZeroXPan, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); - EXPECT_EQ(WriteFile(brJsonDomainName, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonDomainName, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); - EXPECT_EQ(WriteFile(brJsonDomainNameZeroXPan, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonDomainNameZeroXPan, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); } @@ -2351,18 +2355,18 @@ TEST_F(InterpreterTestSuite, PC_BrAddInterObjectInconsistencyFail) Interpreter::Expression expr; Interpreter::Value value; - EXPECT_EQ(WriteFile(brJsonSameAddr, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonSameAddr, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); - EXPECT_EQ(WriteFile(brJsonSameXPanDifferentNwkNames, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonSameXPanDifferentNwkNames, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); - EXPECT_EQ(WriteFile(brJsonSameXPanDifferentDomains, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJsonSameXPanDifferentDomains, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_FALSE(value.HasNoError()); } @@ -2408,8 +2412,8 @@ TEST_F(InterpreterTestSuite, PC_BrAdd) Interpreter::Expression expr; Interpreter::Value value; - EXPECT_EQ(WriteFile(brJson, "./json.json"), Error{}); - expr = ctx.mInterpreter.ParseExpression("br add ./json.json"); + EXPECT_EQ(WriteFile(brJson, "./tmp/json.json"), Error{}); + expr = ctx.mInterpreter.ParseExpression("br add ./tmp/json.json"); value = ctx.mInterpreter.Eval(expr); EXPECT_TRUE(value.HasNoError()); diff --git a/src/app/cli/job_manager_test.cpp b/src/app/cli/job_manager_test.cpp index 8ba188c0..8efacd41 100644 --- a/src/app/cli/job_manager_test.cpp +++ b/src/app/cli/job_manager_test.cpp @@ -119,6 +119,12 @@ class JobManagerTestSuite : public testing::Test EXPECT_EQ(aContext.mJobManager.Init(aContext.mConf).mCode, ErrorCode::kNone); aContext.mInterpreter.mRegistry = aContext.mRegistry; } + + void SetUp() override + { + ASSERT_TRUE(system("rm -rf tmp") == 0); + ASSERT_TRUE(system("mkdir -p tmp") == 0); + } }; TEST_F(JobManagerTestSuite, TestInit) @@ -321,23 +327,22 @@ TEST_F(JobManagerTestSuite, StartCancel) TEST_F(JobManagerTestSuite, MalformedCredentialsJobCreateFailsByXPan) { - // Remove './nwk' subtree - ASSERT_EQ(system("rm -rf ./dom ./nwk"), 0); + ASSERT_EQ(system("rm -rf ./tmp/dom ./tmp/nwk"), 0); - EXPECT_EQ(mkdir("./nwk", 0777), 0); - EXPECT_EQ(mkdir("./nwk/0000000000000001", 0777), 0); - EXPECT_EQ(mkdir("./nwk/0000000000000002", 0777), 0); - EXPECT_EQ(mkdir("./nwk/0000000000000003", 0777), 0); + EXPECT_EQ(mkdir("./tmp/nwk", 0777), 0); + EXPECT_EQ(mkdir("./tmp/nwk/0000000000000001", 0777), 0); + EXPECT_EQ(mkdir("./tmp/nwk/0000000000000002", 0777), 0); + EXPECT_EQ(mkdir("./tmp/nwk/0000000000000003", 0777), 0); // Loose credentials for Network 1 - EXPECT_EQ(WriteFile("1", "./nwk/0000000000000001/ca.pem").mCode, ErrorCode::kNone); - EXPECT_EQ(WriteFile("1", "./nwk/0000000000000001/priv.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/0000000000000001/ca.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/0000000000000001/priv.pem").mCode, ErrorCode::kNone); // Loose credentials for Network 2 - EXPECT_EQ(WriteFile("1", "./nwk/0000000000000002/cert.pem").mCode, ErrorCode::kNone); - EXPECT_EQ(WriteFile("1", "./nwk/0000000000000002/priv.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/0000000000000002/cert.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/0000000000000002/priv.pem").mCode, ErrorCode::kNone); // Loose credentials for Network 3 - EXPECT_EQ(WriteFile("1", "./nwk/0000000000000003/cert.pem").mCode, ErrorCode::kNone); - EXPECT_EQ(WriteFile("1", "./nwk/0000000000000003/ca.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/0000000000000003/cert.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/0000000000000003/ca.pem").mCode, ErrorCode::kNone); TestContext ctx; SetInitialExpectations(ctx); @@ -399,23 +404,22 @@ TEST_F(JobManagerTestSuite, MalformedCredentialsJobCreateFailsByXPan) TEST_F(JobManagerTestSuite, MalformedCredentialsJobCreateFailsByName) { - // Remove './nwk' subtree - ASSERT_EQ(system("rm -rf ./dom ./nwk"), 0); + ASSERT_EQ(system("rm -rf ./tmp/dom ./tmp/nwk"), 0); - EXPECT_EQ(mkdir("./nwk", 0777), 0); - EXPECT_EQ(mkdir("./nwk/pan1", 0777), 0); - EXPECT_EQ(mkdir("./nwk/pan2", 0777), 0); - EXPECT_EQ(mkdir("./nwk/pan3", 0777), 0); + EXPECT_EQ(mkdir("./tmp/nwk", 0777), 0); + EXPECT_EQ(mkdir("./tmp/nwk/pan1", 0777), 0); + EXPECT_EQ(mkdir("./tmp/nwk/pan2", 0777), 0); + EXPECT_EQ(mkdir("./tmp/nwk/pan3", 0777), 0); // Loose credentials for Network 1 - EXPECT_EQ(WriteFile("1", "./nwk/pan1/ca.pem").mCode, ErrorCode::kNone); - EXPECT_EQ(WriteFile("1", "./nwk/pan1/priv.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/pan1/ca.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/pan1/priv.pem").mCode, ErrorCode::kNone); // Loose credentials for panwork 2 - EXPECT_EQ(WriteFile("1", "./nwk/pan2/cert.pem").mCode, ErrorCode::kNone); - EXPECT_EQ(WriteFile("1", "./nwk/pan2/priv.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/pan2/cert.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/pan2/priv.pem").mCode, ErrorCode::kNone); // Loose credentials for panwork 3 - EXPECT_EQ(WriteFile("1", "./nwk/pan3/cert.pem").mCode, ErrorCode::kNone); - EXPECT_EQ(WriteFile("1", "./nwk/pan3/ca.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/pan3/cert.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/nwk/pan3/ca.pem").mCode, ErrorCode::kNone); TestContext ctx; SetInitialExpectations(ctx); @@ -478,22 +482,22 @@ TEST_F(JobManagerTestSuite, MalformedCredentialsJobCreateFailsByName) TEST_F(JobManagerTestSuite, MalformedCredentialsJobCreateFailsByDomain) { // Remove SM subtrees - ASSERT_EQ(system("rm -rf ./dom ./nwk"), 0); + ASSERT_EQ(system("rm -rf ./tmp/dom ./tmp/nwk"), 0); - EXPECT_EQ(mkdir("./dom", 0777), 0); - EXPECT_EQ(mkdir("./dom/domain1", 0777), 0); - EXPECT_EQ(mkdir("./dom/domain2", 0777), 0); - EXPECT_EQ(mkdir("./dom/domain3", 0777), 0); + EXPECT_EQ(mkdir("./tmp/dom", 0777), 0); + EXPECT_EQ(mkdir("./tmp/dom/domain1", 0777), 0); + EXPECT_EQ(mkdir("./tmp/dom/domain2", 0777), 0); + EXPECT_EQ(mkdir("./tmp/dom/domain3", 0777), 0); // Loose credentials for domain1 - EXPECT_EQ(WriteFile("1", "./dom/domain1/ca.pem").mCode, ErrorCode::kNone); - EXPECT_EQ(WriteFile("1", "./dom/domain1/priv.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/dom/domain1/ca.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/dom/domain1/priv.pem").mCode, ErrorCode::kNone); // Loose credentials for domain2 - EXPECT_EQ(WriteFile("1", "./dom/domain2/cert.pem").mCode, ErrorCode::kNone); - EXPECT_EQ(WriteFile("1", "./dom/domain2/priv.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/dom/domain2/cert.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/dom/domain2/priv.pem").mCode, ErrorCode::kNone); // Loose credentials for domain 3 - EXPECT_EQ(WriteFile("1", "./dom/domain3/cert.pem").mCode, ErrorCode::kNone); - EXPECT_EQ(WriteFile("1", "./dom/domain3/ca.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/dom/domain3/cert.pem").mCode, ErrorCode::kNone); + EXPECT_EQ(WriteFile("1", "./tmp/dom/domain3/ca.pem").mCode, ErrorCode::kNone); TestContext ctx; SetInitialExpectations(ctx); diff --git a/src/app/ps/persistent_storage_json_test.cpp b/src/app/ps/persistent_storage_json_test.cpp index 0ca5084f..295dc3f2 100644 --- a/src/app/ps/persistent_storage_json_test.cpp +++ b/src/app/ps/persistent_storage_json_test.cpp @@ -31,46 +31,59 @@ * The file implements unit tests for JSON-based persistent storage */ +#include +#include #include #include "persistent_storage_json.hpp" #include "app/border_agent.hpp" -#include - using namespace ot::commissioner::persistent_storage; using namespace ot::commissioner; -TEST(PSJson, CreateDefaultIfNotExists) +class PersistentStorageJsonTestSuite : public testing::Test +{ +public: + PersistentStorageJsonTestSuite() = default; + virtual ~PersistentStorageJsonTestSuite() = default; + + void SetUp() override + { + ASSERT_TRUE(system("rm -rf tmp") == 0); + ASSERT_TRUE(system("mkdir -p tmp") == 0); + } +}; + +TEST(PersistentStorageJsonTestSuite, CreateDefaultIfNotExists) { - PersistentStorageJson psj("./test_ps.json"); + PersistentStorageJson psj("./tmp/test_ps.json"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, ReadEmptyFile) +TEST(PersistentStorageJsonTestSuite, ReadEmptyFile) { - std::ofstream testTmp("./test.tmp"); + std::ofstream testTmp("./tmp/test.tmp"); testTmp.close(); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, ReadNonEmptyDefault) +TEST(PersistentStorageJsonTestSuite, ReadNonEmptyDefault) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, AddRegistrar) +TEST(PersistentStorageJsonTestSuite, AddRegistrar) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -86,9 +99,9 @@ TEST(PSJson, AddRegistrar) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, AddDomain) +TEST(PersistentStorageJsonTestSuite, AddDomain) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -104,12 +117,12 @@ TEST(PSJson, AddDomain) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, AddNetwork) +TEST(PersistentStorageJsonTestSuite, AddNetwork) { // Make the test independent - unlink("./test.tmp"); + unlink("./tmp/test.tmp"); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -128,9 +141,9 @@ TEST(PSJson, AddNetwork) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, AddBorderRouter) +TEST(PersistentStorageJsonTestSuite, AddBorderRouter) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -161,9 +174,9 @@ TEST(PSJson, AddBorderRouter) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, DelRegistrar) +TEST(PersistentStorageJsonTestSuite, DelRegistrar) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -175,9 +188,9 @@ TEST(PSJson, DelRegistrar) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, DelDomain) +TEST(PersistentStorageJsonTestSuite, DelDomain) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -189,7 +202,7 @@ TEST(PSJson, DelDomain) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, DelNetwork) +TEST(PersistentStorageJsonTestSuite, DelNetwork) { PersistentStorageJson psj(""); @@ -210,9 +223,9 @@ TEST(PSJson, DelNetwork) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, DelBorderRouter) +TEST(PersistentStorageJsonTestSuite, DelBorderRouter) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -224,9 +237,9 @@ TEST(PSJson, DelBorderRouter) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, GetRegistrarFromEmpty) +TEST(PersistentStorageJsonTestSuite, GetRegistrarFromEmpty) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -237,9 +250,9 @@ TEST(PSJson, GetRegistrarFromEmpty) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, GetDomainFromEmpty) +TEST(PersistentStorageJsonTestSuite, GetDomainFromEmpty) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -250,7 +263,7 @@ TEST(PSJson, GetDomainFromEmpty) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, GetNetworkFromEmpty) +TEST(PersistentStorageJsonTestSuite, GetNetworkFromEmpty) { PersistentStorageJson psj(""); @@ -263,9 +276,9 @@ TEST(PSJson, GetNetworkFromEmpty) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, GetBorderRouterFromEmpty) +TEST(PersistentStorageJsonTestSuite, GetBorderRouterFromEmpty) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -276,12 +289,12 @@ TEST(PSJson, GetBorderRouterFromEmpty) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, GetRegistrarNotEmpty) +TEST(PersistentStorageJsonTestSuite, GetRegistrarNotEmpty) { // Make test independent - unlink("./test.tmp"); + unlink("./tmp/test.tmp"); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -306,12 +319,12 @@ TEST(PSJson, GetRegistrarNotEmpty) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, GetDomainNotEmpty) +TEST(PersistentStorageJsonTestSuite, GetDomainNotEmpty) { // Make test independent - unlink("./test.tmp"); + unlink("./tmp/test.tmp"); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -334,9 +347,9 @@ TEST(PSJson, GetDomainNotEmpty) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, GetNetworkNotEmpty) +TEST(PersistentStorageJsonTestSuite, GetNetworkNotEmpty) { - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -363,12 +376,12 @@ TEST(PSJson, GetNetworkNotEmpty) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, GetBorderRouterNotEmpty) +TEST(PersistentStorageJsonTestSuite, GetBorderRouterNotEmpty) { // Make the test independent - unlink("./test.tmp"); + unlink("./tmp/test.tmp"); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -408,12 +421,12 @@ TEST(PSJson, GetBorderRouterNotEmpty) } // UPD -TEST(PSJson, UpdRegistrar) +TEST(PersistentStorageJsonTestSuite, UpdRegistrar) { // Make the test independent - unlink("./test.tmp"); + unlink("./tmp/test.tmp"); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -442,12 +455,12 @@ TEST(PSJson, UpdRegistrar) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, UpdDomain) +TEST(PersistentStorageJsonTestSuite, UpdDomain) { // Make the test independent - unlink("./test.tmp"); + unlink("./tmp/test.tmp"); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -474,10 +487,10 @@ TEST(PSJson, UpdDomain) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, UpdNetwork) +TEST(PersistentStorageJsonTestSuite, UpdNetwork) { - unlink("./tmp.json"); - PersistentStorageJson psj("./tmp.json"); + unlink("./tmp/tmp.json"); + PersistentStorageJson psj("./tmp/tmp.json"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -501,12 +514,12 @@ TEST(PSJson, UpdNetwork) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, UpdBorderRouter) +TEST(PersistentStorageJsonTestSuite, UpdBorderRouter) { // Make test independent - unlink("./test.tmp"); + unlink("./tmp/test.tmp"); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -554,12 +567,12 @@ TEST(PSJson, UpdBorderRouter) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, LookupRegistrar) +TEST(PersistentStorageJsonTestSuite, LookupRegistrar) { // Make test independent - unlink("./test.tmp"); + unlink("./tmp/test.tmp"); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); @@ -630,12 +643,12 @@ TEST(PSJson, LookupRegistrar) EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); } -TEST(PSJson, LookupNetwork) +TEST(PersistentStorageJsonTestSuite, LookupNetwork) { // Make test independent - unlink("./test.tmp"); + unlink("./tmp/test.tmp"); - PersistentStorageJson psj("./test.tmp"); + PersistentStorageJson psj("./tmp/test.tmp"); EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); diff --git a/src/app/ps/registry_test.cpp b/src/app/ps/registry_test.cpp index 281120e5..867b2838 100644 --- a/src/app/ps/registry_test.cpp +++ b/src/app/ps/registry_test.cpp @@ -46,7 +46,7 @@ using namespace ot::commissioner::persistent_storage; using namespace ot::commissioner::utils; using namespace ot::commissioner; -const char json_path[] = "./registry_test.json"; +const char json_path[] = "./tmp/registry_test.json"; TEST(RegJson, CreateEmptyRegistry) {