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 17from utils import GP, check_hdc_cmd, get_shell_result, run_command_with_timeout, load_gp 18 19 20class TestPersistMode: 21 @pytest.mark.L0 22 def test_persist_hdc_mode_tcp(self): 23 assert check_hdc_cmd(f"shell param set persist.hdc.mode.tcp enable") 24 time.sleep(5) 25 run_command_with_timeout("hdc wait", 3) 26 netstat_listen = get_shell_result(f'shell "netstat -anp | grep tcp | grep hdcd"') 27 assert "LISTEN" in netstat_listen 28 assert "hdcd" in netstat_listen 29 assert check_hdc_cmd(f"shell param set persist.hdc.mode.tcp enable") 30 time.sleep(5) 31 run_command_with_timeout("hdc wait", 3) 32 netstat_listen = get_shell_result(f'shell "netstat -anp | grep tcp | grep hdcd"') 33 assert "LISTEN" in netstat_listen 34 assert "hdcd" in netstat_listen 35 assert check_hdc_cmd(f"shell param set persist.hdc.mode.tcp disable") 36 time.sleep(5) 37 run_command_with_timeout("hdc wait", 3) 38 netstat_listen = get_shell_result(f'shell "netstat -anp | grep tcp | grep hdcd"') 39 assert "LISTEN" not in netstat_listen 40 assert "hdcd" not in netstat_listen 41 42 @pytest.mark.L0 43 def test_persist_hdc_mode_usb(self): 44 assert check_hdc_cmd(f"shell param set persist.hdc.mode.usb enable") 45 echo_result = get_shell_result(f'shell "echo 12345"') 46 assert "12345" not in echo_result 47 time.sleep(10) 48 run_command_with_timeout("hdc wait", 3) 49 echo_result = get_shell_result(f'shell "echo 12345"') 50 assert "12345" in echo_result 51 52 @pytest.mark.L0 53 def test_persist_hdc_mode_tcp_usb(self): 54 assert check_hdc_cmd(f"shell param set persist.hdc.mode.tcp enable") 55 time.sleep(5) 56 run_command_with_timeout("hdc wait", 3) 57 assert check_hdc_cmd(f"shell param set persist.hdc.mode.usb enable") 58 time.sleep(10) 59 run_command_with_timeout("hdc wait", 3) 60 netstat_listen = get_shell_result(f'shell "netstat -anp | grep tcp | grep hdcd"') 61 assert "LISTEN" in netstat_listen 62 assert "hdcd" in netstat_listen 63 assert check_hdc_cmd(f"shell param set persist.hdc.mode.tcp disable") 64 time.sleep(5) 65 run_command_with_timeout("hdc wait", 3) 66 netstat_listen = get_shell_result(f'shell "netstat -anp | grep tcp | grep hdcd"') 67 assert "LISTEN" not in netstat_listen 68 assert "hdcd" not in netstat_listen 69 70 71class TestTmodeCommand: 72 @pytest.mark.L0 73 def test_tmode_port(self): 74 assert (check_hdc_cmd("tmode port", "Set device run mode successful")) 75 time.sleep(3) # sleep 3s to wait for the device to connect channel 76 run_command_with_timeout(f"{GP.hdc_head} wait", 3) # wait 3s for the device to connect channel 77 time.sleep(3) # sleep 3s to wait for the device to connect channel 78 run_command_with_timeout(f"{GP.hdc_head} wait", 3) # wait 3s for the device to connect channel 79 assert (check_hdc_cmd("tmode port 12345")) 80 time.sleep(3) # sleep 3s to wait for the device to connect channel 81 run_command_with_timeout(f"{GP.hdc_head} wait", 3) # wait 3s for the device to connect channel 82 time.sleep(3) # sleep 3s to wait for the device to connect channel 83 run_command_with_timeout(f"{GP.hdc_head} wait", 3) # wait 3s for the device to connect channel 84 netstat_port = get_shell_result(f'shell "netstat -anp | grep 12345"') 85 print(netstat_port) 86 assert "LISTEN" in netstat_port 87 assert "hdcd" in netstat_port