import pytest
import subprocess
import re
import sys
sys.path.append("..")
from tools.utils import *
import threading
LIB_PATH = "/system/lib"
THRESH = 25000000000
SLEEP_TWO = 2
SLEEP_THIRTY = 30
CONFIG_INDEX = 6
PORT_INDEX = 5
MALLOCTIME_INDEX = 3
LISTENURI_INDEX = 3
def task(index):
indexstr = str(index)
run_and_get_output(
r"hdc shell hiprofiler_cmd -c /data/local/tmp/config" + indexstr + ".txt -o /data/local/tmp/test" + indexstr + ".htrace -t 20 -s -k")
def malloctest():
run_and_get_output(
r"hdc shell chmod 777 /data/local/tmp/malloctest")
run_and_get_output(
r"hdc shell ./data/local/tmp/malloctest 10 1024 1000000 > /data/local/tmp/malloctest.txt")
class TestHiprofilerMalloctime:
@pytest.mark.L0
def test_malloctime(self):
run_and_get_output(
r"hdc file send .\inputfiles\nativehook\config6.txt /data/local/tmp/", text=True, encoding="utf-8")
malloc_thread = threading.Thread(target=malloctest)
task_thread = threading.Thread(target=task, args=(CONFIG_INDEX, ))
malloc_thread.start()
time.sleep(SLEEP_TWO)
task_thread.start()
time.sleep(SLEEP_THIRTY)
malloc_thread.join()
task_thread.join()
run_and_get_output(
r"hdc file recv /data/local/tmp/malloctest.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
malloctime = 0
with open(r'.\..\outputfiles\malloctest.txt', 'r') as file:
lines = file.readlines()
malloctime = int(lines[MALLOCTIME_INDEX].split()[MALLOCTIME_INDEX])
assert (malloctime < THRESH)
@pytest.mark.L0
def test_listenuri(self):
port = 0
task_thread = threading.Thread(target=task, args=(CONFIG_INDEX, ))
task_thread.start()
run_and_get_output(
r"hdc shell hiprofiler_cmd -q > /data/local/tmp/cmdtmp.txt")
run_and_get_output(
r"hdc file recv /data/local/tmp/cmdtmp.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
check = False
with open(r'.\..\outputfiles\cmdtmp.txt', 'r') as file:
lines = file.readlines()
for line in lines:
if "port" in line:
port = int(line[PORT_INDEX:])
run_and_get_output(
"hdc shell netstat -anp | grep " + str(port) + r" > /data/local/tmp/uri.txt")
run_and_get_output(
r"hdc file recv /data/local/tmp/uri.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
check = False
with open(r'.\..\outputfiles\uri.txt', 'r') as file:
lines = file.readlines()
for line in lines:
if "LISTEN" in line:
target = line.split()[LISTENURI_INDEX]
if "127.0.0.1" in target:
check = True
task_thread.join()
assert check