• 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 sys
20sys.path.append("..")
21from tools.utils import *
22import threading
23
24LIB_PATH = "/system/lib"
25THRESH = 25000000000
26SLEEP_TWO = 2
27SLEEP_THIRTY = 30
28CONFIG_INDEX = 6
29PORT_INDEX = 5
30MALLOCTIME_INDEX = 3
31LISTENURI_INDEX = 3
32
33
34def task(index):
35    indexstr = str(index)
36    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")
37
38
39def malloctest():
40    subprocess.check_output("hdc shell chmod 777 /data/local/tmp/malloctest")
41    subprocess.check_output("hdc shell ./data/local/tmp/malloctest 10 1024 1000000 > /data/local/tmp/malloctest.txt")
42
43
44class TestHiprofilerMalloctime:
45    @pytest.mark.L0
46    def test_malloctime(self):
47        subprocess.check_output(f"hdc file send .\inputfiles\nativehook\config6.txt /data/local/tmp/", text=True, encoding="utf-8")
48        malloc_thread = threading.Thread(target=malloctest)
49        task_thread = threading.Thread(target=task, args=(CONFIG_INDEX, ))
50        malloc_thread.start()
51        time.sleep(SLEEP_TWO)
52        task_thread.start()
53        time.sleep(SLEEP_THIRTY)
54        malloc_thread.join()
55        task_thread.join()
56
57        subprocess.check_output(f"hdc file recv /data/local/tmp/malloctest.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
58        malloctime = 0
59
60        with open(r'.\..\outputfiles\malloctest.txt', 'r') as file:
61            lines = file.readlines()
62            malloctime = int(lines[MALLOCTIME_INDEX].split()[MALLOCTIME_INDEX])
63        assert (malloctime < THRESH)
64
65    @pytest.mark.L0
66    def test_listenuri(self):
67        port = 0
68        task_thread = threading.Thread(target=task, args=(CONFIG_INDEX, ))
69        task_thread.start()
70        subprocess.check_output("hdc shell hiprofiler_cmd -q > /data/local/tmp/cmdtmp.txt")
71        subprocess.check_output(f"hdc file recv /data/local/tmp/cmdtmp.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
72        check = False
73        with open(r'.\..\outputfiles\cmdtmp.txt', 'r') as file:
74            lines = file.readlines()
75            for line in lines:
76                if "port" in line:
77                    port = int(line[PORT_INDEX:])
78
79
80        subprocess.check_output("hdc shell netstat -anp | grep " + str(port) + " > /data/local/tmp/uri.txt")
81        subprocess.check_output(f"hdc file recv /data/local/tmp/uri.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
82        check = False
83        with open(r'.\..\outputfiles\uri.txt', 'r') as file:
84            lines = file.readlines()
85            for line in lines:
86                if "LISTEN" in line:
87                    target = line.split()[LISTENURI_INDEX]
88                    if "127.0.0.1" in target:
89                        check = True
90        task_thread.join()
91        assert check
92