xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-026-coaps-conn-limit.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
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
29from cli import verify
30from cli import verify_within
31import cli
32import time
33
34# -----------------------------------------------------------------------------------------------------------------------
35# Test description:
36#
37# Validate CoaP secure agent related to the maximum number of allowed connection attempts before the socket is a
38# automatically closed and the agent is stopped.
39#
40#
41# Network topology
42#
43#    r1 ---- r2
44#
45
46test_name = __file__[:-3] if __file__.endswith('.py') else __file__
47print('-' * 120)
48print('Starting \'{}\''.format(test_name))
49
50# -----------------------------------------------------------------------------------------------------------------------
51# Creating `cli.Node` instances
52
53speedup = 40
54cli.Node.set_time_speedup_factor(speedup)
55
56r1 = cli.Node()
57r2 = cli.Node()
58
59# -----------------------------------------------------------------------------------------------------------------------
60# Form topology
61
62r1.form('coaps-conn-lmt')
63r2.join(r1)
64
65verify(r1.get_state() == 'leader')
66verify(r2.get_state() == 'router')
67
68# -----------------------------------------------------------------------------------------------------------------------
69# Test Implementation
70
71r1.cli('coaps psk RIGHT 1234')
72
73# Set max connection attempts to 3
74r1.cli('coaps start 3')
75
76r1_mleid = r1.get_mleid_ip_addr()
77
78r2.cli('coaps psk WRONG 1234')
79r2.cli('coaps start')
80
81# First connection attempt
82r2.cli('coaps connect', r1_mleid)
83time.sleep(0.1)
84verify(r1.cli('coaps isconnactive')[-1] == 'no')
85verify(r1.cli('coaps isclosed')[-1] == 'no')
86
87# Second connection attempt
88r2.cli('coaps connect', r1_mleid)
89time.sleep(0.1)
90verify(r1.cli('coaps isconnactive')[-1] == 'no')
91verify(r1.cli('coaps isclosed')[-1] == 'no')
92
93# Third connection attempt
94r2.cli('coaps connect', r1_mleid)
95time.sleep(0.1)
96verify(r1.cli('coaps isconnactive')[-1] == 'no')
97verify(r1.cli('coaps isclosed')[-1] == 'yes')
98
99# -----------------------------------------------------------------------------------------------------------------------
100# Test finished
101
102cli.Node.finalize_all_nodes()
103
104print('\'{}\' passed.'.format(test_name))
105