xref: /aosp_15_r20/external/openthread/tests/scripts/thread-cert/test_diag.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1#!/usr/bin/env python3
2#
3#  Copyright (c) 2018, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29
30import time
31import unittest
32
33import thread_cert
34
35
36class TestDiag(thread_cert.TestCase):
37    SUPPORT_NCP = False
38
39    TOPOLOGY = {1: None}
40
41    def test(self):
42        node = self.nodes[1]
43
44        cases = [
45            ('diag\n', 'diagnostics mode is disabled\r\n'),
46            ('diag send 10 100\n', 'Error 13: InvalidState\r\n'),
47            ('diag start\n', 'Done\r\n'),
48            ('diag invalid test\n', 'diag feature \'invalid\' is not supported'),
49            ('diag', 'diagnostics mode is enabled\r\n'),
50            ('diag channel 10\n', 'failed\r\nstatus 0x7\r\n'),
51            ('diag channel 11\n', 'set channel to 11\r\nstatus 0x00\r\n'),
52            ('diag channel\n', 'channel: 11\r\n'),
53            ('diag power -10\n', 'set tx power to -10 dBm\r\nstatus 0x00\r\n'),
54            ('diag power\n', 'tx power: -10 dBm\r\n'),
55            (
56                'diag stats\n',
57                'received packets: 0\r\nsent packets: 0\r\n'
58                'first received packet: rssi=0, lqi=0\r\n'
59                'last received packet: rssi=0, lqi=0\r\n',
60            ),
61            (
62                'diag send 20 100\n',
63                r'sending 0x14 packet\(s\), length 0x64\r\nstatus 0x00\r\n',
64            ),
65            (
66                '  diag \t send    \t 20\t100',
67                r'sending 0x14 packet\(s\), length 0x64\r\nstatus 0x00\r\n',
68            ),
69            (
70                'diag repeat 100 100\n',
71                'sending packets of length 0x64 at the delay of 0x64 ms\r\nstatus 0x00\r\n',
72            ),
73            (
74                'diag repeat stop\n',
75                'repeated packet transmission is stopped\r\nstatus 0x00\r\n',
76            ),
77            (
78                'diag stop\n',
79                r'received packets: 0\r\nsent packets: ([1-9]\d*)\r\n'
80                'first received packet: rssi=0, lqi=0\r\n'
81                'last received packet: rssi=0, lqi=0\r\n\n'
82                r'stop diagnostics mode\r\nstatus 0x00\r\n',
83            ),
84            ('diag', 'diagnostics mode is disabled\r\n'),
85            (
86                'diag 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32',
87                r'Error 7: InvalidArgs\r\n',
88            ),
89        ]
90
91        for case in cases:
92            node.send_command(case[0], expect_command_echo=False)
93            self.simulator.go(1)
94            if type(self.simulator).__name__ == 'VirtualTime':
95                time.sleep(0.1)
96            node._expect(case[1])
97
98
99if __name__ == '__main__':
100    unittest.main()
101