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 time 16import pytest 17 18from utils import GP, check_hdc_cmd, check_hdc_targets, check_rom, check_hdc_version, get_shell_result, \ 19 run_command_with_timeout, check_shell, get_remote_path, get_local_path, load_gp 20 21 22def clear_and_restart(): 23 check_hdc_cmd("kill") 24 check_hdc_cmd("start") 25 check_hdc_cmd("wait") 26 check_hdc_cmd("shell rm -rf data/local/tmp/it_*") 27 check_hdc_cmd("shell mkdir data/local/tmp/it_send_dir") 28 29 30class TestListTarget: 31 @classmethod 32 def setup_class(self): 33 clear_and_restart() 34 35 @pytest.mark.L0 36 def test_list_targets(self): 37 assert check_hdc_targets() 38 time.sleep(3) 39 40 41class TestROM: 42 @pytest.mark.L0 43 def test_hdcd_rom(self): 44 baseline = 2200 # 2200KB 45 assert check_rom(baseline) 46 47 48class TestVersion: 49 @pytest.mark.L0 50 def test_version_cmd(self): 51 version = "Ver: 3.1.0a" 52 assert check_hdc_version("-v", version) 53 assert check_hdc_version("version", version) 54 assert check_hdc_version("checkserver", version) 55 56 57class TestTargetKey: 58 @pytest.mark.L0 59 def test_target_key(self): 60 device_key = get_shell_result(f"list targets").split("\r\n")[0] 61 hdcd_pid = get_shell_result(f"-t {device_key} shell pgrep -x hdcd").split("\r\n")[0] 62 assert hdcd_pid.isdigit() 63 64 65class TestTargetCommand: 66 @pytest.mark.L0 67 def test_target_cmd(self): 68 assert check_hdc_targets() 69 check_hdc_cmd("target boot") 70 start_time = time.time() 71 run_command_with_timeout(f"{GP.hdc_head} wait", 30) # reboot takes up to 30 seconds 72 time.sleep(3) # sleep 3s to wait for the device to boot 73 run_command_with_timeout(f"{GP.hdc_head} wait", 30) # reboot takes up to 30 seconds 74 end_time = time.time() 75 print(f"command exec time {end_time - start_time}") 76 time.sleep(3) # sleep 3s to wait for the device to connect channel 77 assert (end_time - start_time) > 8 # Reboot takes at least 8 seconds 78 79 @pytest.mark.L0 80 def test_target_mount(self): 81 assert (check_hdc_cmd("target mount", "Mount finish" or "[Fail]Operate need running as root")) 82 sep = "/" 83 remount_vendor = get_shell_result(f'shell "mount |grep {sep}vendor |head -1"') 84 print(remount_vendor) 85 sep = "/" 86 assert "rw" in remount_vendor 87 remount_system = get_shell_result(f'shell "cat proc/mounts | grep {sep}system |head -1"') 88 print(remount_system) 89 assert "rw" in remount_system 90 91 92class TestSwitch: 93 @pytest.mark.L0 94 @pytest.mark.repeat(1) 95 def test_file_switch_off(self): 96 assert check_hdc_cmd("shell param set persist.hdc.control.file false") 97 assert check_shell(f"shell param get persist.hdc.control.file", "false") 98 assert check_shell(f"file send {get_local_path('small')} {get_remote_path('it_small')}", 99 "debugging is not allowed") 100 assert check_shell(f"file recv {get_remote_path('it_small')} {get_local_path('small_recv')}", 101 "debugging is not allowed") 102 103 @pytest.mark.L0 104 @pytest.mark.repeat(1) 105 def test_file_switch_on(self): 106 assert check_hdc_cmd("shell param set persist.hdc.control.file true") 107 assert check_shell(f"shell param get persist.hdc.control.file", "true") 108 assert check_hdc_cmd(f"file send {get_local_path('small')} {get_remote_path('it_small')}") 109 assert check_hdc_cmd(f"file recv {get_remote_path('it_small')} {get_local_path('small_recv')}")