1#!/usr/bin/env python3
2#
3#  Copyright (c) 2016, 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
31
32import thread_cert
33from pktverify.consts import MLE_ADVERTISEMENT, MLE_CHILD_ID_RESPONSE
34from pktverify.packet_verifier import PacketVerifier
35
36LEADER = 1
37ROUTER = 2
38
39
40class Cert_5_8_3_KeyIncrementRollOver(thread_cert.TestCase):
41    TOPOLOGY = {
42        LEADER: {
43            'name': 'LEADER',
44            'key_sequence_counter': 127,
45            'key_switch_guardtime': 0,
46            'mode': 'rdn',
47            'allowlist': [ROUTER]
48        },
49        ROUTER: {
50            'name': 'ROUTER',
51            'key_switch_guardtime': 0,
52            'mode': 'rdn',
53            'allowlist': [LEADER]
54        },
55    }
56
57    def test(self):
58        self.nodes[LEADER].start()
59        self.simulator.go(4)
60        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
61
62        self.nodes[ROUTER].start()
63        self.simulator.go(5)
64        self.assertEqual(self.nodes[ROUTER].get_state(), 'router')
65
66        self.collect_ipaddrs()
67        addrs = self.nodes[ROUTER].get_addrs()
68        for addr in addrs:
69            if addr[0:4] != 'fe80':
70                self.assertTrue(self.nodes[LEADER].ping(addr))
71
72        key_sequence_counter = self.nodes[LEADER].get_key_sequence_counter()
73        self.nodes[LEADER].set_key_sequence_counter(key_sequence_counter + 1)
74
75        addrs = self.nodes[ROUTER].get_addrs()
76        for addr in addrs:
77            if addr[0:4] != 'fe80':
78                self.assertTrue(self.nodes[LEADER].ping(addr))
79
80    def verify(self, pv):
81        pkts = pv.pkts
82        pv.summary.show()
83
84        LEADER = pv.vars['LEADER']
85        ROUTER = pv.vars['ROUTER']
86        leader_pkts = pkts.filter_wpan_src64(LEADER)
87        router_pkts = pkts.filter_wpan_src64(ROUTER)
88
89        # Step 1: The DUT must start the network using
90        # thrKeySequenceCounter = 127
91        _lpkts = leader_pkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_next()
92        self.assertTrue(_lpkts.wpan.aux_sec.key_source == 127)
93
94        # Step 2: Verify that the topology described above is created.
95        # MLE Auxiliary security header shall contain Key Source = 127,
96        # KeyIndex = 128, KeyID Mode = 2
97        leader_pkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next()
98        _lpkts = leader_pkts.copy()
99        _rpkts = router_pkts.range(leader_pkts.index)
100
101        _rpkts.filter_mle_cmd(
102            MLE_ADVERTISEMENT).must_next().must_verify(lambda p: p.wpan.aux_sec.key_index == 128 and p.wpan.aux_sec.
103                                                       key_id_mode == 2 and p.wpan.aux_sec.key_source == 127)
104
105        # Step 3: Leader send an ICMPv6 Echo Request to Router_1.
106        # The MAC Auxiliary security header must contain
107        # KeyIndex = 128, KeyID Mode = 1
108        leader_mleid = pv.vars['LEADER_MLEID']
109        router_mleid = pv.vars['ROUTER_MLEID']
110        _lpkts.filter(lambda p: p.ipv6.dst == router_mleid).filter_ping_request().must_next().must_verify(
111            lambda p: p.wpan.aux_sec.key_index == 128 and p.wpan.aux_sec.key_id_mode == 1)
112
113        # Step 4: Router_1 send an ICMPv6 Echo Reply to Leader.
114        # The MAC Auxiliary security header must contain
115        # KeyIndex = 128, KeyID Mode = 1
116        _rpkts.filter(lambda p: p.ipv6.dst == leader_mleid).filter_ping_reply().must_next().must_verify(
117            lambda p: p.wpan.aux_sec.key_index == 128 and p.wpan.aux_sec.key_id_mode == 1)
118
119        # Step 5: The DUT MUST implementation specific means increment
120        # thrKeySequenceCounter by 1 to force a key switch
121        # Step 6: Leader Send an ICMPv6 Echo Request to Router_1.
122        # The MAC Auxiliary security header must contain
123        # KeyIndex = 1, KeyID Mode = 1
124        _lpkts.filter(lambda p: p.ipv6.dst == router_mleid).filter_ping_request().must_next().must_verify(
125            lambda p: p.wpan.aux_sec.key_index == 1 and p.wpan.aux_sec.key_id_mode == 1)
126
127        # Step 7: Router_1 send an ICMPv6 Echo Reply to Leader.
128        # The MAC Auxiliary security header must contain
129        # KeyIndex = 1, KeyID Mode = 1
130        _rpkts.filter(lambda p: p.ipv6.dst == leader_mleid).filter_ping_reply().must_next().must_verify(
131            lambda p: p.wpan.aux_sec.key_index == 1 and p.wpan.aux_sec.key_id_mode == 1)
132
133
134if __name__ == '__main__':
135    unittest.main()
136