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, get_shell_result
31from dev_hdc_test import make_multiprocess_file, rmdir
32from dev_hdc_test import check_app_install_multi, check_app_uninstall_multi
33from dev_hdc_test import check_rom, check_shell
34
35
36def test_list_targets():
37    assert check_hdc_targets()
38    assert check_hdc_cmd("shell rm -rf data/local/tmp/it_*")
39    assert check_hdc_cmd("shell mkdir data/local/tmp/it_send_dir")
40
41
42@pytest.mark.repeat(5)
43def test_empty_file():
44    assert check_hdc_cmd(f"file send {get_local_path('empty')} {get_remote_path('it_empty')}")
45    assert check_hdc_cmd(f"file recv {get_remote_path('it_empty')} {get_local_path('empty_recv')}")
46
47
48@pytest.mark.repeat(5)
49def test_empty_dir():
50    assert check_shell(f"file send {get_local_path('empty_dir')} {get_remote_path('it_empty_dir')}", "the source folder is empty")
51    assert check_hdc_cmd("shell mkdir data/local/tmp/it_empty_dir_recv")
52    assert check_shell(f"file recv {get_remote_path('it_empty_dir_recv')} {get_local_path('empty_dir_recv')}", "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
72
73@pytest.mark.repeat(1)
74def test_medium_file():
75    assert check_hdc_cmd(f"file send {get_local_path('medium')} {get_remote_path('it_medium')}")
76    assert check_hdc_cmd(f"file recv {get_remote_path('it_medium')} {get_local_path('medium_recv')}")
77
78
79@pytest.mark.repeat(1)
80def test_large_file():
81    assert check_hdc_cmd(f"file send {get_local_path('large')} {get_remote_path('it_large')}")
82    assert check_hdc_cmd(f"file recv {get_remote_path('it_large')} {get_local_path('large_recv')}")
83
84
85@pytest.mark.repeat(1)
86def test_running_file():
87    assert check_hdc_cmd(f"file recv /system/bin/hdcd {get_local_path('running_recv')}")
88
89
90@pytest.mark.repeat(1)
91def test_rate():
92    assert check_rate(f"file send {get_local_path('large')} {get_remote_path('it_large')}", 18000)
93    assert check_rate(f"file recv {get_remote_path('it_large')} {get_local_path('large_recv')}", 18000)
94
95
96@pytest.mark.repeat(1)
97def test_file_error():
98    assert check_hdc_cmd("target mount")
99    assert check_shell(
100        f"file send {get_local_path('small')} system/bin/hdcd",
101        "busy"
102    )
103    assert check_shell(
104        f"file recv",
105        "[Fail]There is no local and remote path"
106    )
107    assert check_shell(
108        f"file send",
109        "[Fail]There is no local and remote path"
110    )
111    assert check_hdc_cmd(f"shell rm -rf {get_remote_path('../../../large')}")
112    assert check_hdc_cmd(f"shell param set persist.hdc.control.file false")
113    assert check_shell(
114        f"file send {get_local_path('small')} {get_remote_path('it_small_false')}",
115        "debugging is not allowed"
116    )
117    assert check_hdc_cmd(f"shell param set persist.hdc.control.file true")
118    assert check_hdc_cmd(f"file send {get_local_path('small')} {get_remote_path('it_small_true')}")
119
120
121@pytest.mark.repeat(5)
122def test_recv_dir():
123    if os.path.exists(get_local_path('it_problem_dir')):
124        rmdir(get_local_path('it_problem_dir'))
125    assert check_hdc_cmd(f"shell rm -rf {get_remote_path('it_problem_dir')}")
126    assert check_hdc_cmd(f"shell rm -rf {get_remote_path('problem_dir')}")
127    assert make_multiprocess_file(get_local_path('problem_dir'), get_remote_path(''), 'send', 1, "dir")
128    assert check_hdc_cmd(f"shell mv {get_remote_path('problem_dir')} {get_remote_path('it_problem_dir')}")
129    assert make_multiprocess_file(get_local_path(''), get_remote_path('it_problem_dir'), 'recv', 1, "dir")
130
131
132@pytest.mark.repeat(5)
133def test_hap_install():
134    assert check_hdc_cmd(f"install -r {get_local_path('entry-default-signed-debug.hap')}",
135                            bundle="com.hmos.diagnosis")
136
137
138@pytest.mark.repeat(5)
139def test_install_hap():
140    package_hap = "entry-default-signed-debug.hap"
141    app_name_default = "com.hmos.diagnosis"
142
143    # default
144    assert check_app_install(package_hap, app_name_default)
145    assert check_app_uninstall(app_name_default)
146
147    # -r
148    assert check_app_install(package_hap, app_name_default, "-r")
149    assert check_app_uninstall(app_name_default)
150
151    # -k
152    assert check_app_install(package_hap, app_name_default, "-r")
153    assert check_app_uninstall(app_name_default, "-k")
154
155    # -s
156    assert check_app_install(package_hap, app_name_default, "-s")
157
158
159@pytest.mark.repeat(5)
160def test_install_hsp():
161    package_hsp = "libA_v10001.hsp"
162    hsp_name_default = "com.example.liba"
163
164    assert check_app_install(package_hsp, hsp_name_default, "-s")
165    assert check_app_uninstall(hsp_name_default, "-s")
166    assert check_app_install(package_hsp, hsp_name_default)
167
168
169@pytest.mark.repeat(5)
170def test_install_multi_hap():
171    # default multi hap
172    tables = {
173        "entry-default-signed-debug.hap" : "com.hmos.diagnosis",
174        "ActsAudioRecorderJsTest.hap" : "ohos.acts.multimedia.audio.audiorecorder"
175    }
176    assert check_app_install_multi(tables)
177    assert check_app_uninstall_multi(tables)
178    assert check_app_install_multi(tables, "-s")
179
180    # default multi hap -r -k
181    tables = {
182        "entry-default-signed-debug.hap" : "com.hmos.diagnosis",
183        "ActsAudioRecorderJsTest.hap" : "ohos.acts.multimedia.audio.audiorecorder"
184    }
185    assert check_app_install_multi(tables, "-r")
186    assert check_app_uninstall_multi(tables, "-k")
187
188
189@pytest.mark.repeat(5)
190def test_install_multi_hsp():
191    # default multi hsp -s
192    tables = {
193        "libA_v10001.hsp" : "com.example.liba",
194        "libB_v10001.hsp" : "com.example.libb",
195    }
196    assert check_app_install_multi(tables, "-s")
197    assert check_app_uninstall_multi(tables, "-s")
198    assert check_app_install_multi(tables)
199
200
201@pytest.mark.repeat(5)
202def test_install_hsp_and_hap():
203    #default multi hsp and hsp
204    tables = {
205        "libA_v10001.hsp" : "com.example.liba",
206        "entry-default-signed-debug.hap" : "com.hmos.diagnosis",
207    }
208    assert check_app_install_multi(tables)
209    assert check_app_install_multi(tables, "-s")
210
211
212@pytest.mark.repeat(5)
213def test_install_dir():
214    package_haps_dir = "app_dir"
215    app_name_default = "com.hmos.diagnosis"
216    assert check_app_install(package_haps_dir, app_name_default)
217    assert check_app_uninstall(app_name_default)
218
219
220def test_server_kill():
221    assert check_hdc_cmd("kill", "Kill server finish")
222    assert check_hdc_cmd("start server", "")
223
224
225def test_target_cmd():
226    assert check_hdc_targets()
227    time.sleep(3)
228    check_hdc_cmd("target boot")
229    start_time = time.time()
230    run_command_with_timeout("hdc wait", 60) # reboot takes up to 60 seconds
231    end_time = time.time()
232    print(f"command exec time {end_time - start_time}")
233    assert (end_time - start_time) > 5 # Reboot takes at least 5 seconds
234
235
236@pytest.mark.repeat(1)
237def test_file_switch_off():
238    assert check_hdc_cmd("shell param set persist.hdc.control.file false")
239    assert check_shell(f"shell param get persist.hdc.control.file", "false")
240    assert check_shell(f"file send {get_local_path('small')} {get_remote_path('it_small')}",
241                       "debugging is not allowed")
242    assert check_shell(f"file recv {get_remote_path('it_small')} {get_local_path('small_recv')}",
243                       "debugging is not allowed")
244
245
246@pytest.mark.repeat(1)
247def test_file_switch_on():
248    assert check_hdc_cmd("shell param set persist.hdc.control.file true")
249    assert check_shell(f"shell param get persist.hdc.control.file", "true")
250    assert check_hdc_cmd(f"file send {get_local_path('small')} {get_remote_path('it_small')}")
251    assert check_hdc_cmd(f"file recv {get_remote_path('it_small')} {get_local_path('small_recv')}")
252
253
254def test_target_mount():
255    assert (check_hdc_cmd("target mount", "Mount finish" or "[Fail]Operate need running as root"))
256    remount_vendor = get_shell_result(f'shell "mount |grep /vendor |head -1"')
257    print(remount_vendor)
258    assert "rw" in remount_vendor
259    remount_system = get_shell_result(f'shell "cat /proc/mounts | grep /system |head -1"')
260    print(remount_system)
261    assert "rw" in remount_system
262
263
264def test_tmode_port():
265    assert (check_hdc_cmd("tmode port", "Set device run mode successful"))
266    time.sleep(5)
267    assert (check_hdc_cmd("tmode port 12345"))
268    time.sleep(5)
269    netstat_port = get_shell_result(f'shell "netstat -anp | grep 12345"')
270    print(netstat_port)
271    assert "LISTEN" in netstat_port
272    assert "hdcd" in netstat_port
273
274
275def test_target_key():
276    device_key = get_shell_result(f"list targets").split("\r\n")[0]
277    hdcd_pid = get_shell_result(f"-t {device_key} shell pgrep -x hdcd").split("\r\n")[0]
278    assert hdcd_pid.isdigit()
279
280
281def test_version_cmd():
282    version = "Ver: 2.0.0a"
283    assert check_hdc_version("-v", version)
284    assert check_hdc_version("version", version)
285    assert check_hdc_version("checkserver", version)
286
287
288def test_fport_cmd():
289    fport_list = []
290    rport_list = []
291    start_port = 10000
292    end_port = 10020
293    for i in range(start_port, end_port):
294        fport = f"tcp:{i+100} tcp:{i+200}"
295        rport = f"tcp:{i+300} tcp:{i+400}"
296        localabs = f"tcp:{i+500} localabstract:{f'helloworld.com.app.{i+600}'}"
297        fport_list.append(fport)
298        rport_list.append(rport)
299        fport_list.append(localabs)
300
301    for fport in fport_list:
302        assert check_hdc_cmd(f"fport {fport}", "Forwardport result:OK")
303        assert check_hdc_cmd(f"fport {fport}", "TCP Port listen failed at")
304        assert check_hdc_cmd("fport ls", fport)
305
306    for fport in fport_list:
307        assert check_hdc_cmd(f"fport rm {fport}", "success")
308        assert not check_hdc_cmd("fport ls", fport)
309
310    for rport in rport_list:
311        assert check_hdc_cmd(f"rport {rport}", "Forwardport result:OK")
312        assert check_hdc_cmd(f"rport {rport}", "TCP Port listen failed at")
313        assert check_hdc_cmd("rport ls", rport) or check_hdc_cmd("fport ls", rport)
314
315    for rport in rport_list:
316        assert check_hdc_cmd(f"fport rm {rport}", "success")
317        assert not check_hdc_cmd("rport ls", fport) and not check_hdc_cmd("fport ls", fport)
318
319    task_str1 = "tcp:33333 tcp:33333"
320    assert check_hdc_cmd(f"fport {task_str1}", "Forwardport result:OK")
321    assert check_hdc_cmd(f"fport rm {task_str1}", "success")
322    assert check_hdc_cmd(f"fport {task_str1}", "Forwardport result:OK")
323    assert check_hdc_cmd(f"fport rm {task_str1}", "success")
324
325    task_str2 = "tcp:44444 tcp:44444"
326    assert check_hdc_cmd(f"rport {task_str2}", "Forwardport result:OK")
327    assert check_hdc_cmd(f"fport rm {task_str2}", "success")
328    assert check_hdc_cmd(f"rport {task_str2}", "Forwardport result:OK")
329    assert check_hdc_cmd(f"fport rm {task_str2}", "success")
330
331
332def test_shell_cmd_timecost():
333    assert check_cmd_time(
334        cmd="shell \"ps -ef | grep hdcd\"",
335        pattern="hdcd",
336        duration=None,
337        times=10)
338
339
340def test_shell_huge_cat():
341    assert check_hdc_cmd(f"file send {get_local_path('word_100M.txt')} {get_remote_path('it_word_100M.txt')}")
342    assert check_cmd_time(
343        cmd=f"shell cat {get_remote_path('it_word_100M.txt')}",
344        pattern=None,
345        duration=10000, # 10 seconds
346        times=10)
347
348
349def test_hdcd_rom():
350    baseline = 2200 # 2200KB
351    assert check_rom(baseline)
352
353
354def test_smode_r():
355    assert check_hdc_cmd(f'smode -r')
356    run_command_with_timeout("hdc wait", 5)
357    time.sleep(1)
358    assert check_shell(f"shell id", "context=u:r:sh:s0")
359
360
361def test_smode():
362    assert check_hdc_cmd(f'smode')
363    run_command_with_timeout("hdc wait", 5)
364    time.sleep(1)
365    assert check_shell(f"shell id", "context=u:r:su:s0")
366    assert not check_hdc_cmd("ls /data/log/faultlog/faultlogger | grep hdcd", "hdcd")
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