xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-003-join.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
29from wpan import verify
30import wpan
31
32# -----------------------------------------------------------------------------------------------------------------------
33# Test description: joining (as router, end-device, sleepy) - two node network
34
35test_name = __file__[:-3] if __file__.endswith('.py') else __file__
36print('-' * 120)
37print('Starting \'{}\''.format(test_name))
38
39# -----------------------------------------------------------------------------------------------------------------------
40# Creating `wpan.Nodes` instances
41
42speedup = 4
43wpan.Node.set_time_speedup_factor(speedup)
44
45node1 = wpan.Node()
46node2 = wpan.Node()
47
48# -----------------------------------------------------------------------------------------------------------------------
49# Init all nodes
50
51wpan.Node.init_all_nodes()
52
53# -----------------------------------------------------------------------------------------------------------------------
54# Test implementation
55
56# Form a network on node1
57node1.form('PAN-aB71n')
58verify(node1.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED)
59verify(node1.get(wpan.WPAN_NODE_TYPE) == wpan.NODE_TYPE_LEADER)
60
61# Join from node2 as a router
62node2.join_node(node1, node_type=wpan.JOIN_TYPE_ROUTER)
63verify(node2.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED)
64verify(node2.get(wpan.WPAN_NAME) == node1.get(wpan.WPAN_NAME))
65verify(node2.get(wpan.WPAN_PANID) == node1.get(wpan.WPAN_PANID))
66verify(node2.get(wpan.WPAN_XPANID) == node1.get(wpan.WPAN_XPANID))
67verify(node2.get(wpan.WPAN_KEY) == node1.get(wpan.WPAN_KEY))
68
69node2.leave()
70
71# Join from node2 as an end-device
72node2.join_node(node1, node_type=wpan.JOIN_TYPE_END_DEVICE)
73verify(node2.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED)
74verify(node2.get(wpan.WPAN_NAME) == node1.get(wpan.WPAN_NAME))
75verify(node2.get(wpan.WPAN_PANID) == node1.get(wpan.WPAN_PANID))
76verify(node2.get(wpan.WPAN_XPANID) == node1.get(wpan.WPAN_XPANID))
77verify(node2.get(wpan.WPAN_KEY) == node1.get(wpan.WPAN_KEY))
78
79node2.leave()
80
81# Join from node2 as a sleepy-end-device
82node2.join_node(node1, node_type=wpan.JOIN_TYPE_SLEEPY_END_DEVICE)
83verify(node2.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED)
84verify(node2.get(wpan.WPAN_NAME) == node1.get(wpan.WPAN_NAME))
85verify(node2.get(wpan.WPAN_PANID) == node1.get(wpan.WPAN_PANID))
86verify(node2.get(wpan.WPAN_XPANID) == node1.get(wpan.WPAN_XPANID))
87verify(node2.get(wpan.WPAN_KEY) == node1.get(wpan.WPAN_KEY))
88
89node2.leave()
90
91# Join from node2 without setting network key
92node2.join_node(node1, should_set_key=False)
93verify(node2.get(wpan.WPAN_STATE) == wpan.STATE_CREDENTIALS_NEEDED)
94verify(node2.get(wpan.WPAN_NAME) == node1.get(wpan.WPAN_NAME))
95verify(node2.get(wpan.WPAN_PANID) == node1.get(wpan.WPAN_PANID))
96verify(node2.get(wpan.WPAN_XPANID) == node1.get(wpan.WPAN_XPANID))
97node2.set(wpan.WPAN_KEY, node1.get(wpan.WPAN_KEY)[1:-1], binary_data=True)
98verify(node2.get(wpan.WPAN_KEY) == node1.get(wpan.WPAN_KEY))
99verify(node2.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED)
100
101# -----------------------------------------------------------------------------------------------------------------------
102# Test finished
103
104wpan.Node.finalize_all_nodes()
105
106print('\'{}\' passed.'.format(test_name))
107