1#!/usr/bin/env python3
2#
3#  Copyright (c) 2024, 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 cli import verify
30from cli import verify_within
31import cli
32import time
33
34# -----------------------------------------------------------------------------------------------------------------------
35# Test description:
36#
37# Two BRs on two different Thread networks.
38
39test_name = __file__[:-3] if __file__.endswith('.py') else __file__
40print('-' * 120)
41print('Starting \'{}\''.format(test_name))
42
43# -----------------------------------------------------------------------------------------------------------------------
44# Creating `cli.Nodes` instances
45
46speedup = 60
47cli.Node.set_time_speedup_factor(speedup)
48
49br1 = cli.Node()
50br2 = cli.Node()
51
52WAIT_TIME = 5
53IF_INDEX = 1
54
55# -----------------------------------------------------------------------------------------------------------------------
56# Test implementation
57
58# Start first BR with its own Thread network
59
60br1.form('net1')
61verify(br1.get_state() == 'leader')
62
63br1.br_init(IF_INDEX, 1)
64br1.br_enable()
65
66time.sleep(1)
67verify(br1.br_get_state() == 'running')
68
69br1_local_omr = br1.br_get_local_omrprefix()
70br1_favored_omr = br1.br_get_favored_omrprefix().split()[0]
71verify(br1_local_omr == br1_favored_omr)
72
73br1_local_onlink = br1.br_get_local_onlinkprefix()
74br1_favored_onlink = br1.br_get_favored_onlinkprefix().split()[0]
75verify(br1_local_onlink == br1_favored_onlink)
76
77# Start second BR with its own Thread network.
78
79br2.form('net2')
80verify(br2.get_state() == 'leader')
81
82br2.br_init(IF_INDEX, 1)
83br2.br_enable()
84
85time.sleep(1)
86verify(br2.br_get_state() == 'running')
87
88br2_local_omr = br2.br_get_local_omrprefix()
89br2_favored_omr = br2.br_get_favored_omrprefix().split()[0]
90verify(br2_local_omr == br2_favored_omr)
91
92br2_local_onlink = br2.br_get_local_onlinkprefix()
93br2_favored_onlink = br2.br_get_favored_onlinkprefix().split()[0]
94verify(br2_local_onlink != br2_favored_onlink)
95
96# BR2 should see and favor the on-link prefix already advertised by BR1.
97
98verify(br1_favored_onlink == br2_favored_onlink)
99
100# Check that the two BRs discover and track each other (not as peer BR since
101# connected to different networks).
102
103for br in [br1, br1]:
104    routers = br.br_get_routers()
105    verify(len(routers) > 0)
106    for router in routers:
107        verify('reachable:yes' in router)
108        verify('Stub:1' in router)
109        verify(not router.endswith('(peer BR)'))
110
111# -----------------------------------------------------------------------------------------------------------------------
112# Test finished
113
114cli.Node.finalize_all_nodes()
115
116print('\'{}\' passed.'.format(test_name))
117