From eba18e4b60ff5ec7deffc01c5387c50997458878 Mon Sep 17 00:00:00 2001 From: kangping Date: Fri, 24 Jul 2020 23:21:24 +0800 Subject: [PATCH 1/5] [tests] test cancelling requests from CLI --- tests/integration/common.sh | 13 +++++++++++-- tests/integration/test_cli.sh | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/integration/common.sh b/tests/integration/common.sh index 889abfb06..add8949a5 100755 --- a/tests/integration/common.sh +++ b/tests/integration/common.sh @@ -136,17 +136,26 @@ stop_commissioner() { } ## Send command to commissioner. -## Args: $1: the command. +## Args: +## $1: the command. +## $2: the expected error (optional). The command +## is expected to success if this argument is +## not specified. send_command_to_commissioner() { set -e local command=$1 + local expect_error=$2 local result result=$(${COMMISSIONER_CTL} execute "${command}") echo "${result}" [ -n "${result}" ] || return 1 - echo "${result}" | grep -q "\[done\]" || return 1 + if [ -z "${expect_error}" ]; then + echo "${result}" | grep -q "\[done\]" || return 1 + else + echo "${result}" | grep -q "${expect_error}" || return 1 + fi } ## Start a joiner diff --git a/tests/integration/test_cli.sh b/tests/integration/test_cli.sh index 990a9aed4..121af3386 100755 --- a/tests/integration/test_cli.sh +++ b/tests/integration/test_cli.sh @@ -58,3 +58,17 @@ test_get_commissioner_dataset() stop_daemon } + +test_cancel_command() { + set -e + + start_commissioner "${NON_CCM_CONFIG}" + + ## Expecting error CANCELLED after sending SIGINT + send_command_to_commissioner "borderagent discover" "CANCELLED" & + sleep 1 + + pkill -s SIGINT "${COMMISSIONER_CLI}" + + stop_commissioner +} From fdc17a06ba7006dc6b5cef7a6a4093fde2806190 Mon Sep 17 00:00:00 2001 From: kangping Date: Sat, 25 Jul 2020 18:53:27 +0800 Subject: [PATCH 2/5] fix pkill command --- tests/integration/test_cli.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_cli.sh b/tests/integration/test_cli.sh index 121af3386..d0f6f3a1b 100755 --- a/tests/integration/test_cli.sh +++ b/tests/integration/test_cli.sh @@ -68,7 +68,7 @@ test_cancel_command() { send_command_to_commissioner "borderagent discover" "CANCELLED" & sleep 1 - pkill -s SIGINT "${COMMISSIONER_CLI}" + pkill --signal SIGINT "${COMMISSIONER_CLI}" stop_commissioner } From ca3abe3ca339a4e94f3e4023c0e645754ff6625e Mon Sep 17 00:00:00 2001 From: kangping Date: Sat, 25 Jul 2020 18:56:55 +0800 Subject: [PATCH 3/5] add comment for the pkill command --- tests/integration/test_cli.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/test_cli.sh b/tests/integration/test_cli.sh index d0f6f3a1b..c14f4b292 100755 --- a/tests/integration/test_cli.sh +++ b/tests/integration/test_cli.sh @@ -68,6 +68,7 @@ test_cancel_command() { send_command_to_commissioner "borderagent discover" "CANCELLED" & sleep 1 + ## send SIGINT to the commissioner CLI to cancel outstanding requests ('borderagent discover' in this case). pkill --signal SIGINT "${COMMISSIONER_CLI}" stop_commissioner From 2d121aedd6375f6146b6b64a8e61d0148be79bc8 Mon Sep 17 00:00:00 2001 From: kangping Date: Sun, 26 Jul 2020 15:03:19 +0800 Subject: [PATCH 4/5] fix cancelling Border Agent discovery --- src/app/border_agent.cpp | 17 ++++++++++++++++- src/app/border_agent.hpp | 8 ++++++++ src/app/cli/interpreter.cpp | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/app/border_agent.cpp b/src/app/border_agent.cpp index 164bb97db..5a819c3e9 100644 --- a/src/app/border_agent.cpp +++ b/src/app/border_agent.cpp @@ -28,6 +28,7 @@ #include "border_agent.hpp" +#include #include #include @@ -42,6 +43,8 @@ namespace ot { namespace commissioner { +static std::atomic gIsDiscoverCancelled; + struct BorderAgentOrErrorMsg { BorderAgent mBorderAgent; @@ -59,6 +62,11 @@ static int HandleRecord(const struct sockaddr *from, size_t length, void * user_data); +void CancelDiscoverBorderAgent() +{ + gIsDiscoverCancelled = true; +} + Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout) { static constexpr size_t kDefaultBufferSize = 1024 * 16; @@ -67,18 +75,21 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou Error error; uint8_t buf[kDefaultBufferSize]; + size_t borderAgentCount = 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")); + gIsDiscoverCancelled = false; + if (mdns_query_send(socket, kMdnsQueryType, kServiceName, strlen(kServiceName), buf, sizeof(buf)) != 0) { ExitNow(error = ERROR_IO_ERROR("failed to send mDNS query")); } - while (begin + std::chrono::milliseconds(aTimeout) >= std::chrono::system_clock::now()) + while (!gIsDiscoverCancelled && begin + std::chrono::milliseconds(aTimeout) >= std::chrono::system_clock::now()) { BorderAgentOrErrorMsg curBorderAgentOrErrorMsg; @@ -91,11 +102,15 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou else if (curBorderAgentOrErrorMsg.mBorderAgent.mPresentFlags != 0) { aBorderAgentHandler(&curBorderAgentOrErrorMsg.mBorderAgent, ERROR_NONE); + ++borderAgentCount; } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } + VerifyOrExit(!gIsDiscoverCancelled, error = ERROR_CANCELLED("Border Agent discovery was cancelled")); + VerifyOrExit(borderAgentCount != 0, error = ERROR_TIMEOUT("Found no Border Agent")); + exit: if (socket >= 0) { diff --git a/src/app/border_agent.hpp b/src/app/border_agent.hpp index b2418d3af..555e4f30a 100644 --- a/src/app/border_agent.hpp +++ b/src/app/border_agent.hpp @@ -177,6 +177,14 @@ using BorderAgentHandler = std::functionStop(); } + + CancelDiscoverBorderAgent(); } Interpreter::Expression Interpreter::Read() From cbae49489ed89c4a5ea34275466b5ba3a1fe2fa9 Mon Sep 17 00:00:00 2001 From: kangping Date: Wed, 29 Jul 2020 21:29:03 +0800 Subject: [PATCH 5/5] make sure the mDNS service has been stopped --- tests/integration/test_cli.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integration/test_cli.sh b/tests/integration/test_cli.sh index c14f4b292..3cdc4977d 100755 --- a/tests/integration/test_cli.sh +++ b/tests/integration/test_cli.sh @@ -64,6 +64,9 @@ test_cancel_command() { start_commissioner "${NON_CCM_CONFIG}" + ## Make sure that the mDNS service has been stopped. + stop_border_agent_mdns_service + ## Expecting error CANCELLED after sending SIGINT send_command_to_commissioner "borderagent discover" "CANCELLED" & sleep 1