• 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_REQUEST
35from pktverify.packet_verifier import PacketVerifier
36
37LEADER = 1
38ED = 2
39
40
41class Cert_6_6_1_KeyIncrement(thread_cert.TestCase):
42    TOPOLOGY = {
43        LEADER: {
44            'name': 'LEADER',
45            'key_switch_guardtime': 0,
46            'mode': 'rdn',
47            'allowlist': [ED]
48        },
49        ED: {
50            'name': 'ED',
51            'is_mtd': True,
52            'key_switch_guardtime': 0,
53            'mode': 'rn',
54            'allowlist': [LEADER]
55        },
56    }
57
58    def test(self):
59        self.nodes[LEADER].start()
60        self.simulator.go(config.LEADER_STARTUP_DELAY)
61        self.assertEqual(self.nodes[LEADER].get_state(), "leader")
62
63        self.nodes[ED].start()
64        self.simulator.go(5)
65        self.assertEqual(self.nodes[ED].get_state(), "child")
66
67        self.collect_rloc16s()
68        addrs = self.nodes[ED].get_addrs()
69        for addr in addrs:
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[ED].get_addrs()
76        for addr in addrs:
77            self.assertTrue(self.nodes[LEADER].ping(addr))
78
79    def verify(self, pv):
80        pkts = pv.pkts
81        pv.summary.show()
82
83        LEADER = pv.vars['LEADER']
84        ED = pv.vars['ED']
85        _leader_pkts = pkts.filter_wpan_src64(LEADER)
86        _ed_pkts = pkts.filter_wpan_src64(ED)
87
88        # Step 1: The DUT must start the network using
89        # thrKeySequenceCounter = 0
90        _leader_pkts.filter_mle_cmd(MLE_ADVERTISEMENT).must_next().must_verify(
91            lambda p: p.wpan.aux_sec.key_source == 0)
92
93        # Step 2: Verify that the topology described above is created.
94        # MLE Auxiliary security header shall contain Key Source = 0,
95        # KeyIndex = 1, KeyID Mode = 2
96        _ed_pkts.filter_mle_cmd(
97            MLE_CHILD_ID_REQUEST).must_next().must_verify(lambda p: p.wpan.aux_sec.key_index == 1 and p.wpan.aux_sec.
98                                                          key_id_mode == 2 and p.wpan.aux_sec.key_source == 0)
99
100        # Step 3: Leader send an ICMPv6 Echo Request to DUT.
101        # The MAC Auxiliary security header must contain
102        # KeyIndex = 1, KeyID Mode = 1
103        lp = _leader_pkts.filter_ping_request().filter(
104            lambda p: p.wpan.aux_sec.key_index == 1 and p.wpan.aux_sec.key_id_mode == 1 and p.wpan.dst16 == pv.vars[
105                'ED_RLOC16']).must_next()
106
107        # Step 4: DUT send an ICMPv6 Echo Reply to Leader.
108        # The MAC Auxiliary security header must contain
109        # KeyIndex = 1, KeyID Mode = 1
110        _ed_pkts.filter_ping_reply(identifier=lp.icmpv6.echo.identifier).must_next().must_verify(
111            lambda p: p.wpan.aux_sec.key_index == 1 and p.wpan.aux_sec.key_id_mode == 1)
112
113        # Step 5: Leader increment thrKeySequenceCounter by 1 to force a key switch.
114        # Step 6: Leader Send an ICMPv6 Echo Request to DUT.
115        # The MAC Auxiliary security header must contain
116        # KeyIndex = 2, KeyID Mode = 1
117        lp = _leader_pkts.filter_ping_request().filter(
118            lambda p: p.wpan.aux_sec.key_index == 2 and p.wpan.aux_sec.key_id_mode == 1 and p.wpan.dst16 == pv.vars[
119                'ED_RLOC16']).must_next()
120
121        # Step 7: DUT send an ICMPv6 Echo Reply to Leader.
122        # The MAC Auxiliary security header must contain
123        # KeyIndex = 2, KeyID Mode = 1
124        _ed_pkts.filter_ping_reply(identifier=lp.icmpv6.echo.identifier).must_next().must_verify(
125            lambda p: p.wpan.aux_sec.key_index == 2 and p.wpan.aux_sec.key_id_mode == 1)
126
127
128if __name__ == '__main__':
129    unittest.main()
130