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
19from utils import *
20
21OUTPUT_PATH = "testModule/output"
22LIB_PATH = "/system/lib"
23ROM_BASE_SIZE = 1363
24RAM_LIMIT_SIZE = 20 * 1024
25
26def CheckRom(output):
27    result = re.findall("(/d+)K", output)
28    return sum(int(x) for x in result) <= ROM_BASE_SIZE
29
30def CheckRam(output):
31    result = re.findall("\s*Total\s*(\d+)\s*", output)[0]
32    return int(result) <= RAM_LIMIT_SIZE
33
34class TestHidumperPerformance:
35
36    @pytest.mark.L0
37    def test_rom(self):
38        # 校验命令行输出
39        output = subprocess.check_output(f"hdc shell ls -lh /system/lib/libhidumper*", shell=True, text=True, encoding="utf-8")
40        assert CheckRom(output)
41
42    @pytest.mark.L0
43    def test_ram(self):
44        # 校验命令行输出
45        subprocess.check_call(f"hdc shell hidumper --mem 1", shell=True, text=True, encoding="utf-8")
46        output = subprocess.check_output(f"hdc shell hidumper --mem `pidof hidumper_service`", shell=True, text=True, encoding="utf-8")
47        assert CheckRam(output)
48
49    @pytest.mark.L0
50    def test_file_tag(self):
51        # 校验命令行输出
52        output = subprocess.check_output(f"hdc shell \"ls -Z /system/bin | grep hidumper\"", shell=True, text=True, encoding="utf-8")
53        assert "u:object_r:hidumper_exec:s0" in output
54
55    @pytest.mark.L0
56    def test_process_tag(self):
57        # 校验命令行输出
58        subprocess.check_call(f"hdc shell hidumper --mem 1", shell=True, text=True, encoding="utf-8")
59        output = subprocess.check_output(f"hdc shell \"ps -eZ | grep hidumper\"", shell=True, text=True, encoding="utf-8")
60        assert "u:r:hidumper_service:s0" in output
61
62