• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import pytest
17import subprocess
18import re
19import time
20import sys
21sys.path.append("..")
22from tools.utils import *
23import threading
24WAIT_TIMES = 7
25SLEEP_TWENTY = 20
26SLEEP_FIVE = 5
27GET_PID_TIME = 30
28TOUCH_TIMES = 67
29
30
31def get_daemon_pid():
32    subprocess.check_output(f"hdc shell ps -ef | grep daemon > /data/local/tmp/daemon.txt")
33    subprocess.check_output(f"hdc file recv /data/local/tmp/daemon.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
34    check = True
35    with open(r'.\..\outputfiles\daemon.txt', 'r') as file:
36        lines = file.readlines()
37        for line in lines:
38            if "native_daemon sa" in line:
39                return line.split()[1]
40    return -1
41
42
43class TestHiprofilerReliability:
44    @pytest.mark.L0
45    def test_badfd(self):
46        subprocess.check_output("hdc shell rm /data/local/tmp/test.htrace")
47        try:
48            subprocess.check_output(r"del .\..\outputfiles\test.htrace", text=True, encoding="utf-8")
49            subprocess.check_output(r"del .\..\outputfiles\nativehook.db", text=True, encoding="utf-8")
50        except Exception as e:
51            print(f"An error occurred: {e}")
52            pass
53        subprocess.check_output("hdc target mount", text=True, encoding="utf-8")
54        subprocess.check_output(f"hdc file send .\..\inputfiles\process_resource_limit.json /system/variant/phone/base/etc/efficiency_manager", text=True, encoding="utf-8")
55        subprocess.check_output("hdc shell reboot", text=True, encoding="utf-8")
56        time.sleep(SLEEP_TWENTY)
57        j = 0
58        while j < WAIT_TIMES:
59            output = subprocess.check_output("hdc list targets", text=True, encoding="utf-8")
60            if output == '[Empty]\n\n':
61                time.sleep(SLEEP_FIVE)
62                j += 1
63            else:
64                break
65
66        #解除锁屏
67        subprocess.check_output("hdc shell uitest uiInput drag 100 500 100 100 1000")
68        time.sleep(SLEEP_FIVE)
69        subprocess.check_output("hdc shell uitest uiInput drag 100 500 100 100 1000")
70        time.sleep(SLEEP_FIVE)
71        subprocess.check_output("hdc shell uitest uiInput drag 100 500 100 100 1000")
72
73        subprocess.check_output("hdc shell power-shell setmode 602")
74
75        subprocess.check_output("hdc shell killall com.example.insight_test_stage")
76        subprocess.check_output("hdc shell param set hiview.memleak.test enable")
77        subprocess.check_output("hdc shell killall hiview")
78
79        subprocess.check_output("hdc shell aa start -a EntryAbility -b com.example.insight_test_stage")
80        process = subprocess.Popen(['hdc', 'shell', 'dmesg -w | grep avc > /data/local/tmp/avc.txt'])
81        process_hilog = subprocess.Popen(['hdc', 'shell', 'hilog | grep BADFD > /data/local/tmp/daemonhilog.txt'])
82        time.sleep(1)
83        touch_button("模板测试")
84        time.sleep(1)
85        subprocess.check_output("hdc shell uitest uiInput drag 100 800 100 100 1000")
86        time.sleep(1)
87        touch_button("Allocations_Js_Depth")
88        i = 0
89        daemon_pid = 0
90        while i < TOUCH_TIMES:
91            touch_button("malloc-release(depth 6)")
92            if (i == GET_PID_TIME):
93                daemon_pid = get_daemon_pid()
94            touch_button("small-malloc(depth 7)")
95            i += 1
96        process.terminate()
97        process_hilog.terminate()
98        subprocess.check_output(f"hdc file recv /data/local/tmp/avc.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
99        subprocess.check_output(f"hdc file recv /data/local/tmp/daemonhilog.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
100        check = True
101        with open(r'.\..\outputfiles\avc.txt', 'r') as file:
102            lines = file.readlines()
103            for line in lines:
104                if "BADFD" in line:
105                    if "pid=" + str(daemon_pid) + " tid=" in line:
106                        check = False
107
108        with open(r'.\..\outputfiles\daemonhilog.txt', 'r') as file:
109            lines = file.readlines()
110            for line in lines:
111                if "BADFD" in line:
112                    if "pid=" + str(daemon_pid) + " tid=" in line:
113                        check = False
114        assert check == True