1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (C) 2025 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15import os 16import time 17import multiprocessing 18import pytest 19 20from utils import GP, check_hdc_cmd, check_shell, check_version, get_shell_result, run_command_with_timeout, load_gp 21 22 23class TestShellHilog: 24 #子进程执行函数 25 def new_process_run(self, cmd): 26 # 重定向 stdout 和 stderr 到 /dev/null 27 with open(os.devnull, 'w') as devnull: 28 old_stdout = os.dup2(devnull.fileno(), 1) # 重定向 stdout 29 old_stderr = os.dup2(devnull.fileno(), 2) # 重定向 stderr 30 try: 31 # 这里是子进程的代码,不会有任何输出到控制台 32 check_shell(f'{cmd}') 33 finally: 34 # 恢复原始的 stdout 和 stderr 35 os.dup2(old_stdout, 1) 36 os.dup2(old_stderr, 2) 37 38 @pytest.mark.L0 39 def test_hilog_exit_after_hdc_kill(self): 40 # 新开进程执行hdc shell hilog,防止阻塞主进程 41 p = multiprocessing.Process(target=self.new_process_run, args=("shell hilog",)) 42 p.start() 43 time.sleep(1) 44 hilog_pid = get_shell_result(f'shell pidof hilog') 45 hilog_pid = hilog_pid.replace("\r\n", "") 46 assert hilog_pid.isdigit() 47 assert check_hdc_cmd(f'kill', "Kill server finish") 48 assert check_hdc_cmd("start") 49 time.sleep(3) # sleep 3s to wait for the device to connect channel 50 run_command_with_timeout(f"{GP.hdc_head} wait", 3) # wait 3s for the device to connect channel 51 hilog_pid2 = get_shell_result(f'shell pidof hilog') 52 assert hilog_pid2 == '' 53 p.join() 54 55 56class TestShellBundleOption: 57 a_long = "a" * 129 58 a_short = "a" * 8 59 data_storage_el2_path = "data/storage/el2/base" 60 test_bundle_fail_data = [ 61 ("bundle name unknown", "-b ""com.XXXX.not.exist.app", "pwd", "[Fail][E003001]"), 62 ("bundle name with path ../", "-b ""../../../../", "pwd", "[Fail][E003001]"), 63 ("bundle name with path ./", "-b ""././././pwd", "pwd", "[Fail][E003001]"), 64 ("bundle name with path /", "-b ""/", "pwd", "[Fail][E003001]"), 65 ("bundle name without command", "-b "f"{GP.debug_app}", "", "[Fail][E003002]"), 66 ("bundle name too long: length > 128", f"-b {a_long}", "pwd", "[Fail][E003001]"), 67 ("bundle name too short: length < 9", f"-b {a_short}", "pwd", "[Fail][E003001]"), 68 ("bundle name with unsupport symbol: #", "-b #########", "pwd", "[Fail][E003001]"), 69 ("option: -param", "-param 1234567890", "pwd", "[Fail][E003003]"), 70 ("option: -basd", "-basd {GP.debug_app}", "pwd", "[Fail][E003003]"), 71 ("parameter missing", "- {GP.debug_app}", "ls", "[Fail][E003003]"), 72 ("bundle name missing", "-b", "ls", "[Fail][E003001]"), 73 ("bundle name & command missing", "-b", "", "[Fail][E003005]"), 74 ("option: -t -b", "-t -b {GP.debug_app}", "ls", "[Fail][E003003]"), 75 ("command with similar parameter: ls -b", "ls -b {GP.debug_app}", "", "No such file or directory"), 76 ("option: -b -b", "-b -b {GP.debug_app}", "ls", "[Fail][E003001]"), 77 ("option: --b", "--b {GP.debug_app}", "", "[Fail][E003003]"), 78 ] 79 80 test_bundle_normal_data = [ 81 ("mkdir", f"shell mkdir -p mnt/debug/100/debug_hap/{GP.debug_app}/{data_storage_el2_path}", None, True), 82 ("pwd", f"shell -b {GP.debug_app} pwd", f"mnt/debug/100/debug_hap/{GP.debug_app}", True), 83 ("cd_pwd", f"shell -b {GP.debug_app} cd {data_storage_el2_path}; pwd", 84 f"mnt/debug/100/debug_hap/{GP.debug_app}/{data_storage_el2_path}", True), 85 ("touch", f"shell -b {GP.debug_app} touch {data_storage_el2_path}/test01", None, True), 86 ("touch_denied", f"shell -b {GP.debug_app} touch {data_storage_el2_path}/test01", "denied", False), 87 ("touch_a_denied", f"shell -b {GP.debug_app} touch -a {data_storage_el2_path}/test01", "denied", False), 88 ("ls_test01", f"shell -b {GP.debug_app} ls {data_storage_el2_path}/", "test01", True), 89 ("echo_123", f"shell -b {GP.debug_app} echo 123", "123", True), 90 ("echo_to_test02", f"shell -b {GP.debug_app} \"echo 123 > {data_storage_el2_path}/test02\"", None, True), 91 ("cat_test02", f"shell -b {GP.debug_app} cat {data_storage_el2_path}/test02", "123", True), 92 ("mkdir_test03", f"shell -b {GP.debug_app} mkdir {data_storage_el2_path}/test03", None, True), 93 ("stat_test03", f"shell -b {GP.debug_app} stat {data_storage_el2_path}/test03", "Access", True), 94 ("rm_rf", f"shell -b {GP.debug_app} rm -rf {data_storage_el2_path}/test01 " 95 f"{data_storage_el2_path}/test02 {data_storage_el2_path}/test03", None, True), 96 ("ls_test01_not_exist", f"shell -b {GP.debug_app} ls {data_storage_el2_path}/test01", 97 "test01: No such file or directory", True), 98 ("ls_test02_not_exist", f"shell -b {GP.debug_app} ls {data_storage_el2_path}/test02", 99 "test02: No such file or directory", True), 100 ("ls_test03_not_exist", f"shell -b {GP.debug_app} ls {data_storage_el2_path}/test03", 101 "test03: No such file or directory", True), 102 ] 103 104 @classmethod 105 def setup_class(self): 106 data_storage_el2_path = "data/storage/el2/base" 107 check_shell(f"shell mkdir -p mnt/debug/100/debug_hap/{GP.debug_app}/{data_storage_el2_path}") 108 check_shell(f"shell rm -rf -p mnt/debug/100/debug_hap/{GP.debug_app}/{data_storage_el2_path}/it_*") 109 110 @pytest.mark.L0 111 @check_version("Ver: 3.1.0e") 112 @pytest.mark.parametrize("test_name, bundle_option, command, expected_output", test_bundle_fail_data, 113 ids=[name for name, _, _, _ in test_bundle_fail_data]) 114 def test_bundle_option_error(self, test_name, bundle_option, command, expected_output): 115 test_command = f"shell {bundle_option} {command}" 116 assert check_shell(test_command, expected_output) 117 118 @pytest.mark.L0 119 @check_version("Ver: 3.1.0e") 120 @pytest.mark.parametrize("test_name, command, expected_output, assert_bool", test_bundle_normal_data, 121 ids=[name for name, _, _, _ in test_bundle_normal_data]) 122 def test_shell_option_bundle_normal(self, test_name, command, expected_output, assert_bool): 123 if assert_bool: 124 assert check_shell(f"{command}", expected_output) 125 else: 126 assert not check_shell(f"{command}", expected_output)