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 re 16import subprocess 17import time 18import logging 19import threading 20import pytest 21 22from utils import GP, check_shell, run_command_with_timeout, get_cmd_block_output, get_end_symbol, get_shell_result, load_gp 23from enum import Enum 24 25 26class OsValue(Enum): 27 UNKNOWN = 1 28 OHOS = 2 29 OTHER = 3 30 31 32logger = logging.getLogger(__name__) 33lock = threading.Lock() 34 35 36class TestCommonSupport: 37 os_value = OsValue.UNKNOWN 38 39 @staticmethod 40 def check_track_jpid(): 41 result = get_cmd_block_output("hdc track-jpid -a", timeout=2) 42 result = result.split('\n') 43 content_size = 0 # 所有表示长度的加起来 44 first_line_size = 0 # 所有表示长度的内容长度之和 45 size = 0 46 for line in result: 47 size = size + len(line) + 1 48 49 if " " in line: # 数据行 50 print("data row, line size all size ", content_size, size) 51 elif len(line) == 0: # 最后一行 52 size = size - 1 53 print("last row", size) 54 else: 55 first_line_size = first_line_size + len(line) + 1 56 line = line.replace(get_end_symbol(), "") 57 line = line.replace("\n", "") 58 content_size = content_size + int(line, 16) 59 60 return size == first_line_size + content_size 61 62 @staticmethod 63 def kill_process(pid): 64 cmd = f"taskkill /F /PID {pid}" 65 subprocess.Popen(cmd, shell=False) 66 67 def is_ohos(self): # 此项依赖toybox和实际的OS 68 with lock: 69 if self.os_value != OsValue.UNKNOWN: 70 return self.os_value == OsValue.OHOS 71 if check_shell(f"shell uname -a", "HongMeng Kernel"): 72 self.os_value = OsValue.OHOS 73 else: 74 self.os_value = OsValue.OTHER 75 return self.os_value == OsValue.OHOS 76 77 @pytest.mark.L0 78 @pytest.mark.repeat(2) 79 def test_target_key(self): 80 device_key = re.split("\r|\n", get_shell_result(f"list targets"))[0] 81 hdcd_pid = re.split("\r|\n", get_shell_result(f"-t {device_key} shell pgrep -x hdcd"))[0] 82 assert hdcd_pid.isdigit() 83 84 @pytest.mark.L0 85 @pytest.mark.repeat(2) 86 def test_check_ide_sdk_compatible(self): 87 output_str, error_str = run_command_with_timeout(f"{GP.hdc_head} checkserver", 2) 88 if output_str: # Client version:Ver: 3.1.0e, server version:Ver: 3.1.0e 89 output_str = output_str.replace(get_end_symbol(), "") 90 logging.warning(output_str) 91 versions = output_str.split(",") 92 if len(versions) == 2: # Client version:Ver: 3.1.0e server version:Ver: 3.1.0e 93 client_strs = versions[0].split(":") 94 server_strs = versions[1].split(":") 95 if len(client_strs) == 3 and len(server_strs) == 3: 96 logging.warning(client_strs[2]) 97 logging.warning(server_strs[2]) 98 assert client_strs[2] == server_strs[2] # 3.1.0e 99 return 100 assert False 101 102 @pytest.mark.L0 103 @pytest.mark.repeat(2) 104 def test_check_sdk_support_bundle_file(self): 105 client_version = get_shell_result(f"version") 106 assert client_version >= "Ver: 3.1.0e" 107 108 @pytest.mark.L0 109 @pytest.mark.repeat(2) 110 def test_check_daemon_support_bundle_file(self): 111 daemon_version = get_shell_result(f"shell param get const.hdc.version") 112 assert daemon_version >= "Ver: 3.1.0e" 113 114 @pytest.mark.L0 115 @pytest.mark.repeat(2) 116 def test_check_software_version(self): 117 software_version = get_shell_result(f"shell param get const.product.software.version") 118 pattern = r'[A-Za-z0-9-]+ \d+.\d+.\d+.\d+([A-Za-z0-9]+)' 119 match = re.search(pattern, software_version) 120 assert match is not None 121 122 @pytest.mark.L0 123 @pytest.mark.repeat(2) 124 def test_check_is_hongmeng_os(self): 125 if not self.is_ohos(): 126 assert True 127 return 128 assert check_shell(f"shell uname -a", "HongMeng Kernel") 129 130 @pytest.mark.L0 131 @pytest.mark.repeat(2) 132 def test_check_support_ftp(self): 133 if not self.is_ohos(): 134 assert True 135 return 136 assert not check_shell(f"shell ls -d system/bin/bftpd", "No such file or directory") 137 138 @pytest.mark.L0 139 @pytest.mark.repeat(2) 140 def test_check_hiview_service_started(self): 141 assert check_shell(f"shell hidumper -s 1201 -a '-p Faultlogger'", "HiviewService") 142 143 @pytest.mark.L0 144 @pytest.mark.repeat(2) 145 def test_check_bm_dump_support_options(self): 146 assert not check_shell(f"shell bm dump -a", "error: unknown option") 147 148 @pytest.mark.L0 149 @pytest.mark.repeat(2) 150 def test_check_support_tsan(self): 151 if not self.is_ohos(): 152 assert True 153 return 154 assert not check_shell(f"shell ls -d system/lib64/libclang_rt.tsan.so", "No such file or directory") 155 156 @pytest.mark.L0 157 @pytest.mark.repeat(2) 158 def test_check_support_faultlog_with_ms(self): 159 output = get_shell_result(f"shell hidumper -s 1201 -a '-p Faultlogger -LogSuffixWithMs'") 160 assert "HiviewService" in output 161 assert "Unknown command" not in output 162 163 @pytest.mark.L0 164 @pytest.mark.repeat(2) 165 def test_check_screenshot_with_png_format(self): 166 assert check_shell(f"shell snapshot_display -f data/local/tmp/example.png -t png", "success") 167 168 @pytest.mark.L0 169 @pytest.mark.repeat(2) 170 def test_check_hilog_not_support_options(self): 171 assert check_shell(f"shell hilog -v xxx", "Invalid argument") 172 173 @pytest.mark.L0 174 @pytest.mark.repeat(2) 175 def test_check_track_jpid(self): 176 assert self.check_track_jpid() 177