diff --git a/build/compile_script/ark.py b/build/compile_script/ark.py index 97badc5abcef721fd9bea13604df14d24a712731..546647833737953e0c8e30b0b0fe4f9fa5ea25ed 100755 --- a/build/compile_script/ark.py +++ b/build/compile_script/ark.py @@ -199,7 +199,7 @@ class ArkPy: "description": "Compile and run unittest of arkcompiler target. " "Add --keep-going=N to keep running unittest when errors occured less than N. " - "Add --gn-args=\"run_with_qemu=true\" \ + "Add --gn-args=\"run_with_qemu=true\" timeout=\"1200\"\ \"disable_force_gc=true\" to command when running unittest of non-host type with qemu.", "gn_targets_depend_on": ["unittest_packages"], }, @@ -302,12 +302,13 @@ class ArkPy: return False @staticmethod - def get_test262_cmd(gn_args, out_path, x64_out_path, aot_mode, run_pgo, enable_litecg, args_to_test262_cmd): + def get_test262_cmd(gn_args, out_path, x64_out_path, aot_mode, run_pgo, enable_litecg, args_to_test262_cmd, + timeout): if aot_mode: print("running test262 in AotMode\n") if any('target_cpu="arm64"' in arg for arg in gn_args): if run_pgo: - test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout 180000" \ + test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout {4}" \ " --libs-dir ../../{1}/arkcompiler/ets_runtime:../../{1}/thirdparty/icu:" \ "../../{1}/thirdparty/zlib:../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \ " --ark-arch aarch64" \ @@ -318,9 +319,9 @@ class ArkPy: " --merge-abc-binary=../../{2}/arkcompiler/ets_frontend/merge_abc" \ " --ark-aot" \ " --ark-frontend=es2panda"\ - "{3}".format(args_to_test262_cmd, out_path, x64_out_path, " --run-pgo") + "{3}".format(args_to_test262_cmd, out_path, x64_out_path, " --run-pgo", timeout) else: - test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout 180000" \ + test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout {3}" \ " --libs-dir ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib:../../{2}/thirdparty/icu/" \ " --ark-arch aarch64" \ " --ark-arch-root=../../{1}/common/common/libc/" \ @@ -329,9 +330,9 @@ class ArkPy: " --ark-tool=../../{1}/arkcompiler/ets_runtime/ark_js_vm" \ " --ark-frontend-binary=../../{2}/arkcompiler/ets_frontend/es2abc" \ " --merge-abc-binary=../../{2}/arkcompiler/ets_frontend/merge_abc" \ - " --ark-frontend=es2panda".format(args_to_test262_cmd, out_path, x64_out_path) + " --ark-frontend=es2panda".format(args_to_test262_cmd, out_path, x64_out_path, timeout) else: - test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout 180000" \ + test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout {3}" \ " --libs-dir ../../{1}/arkcompiler/ets_runtime:../../{1}/thirdparty/icu" \ ":../../{1}/thirdparty/zlib:../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \ " --ark-tool=../../{1}/arkcompiler/ets_runtime/ark_js_vm" \ @@ -340,24 +341,24 @@ class ArkPy: " --merge-abc-binary=../../{1}/arkcompiler/ets_frontend/merge_abc" \ " --ark-aot" \ " --ark-frontend=es2panda"\ - "{2}".format(args_to_test262_cmd, out_path, " --run-pgo" if run_pgo else "") + "{2}".format(args_to_test262_cmd, out_path, " --run-pgo" if run_pgo else "", timeout) if enable_litecg: test262_cmd = test262_cmd + " --enable-litecg" else: print("running test262 in AsmMode\n") - test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout 180000" \ + test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout {2}" \ " --libs-dir ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \ " --ark-tool=../../{1}/arkcompiler/ets_runtime/ark_js_vm" \ " --ark-frontend-binary=../../{1}/arkcompiler/ets_frontend/es2abc" \ " --merge-abc-binary=../../{1}/arkcompiler/ets_frontend/merge_abc" \ - " --ark-frontend=es2panda".format(args_to_test262_cmd, out_path) + " --ark-frontend=es2panda".format(args_to_test262_cmd, out_path, timeout) return test262_cmd @staticmethod - def get_test262_jit_cmd(gn_args, out_path, x64_out_path, args_to_test262_cmd): + def get_test262_jit_cmd(gn_args, out_path, x64_out_path, args_to_test262_cmd, timeout): print("running test262 in JIT mode\n") if any('target_cpu="arm64"' in arg for arg in gn_args): - test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout 400000" \ + test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout {3}" \ " --libs-dir ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib:../../{1}/thirdparty/icu/" \ ":../../{1}/thirdparty/bounds_checking_function" \ ":../../{1}/arkcompiler/ets_runtime:" \ @@ -368,9 +369,9 @@ class ArkPy: " --ark-tool=../../{1}/arkcompiler/ets_runtime/ark_js_vm" \ " --ark-frontend-binary=../../{2}/arkcompiler/ets_frontend/es2abc" \ " --merge-abc-binary=../../{2}/arkcompiler/ets_frontend/merge_abc" \ - " --ark-frontend=es2panda".format(args_to_test262_cmd, out_path, x64_out_path) + " --ark-frontend=es2panda".format(args_to_test262_cmd, out_path, x64_out_path, timeout) else: - test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout 400000" \ + test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout {3}" \ " --libs-dir ../../{1}/arkcompiler/ets_runtime:../../{1}/thirdparty/icu" \ ":../../{1}/thirdparty/zlib:../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \ " --run-jit" \ @@ -378,14 +379,14 @@ class ArkPy: " --ark-frontend-binary=../../{1}/arkcompiler/ets_frontend/es2abc" \ " --merge-abc-binary=../../{1}/arkcompiler/ets_frontend/merge_abc" \ " --ark-frontend=es2panda"\ - "{2}".format(args_to_test262_cmd, out_path, x64_out_path) + "{2}".format(args_to_test262_cmd, out_path, x64_out_path, timeout) return test262_cmd @staticmethod - def get_test262_baseline_jit_cmd(gn_args, out_path, x64_out_path, args_to_test262_cmd): + def get_test262_baseline_jit_cmd(gn_args, out_path, x64_out_path, args_to_test262_cmd, timeout): print("running test262 in baseline JIT mode\n") if any('target_cpu="arm64"' in arg for arg in gn_args): - test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout 400000" \ + test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout {3}" \ " --libs-dir ../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \ ":../../{1}/thirdparty/icu" \ ":../../prebuilts/clang/ohos/linux-x86_64/llvm/lib/aarch64-linux-ohos" \ @@ -399,9 +400,9 @@ class ArkPy: " --ark-tool=../../{1}/arkcompiler/ets_runtime/ark_js_vm" \ " --ark-frontend-binary=../../{2}/arkcompiler/ets_frontend/es2abc" \ " --merge-abc-binary=../../{2}/arkcompiler/ets_frontend/merge_abc" \ - " --ark-frontend=es2panda".format(args_to_test262_cmd, out_path, x64_out_path) + " --ark-frontend=es2panda".format(args_to_test262_cmd, out_path, x64_out_path, timeout) else: - test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout 400000" \ + test262_cmd = "cd arkcompiler/ets_frontend && python3 test262/run_test262.py {0} --timeout {3}" \ " --libs-dir ../../{1}/lib.unstripped/arkcompiler/ets_runtime" \ ":../../{1}/lib.unstripped/thirdparty/icu" \ ":../../prebuilts/clang/ohos/linux-x86_64/llvm/lib" \ @@ -411,7 +412,7 @@ class ArkPy: " --ark-frontend-binary=../../{1}/arkcompiler/ets_frontend/es2abc" \ " --merge-abc-binary=../../{1}/arkcompiler/ets_frontend/merge_abc" \ " --ark-frontend=es2panda"\ - "{2}".format(args_to_test262_cmd, out_path, x64_out_path) + "{2}".format(args_to_test262_cmd, out_path, x64_out_path, timeout) return test262_cmd def get_binaries(self): @@ -574,7 +575,7 @@ class ArkPy: print("=== ninja success! ===\n") return - def build_for_test262(self, out_path, gn_args: list, arg_list: list, log_file_name: str, + def build_for_test262(self, out_path, timeout, gn_args: list, arg_list: list, log_file_name: str, aot_mode: bool, run_pgo=False, enable_litecg=False, run_jit=False, run_baseline_jit=False): args_to_test262_cmd = "" if len(arg_list) >= 1 and "disable-force-gc" in arg_list[-1]: @@ -609,12 +610,13 @@ class ArkPy: self.build_for_gn_target( out_path, gn_args, self.ARG_DICT["target"]["test262"]["gn_targets_depend_on"], log_file_name) if run_jit: - test262_cmd = self.get_test262_jit_cmd(gn_args, out_path, x64_out_path, args_to_test262_cmd) + test262_cmd = self.get_test262_jit_cmd(gn_args, out_path, x64_out_path, args_to_test262_cmd, timeout) elif run_baseline_jit: - test262_cmd = self.get_test262_baseline_jit_cmd(gn_args, out_path, x64_out_path, args_to_test262_cmd) + test262_cmd = self.get_test262_baseline_jit_cmd(gn_args, out_path, x64_out_path, + args_to_test262_cmd, timeout) else: test262_cmd = self.get_test262_cmd(gn_args, out_path, x64_out_path, aot_mode, run_pgo, - enable_litecg, args_to_test262_cmd) + enable_litecg, args_to_test262_cmd, timeout) test262_log_path = os.path.join(out_path, log_file_name) str_to_test262_log = "================================\ntest262_time: {0}\ntest262_target: {1}\n\n".format( str_of_time_now(), args_to_test262_cmd) @@ -625,7 +627,6 @@ class ArkPy: print("=== test262 fail! ===\n") sys.exit(code) print("=== test262 success! ===\n") - return def build_for_unittest(self, out_path: str, gn_args: list, log_file_name:str): self.build_for_gn_target( @@ -633,7 +634,7 @@ class ArkPy: log_file_name) return - def build_for_regress_test(self, out_path, gn_args: list, arg_list: list, log_file_name: str): + def build_for_regress_test(self, out_path, gn_args: list, arg_list: list, log_file_name: str, timeout): args_to_regress_test_cmd = "" if len(arg_list) == 0: args_to_regress_test_cmd = "" @@ -647,12 +648,12 @@ class ArkPy: sys.exit(0) self.build_for_gn_target( out_path, gn_args, self.ARG_DICT["target"]["regresstest"]["gn_targets_depend_on"], log_file_name) - regress_test_cmd = "python3 arkcompiler/ets_runtime/test/regresstest/run_regress_test.py" \ + regress_test_cmd = "python3 arkcompiler/ets_runtime/test/regresstest/run_regress_test.py --timeout {2}" \ " --ark-tool ./{0}/arkcompiler/ets_runtime/ark_js_vm" \ " --ark-frontend-binary ./{0}/arkcompiler/ets_frontend/es2abc" \ " --LD_LIBRARY_PATH ./{0}/arkcompiler/ets_runtime:./{0}/thirdparty/icu:" \ "./prebuilts/clang/ohos/linux-x86_64/llvm/lib" \ - " --out-dir ./{0}/ {1}".format(out_path, args_to_regress_test_cmd) + " --out-dir ./{0}/ {1}".format(out_path, args_to_regress_test_cmd, timeout) regress_test_log_path = os.path.join(out_path, log_file_name) str_to_test_log = "============\n regresstest_time: {0}\nregresstest_target: {1}\n\n".format( str_of_time_now(), regress_test_cmd) @@ -674,6 +675,7 @@ class ArkPy: elif self.is_dict_flags_match_arg(self.ARG_DICT["target"]["workload"], arg_list[0]): self.build_for_workload(arg_list, out_path, gn_args, 'workload.log') elif self.is_dict_flags_match_arg(self.ARG_DICT["target"]["test262"], arg_list[0]): + timeout, arg_list = self.parse_timeout(arg_list) run_aot_mode = len(arg_list) >= 2 and arg_list[1] == "--aot" run_aot_pgo_litecg = len(arg_list) >= 4 and ((arg_list[2] == "--pgo" and arg_list[3] == "--litecg") or (arg_list[3] == "--pgo" and arg_list[2] == "--litecg")) @@ -683,34 +685,66 @@ class ArkPy: run_baseline_jit = len(arg_list) >= 2 and arg_list[1] == "--baseline-jit" if run_aot_mode: if run_aot_pgo_litecg: - self.build_for_test262(out_path, gn_args, arg_list[4:], self.TEST262_LOG_FILE_NAME, True, + self.build_for_test262(out_path, timeout, gn_args, arg_list[4:], self.TEST262_LOG_FILE_NAME, True, True, True) elif run_aot_litecg: - self.build_for_test262(out_path, gn_args, arg_list[3:], self.TEST262_LOG_FILE_NAME, True, + self.build_for_test262(out_path, timeout, gn_args, arg_list[3:], self.TEST262_LOG_FILE_NAME, True, False, True) elif run_aot_pgo: - self.build_for_test262(out_path, gn_args, arg_list[3:], self.TEST262_LOG_FILE_NAME, True, True) + self.build_for_test262(out_path, timeout, gn_args, arg_list[3:], + self.TEST262_LOG_FILE_NAME, True, True) else: - self.build_for_test262(out_path, gn_args, arg_list[2:], self.TEST262_LOG_FILE_NAME, True) + self.build_for_test262(out_path, timeout, gn_args, arg_list[2:], self.TEST262_LOG_FILE_NAME, True) elif run_jit: - self.build_for_test262(out_path, gn_args, arg_list[2:], self.TEST262_LOG_FILE_NAME, False, False, + self.build_for_test262(out_path, timeout, gn_args, arg_list[2:], + self.TEST262_LOG_FILE_NAME, False, False, False, True) elif run_baseline_jit: - self.build_for_test262(out_path, gn_args, arg_list[2:], self.TEST262_LOG_FILE_NAME, False, False, + self.build_for_test262(out_path, timeout, gn_args, arg_list[2:], + self.TEST262_LOG_FILE_NAME, False, False, False, False, True) else: - self.build_for_test262(out_path, gn_args, arg_list[1:], self.TEST262_LOG_FILE_NAME, False) + self.build_for_test262(out_path, timeout, gn_args, arg_list[1:], self.TEST262_LOG_FILE_NAME, False) elif self.is_dict_flags_match_arg(self.ARG_DICT["target"]["unittest"], arg_list[0]): if len(arg_list) > 1: print("\033[92m\"unittest\" not support additional arguments.\033[0m\n".format()) sys.exit(0) self.build_for_unittest(out_path, gn_args, self.UNITTEST_LOG_FILE_NAME) elif self.is_dict_flags_match_arg(self.ARG_DICT["target"]["regresstest"], arg_list[0]): - self.build_for_regress_test(out_path, gn_args, arg_list[1:], self.REGRESS_TEST_LOG_FILE_NAME) + timeout = 200000 + if '--timeout' in arg_list: + timeout_index = arg_list.index('--timeout') + if len(arg_list) > timeout_index + 1: + try: + timeout = int(arg_list[timeout_index + 1]) + arg_list = arg_list[:timeout_index] + arg_list[timeout_index + 2:] + except ValueError: + print("Invalid timeout value.") + sys.exit(1) + else: + print("Missing timeout value.") + sys.exit(1) + self.build_for_regress_test(out_path, gn_args, arg_list[1:], self.REGRESS_TEST_LOG_FILE_NAME, timeout) else: self.build_for_gn_target(out_path, gn_args, arg_list, self.GN_TARGET_LOG_FILE_NAME) return + def parse_timeout(self, arg_list): + timeout = 400000 + if '--timeout' in arg_list: + timeout_index = arg_list.index('--timeout') + if len(arg_list) > timeout_index + 1: + try: + timeout = int(arg_list[timeout_index + 1]) + arg_list = arg_list[:timeout_index] + arg_list[timeout_index + 2:] + except ValueError: + print("Invalid timeout value.") + sys.exit(1) + else: + print("Missing timeout value.") + sys.exit(1) + return timeout, arg_list + def match_options(self, arg_list: list, out_path: str) -> [list, list]: arg_list_ret = [] gn_args_ret = [] diff --git a/build/config/BUILDCONFIG.gn b/build/config/BUILDCONFIG.gn index 82596fd2b1b6fa8939520beb29ea8e22c44e3ab4..7be5abe41a6be6b96466417a6e73bfe17d89a207 100644 --- a/build/config/BUILDCONFIG.gn +++ b/build/config/BUILDCONFIG.gn @@ -18,6 +18,7 @@ declare_args() { so_dir_for_qemu = "common/common/libc" enable_lto_O0 = false disable_force_gc = false + timeout = 1200 } check_mac_system_and_cpu_script = diff --git a/test/test_helper.gni b/test/test_helper.gni index 6292a4f53ac19ae5a85af7b87a0ba09d4dc557fe..f1f988434c731e846034694e8d5686d7032d44e0 100644 --- a/test/test_helper.gni +++ b/test/test_helper.gni @@ -20,7 +20,11 @@ if (is_standard_system || ark_standalone_build) { } else { _icu_path_ = "global/i18n" } - +if (defined(timeout)) { + _timeout_ = timeout +} else { + _timeout_ = 1200 +} template("host_unittest_action") { _target_name_ = "${target_name}" @@ -65,7 +69,7 @@ template("host_unittest_action") { rebase_path(_root_out_dir_) + "/hiviewdfx/hilog:" + rebase_path("//prebuilts/clang/ohos/linux-x86_64/llvm/lib/"), "--timeout-limit", - "1200", + "${_timeout_}", ] inputs = [ @@ -107,7 +111,7 @@ template("host_unittest_action") { "--qemu-ld-prefix", rebase_path(musl_linker_so_out_dir, root_build_dir), "--timeout-limit", - "1200", + "${_timeout_}", ] inputs = [ diff --git a/tooling/agent/debugger_impl.cpp b/tooling/agent/debugger_impl.cpp index fb5f68fa3b8d5902d06b688a894bc96b60f1bcd7..e8d2bba63d8e8b87d63c02a03e7d6dbb655d6bca 100644 --- a/tooling/agent/debugger_impl.cpp +++ b/tooling/agent/debugger_impl.cpp @@ -81,6 +81,9 @@ bool DebuggerImpl::NotifyScriptParsed(ScriptId scriptId, const std::string &file if (!vm_->GetJsDebuggerManager()->IsDebugApp()) { return false; } + if (!vm_->GetJsDebuggerManager()->IsDebugMode()) { + return false; + } const JSPandaFile *jsPandaFile = JSPandaFileManager::GetInstance()->FindJSPandaFile(fileName.c_str()).get(); if (jsPandaFile == nullptr) { diff --git a/websocket/test/http_decoder_test.cpp b/websocket/test/http_decoder_test.cpp index 655e9d2dbffb01b3daa23b34f3e31bb880b51720..55472dbe4b91ba94248ab5660bf0476ba6791f55 100644 --- a/websocket/test/http_decoder_test.cpp +++ b/websocket/test/http_decoder_test.cpp @@ -38,12 +38,24 @@ public: "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n\r\n"; std::string requestHeaders = std::string(REQUEST_HEADERS); + static constexpr std::string_view ERR_REQUEST_HEADERS = "GEY\r\n"; + std::string errRequestHeaders = std::string(ERR_REQUEST_HEADERS); + + static constexpr std::string_view NO_INFO_REQUEST_HEADERS = "GET\r\n"; + std::string noInfoRequestHeaders = std::string(NO_INFO_REQUEST_HEADERS); + static constexpr std::string_view RESPONSE_HEADERS = "HTTP/1.1 101 Switching Protocols\r\n" "Connection: Upgrade\r\n" "Upgrade: websocket\r\n" "Sec-WebSocket-Accept: AyuTxzyBTJJdViDskomT0Q==\r\n\r\n"; std::string responseHeaders = std::string(RESPONSE_HEADERS); + static constexpr std::string_view ERR_RESPONSE_HEADERS = "HTTB\r\n"; + std::string errResponseHeaders = std::string(ERR_RESPONSE_HEADERS); + + static constexpr std::string_view NO_INFO_RESPONSE_HEADERS = "HTTP\r\n"; + std::string noInfoResponseHeaders = std::string(NO_INFO_RESPONSE_HEADERS); + static constexpr std::string_view EXPECTED_VERSION = "HTTP/1.1"; static constexpr std::string_view EXPECTED_STATUS = "101"; static constexpr std::string_view EXPECTED_CONNECTION = "Upgrade"; @@ -63,6 +75,18 @@ HWTEST_F(HttpDecoderTest, TestRequestDecode, testing::ext::TestSize.Level0) ASSERT_EQ(parsed.secWebSocketKey, EXPECTED_SEC_WEBSOCKET_KEY); } +HWTEST_F(HttpDecoderTest, TestAbnormalRequestDecode, testing::ext::TestSize.Level0) +{ + HttpRequest parsed; + ASSERT_FALSE(HttpRequest::Decode(errRequestHeaders, parsed)); + ASSERT_TRUE(HttpRequest::Decode(noInfoRequestHeaders, parsed)); + + ASSERT_EQ(parsed.version, ""); + ASSERT_EQ(parsed.connection, ""); + ASSERT_EQ(parsed.upgrade, ""); + ASSERT_EQ(parsed.secWebSocketKey, ""); +} + HWTEST_F(HttpDecoderTest, TestResponseDecode, testing::ext::TestSize.Level0) { HttpResponse parsed; @@ -75,4 +99,18 @@ HWTEST_F(HttpDecoderTest, TestResponseDecode, testing::ext::TestSize.Level0) ASSERT_EQ(parsed.upgrade, EXPECTED_UPGRADE); ASSERT_EQ(parsed.secWebSocketAccept, EXPECTED_SEC_WEBSOCKET_KEY); } + +HWTEST_F(HttpDecoderTest, TestAbnormalResponseDecode, testing::ext::TestSize.Level0) +{ + HttpResponse parsed; + ASSERT_FALSE(HttpResponse::Decode(errResponseHeaders, parsed)); + ASSERT_TRUE(HttpResponse::Decode(noInfoResponseHeaders, parsed)); + + ASSERT_EQ(parsed.version, ""); + ASSERT_EQ(parsed.status, ""); + ASSERT_EQ(parsed.connection, ""); + ASSERT_EQ(parsed.upgrade, ""); + ASSERT_EQ(parsed.secWebSocketAccept, ""); +} + } // namespace panda::test diff --git a/websocket/test/websocket_test.cpp b/websocket/test/websocket_test.cpp index 45edab129e180d284fe3ff162e157b20f29fc1f4..6f2df8b32c973cea72e2cb2fe1a36ac75b2eccb9 100644 --- a/websocket/test/websocket_test.cpp +++ b/websocket/test/websocket_test.cpp @@ -125,6 +125,11 @@ HWTEST_F(WebSocketTest, ConnectWebSocketTest, testing::ext::TestSize.Level0) exit(0); } else if (pid > 0) { // mainprocess, handle server connect and recv/send message + auto openCallBack = []() -> void { + GTEST_LOG_(INFO) << "ConnectWebSocketTest connection is open."; + }; + serverSocket.SetOpenConnectionCallback(openCallBack); + ret = serverSocket.AcceptNewConnection(); ASSERT_TRUE(ret); std::string recv = serverSocket.Decode(); @@ -213,4 +218,63 @@ HWTEST_F(WebSocketTest, ReConnectWebSocketTest, testing::ext::TestSize.Level0) } serverSocket.Close(); } + +HWTEST_F(WebSocketTest, ClientAbnormalTest, testing::ext::TestSize.Level0) +{ + WebSocketClient clientSocket; + ASSERT_STREQ(clientSocket.GetSocketStateString().c_str(), "uninited"); + ASSERT_FALSE(clientSocket.ClientSendWSUpgradeReq()); + ASSERT_FALSE(clientSocket.ClientRecvWSUpgradeRsp()); + ASSERT_FALSE(clientSocket.SendReply(HELLO_SERVER)); +} + +HWTEST_F(WebSocketTest, ServerAbnormalTest, testing::ext::TestSize.Level0) +{ + WebSocketServer serverSocket; + // No connection established, the function returns directly. + serverSocket.Close(); + ASSERT_FALSE(serverSocket.AcceptNewConnection()); + +#if defined(OHOS_PLATFORM) + int appPid = getpid(); + ASSERT_TRUE(serverSocket.InitUnixWebSocket(UNIX_DOMAIN_PATH + std::to_string(appPid), 5)); +#else + ASSERT_TRUE(serverSocket.InitTcpWebSocket(TCP_PORT, 5)); +#endif + pid_t pid = fork(); + if (pid == 0) { + WebSocketClient clientSocket; + auto closeCallBack = []() -> void { + GTEST_LOG_(INFO) << "ServerAbnormalTest client connection is closed."; + }; + clientSocket.SetCloseConnectionCallback(closeCallBack); + +#if defined(OHOS_PLATFORM) + ASSERT_TRUE(clientSocket.InitToolchainWebSocketForSockName(UNIX_DOMAIN_PATH + std::to_string(appPid), 5)); + // state is not UNITED, the function returns directly. + ASSERT_TRUE(clientSocket.InitToolchainWebSocketForSockName(UNIX_DOMAIN_PATH + std::to_string(appPid), 5)); +#else + ASSERT_TRUE(clientSocket.InitToolchainWebSocketForPort(TCP_PORT, 5)); + // state is not UNITED, the function returns directly. + ASSERT_TRUE(clientSocket.InitToolchainWebSocketForPort(TCP_PORT, 5)); +#endif + ASSERT_TRUE(clientSocket.ClientSendWSUpgradeReq()); + ASSERT_FALSE(clientSocket.ClientRecvWSUpgradeRsp()); + exit(0); + } else if (pid > 0) { + auto failCallBack = []() -> void { + GTEST_LOG_(INFO) << "ServerAbnormalTest server connection is failed."; + }; + serverSocket.SetFailConnectionCallback(failCallBack); + auto notValidCallBack = [](const HttpRequest&) -> bool { + GTEST_LOG_(INFO) << "ServerAbnormalTest server connection request is not valid."; + return false; + }; + serverSocket.SetValidateConnectionCallback(notValidCallBack); + ASSERT_FALSE(serverSocket.AcceptNewConnection()); + } else { + std::cerr << "ServerAbnormalTest::fork failed, error = " + << errno << ", desc = " << strerror(errno) << std::endl; + } +} } // namespace panda::test \ No newline at end of file