diff --git a/test/BUILD.gn b/test/BUILD.gn index 6588f5dfa7771c375901f70ed4cd55dcd0c7af99..019d748d1704c540f3d28e278da9c2a9fa2b98ce 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -316,6 +316,7 @@ ohos_unittest("hdc_host_base_unittest") { "unittest/daemon/daemon_app_test.cpp", "unittest/common/define_test.cpp", "unittest/host/host_app_test.cpp", + "unittest/host/main_test.cpp", ] configs = [ diff --git a/test/scripts/testModule/test_hdc_base.py b/test/scripts/testModule/test_hdc_base.py index 0eb547114eccb0bda900bf686f5d9636242dbf6d..b80b9454904f42c9e1ba490e8b2c6f332373734a 100644 --- a/test/scripts/testModule/test_hdc_base.py +++ b/test/scripts/testModule/test_hdc_base.py @@ -105,11 +105,11 @@ class TestTargetCommand: @pytest.mark.L0 def test_target_mount(self): assert (check_hdc_cmd("target mount", "Mount finish" or "[Fail]Operate need running as root")) - uname = get_shell_result("shell uname") + is_linux = check_hdc_cmd("shell uname", "Linux") sep = "/" remount_vendor = get_shell_result(f'shell "mount |grep {sep}vendor |head -1"') print(remount_vendor) - if uname == "Linux": + if is_linux: assert "rw" in remount_vendor else: assert "ro" in remount_vendor diff --git a/test/scripts/testModule/test_hdc_return_value_check.py b/test/scripts/testModule/test_hdc_return_value_check.py index a10d345a4936a73198b7cd5c12d3b31790686c2a..4b291aa60735807740263ced9b4ecfd5aa8cbdbc 100644 --- a/test/scripts/testModule/test_hdc_return_value_check.py +++ b/test/scripts/testModule/test_hdc_return_value_check.py @@ -182,7 +182,7 @@ class TestHdcReturnValue: def test_hdc_file(self): run_command_with_timeout(f"{GP.hdc_head} wait", 20) result = get_shell_result(f"file send {get_local_path('small')} {get_remote_path('test')}") - result = re.sub("[\r\n]", "", result) + result = result.replace(get_end_symbol(), "") pattern = r'FileTransfer finish, Size:(\d+), File count = (\d+), time:(\d+)ms rate:(\d+.\d+)kB/s' match_send = re.match(pattern, result) assert match_send is not None @@ -192,7 +192,7 @@ class TestHdcReturnValue: "[Fail]Error opening file: read-only file system, path:abc") result = get_shell_result(f"file send {get_local_path('small_no_exsit')} {get_remote_path('test')}") - result = re.split("\r|\n", result) + result = result.split(get_end_symbol()) pattern = r'\[Fail\]Error opening file: no such file or directory, path:(.*)small_no_exsit' match = re.match(pattern, result[0]) assert match is not None @@ -209,7 +209,7 @@ class TestHdcReturnValue: check_shell(f"smode -r") run_command_with_timeout(f"{GP.hdc_head} wait", 20) result = get_shell_result(f"file send {get_local_path('small')} /system/lib/") - result = result.replace("\r\n", "") + result = result.replace(get_end_symbol(), "") result = result.replace("\r", "") assert (result == "[Fail]Error opening file: permission denied, path:/system/lib/small" or result == "[Fail]Error opening file: read-only file system, path:/system/lib/small") @@ -304,7 +304,7 @@ class TestHdcReturnValue: check_shell(f"smode") run_command_with_timeout(f"{GP.hdc_head} wait", 20) result = get_shell_result(f"shell ls") - result = result.split("\r\n") + result = result.split(get_end_symbol()) assert len(result) > 5 result = get_cmd_block_output(f"{GP.hdc_head} shell", 10) @@ -324,7 +324,7 @@ class TestHdcReturnValue: while True: if check_shell(f"fport tcp:{12345 + n} tcp:7777", "Forwardport result:OK"): assert check_shell(f"tconn 127.0.0.1:{12345 + n}", "Connect OK") - assert check_shell(f"list targets", "127.0.0.1:{12345 + n}") + assert check_shell(f"list targets", f"127.0.0.1:{12345 + n}") assert check_shell(f"kill") break else: diff --git a/test/scripts/testModule/test_hdc_session_and_channel_lifecycle.py b/test/scripts/testModule/test_hdc_session_and_channel_lifecycle.py index 890b012c11d2f2950e149323bbded60bc288bb51..4a9a2e8a707f6d9a9a4338565208f703f56002c0 100644 --- a/test/scripts/testModule/test_hdc_session_and_channel_lifecycle.py +++ b/test/scripts/testModule/test_hdc_session_and_channel_lifecycle.py @@ -17,8 +17,9 @@ import time import logging import multiprocessing import pytest +import tempfile -from utils import GP, check_shell, run_command_with_timeout, get_cmd_block_output, get_shell_result +from utils import GP, check_shell, run_command_with_timeout, get_cmd_block_output, get_shell_result, get_end_symbol logger = logging.getLogger(__name__) @@ -47,7 +48,7 @@ class TestSessionLifeCycle: run_command_with_timeout(f"{GP.hdc_head} wait", 20) time.sleep(3) - devices = get_shell_result(f"list targets").split("\r\n") + devices = get_shell_result(f"list targets").split(get_end_symbol()) target_lines = list() for item in devices: if len(item) > 0: @@ -56,15 +57,12 @@ class TestSessionLifeCycle: target_lines.append(line) line = f"connectKey:{connect_key} connType:0 connect state:0 faultInfo:LIBUSB_TRANSFER_" target_lines.append(line) - # # HdcServer [ sessionId:3315043252 connect_key:012******DEF(L:16) connType:0 connect state:1 faultInfo: ] - # # HdcServer [ sessionId:4181956210 connect_key:012******DEF(L:16) connType:0 connect state:0 faultInfo:LIBUSB_TRANSFER_ ] - # time.sleep(3) check_shell(f"shell reboot") time.sleep(5) check_shell(f"kill") # 检查serverOutput是否包含上述字符串 - tmp_path = os.getenv('TMP') + tmp_path = tempfile.gettempdir() hdc_log_path = f"{tmp_path}{os.sep}hdc.log" with open(hdc_log_path, 'r') as file: for line in file.readlines(): @@ -90,7 +88,7 @@ class TestSessionLifeCycle: time.sleep(3) for i in range(4): assert check_shell(f"tconn 127.0.0.1:{fport_tcp_host_port + i}", "Connect OK") - devices = get_shell_result(f"list targets").split("\r\n") + devices = get_shell_result(f"list targets").split(get_end_symbol()) target_lines = list() for item in devices: if len(item) > 0 and item.find(".") >= 0: @@ -116,7 +114,7 @@ class TestSessionLifeCycle: time.sleep(5) check_shell(f"kill") # 检查serverOutput是否包含上述字符串 - tmp_path = os.getenv('TMP') + tmp_path = tempfile.gettempdir() hdc_log_path = f"{tmp_path}{os.sep}hdc.log" with open(hdc_log_path, 'r') as file: for line in file.readlines(): diff --git a/test/scripts/testModule/test_hdc_shell.py b/test/scripts/testModule/test_hdc_shell.py index 1cdf903db770d2dfb681dd85e913ce5f17b88e00..24ca40eb9860dc17a5700d798d6ebd21e4f1e634 100644 --- a/test/scripts/testModule/test_hdc_shell.py +++ b/test/scripts/testModule/test_hdc_shell.py @@ -50,7 +50,7 @@ class TestShellHilog: hilog_pid = get_shell_result(f'shell pidof hilog') hilog_pid = hilog_pid.replace(get_end_symbol(), "") assert hilog_pid.isdigit() - assert check_hdc_cmd(f'kill', "Kill server finish") + assert check_hdc_cmd(f'-l5 kill', "Kill server finish") assert check_hdc_cmd("start") time.sleep(3) # sleep 3s to wait for the device to connect channel run_command_with_timeout(f"{GP.hdc_head} wait", 3) # wait 3s for the device to connect channel diff --git a/test/scripts/testModule/test_hdc_track_jpid.py b/test/scripts/testModule/test_hdc_track_jpid.py index 3499b6651d7d02c453d08c946a4c8a3badaf4359..258d7d41c4f73f89031a5c4b115cbce8439ac410 100644 --- a/test/scripts/testModule/test_hdc_track_jpid.py +++ b/test/scripts/testModule/test_hdc_track_jpid.py @@ -14,7 +14,7 @@ # limitations under the License. import pytest from utils import GP, check_app_install, check_app_uninstall, check_cmd_block, \ - check_hdc_cmd, check_shell, get_shell_result, load_gp + check_hdc_cmd, check_shell, get_shell_result, load_gp, get_end_symbol class TestHdcTrackJpid: @@ -33,12 +33,12 @@ class TestHdcTrackJpid: @pytest.mark.L0 def test_hdc_jpid(self): - pidstr = get_shell_result(f'shell "pidof {self.bundle_name}"').split("\r")[0] + pidstr = get_shell_result(f'shell "pidof {self.bundle_name}"').split(get_end_symbol())[0] assert check_hdc_cmd(f"jpid", pidstr) @pytest.mark.L0 def test_hdc_track_jpid(self): - pidstr = get_shell_result(f'shell "pidof {self.bundle_name}"').split("\r")[0] + pidstr = get_shell_result(f'shell "pidof {self.bundle_name}"').split(get_end_symbol())[0] track_cmd_p = f"{GP.hdc_exe} -t {GP.device_name} track-jpid -p" pattern_p = f"{pidstr} {self.bundle_name}" assert check_cmd_block(track_cmd_p, pattern_p, timeout=2) diff --git a/test/scripts/testModule/utils.py b/test/scripts/testModule/utils.py index 79f31cecc7106e540c83b22344ad9d2a00760768..636b3cc9b2d0292cd141b6b236f5d0618b6e4cf7 100644 --- a/test/scripts/testModule/utils.py +++ b/test/scripts/testModule/utils.py @@ -884,7 +884,7 @@ def check_hdc_version(cmd, version): def _convert_version_to_hex(_version): parts = _version.split("Ver: ")[1].split('.') hex_version = ''.join(parts) - return int(hex_version, 16) + return int(hex_version, 36) expected_version = _convert_version_to_hex(version) cmd = f"{GP.hdc_head} {cmd}" diff --git a/test/unittest/host/include/main_test.h b/test/unittest/host/include/main_test.h new file mode 100644 index 0000000000000000000000000000000000000000..0b142c3d9e43f5c20a0f48dc1444a7f79421fbf5 --- /dev/null +++ b/test/unittest/host/include/main_test.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * 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. + */ +#ifndef HDC_MAIN_TEST_H +#define HDC_MAIN_TEST_H +#include +namespace Hdc { +class HdcMainTest : public ::testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + bool TestIsHishellLabel(pid_t pid); +}; +} // namespace Hdc +#endif // HDC_MAIN_TEST_H \ No newline at end of file diff --git a/test/unittest/host/main_test.cpp b/test/unittest/host/main_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ccbc38be91f9e040fdd392b1441769827bac8054 --- /dev/null +++ b/test/unittest/host/main_test.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * 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. + */ +#ifdef __OHOS__ +#include +#include +#endif +#include "base.h" +#include "main_test.h" + +using namespace testing::ext; + +namespace Hdc { +void HdcMainTest::SetUpTestCase() {} +void HdcMainTest::TearDownTestCase() {} +void HdcMainTest::SetUp() +{ +#ifdef UT_DEBUG + Base::SetLogLevel(LOG_ALL); +#else + Base::SetLogLevel(LOG_OFF); +#endif +} +void HdcMainTest::TearDown() {} + +bool HdcMainTest::TestIsHishellLabel(pid_t pid) +{ + if (pid == 0) { + return false; + } + // static path + std::stringstream ss; + ss << "/proc/" << pid << "/attr/current"; + std::string cmd = ss.str(); + const char *pathBuf = cmd.c_str(); + const char* attrName = "security.selinux"; + // get attribute size + ssize_t attrSize = getxattr(pathBuf, attrName, nullptr, 0); + if (attrSize == 0 || attrSize == - 1) { + return false; + } + char *attrValue = new(std::nothrow) char[attrSize]; + if (attrValue == nullptr) { + return false; + } + // get attribute value + if (getxattr(pathBuf, attrName, attrValue, attrSize) == -1) { + delete []attrValue; + return false; + } + string label(attrValue, attrSize - 1); + delete []attrValue; + return label == "u:r:hishell_hap:s0"; +} + +HWTEST_F(HdcMainTest, TestIsHishell_1, TestSize.Level0) +{ + EXPECT_FALSE(TestIsHishellLabel(0)); +} + +HWTEST_F(HdcMainTest, TestIsHishell_2, TestSize.Level0) +{ + pid_t pid = fork(); + ASSERT_GT(pid, 0); + if (pid == 0) { + pause(); + } else { + // parent process + kill(pid, SIGUSR1); + EXPECT_TRUE(TestIsHishellLabel(pid)); + } +} + +HWTEST_F(HdcMainTest, TestIsHishell_3, TestSize.Level0) +{ + pid_t pid = fork(); + ASSERT_GT(pid, 0); + if (pid == 0) { + pause(); + } else { + // parent process + kill(pid, SIGUSR1); + EXPECT_TRUE(TestIsHishellLabel(pid)); + } +} + +HWTEST_F(HdcMainTest, TestIsHishell_4, TestSize.Level0) +{ + pid_t pid = fork(); + ASSERT_GT(pid, 0); + if (pid == 0) { + pause(); + } else { + // parent process + kill(pid, SIGUSR1); + EXPECT_TRUE(TestIsHishellLabel(pid)); + } +} +} \ No newline at end of file