• 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.
15import pytest
16import re
17from utils import *
18
19@print_check_result
20def CheckNetTraffic(output):
21    result = re.search("Received Bytes:\d+\nSent Bytes:\d+\n", output)
22    return result is not None
23
24@print_check_result
25def CheckNetstat(output):
26    netstat = re.search("cmd is: netstat -nW\n", output)
27    result = re.search("Proto RefCnt Flags\s+Type\s+State\s+I-Node Path\n([^\n]+\n){4,}", output)
28    return result is not None and netstat is not None
29
30@print_check_result
31def CheckNetDev(output):
32    proc_net_dev = re.search("/proc/net/dev\n", output)
33    result = re.search("face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compresse\n([^\n]+\n){4,}", output)
34    return result is not None and proc_net_dev is not None
35
36@print_check_result
37def CheckIfconfig(output):
38    result = re.search("cmd is: ifconfig -a\n\n([^\n]+){4,}\n", output)
39    assert re.search("Loopback", output) is not None
40    assert re.search("inet", output) is not None
41    return result is not None
42
43@print_check_result
44def CheckIptables(output):
45    result1 = re.search("cmd is: iptables -L -nvx\n\ncmd is: ip6tables -L -nvx\n\ncmd is: iptables -t nat -L -nvx", output)
46    result2 = re.search("cmd is: iptables -t mangle -L -nvx\n\ncmd is: ip6tables -t mangle -L -nvx\n\ncmd is: iptables -t raw -L -nvx", output)
47    result3 = re.search("cmd is: ip6tables -t raw -L -nvx", output)
48    return result1 is not None and result2 is not None and result3 is not None
49
50@print_check_result
51def CheckNetDev(output):
52    assert re.search("/sys/kernel/debug/binder/failed_transaction_log", output) is not None
53    assert re.search("call  from", output) is not None
54    assert re.search("/sys/kernel/debug/binder/transaction_log\n", output) is not None
55    assert re.search("context binder", output) is not None
56    assert re.search("/sys/kernel/debug/binder/transactions\n", output) is not None
57    assert re.search("binder transactions:", output) is not None
58    assert re.search("/sys/kernel/debug/binder/stats\n", output) is not None
59    assert re.search("binder stats:", output) is not None
60    assert re.search("/sys/kernel/debug/binder/state\n", output) is not None
61    lines = output.split(': u')
62    isShowAddress = False
63    for line in lines:
64        if "0000000000000000" not in line and " c0" in line:
65            print(line)
66            isShowAddress = True
67            break
68    if IsRootVersion():
69        assert isShowAddress
70    else:
71        assert not isShowAddress
72    result = re.search("binder state:", output) is not None
73    return result is not None
74
75def CheckNetAllOutput(output):
76    ret = all([CheckNetTraffic(output), CheckNetstat(output), CheckNetDev(output), CheckIfconfig(output)])
77    return ret
78
79class TestHidumperNet:
80
81    @pytest.mark.L0
82    def test_net_all(self):
83        command = f"hidumper --net"
84        hidumperTmpCmd = "OPT:net SUB_OPT:"
85        # 校验命令行输出
86        CheckCmd(command, CheckNetAllOutput, hidumperTmpCmd)
87
88    @pytest.mark.L0
89    def test_net_pid(self):
90        command = f"hidumper --net `pidof samgr`"
91        hidumperTmpCmd = "OPT:net SUB_OPT:"
92        # 校验命令行输出
93        CheckCmd(command, CheckNetTraffic, hidumperTmpCmd)
94        # 校验命令行重定向输出
95        CheckCmdRedirect(command, CheckNetTraffic, None, hidumperTmpCmd)
96        # 校验命令行输出到zip文件
97        CheckCmdZip(command, CheckNetTraffic)
98
99    @pytest.mark.L3
100    def test_net_error_pid(self):
101        command = f"hidumper --net 2147483647;hidumper --net -2147483647"
102        hidumperTmpCmd = "OPT:net SUB_OPT:"
103        # 校验命令行输出
104        CheckCmd(command, lambda output : "hidumper: No such process: 2147483647\nhidumper: option pid missed. 2" in output, hidumperTmpCmd)
105        command = f"hidumper --net 2147483648;hidumper --net -2147483648"
106        CheckCmd(command, lambda output : "hidumper: option pid missed. 2" in output, hidumperTmpCmd)