xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-009-insecure-traffic-join.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2018, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Workerimport wpan
30*cfb92d14SAndroid Build Coastguard Workerfrom wpan import verify
31*cfb92d14SAndroid Build Coastguard Worker
32*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
33*cfb92d14SAndroid Build Coastguard Worker# Test description: Check insecure data transmission during joining.
34*cfb92d14SAndroid Build Coastguard Worker
35*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
36*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
37*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
38*cfb92d14SAndroid Build Coastguard Worker
39*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
40*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Workernode1 = wpan.Node()
43*cfb92d14SAndroid Build Coastguard Workernode2 = wpan.Node()
44*cfb92d14SAndroid Build Coastguard Worker
45*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
46*cfb92d14SAndroid Build Coastguard Worker# Init all nodes
47*cfb92d14SAndroid Build Coastguard Worker
48*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes()
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
51*cfb92d14SAndroid Build Coastguard Worker# Build network topology
52*cfb92d14SAndroid Build Coastguard Worker
53*cfb92d14SAndroid Build Coastguard Workernode1.form("insec-join-test")
54*cfb92d14SAndroid Build Coastguard Worker
55*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
56*cfb92d14SAndroid Build Coastguard Worker# Test implementation
57*cfb92d14SAndroid Build Coastguard Worker
58*cfb92d14SAndroid Build Coastguard Workerinsecure_port = 1234
59*cfb92d14SAndroid Build Coastguard WorkerNUM_MSGS = 4
60*cfb92d14SAndroid Build Coastguard Worker
61*cfb92d14SAndroid Build Coastguard Worker# Make node1 joinable and set the insecure port
62*cfb92d14SAndroid Build Coastguard Workernode1.permit_join(duration_sec='100', port=str(insecure_port))
63*cfb92d14SAndroid Build Coastguard Worker
64*cfb92d14SAndroid Build Coastguard Worker# Join node1 network from node2 without setting the key
65*cfb92d14SAndroid Build Coastguard Workernode2.join_node(node1, should_set_key=False)
66*cfb92d14SAndroid Build Coastguard Worker
67*cfb92d14SAndroid Build Coastguard Workerverify(node2.get(wpan.WPAN_STATE) == wpan.STATE_CREDENTIALS_NEEDED)
68*cfb92d14SAndroid Build Coastguard Workerverify(node2.get(wpan.WPAN_NAME) == node1.get(wpan.WPAN_NAME))
69*cfb92d14SAndroid Build Coastguard Workerverify(node2.get(wpan.WPAN_PANID) == node1.get(wpan.WPAN_PANID))
70*cfb92d14SAndroid Build Coastguard Workerverify(node2.get(wpan.WPAN_XPANID) == node1.get(wpan.WPAN_XPANID))
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Workerll1 = node1.get(wpan.WPAN_IP6_LINK_LOCAL_ADDRESS)[1:-1]
73*cfb92d14SAndroid Build Coastguard Workerll2 = node2.get(wpan.WPAN_IP6_LINK_LOCAL_ADDRESS)[1:-1]
74*cfb92d14SAndroid Build Coastguard Worker
75*cfb92d14SAndroid Build Coastguard Worker# Send insecure traffic from node2 to node1 using link-local IP address
76*cfb92d14SAndroid Build Coastguard Worker# for src/dst and insecure port number
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Workersender = node2.prepare_tx(ll2, (ll1, insecure_port), "Hi (insecure)", NUM_MSGS)
79*cfb92d14SAndroid Build Coastguard Workerrecver = node1.prepare_rx(sender)
80*cfb92d14SAndroid Build Coastguard Workerwpan.Node.perform_async_tx_rx()
81*cfb92d14SAndroid Build Coastguard Workerverify(sender.was_successful)
82*cfb92d14SAndroid Build Coastguard Workerverify(recver.was_successful)
83*cfb92d14SAndroid Build Coastguard Worker
84*cfb92d14SAndroid Build Coastguard Worker# Get the random src port number used by node1 and ensure node2 allows
85*cfb92d14SAndroid Build Coastguard Worker# insecure rx traffic on that port
86*cfb92d14SAndroid Build Coastguard Worker
87*cfb92d14SAndroid Build Coastguard Workerrx_port = recver.all_rx_msg[0][1][1]
88*cfb92d14SAndroid Build Coastguard Workernode2.permit_join(duration_sec='100', port=str(rx_port))
89*cfb92d14SAndroid Build Coastguard Worker
90*cfb92d14SAndroid Build Coastguard Worker# Send insecure reply from node1 to node2
91*cfb92d14SAndroid Build Coastguard Worker
92*cfb92d14SAndroid Build Coastguard Workersender2 = node1.prepare_tx((ll1, insecure_port), (ll2, rx_port), "Hi back! (insecure)", NUM_MSGS)
93*cfb92d14SAndroid Build Coastguard Workerrecver2 = node2.prepare_rx(sender2)
94*cfb92d14SAndroid Build Coastguard Workerwpan.Node.perform_async_tx_rx()
95*cfb92d14SAndroid Build Coastguard Workerverify(sender2.was_successful)
96*cfb92d14SAndroid Build Coastguard Workerverify(recver2.was_successful)
97*cfb92d14SAndroid Build Coastguard Worker
98*cfb92d14SAndroid Build Coastguard Worker# Now node2 fully joins the network (set the network key), check all
99*cfb92d14SAndroid Build Coastguard Worker# secure traffic exchange between the nodes
100*cfb92d14SAndroid Build Coastguard Worker
101*cfb92d14SAndroid Build Coastguard Workernode2.set(wpan.WPAN_KEY, node1.get(wpan.WPAN_KEY)[1:-1], binary_data=True)
102*cfb92d14SAndroid Build Coastguard Workerverify(node2.is_associated())
103*cfb92d14SAndroid Build Coastguard Worker
104*cfb92d14SAndroid Build Coastguard Workernode1.permit_join('0')
105*cfb92d14SAndroid Build Coastguard Worker
106*cfb92d14SAndroid Build Coastguard Workersender = node2.prepare_tx(ll2, (ll1, insecure_port), "Hi (now secure)", NUM_MSGS)
107*cfb92d14SAndroid Build Coastguard Workerrecver = node1.prepare_rx(sender)
108*cfb92d14SAndroid Build Coastguard Workerwpan.Node.perform_async_tx_rx()
109*cfb92d14SAndroid Build Coastguard Workerverify(sender.was_successful)
110*cfb92d14SAndroid Build Coastguard Workerverify(recver.was_successful)
111*cfb92d14SAndroid Build Coastguard Worker
112*cfb92d14SAndroid Build Coastguard Workernode2.permit_join('0')
113*cfb92d14SAndroid Build Coastguard Worker
114*cfb92d14SAndroid Build Coastguard Workersender2 = node1.prepare_tx((ll1, insecure_port), (ll2, rx_port), "Hi back! (secure now)", NUM_MSGS)
115*cfb92d14SAndroid Build Coastguard Workerrecver2 = node2.prepare_rx(sender2)
116*cfb92d14SAndroid Build Coastguard Workerwpan.Node.perform_async_tx_rx()
117*cfb92d14SAndroid Build Coastguard Workerverify(sender2.was_successful)
118*cfb92d14SAndroid Build Coastguard Workerverify(recver2.was_successful)
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
121*cfb92d14SAndroid Build Coastguard Worker# Test finished
122*cfb92d14SAndroid Build Coastguard Worker
123*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes()
124*cfb92d14SAndroid Build Coastguard Worker
125*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
126