• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 time
17import pytest
18
19from utils import GP, check_hdc_cmd, check_hdc_targets, check_rom, check_hdc_version, get_shell_result, \
20     run_command_with_timeout, check_shell, get_remote_path, get_local_path, load_gp
21
22
23def clear_and_restart():
24    check_hdc_cmd("kill")
25    check_hdc_cmd("start")
26    check_hdc_cmd("wait")
27    check_hdc_cmd("shell rm -rf data/local/tmp/it_*")
28    check_hdc_cmd("shell rm data/log/faultlog/faultlogger/cppcrash*hdcd*")
29    check_hdc_cmd("shell mkdir data/local/tmp/it_send_dir")
30
31
32class TestListTarget:
33    @classmethod
34    def setup_class(self):
35        clear_and_restart()
36
37    @pytest.mark.L0
38    def test_list_targets(self):
39        assert check_hdc_targets()
40        time.sleep(3)
41
42
43class TestROM:
44    @pytest.mark.L0
45    def test_hdcd_rom(self):
46        baseline = 2200 # 2200KB
47        assert check_rom(baseline)
48
49
50class TestVersion:
51    @pytest.mark.L0
52    def test_version_cmd(self):
53        version = "Ver: 3.1.0a"
54        assert check_hdc_version("-v", version)
55        assert check_hdc_version("version", version)
56
57    @pytest.mark.L0
58    def test_version_match(self):
59        """
60        Check that the version numbers of each program are the same
61        1. client
62        2. server
63        3. daemon program
64        4. device param
65        """
66        daemon_param_version = get_shell_result("shell param get const.hdc.version").strip()
67        print(f"param version:{daemon_param_version}")
68        daemon_version = get_shell_result("shell hdcd -v").strip()
69        print(f"daemon version:{daemon_version}")
70        client_version = get_shell_result("-v").strip()
71        print(f"client version:{client_version}")
72        server_version = get_shell_result("version").strip()
73        print(f"server version:{server_version}")
74        client_server_version = get_shell_result("checkserver").strip()
75        print(f"checkserver version:{client_server_version}")
76
77        assert(daemon_param_version == daemon_version)
78        assert(daemon_param_version == client_version)
79        assert(daemon_param_version == server_version)
80        assert(client_server_version.count(daemon_param_version) == 2)
81
82
83class TestTargetKey:
84    @pytest.mark.L0
85    def test_target_key(self):
86        device_key = re.split("\r|\n", get_shell_result(f"list targets"))[0]
87        hdcd_pid = re.split("\r|\n", get_shell_result(f"-t {device_key} shell pgrep -x hdcd"))[0]
88        assert hdcd_pid.isdigit()
89
90
91class TestTargetCommand:
92    @pytest.mark.L0
93    def test_target_cmd(self):
94        assert check_hdc_targets()
95        check_hdc_cmd("target boot")
96        start_time = time.time()
97        run_command_with_timeout(f"{GP.hdc_head} wait", 30) # reboot takes up to 30 seconds
98        time.sleep(3) # sleep 3s to wait for the device to boot
99        run_command_with_timeout(f"{GP.hdc_head} wait", 30) # reboot takes up to 30 seconds
100        end_time = time.time()
101        print(f"command exec time {end_time - start_time}")
102        time.sleep(3) # sleep 3s to wait for the device to connect channel
103        assert (end_time - start_time) > 8 # Reboot takes at least 8 seconds
104
105    @pytest.mark.L0
106    def test_target_mount(self):
107        assert (check_hdc_cmd("target mount", "Mount finish" or "[Fail]Operate need running as root"))
108        is_linux = check_hdc_cmd("shell uname", "Linux")
109        sep = "/"
110        remount_vendor = get_shell_result(f'shell "mount |grep {sep}vendor |head -1"')
111        print(remount_vendor)
112        if is_linux:
113            assert "rw" in remount_vendor
114        else:
115            assert "ro" in remount_vendor
116        remount_system = get_shell_result(f'shell "cat proc/mounts | grep {sep}system |head -1"')
117        print(remount_system)
118        assert "rw" in remount_system
119
120
121class TestSwitch:
122    @pytest.mark.L0
123    @pytest.mark.repeat(1)
124    def test_file_switch_off(self):
125        assert check_hdc_cmd("shell param set persist.hdc.control.file false")
126        assert check_shell(f"shell param get persist.hdc.control.file", "false")
127        assert check_shell(f"file send {get_local_path('small')} {get_remote_path('it_small')}",
128                        "debugging is not allowed")
129        assert check_shell(f"file recv {get_remote_path('it_small')} {get_local_path('small_recv')}",
130                        "debugging is not allowed")
131
132    @pytest.mark.L0
133    @pytest.mark.repeat(1)
134    def test_file_switch_on(self):
135        assert check_hdc_cmd("shell param set persist.hdc.control.file true")
136        assert check_shell(f"shell param get persist.hdc.control.file", "true")
137        assert check_hdc_cmd(f"file send {get_local_path('small')} {get_remote_path('it_small')}")
138        assert check_hdc_cmd(f"file recv {get_remote_path('it_small')} {get_local_path('small_recv')}")