• 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
24import sqlite3
25
26LIB_PATH = "/system/lib"
27NETWORK_PROFILER_RESULT = 2
28SLEEP_TWO = 2
29SLEEP_THREE = 3
30SLEEP_FOUR = 4
31CONFIG_NETWORK_PROFILER = 10
32CONFIG_NETWORK_PROFILER_NONDEBUG = 12
33CONFIG_SCENEBOARD = 7
34SIZE_INDEX = 4
35
36
37def task(index):
38    indexstr = str(index)
39    subprocess.check_output("hdc shell hiprofiler_cmd -c /data/local/tmp/config" + indexstr + ".txt -o /data/local/tmp/test" + indexstr + ".htrace -t 20 -s -k")
40
41
42def malloctest():
43    subprocess.check_output("hdc shell chmod 777 /data/local/tmp/malloctest")
44    subprocess.check_output("hdc shell ./data/local/tmp/malloctest 10 1024 1000000 > /data/local/tmp/malloctest.txt")
45
46
47class TestHiprofilerUserMode:
48    @pytest.mark.L0
49    def test_usermode_nativehook_debug_app(self):
50        subprocess.check_output(r"hdc file send .\..\inputfiles\nativehook\config1.txt /data/local/tmp/", text=True, encoding="utf-8")
51        subprocess.check_output("hdc shell power-shell setmode 602")
52        subprocess.check_output("hdc shell killall com.example.insight_test_stage")
53        subprocess.check_output("hdc shell rm /data/local/tmp/test1.htrace")
54        task_thread = threading.Thread(target=task, args=(1, ))
55        task_thread.start()
56        time.sleep(SLEEP_TWO)
57        subprocess.check_output("hdc shell aa start -a EntryAbility -b com.example.insight_test_stage")
58        time.sleep(SLEEP_FOUR)
59        touch_button("模板测试")
60        time.sleep(SLEEP_TWO)
61        subprocess.check_output("hdc shell uitest uiInput drag 100 800 100 100 1000")
62        time.sleep(1)
63        touch_button("Allocations_Js_Depth")
64        touch_button("malloc-release(depth 6)")
65        touch_button("small-malloc(depth 7)")
66        task_thread.join()
67        subprocess.check_output(f"hdc file recv /data/local/tmp/test1.htrace .\..\outputfiles\ ", text=True, encoding="utf-8")
68        subprocess.check_output("hdc shell ls -lh /data/local/tmp/ > /data/local/tmp/tmp.txt")
69        subprocess.check_output(f"hdc file recv /data/local/tmp/tmp.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
70        result = False
71        with open(r'.\..\outputfiles\tmp.txt', 'r') as file:
72            lines = file.readlines()
73            for line in lines:
74                if "test1.htrace" in line:
75                    result = (line.split()[SIZE_INDEX][-1] == 'M')
76        assert result
77
78    @pytest.mark.L0
79    def test_usermode_nondebug_app(self):
80        # 校验命令行输出
81        subprocess.check_output("hdc shell rm /data/local/tmp/test7.htrace")
82        subprocess.check_output("hdc shell rm /data/local/tmp/tmp.txt")
83        subprocess.check_output(r"hdc file send .\..\inputfiles\nativehook\config7.txt /data/local/tmp/", text=True, encoding="utf-8")
84        task_thread = threading.Thread(target=task, args=(CONFIG_SCENEBOARD, ))
85        task_thread.start()
86        time.sleep(SLEEP_TWO)
87        subprocess.check_output("hdc shell uitest uinput drag 100 800 100 100 1000")
88        time.sleep(1)
89        subprocess.check_output("hdc shell uitest uinput drag 100 100 800 100 1000")
90        task_thread.join()
91        subprocess.check_output(f"hdc file recv /data/local/tmp/test7.htrace .\..\outputfiles\ ", text=True, encoding="utf-8")
92        subprocess.check_output("hdc shell ls -lh /data/local/tmp/ > /data/local/tmp/tmp.txt")
93        subprocess.check_output(f"hdc file recv /data/local/tmp/tmp.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
94        result = False
95        with open(r'.\..\outputfiles\tmp.txt', 'r') as file:
96            lines = file.readlines()
97            for line in lines:
98                if "test7.htrace" in line:
99                    result = (line.split()[SIZE_INDEX][:-1] == "1.0")
100        assert result
101
102    @pytest.mark.L0
103    def test_usermode_network_profiler_debugapp(self):
104        subprocess.check_output(r"hdc file send .\..\inputfiles\network_profiler\config10.txt /data/local/tmp/", text=True, encoding="utf-8")
105        subprocess.check_output("hdc shell killall com.example.myapplication523")
106        task_thread = threading.Thread(target=task, args=(CONFIG_NETWORK_PROFILER, ))
107        time.sleep(SLEEP_TWO)
108        task_thread.start()
109        time.sleep(SLEEP_TWO)
110        subprocess.check_output("hdc shell aa start -a EntryAbility -b com.example.myapplication523")
111        time.sleep(SLEEP_THREE)
112        touch_button("http_request")
113        task_thread.join()
114
115        subprocess.check_output("hdc shell chmod 777 /data/local/tmp/hookDecoder")
116        subprocess.check_output("hdc shell ./data/local/tmp/hookDecoder -f /data/local/tmp/test10.htrace > /data/local/tmp/test10_result.txt")
117        subprocess.check_output(f"hdc file recv /data/local/tmp/test10.htrace .\..\outputfiles\ ", text=True, encoding="utf-8")
118        subprocess.check_output(f"hdc file recv /data/local/tmp/test10_result.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
119        count = 0
120        with open(r'.\..\outputfiles\test10_result.txt', 'r') as file:
121            lines = file.readlines()
122            for line in lines:
123                if "tv_nsec" in line:
124                    count += 1
125        assert count == NETWORK_PROFILER_RESULT
126        #包括文件头
127
128    @pytest.mark.L0
129    def test_usermode_network_profiler_nondebugapp(self):
130        subprocess.check_output("hdc shell rm /data/local/tmp/test12.htrace")
131        subprocess.check_output(r"hdc file send .\..\inputfiles\network_profiler\config12.txt /data/local/tmp/", text=True, encoding="utf-8")
132        task_thread = threading.Thread(target=task, args=(CONFIG_NETWORK_PROFILER_NONDEBUG, ))
133        time.sleep(SLEEP_TWO)
134        task_thread.start()
135        time.sleep(SLEEP_THREE)
136        subprocess.check_output("hdc shell aa start -a EntryAbility -b com.tencent.mtthm")
137        time.sleep(SLEEP_FIVE)
138        subprocess.check_output("hdc shell uinput -T -c 850 1550")
139        time.sleep(SLEEP_TWO)
140        touch_button("微信")
141        task_thread.join()
142
143        subprocess.check_output("hdc shell chmod 777 /data/local/tmp/hookDecoder")
144        subprocess.check_output("hdc shell ./data/local/tmp/hookDecoder -f /data/local/tmp/test12.htrace > /data/local/tmp/test12_result.txt")
145        subprocess.check_output(f"hdc file recv /data/local/tmp/test12.htrace .\..\outputfiles\ ", text=True, encoding="utf-8")
146        subprocess.check_output(f"hdc file recv /data/local/tmp/test12_result.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
147        count = 0
148        with open(r'.\..\outputfiles\test12_result.txt', 'r') as file:
149            lines = file.readlines()
150            for line in lines:
151                if "tv_nsec" in line:
152                    count += 1
153        assert count == 0
154
155    @pytest.mark.L0
156    def test_usermode_kernel_symbols(self):
157        subprocess.check_output(r"hdc file recv /data/local/tmp/test1.htrace .\..\outputfiles\ ", text=True, encoding="utf-8")
158        subprocess.check_output(r".\..\inputfiles\trace_streamer_nativehook.exe .\..\outputfiles\test1.htrace -e .\..\outputfiles\nativehook.db", text=True, encoding="utf-8")
159        conn = sqlite3.connect(r'./../outputfiles/nativehook.db')
160        cursor = conn.cursor()
161        cursor.execute('SELECT * FROM data_dict')
162        result = cursor.fetchall()
163        check = True
164        for row in result:
165            if 'kallsyms' in row[1]:
166                check = False
167        assert check
168