• 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.
15
16import pytest
17import re
18import time
19import subprocess
20from utils import *
21
22@print_check_result
23def CheckPseft(output):
24    assert re.search("cmd is: ps -efT", output) is not None
25    result = re.search("UID\s+PID\s+TID  PPID TCNT STIME TTY\s+TIME CMD\n([^\n]+\n){1,}", output)
26    return result is not None
27
28@print_check_result
29def CheckMountInfo(output):
30    result = re.search("/proc/\d+/mountinfo\n\n([^\n]+\n){4,}", output)
31    return result is not None
32
33def CheckSmaps(output):
34    result = re.search(r"/proc/\d+/maps", output)
35    return result is not None
36
37@print_check_result
38def CheckWchan(output):
39    result = re.search("/proc/\d+/wchan", output)
40    return result is not None
41
42@print_check_result
43def CheckPsOTime(output):
44    result = re.search("cmd is: ps -o", output)
45    return result is not None
46
47
48class TestHidumperProcess:
49    @classmethod
50    def setup_class(cls):
51        if not IsRootVersion():
52            subprocess.check_call("hdc shell aa start -a EntryAbility -b com.example.jsleakwatcher", shell=True)
53
54    @classmethod
55    def teardown_class(cls):
56        if not IsRootVersion():
57            subprocess.check_call("hdc shell aa force-stop -b com.example.jsleakwatcher", shell=True)
58
59    @pytest.mark.L0
60    def test_process_all(self):
61        if IsRootVersion():
62            CheckFunc = lambda x : all([CheckSmaps(x), CheckPseft(x), CheckMountInfo(x), CheckPsOTime(x), CheckWchan(x)])
63        else:
64            CheckFunc = lambda x : all([not CheckSmaps(x), CheckPseft(x), CheckMountInfo(x), CheckPsOTime(x), CheckWchan(x)])
65            pid = GetPidByProcessName("com.example.myapplication")
66            if pid == "":
67                pytest.skip("test application not found")
68        command = f"hidumper -p"
69        hidumperTmpCmd = "OPT:p SUB_OPT:"
70        # 校验命令行输出
71        CheckCmd(command, CheckFunc, hidumperTmpCmd)
72
73    @pytest.mark.L0
74    def test_process_pid(self):
75        command = None
76        pid = 1
77        if IsRootVersion():
78            CheckFunc = lambda x : all([CheckSmaps(x), CheckPseft(x), CheckMountInfo(x), CheckPsOTime(x), CheckWchan(x)])
79        else:
80            CheckFunc = lambda x : all([not CheckSmaps(x), CheckPseft(x), CheckMountInfo(x), CheckPsOTime(x), CheckWchan(x)])
81            pid = GetPidByProcessName("com.example.myapplication")
82            if pid == "":
83                pytest.skip("test application not found")
84        command = f"hidumper -p {pid}"
85        hidumperTmpCmd = "OPT:p SUB_OPT:"
86        # 校验命令行输出
87        CheckCmd(command, CheckFunc, hidumperTmpCmd)
88
89    @pytest.mark.L0
90    def test_process_with_non_debug_pid(self):
91        if not IsRootVersion():
92            output = subprocess.check_output("hdc shell \"hidumper -p 1\"", shell=True, text=True, encoding="utf-8")
93            assert "only support debug application" in output
94
95    @pytest.mark.L3
96    def test_process_error_pid(self):
97        command = f"hidumper -p 2147483647;hidumper -p -2147483647"
98        hidumperTmpCmd = "OPT:p SUB_OPT:"
99        # 校验命令行输出
100        CheckCmd(command, lambda output : "hidumper: No such process: 2147483647\nhidumper: option pid missed. 2" in output, hidumperTmpCmd)
101        command = f"hidumper -p 2147483648;hidumper -p -2147483648"
102        CheckCmd(command, lambda output : "hidumper: option pid missed. 2" in output, hidumperTmpCmd)
103
104