xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-042-meshcop-joiner-discerner.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1#!/usr/bin/env python3
2#
3#  Copyright (c) 2020, 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
29import wpan
30from wpan import verify
31
32# -----------------------------------------------------------------------------------------------------------------------
33# Test description: Test use of Joiner Discerner for commissioning
34#
35
36test_name = __file__[:-3] if __file__.endswith('.py') else __file__
37print('-' * 120)
38print('Starting \'{}\''.format(test_name))
39
40# -----------------------------------------------------------------------------------------------------------------------
41# Creating `wpan.Nodes` instances
42
43speedup = 4
44wpan.Node.set_time_speedup_factor(speedup)
45
46commr = wpan.Node()
47joiner1 = wpan.Node()
48joiner2 = wpan.Node()
49joiner3 = wpan.Node()
50
51# -----------------------------------------------------------------------------------------------------------------------
52# Init all nodes
53
54wpan.Node.init_all_nodes()
55
56# -----------------------------------------------------------------------------------------------------------------------
57# Build network topology
58
59commr.form('discerner')
60
61# -----------------------------------------------------------------------------------------------------------------------
62# Test implementation
63
64WAIT_TIME = 2  # seconds
65
66PSK1 = 'UNCHARTEDTHEL0STLEGACY'
67PSK2 = 'UNCHARTED4ATH1EFSEND'
68PSK3 = 'THELAST0FUS'
69
70DISCERNER1 = '0x777'
71D_LEN1 = 12
72
73DISCERNER2 = '0x7777777'
74D_LEN2 = 32
75
76EUI64_3 = joiner3.get(wpan.WPAN_HW_ADDRESS)[1:-1]  # Remove the "[]"
77
78JOINER_TIMOUT = '500'  # in seconds
79
80# Verify Discerner value after device reset on Joiner
81
82verify(int(joiner1.get(wpan.WPAN_THREAD_JOINER_DISCERNER_BIT_LENGTH), 0) == 0)
83verify(int(joiner1.get(wpan.WPAN_THREAD_JOINER_DISCERNER_VALUE), 0) == 0)
84
85# Set Joiner Discerner on `joiner1` and `joiner2`
86
87joiner1.set(wpan.WPAN_THREAD_JOINER_DISCERNER_BIT_LENGTH, str(D_LEN1))
88joiner1.set(wpan.WPAN_THREAD_JOINER_DISCERNER_VALUE, DISCERNER1)
89verify(int(joiner1.get(wpan.WPAN_THREAD_JOINER_DISCERNER_BIT_LENGTH), 0) == D_LEN1)
90verify(int(joiner1.get(wpan.WPAN_THREAD_JOINER_DISCERNER_VALUE), 0) == int(DISCERNER1, 0))
91
92joiner2.set(wpan.WPAN_THREAD_JOINER_DISCERNER_BIT_LENGTH, str(D_LEN2))
93joiner2.set(wpan.WPAN_THREAD_JOINER_DISCERNER_VALUE, DISCERNER2)
94verify(int(joiner2.get(wpan.WPAN_THREAD_JOINER_DISCERNER_BIT_LENGTH), 0) == D_LEN2)
95verify(int(joiner2.get(wpan.WPAN_THREAD_JOINER_DISCERNER_VALUE), 0) == int(DISCERNER2, 0))
96
97# Check clearing of a previously set Joiner Discerner
98
99joiner3.set(wpan.WPAN_THREAD_JOINER_DISCERNER_BIT_LENGTH, str(D_LEN2))
100joiner3.set(wpan.WPAN_THREAD_JOINER_DISCERNER_VALUE, DISCERNER2)
101verify(int(joiner3.get(wpan.WPAN_THREAD_JOINER_DISCERNER_BIT_LENGTH), 0) == D_LEN2)
102verify(int(joiner3.get(wpan.WPAN_THREAD_JOINER_DISCERNER_VALUE), 0) == int(DISCERNER2, 0))
103
104joiner3.set(wpan.WPAN_THREAD_JOINER_DISCERNER_BIT_LENGTH, '0')
105verify(int(joiner3.get(wpan.WPAN_THREAD_JOINER_DISCERNER_BIT_LENGTH), 0) == 0)
106verify(int(joiner3.get(wpan.WPAN_THREAD_JOINER_DISCERNER_VALUE), 0) == 0)
107
108# Adding Joiners on Commissioner
109
110commr.commissioner_start()
111
112commr.commissioner_add_joiner_with_discerner(DISCERNER1, D_LEN1, PSK1, JOINER_TIMOUT)
113commr.commissioner_add_joiner_with_discerner(DISCERNER2, D_LEN2, PSK2, JOINER_TIMOUT)
114commr.commissioner_add_joiner(EUI64_3, PSK3, JOINER_TIMOUT)
115
116verify(len(wpan.parse_list(commr.get(wpan.WPAN_THREAD_COMMISSIONER_JOINERS))) == 3)
117
118#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119# Start `joiner2` first
120
121# Starting with `joiner2` verifies the behavior of Commissioner to
122# prefer the Joiner entry with the longest matching discriminator. Note
123# that `joiner2` uses a longer discriminator compared to `joiner1` with
124# similar value.
125
126joiner2.joiner_join(PSK2)
127verify(joiner2.get(wpan.WPAN_STATE) == wpan.STATE_COMMISSIONED)
128joiner2.joiner_attach()
129
130
131def joiner2_is_associated():
132    verify(joiner2.is_associated())
133
134
135wpan.verify_within(joiner2_is_associated, WAIT_TIME)
136
137# Start `joiner1`
138
139joiner1.joiner_join(PSK1)
140verify(joiner1.get(wpan.WPAN_STATE) == wpan.STATE_COMMISSIONED)
141joiner1.joiner_attach()
142
143
144def joiner1_is_associated():
145    verify(joiner1.is_associated())
146
147
148wpan.verify_within(joiner1_is_associated, WAIT_TIME)
149
150# Start `joiner3`
151
152joiner3.joiner_join(PSK3)
153verify(joiner3.get(wpan.WPAN_STATE) == wpan.STATE_COMMISSIONED)
154joiner3.joiner_attach()
155
156
157def joiner3_is_associated():
158    verify(joiner3.is_associated())
159
160
161wpan.verify_within(joiner3_is_associated, WAIT_TIME)
162
163# -----------------------------------------------------------------------------------------------------------------------
164# Test finished
165
166wpan.Node.finalize_all_nodes()
167
168print('\'{}\' passed.'.format(test_name))
169