1#!/usr/bin/env python3
2#
3#  Copyright (c) 2021, 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#
29
30import unittest
31import thread_cert
32import config
33
34import pktverify
35from pktverify.consts import MA1
36
37# Test description:
38# The purpose of this test case is to verify that a Border Router device is able to properly
39# forward traffic in response to multicast destination when it comes from another BBR.
40#
41# Topology:
42#    ----------------(eth)------------------
43#           |                  |     |
44#          BR1 (Leader) ------ TD   HOST
45#
46
47BR_1 = 1
48TD = 2
49HOST = 3
50
51
52class ManualMulticastAddressConfig(thread_cert.TestCase):
53    USE_MESSAGE_FACTORY = False
54
55    TOPOLOGY = {
56        BR_1: {
57            'name': 'BR_1',
58            'is_otbr': True,
59            'version': '1.2',
60        },
61        TD: {
62            'name': 'TD',
63            'is_otbr': True,
64            'router_eligible': False,
65            'version': '1.2',
66        },
67        HOST: {
68            'name': 'Host',
69            'is_host': True,
70        },
71    }
72
73    def test(self):
74        br1 = self.nodes[BR_1]
75        td = self.nodes[TD]
76        host = self.nodes[HOST]
77
78        br1.start()
79        self.simulator.go(config.LEADER_STARTUP_DELAY)
80        self.assertEqual('leader', br1.get_state())
81        self.assertTrue(br1.is_primary_backbone_router)
82
83        td.start()
84        self.simulator.go(5)
85        self.assertEqual('child', td.get_state())
86
87        # TD registers for multicast address, MA1, at BR_1.
88        td.add_ipmaddr_tun(MA1)
89        self.simulator.go(10)
90
91        # Host sends a ping packet to the multicast address, MA1. TD should
92        # respond to the ping request.
93        self.assertTrue(
94            host.ping(MA1, backbone=True, ttl=10, interface=host.get_ip6_address(config.ADDRESS_TYPE.ONLINK_ULA)[0]))
95        self.simulator.go(5)
96
97    def verify(self, pv: pktverify.packet_verifier.PacketVerifier):
98        pkts = pv.pkts
99        vars = pv.vars
100        pv.summary.show()
101
102        # 1. Host sends a ping packet to the multicast address, MA1.
103        _pkt = pkts.filter_eth_src(vars['Host_ETH']) \
104            .filter_ipv6_dst(MA1) \
105            .filter_ping_request() \
106            .must_next()
107
108        # 1. TD receives the multicast ping packet and sends a ping response
109        # packet back to Host.
110        # TD receives the MPL packet containing an encapsulated ping packet to
111        # MA1, sent by Host, and unicasts a ping response packet back to Host.
112        pkts.filter_ipv6_dst(_pkt.ipv6.src) \
113            .filter_ping_reply(identifier=_pkt.icmpv6.echo.identifier) \
114            .must_next()
115
116
117if __name__ == '__main__':
118    unittest.main()
119