xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-041-lowpan-fragmentation.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: This test verifies 6LoWPAN fragmentation code by exchanging IPv6 messages with
34# many different lengths between two nodes.
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
46node1 = wpan.Node()
47node2 = wpan.Node()
48
49# -----------------------------------------------------------------------------------------------------------------------
50# Init all nodes
51
52wpan.Node.init_all_nodes()
53
54# -----------------------------------------------------------------------------------------------------------------------
55# Build network topology
56
57# Two-node network (node1 leader/router, node2 end-device)
58
59node1.form('horizon')  # "zero dawn"
60node2.join_node(node1, node_type=wpan.JOIN_TYPE_END_DEVICE)
61
62# -----------------------------------------------------------------------------------------------------------------------
63# Test implementation
64
65# Get the link local addresses
66ll1 = node1.get(wpan.WPAN_IP6_LINK_LOCAL_ADDRESS)[1:-1]
67ll2 = node2.get(wpan.WPAN_IP6_LINK_LOCAL_ADDRESS)[1:-1]
68
69PORT = 1234
70MSG_LEN_START = 100
71MSG_LEN_END = 500
72
73for msg_length in range(MSG_LEN_START, MSG_LEN_END):
74    sender = node1.prepare_tx((ll1, PORT), (ll2, PORT), msg_length)
75    recver = node2.prepare_rx(sender)
76
77    wpan.Node.perform_async_tx_rx()
78
79    verify(sender.was_successful)
80    verify(recver.was_successful)
81
82# -----------------------------------------------------------------------------------------------------------------------
83# Test finished
84
85wpan.Node.finalize_all_nodes()
86
87print('\'{}\' passed.'.format(test_name))
88