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