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#
29import logging
30import unittest
31
32import config
33import thread_cert
34
35# Test description:
36#   This test verifies the counters of ephemeral key activations/deactivations.
37#
38# Topology:
39#    --------------
40#           |
41#          BR1
42#
43
44BR1 = 1
45
46
47class EphemeralKeyCountersTest(thread_cert.TestCase):
48    USE_MESSAGE_FACTORY = False
49
50    TOPOLOGY = {
51        BR1: {
52            'name': 'BR_1',
53            'is_otbr': True,
54            'version': '1.4',
55            'network_name': 'ot-br1',
56        },
57    }
58
59    def test(self):
60        br1 = self.nodes[BR1]
61
62        counters = br1.get_border_agent_counters()
63        self.assertEqual(counters['epskcActivation'], 0)
64        self.assertEqual(counters['epskcApiDeactivation'], 0)
65        self.assertEqual(counters['epskcTimeoutDeactivation'], 0)
66        self.assertEqual(counters['epskcMaxAttemptDeactivation'], 0)
67        self.assertEqual(counters['epskcDisconnectDeactivation'], 0)
68        self.assertEqual(counters['epskcInvalidBaStateError'], 0)
69        self.assertEqual(counters['epskcInvalidArgsError'], 0)
70        self.assertEqual(counters['epskcStartSecureSessionError'], 0)
71        self.assertEqual(counters['epskcSecureSessionSuccess'], 0)
72        self.assertEqual(counters['epskcSecureSessionFailure'], 0)
73        self.assertEqual(counters['epskcCommissionerPetition'], 0)
74        self.assertEqual(counters['pskcSecureSessionSuccess'], 0)
75        self.assertEqual(counters['pskcSecureSessionFailure'], 0)
76        self.assertEqual(counters['pskcCommissionerPetition'], 0)
77        self.assertEqual(counters['mgmtActiveGet'], 0)
78        self.assertEqual(counters['mgmtPendingGet'], 0)
79
80        # activate epskc before border agent is up returns an error
81        br1.set_epskc('123456789')
82
83        counters = br1.get_border_agent_counters()
84        self.assertEqual(counters['epskcActivation'], 0)
85        self.assertEqual(counters['epskcInvalidBaStateError'], 1)
86
87        br1.set_active_dataset(updateExisting=True, network_name='ot-br1')
88        br1.start()
89        self.simulator.go(config.BORDER_ROUTER_STARTUP_DELAY)
90        self.assertEqual('leader', br1.get_state())
91
92        # activate epskc and let it timeout
93        br1.set_epskc('123456789', 10)
94        self.simulator.go(1)
95
96        counters = br1.get_border_agent_counters()
97        self.assertEqual(counters['epskcActivation'], 1)
98        self.assertEqual(counters['epskcApiDeactivation'], 0)
99        self.assertEqual(counters['epskcTimeoutDeactivation'], 1)
100
101        # activate epskc and clear it
102        br1.set_epskc('123456789', 10000)
103        self.simulator.go(1)
104        br1.clear_epskc()
105
106        counters = br1.get_border_agent_counters()
107        self.assertEqual(counters['epskcActivation'], 2)
108        self.assertEqual(counters['epskcApiDeactivation'], 1)
109        self.assertEqual(counters['epskcTimeoutDeactivation'], 1)
110
111        # set epskc with invalid passcode
112        br1.set_epskc('123')
113        self.simulator.go(1)
114
115        counters = br1.get_border_agent_counters()
116        self.assertEqual(counters['epskcActivation'], 2)
117        self.assertEqual(counters['epskcApiDeactivation'], 1)
118        self.assertEqual(counters['epskcTimeoutDeactivation'], 1)
119        self.assertEqual(counters['epskcInvalidArgsError'], 1)
120
121
122if __name__ == '__main__':
123    unittest.main()
124