From f5e995135eacab61a5cc041d1b5b92172d88fab7 Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Fri, 29 Oct 2021 12:41:07 +0800 Subject: [PATCH] [thread-cert] re-add `test_mac_scan` test (#7112) --- tests/scripts/thread-cert/Makefile.am | 2 ++ tests/scripts/thread-cert/node.py | 28 +++++++++++++++++-- .../{Test_MacScan.py => test_mac_scan.py} | 24 +++++++++++----- 3 files changed, 44 insertions(+), 10 deletions(-) rename tests/scripts/thread-cert/{Test_MacScan.py => test_mac_scan.py} (77%) diff --git a/tests/scripts/thread-cert/Makefile.am b/tests/scripts/thread-cert/Makefile.am index 43356a1eb..95ad05a39 100644 --- a/tests/scripts/thread-cert/Makefile.am +++ b/tests/scripts/thread-cert/Makefile.am @@ -170,6 +170,7 @@ EXTRA_DIST = \ test_ipv6_source_selection.py \ test_lowpan.py \ test_mac802154.py \ + test_mac_scan.py \ test_mle.py \ test_netdata_publisher.py \ test_network_data.py \ @@ -241,6 +242,7 @@ check_SCRIPTS = \ test_ipv6_source_selection.py \ test_lowpan.py \ test_mac802154.py \ + test_mac_scan.py \ test_mle.py \ test_netdata_publisher.py \ test_network_data.py \ diff --git a/tests/scripts/thread-cert/node.py b/tests/scripts/thread-cert/node.py index 7b53bfaa1..a13b314d7 100755 --- a/tests/scripts/thread-cert/node.py +++ b/tests/scripts/thread-cert/node.py @@ -1289,6 +1289,10 @@ class NodeImpl: self.send_command('extpanid %s' % extpanid) self._expect_done() + def get_extpanid(self): + self.send_command('extpanid') + return self._expect_result('[0-9a-fA-F]{16}') + def get_joiner_id(self): self.send_command('joiner id') return self._expect_result('[0-9a-fA-F]{16}') @@ -1922,12 +1926,30 @@ class NodeImpl: self._expect('Conflict:', timeout=timeout) - def scan(self, result=1): + def scan(self, result=1, timeout=10): self.send_command('scan') + self.simulator.go(timeout) + if result == 1: - return self._expect_results( - r'\|\s(\S+)\s+\|\s(\S+)\s+\|\s([0-9a-fA-F]{4})\s\|\s([0-9a-fA-F]{16})\s\|\s(\d+)') + networks = [] + for line in self._expect_command_output()[2:]: + _, J, networkname, extpanid, panid, extaddr, channel, dbm, lqi, _ = map(str.strip, line.split('|')) + J = bool(int(J)) + panid = int(panid, 16) + channel, dbm, lqi = map(int, (channel, dbm, lqi)) + + networks.append({ + 'joinable': J, + 'networkname': networkname, + 'extpanid': extpanid, + 'panid': panid, + 'extaddr': extaddr, + 'channel': channel, + 'dbm': dbm, + 'lqi': lqi, + }) + return networks def ping(self, ipaddr, num_responses=1, size=8, timeout=5, count=1, interval=1, hoplimit=64, interface=None): args = f'{ipaddr} {size} {count} {interval} {hoplimit} {timeout}' diff --git a/tests/scripts/thread-cert/Test_MacScan.py b/tests/scripts/thread-cert/test_mac_scan.py similarity index 77% rename from tests/scripts/thread-cert/Test_MacScan.py rename to tests/scripts/thread-cert/test_mac_scan.py index bdc622894..e3214b89a 100755 --- a/tests/scripts/thread-cert/Test_MacScan.py +++ b/tests/scripts/thread-cert/test_mac_scan.py @@ -26,8 +26,7 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # - -import time +import logging import unittest import thread_cert @@ -35,17 +34,22 @@ import thread_cert LEADER = 1 ROUTER = 2 +CHANNEL = 12 + class Test_MacScan(thread_cert.TestCase): + USE_MESSAGE_FACTORY = False + SUPPORT_NCP = False + TOPOLOGY = { LEADER: { - 'channel': 12, + 'channel': CHANNEL, 'mode': 'rdn', 'network_name': 'OpenThread', 'allowlist': [ROUTER] }, ROUTER: { - 'channel': 12, + 'channel': CHANNEL, 'mode': 'rdn', 'network_name': 'OpenThread', 'allowlist': [LEADER] @@ -58,11 +62,17 @@ class Test_MacScan(thread_cert.TestCase): self.assertEqual(self.nodes[LEADER].get_state(), 'leader') self.nodes[ROUTER].start() - time.sleep(5) + self.simulator.go(10) self.assertEqual(self.nodes[ROUTER].get_state(), 'router') - results = self.nodes[LEADER].scan() - self.assertEqual(len(results), 16) + results = self.nodes[LEADER].scan(result=True) + logging.info('scan result: %r', results) + self.assertEqual(len(results), 1) + network = results[0] + self.assertEqual(network['extaddr'], self.nodes[ROUTER].get_addr64()) + self.assertEqual(network['extpanid'], self.nodes[ROUTER].get_extpanid()) + self.assertEqual(network['networkname'], self.nodes[ROUTER].get_network_name()) + self.assertEqual(network['channel'], CHANNEL) if __name__ == '__main__':