1#!/usr/bin/env python3 2# 3# Copyright (c) 2023, 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: Network Diagnostics Vendor Name, Vendor Model, Vendor SW Version TLVs. 36# 37# Network topology 38# 39# r1 ---- r2 40# 41 42test_name = __file__[:-3] if __file__.endswith('.py') else __file__ 43print('-' * 120) 44print('Starting \'{}\''.format(test_name)) 45 46# ----------------------------------------------------------------------------------------------------------------------- 47# Creating `cli.Node` instances 48 49speedup = 40 50cli.Node.set_time_speedup_factor(speedup) 51 52r1 = cli.Node() 53r2 = cli.Node() 54 55# ----------------------------------------------------------------------------------------------------------------------- 56# Form topology 57 58r1.form('netdiag-vendor') 59r2.join(r1) 60 61verify(r1.get_state() == 'leader') 62verify(r2.get_state() == 'router') 63 64# ----------------------------------------------------------------------------------------------------------------------- 65# Test Implementation 66 67VENDOR_NAME_TLV = 25 68VENDOR_MODEL_TLV = 26 69VENDOR_SW_VERSION_TLV = 27 70THREAD_STACK_VERSION_TLV = 28 71MLE_COUNTERS_TLV = 34 72VENDOR_APP_URL = 35 73 74#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 75# Check setting vendor name, model, ans sw version 76 77r1.set_vendor_name('nest') 78r1.set_vendor_model('marble') 79r1.set_vendor_sw_version('ot-1.4') 80r1.set_vendor_app_url('https://example.com/vendor-app') 81 82verify(r1.get_vendor_name() == 'nest') 83verify(r1.get_vendor_model() == 'marble') 84verify(r1.get_vendor_sw_version() == 'ot-1.4') 85verify(r1.get_vendor_app_url() == 'https://example.com/vendor-app') 86 87#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 88# Check invalid names (too long) 89 90# Vendor name should accept up to 32 chars 91 92r2.set_vendor_name('01234567890123456789012345678901') # 32 chars 93 94errored = False 95 96try: 97 r2.set_vendor_name('012345678901234567890123456789012') # 33 chars 98except cli.CliError as e: 99 verify(e.message == 'InvalidArgs') 100 errored = True 101 102verify(errored) 103 104# Vendor model should accept up to 32 chars 105 106r2.set_vendor_model('01234567890123456789012345678901') # 32 chars 107 108errored = False 109 110try: 111 r2.set_vendor_model('012345678901234567890123456789012') # 33 chars 112except cli.CliError as e: 113 verify(e.message == 'InvalidArgs') 114 errored = True 115 116verify(errored) 117 118# Vendor SW version should accept up to 16 chars 119 120r2.set_vendor_sw_version('0123456789012345') # 16 chars 121 122errored = False 123 124try: 125 r2.set_vendor_sw_version('01234567890123456') # 17 chars 126except cli.CliError as e: 127 verify(e.message == 'InvalidArgs') 128 errored = True 129 130verify(errored) 131 132r2.set_vendor_app_url("https://example.com/vendor-app") 133 134#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 135# Perform net diag query 136 137r1_rloc = r1.get_rloc_ip_addr() 138r2_rloc = r2.get_rloc_ip_addr() 139 140# Get vendor name (TLV 27) 141 142result = r2.cli('networkdiagnostic get', r1_rloc, VENDOR_NAME_TLV) 143verify(len(result) == 2) 144verify(result[1].startswith("Vendor Name:")) 145verify(result[1].split(':')[1].strip() == r1.get_vendor_name()) 146 147# Get vendor model (TLV 28) 148 149result = r2.cli('networkdiagnostic get', r1_rloc, VENDOR_MODEL_TLV) 150verify(len(result) == 2) 151verify(result[1].startswith("Vendor Model:")) 152verify(result[1].split(':')[1].strip() == r1.get_vendor_model()) 153 154# Get vendor sw version (TLV 29) 155 156result = r2.cli('networkdiagnostic get', r1_rloc, VENDOR_SW_VERSION_TLV) 157verify(len(result) == 2) 158verify(result[1].startswith("Vendor SW Version:")) 159verify(result[1].split(':')[1].strip() == r1.get_vendor_sw_version()) 160 161# Get vendor app URL (TLV 35) 162 163result = r2.cli('networkdiagnostic get', r1_rloc, VENDOR_APP_URL) 164verify(len(result) == 2) 165verify(result[1].startswith("Vendor App URL:")) 166verify(result[1].split(':', 1)[1].strip() == r1.get_vendor_app_url()) 167 168# Get thread stack version (TLV 30) 169 170result = r2.cli('networkdiagnostic get', r1_rloc, THREAD_STACK_VERSION_TLV) 171verify(len(result) == 2) 172verify(result[1].startswith("Thread Stack Version:")) 173verify(r1.get_version().startswith(result[1].split(':', 1)[1].strip())) 174 175# Get all three TLVs (now from `r1`) 176 177result = r1.cli('networkdiagnostic get', r2_rloc, VENDOR_NAME_TLV, VENDOR_MODEL_TLV, VENDOR_SW_VERSION_TLV, 178 THREAD_STACK_VERSION_TLV, VENDOR_APP_URL) 179verify(len(result) == 6) 180for line in result[1:]: 181 if line.startswith("Vendor Name:"): 182 verify(line.split(':')[1].strip() == r2.get_vendor_name()) 183 elif line.startswith("Vendor Model:"): 184 verify(line.split(':')[1].strip() == r2.get_vendor_model()) 185 elif line.startswith("Vendor SW Version:"): 186 verify(line.split(':')[1].strip() == r2.get_vendor_sw_version()) 187 elif line.startswith("Vendor App URL:"): 188 verify(line.split(':', 1)[1].strip() == r2.get_vendor_app_url()) 189 elif line.startswith("Thread Stack Version:"): 190 verify(r2.get_version().startswith(line.split(':', 1)[1].strip())) 191 else: 192 verify(False) 193 194result = r2.cli('networkdiagnostic get', r1_rloc, MLE_COUNTERS_TLV) 195print(len(result) >= 1) 196verify(result[1].startswith("MLE Counters:")) 197 198# ----------------------------------------------------------------------------------------------------------------------- 199# Test finished 200 201cli.Node.finalize_all_nodes() 202 203print('\'{}\' passed.'.format(test_name)) 204