1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (C) 2024 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. 15# 运行环境: python 3.10+, pytest, pytest-repeat, pytest-testreport 16# 准备文件:package.zip 17# pip install pytest pytest-testreport pytest-repeat 18# python hdc_normal_test.py 19 20 21import argparse 22import time 23import os 24 25import pytest 26 27from dev_hdc_test import GP 28from dev_hdc_test import check_library_installation, check_hdc_version, check_cmd_time 29from dev_hdc_test import check_hdc_cmd, check_hdc_targets, get_local_path, get_remote_path, run_command_with_timeout 30from dev_hdc_test import check_app_install, check_app_uninstall, prepare_source, pytest_run, update_source, check_rate 31from dev_hdc_test import make_multiprocess_file, rmdir, get_shell_result 32from dev_hdc_test import check_app_install_multi, check_app_uninstall_multi 33 34 35def test_list_targets(): 36 assert check_hdc_targets() 37 assert check_hdc_cmd("shell rm -rf data/local/tmp/it_*") 38 39 40@pytest.mark.repeat(5) 41def test_empty_file(): 42 assert check_hdc_cmd(f"file send {get_local_path('empty')} {get_remote_path('it_empty')}") 43 assert check_hdc_cmd(f"file recv {get_remote_path('it_empty')} {get_local_path('empty_recv')}") 44 45 46@pytest.mark.repeat(5) 47def test_empty_dir(): 48 assert check_shell(f"file send {get_local_path('empty_dir')} {get_remote_path('it_empty_dir')}", 49 "the source folder is empty") 50 assert check_hdc_cmd("shell mkdir data/local/tmp/it_empty_dir_recv") 51 assert check_shell(f"file recv {get_remote_path('it_empty_dir_recv')} {get_local_path('empty_dir_recv')}", 52 "the source folder is empty") 53 54 55@pytest.mark.repeat(5) 56def test_long_path(): 57 assert check_hdc_cmd(f"file send {get_local_path('deep_test_dir')} {get_remote_path('it_send_dir')}") 58 assert check_hdc_cmd(f"file recv {get_remote_path('it_send_dir/deep_test_dir')} {get_local_path('recv_test_dir')}") 59 60 61@pytest.mark.repeat(5) 62def test_small_file(): 63 assert check_hdc_cmd(f"file send {get_local_path('small')} {get_remote_path('it_small')}") 64 assert check_hdc_cmd(f"file recv {get_remote_path('it_small')} {get_local_path('small_recv')}") 65 66 67@pytest.mark.repeat(1) 68def test_node_file(): 69 assert check_hdc_cmd(f"file recv {get_remote_path('../../../sys/power/state')} {get_local_path('state')}") 70 assert check_hdc_cmd(f"file recv {get_remote_path('../../../sys/firmware/fdt')} {get_local_path('fdt')}") 71 assert check_hdc_cmd(f"file recv {get_remote_path('../../../proc/cpuinfo')} {get_local_path('cpuinfo')}") 72 73 74@pytest.mark.repeat(1) 75def test_medium_file(): 76 assert check_hdc_cmd(f"file send {get_local_path('medium')} {get_remote_path('it_medium')}") 77 assert check_hdc_cmd(f"file recv {get_remote_path('it_medium')} {get_local_path('medium_recv')}") 78 79 80@pytest.mark.repeat(1) 81def test_large_file(): 82 assert check_hdc_cmd(f"file send {get_local_path('large')} {get_remote_path('it_large')}") 83 assert check_hdc_cmd(f"file recv {get_remote_path('it_large')} {get_local_path('large_recv')}") 84 85 86@pytest.mark.repeat(1) 87def test_running_file(): 88 assert check_hdc_cmd(f"file recv /system/bin/hdcd {get_local_path('running_recv')}") 89 90 91@pytest.mark.repeat(1) 92def test_rate(): 93 assert check_rate(f"file send {get_local_path('large')} {get_remote_path('it_large')}", 38000) 94 assert check_rate(f"file recv {get_remote_path('it_large')} {get_local_path('large_recv')}", 38000) 95 96 97@pytest.mark.repeat(1) 98def test_file_error(): 99 assert check_hdc_cmd("target mount") 100 assert check_shell( 101 f"file send {get_local_path('small')} system/bin/hdcd", 102 "busy" 103 ) 104 assert check_shell( 105 f"file recv", 106 "[Fail]There is no local and remote path" 107 ) 108 assert check_shell( 109 f"file send", 110 "[Fail]There is no local and remote path" 111 ) 112 assert check_shell( 113 f"file send {get_local_path('large')} {get_remote_path('../../../')}", 114 "space left on device" 115 ) 116 assert check_hdc_cmd(f"shell rm -rf {get_remote_path('../../../large')}") 117 assert check_hdc_cmd(f"shell param set persist.hdc.control.file false") 118 assert check_shell( 119 f"file send {get_local_path('small')} {get_remote_path('it_small_false')}", 120 "debugging is not allowed" 121 ) 122 assert check_hdc_cmd(f"shell param set persist.hdc.control.file true") 123 assert check_hdc_cmd(f"file send {get_local_path('small')} {get_remote_path('it_small_true')}") 124 125 126@pytest.mark.repeat(5) 127def test_recv_dir(): 128 if os.path.exists(get_local_path('it_problem_dir')): 129 rmdir(get_local_path('it_problem_dir')) 130 assert check_hdc_cmd(f"shell rm -rf {get_remote_path('it_problem_dir')}") 131 assert check_hdc_cmd(f"shell rm -rf {get_remote_path('problem_dir')}") 132 assert make_multiprocess_file(get_local_path('problem_dir'), get_remote_path(''), 'send', 1, "dir") 133 assert check_hdc_cmd(f"shell mv {get_remote_path('problem_dir')} {get_remote_path('it_problem_dir')}") 134 assert make_multiprocess_file(get_local_path(''), get_remote_path('it_problem_dir'), 'recv', 1, "dir") 135 136 137@pytest.mark.repeat(5) 138def test_hap_install(): 139 assert check_hdc_cmd(f"install -r {get_local_path('entry-default-signed-debug.hap')}", 140 bundle="com.hmos.diagnosis") 141 142 143@pytest.mark.repeat(5) 144def test_install_hap(): 145 package_hap = "entry-default-signed-debug.hap" 146 app_name_default = "com.hmos.diagnosis" 147 148 # default 149 assert check_app_install(package_hap, app_name_default) 150 assert check_app_uninstall(app_name_default) 151 152 # -r 153 assert check_app_install(package_hap, app_name_default, "-r") 154 assert check_app_uninstall(app_name_default) 155 156 # -k 157 assert check_app_install(package_hap, app_name_default, "-r") 158 assert check_app_uninstall(app_name_default, "-k") 159 160 # -s 161 assert check_app_install(package_hap, app_name_default, "-s") 162 163 164@pytest.mark.repeat(5) 165def test_install_hsp(): 166 package_hsp = "libA_v10001.hsp" 167 hsp_name_default = "com.example.liba" 168 169 assert check_app_install(package_hsp, hsp_name_default, "-s") 170 assert check_app_uninstall(hsp_name_default, "-s") 171 assert check_app_install(package_hsp, hsp_name_default) 172 173 174@pytest.mark.repeat(5) 175def test_install_multi_hap(): 176 # default multi hap 177 tables = { 178 "entry-default-signed-debug.hap" : "com.hmos.diagnosis", 179 "ActsAudioRecorderJsTest.hap" : "ohos.acts.multimedia.audio.audiorecorder" 180 } 181 assert check_app_install_multi(tables) 182 assert check_app_uninstall_multi(tables) 183 assert check_app_install_multi(tables, "-s") 184 185 # default multi hap -r -k 186 tables = { 187 "entry-default-signed-debug.hap" : "com.hmos.diagnosis", 188 "ActsAudioRecorderJsTest.hap" : "ohos.acts.multimedia.audio.audiorecorder" 189 } 190 assert check_app_install_multi(tables, "-r") 191 assert check_app_uninstall_multi(tables, "-k") 192 193 194@pytest.mark.repeat(5) 195def test_install_multi_hsp(): 196 # default multi hsp -s 197 tables = { 198 "libA_v10001.hsp" : "com.example.liba", 199 "libB_v10001.hsp" : "com.example.libb", 200 } 201 assert check_app_install_multi(tables, "-s") 202 assert check_app_uninstall_multi(tables, "-s") 203 assert check_app_install_multi(tables) 204 205 206@pytest.mark.repeat(5) 207def test_install_hsp_and_hap(): 208 #default multi hsp and hsp 209 tables = { 210 "libA_v10001.hsp" : "com.example.liba", 211 "entry-default-signed-debug.hap" : "com.hmos.diagnosis", 212 } 213 assert check_app_install_multi(tables) 214 assert check_app_install_multi(tables, "-s") 215 216 217@pytest.mark.repeat(5) 218def test_install_dir(): 219 package_haps_dir = "app_dir" 220 app_name_default = "com.hmos.diagnosis" 221 assert check_app_install(package_haps_dir, app_name_default) 222 assert check_app_uninstall(app_name_default) 223 224 225def test_server_kill(): 226 assert check_hdc_cmd("kill", "Kill server finish") 227 assert check_hdc_cmd("start server", "") 228 229 230def test_target_cmd(): 231 assert check_hdc_targets() 232 time.sleep(3) 233 check_hdc_cmd("target boot") 234 start_time = time.time() 235 run_command_with_timeout("hdc wait", 60) # reboot takes up to 60 seconds 236 end_time = time.time() 237 print(f"command exec time {end_time - start_time}") 238 assert (end_time - start_time) > 5 # Reboot takes at least 5 seconds 239 240 241@pytest.mark.repeat(1) 242def test_file_switch_off(): 243 assert check_hdc_cmd("shell param set persist.hdc.control.file false") 244 assert check_shell(f"shell param get persist.hdc.control.file", "false") 245 assert check_shell(f"file send {get_local_path('small')} {get_remote_path('it_small')}", 246 "debugging is not allowed") 247 assert check_shell(f"file recv {get_remote_path('it_small')} {get_local_path('small_recv')}", 248 "debugging is not allowed") 249 250 251@pytest.mark.repeat(1) 252def test_file_switch_on(): 253 assert check_hdc_cmd("shell param set persist.hdc.control.file true") 254 assert check_shell(f"shell param get persist.hdc.control.file", "true") 255 assert check_hdc_cmd(f"file send {get_local_path('small')} {get_remote_path('it_small')}") 256 assert check_hdc_cmd(f"file recv {get_remote_path('it_small')} {get_local_path('small_recv')}") 257 258 259def test_target_mount(): 260 assert (check_hdc_cmd("target mount", "Mount finish" or "[Fail]Operate need running as root")) 261 remount_vendor = get_shell_result(f'shell "mount |grep /vendor |head -1"') 262 print(remount_vendor) 263 assert "rw" in remount_vendor 264 remount_system = get_shell_result(f'shell "cat proc/mounts | grep /system |head -1"') 265 print(remount_system) 266 assert "rw" in remount_system 267 268 269def test_tmode_port(): 270 assert (check_hdc_cmd("tmode port", "Set device run mode successful")) 271 time.sleep(5) 272 assert (check_hdc_cmd("tmode port 12345")) 273 time.sleep(5) 274 netstat_port = get_shell_result(f'shell "netstat -anp | grep 12345"') 275 print(netstat_port) 276 assert "LISTEN" in netstat_port 277 assert "hdcd" in netstat_port 278 279 280def test_target_key(): 281 device_key = get_shell_result(f"list targets").split("\r\n")[0] 282 hdcd_pid = get_shell_result(f"-t {device_key} shell pgrep -x hdcd").split("\r\n")[0] 283 assert hdcd_pid.isdigit() 284 285 286def test_version_cmd(): 287 version = "Ver: 2.0.0a" 288 assert check_hdc_version("-v", version) 289 assert check_hdc_version("version", version) 290 assert check_hdc_version("checkserver", version) 291 292 293def test_fport_cmd(): 294 fport_list = [] 295 start_port = 10000 296 end_port = 10020 297 for i in range(start_port, end_port): 298 fport = f"tcp:{i+100} tcp:{i+200}" 299 rport = f"tcp:{i+300} tcp:{i+400}" 300 localabs = f"tcp:{i+500} localabstract:{f'helloworld.com.app.{i+600}'}" 301 fport_list.append(fport) 302 fport_list.append(rport) 303 fport_list.append(localabs) 304 305 for fport in fport_list: 306 assert check_hdc_cmd(f"fport {fport}", "Forwardport result:OK") 307 assert check_hdc_cmd("fport ls", fport) 308 309 for fport in fport_list: 310 assert check_hdc_cmd(f"fport rm {fport}", "success") 311 assert not check_hdc_cmd("fport ls", fport) 312 313 for rport in rport_list: 314 assert check_hdc_cmd(f"rport {rport}", "Forwardport result:OK") 315 assert check_hdc_cmd(f"rport {rport}", "TCP Port listen failed at") 316 assert check_hdc_cmd("rport ls", rport) or check_hdc_cmd("fport ls", rport) 317 318 for rport in rport_list: 319 assert check_hdc_cmd(f"fport rm {rport}", "success") 320 assert not check_hdc_cmd("rport ls", fport) and not check_hdc_cmd("fport ls", fport) 321 322 task_str1 = "tcp:33333 tcp:33333" 323 assert check_hdc_cmd(f"fport {task_str1}", "Forwardport result:OK") 324 assert check_hdc_cmd(f"fport rm {task_str1}", "success") 325 assert check_hdc_cmd(f"fport {task_str1}", "Forwardport result:OK") 326 assert check_hdc_cmd(f"fport rm {task_str1}", "success") 327 328 task_str2 = "tcp:44444 tcp:44444" 329 assert check_hdc_cmd(f"rport {task_str2}", "Forwardport result:OK") 330 assert check_hdc_cmd(f"fport rm {task_str2}", "success") 331 assert check_hdc_cmd(f"rport {task_str2}", "Forwardport result:OK") 332 assert check_hdc_cmd(f"fport rm {task_str2}", "success") 333 334 335def test_shell_cmd_timecost(): 336 assert check_cmd_time( 337 cmd="shell \"ps -ef | grep hdcd\"", 338 pattern="hdcd", 339 duration=None, 340 times=10) 341 342 343def test_shell_huge_cat(): 344 assert check_hdc_cmd(f"file send {get_local_path('word_100M.txt')} {get_remote_path('it_word_100M.txt')}") 345 assert check_cmd_time( 346 cmd=f"shell cat {get_remote_path('it_word_100M.txt')}", 347 pattern=None, 348 duration=10000, # 10 seconds 349 times=10) 350 351 352def test_hdcd_rom(): 353 baseline = 2200 # 2200KB 354 assert check_rom(baseline) 355 356 357def test_smode_r(): 358 assert check_hdc_cmd(f'smode -r') 359 run_command_with_timeout("hdc wait", 5) 360 assert check_shell(f"shell id", "context=u:r:sh:s0") 361 362 363def test_smode(): 364 assert check_hdc_cmd(f'smode') 365 run_command_with_timeout("hdc wait", 5) 366 assert check_shell(f"shell id", "context=u:r:su:s0") 367 368 369def setup_class(): 370 print("setting up env ...") 371 check_hdc_cmd("shell rm -rf data/local/tmp/it_*") 372 GP.load() 373 374 375def teardown_class(): 376 pass 377 378 379def run_main(): 380 if check_library_installation("pytest"): 381 exit(1) 382 383 if check_library_installation("pytest-testreport"): 384 exit(1) 385 386 if check_library_installation("pytest-repeat"): 387 exit(1) 388 389 GP.init() 390 391 prepare_source() 392 update_source() 393 394 choice_default = "" 395 parser = argparse.ArgumentParser() 396 parser.add_argument('--count', type=int, default=1, 397 help='test times') 398 parser.add_argument('--verbose', '-v', default=__file__, 399 help='filename') 400 parser.add_argument('--desc', '-d', default='Test for function.', 401 help='Add description on report') 402 args = parser.parse_args() 403 404 pytest_run(args) 405 406 407if __name__ == "__main__": 408 run_main() 409