|
| 1 | +#!/usr/bin/env -S python3 -B |
| 2 | + |
| 3 | +# Copyright (c) 2023 Project CHIP Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import signal |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | +import time |
| 23 | + |
| 24 | +DEFAULT_CHIP_ROOT = os.path.abspath( |
| 25 | + os.path.join(os.path.dirname(__file__), '..', '..')) |
| 26 | + |
| 27 | + |
| 28 | +class TestDriver: |
| 29 | + def __init__(self): |
| 30 | + self.app_path = os.path.abspath(os.path.join(DEFAULT_CHIP_ROOT, 'out', |
| 31 | + 'linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test', 'chip-all-clusters-app')) |
| 32 | + self.run_python_test_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'run_python_test.py')) |
| 33 | + |
| 34 | + self.script_path = os.path.abspath(os.path.join( |
| 35 | + DEFAULT_CHIP_ROOT, 'src', 'python_testing', 'TestTimeSyncTrustedTimeSource.py')) |
| 36 | + if not os.path.exists(self.app_path): |
| 37 | + msg = 'chip-all-clusters-app not found' |
| 38 | + logging.error(msg) |
| 39 | + raise FileNotFoundError(msg) |
| 40 | + if not os.path.exists(self.run_python_test_path): |
| 41 | + msg = 'run_python_test.py script not found' |
| 42 | + logging.error(msg) |
| 43 | + raise FileNotFoundError(msg) |
| 44 | + if not os.path.exists(self.script_path): |
| 45 | + msg = 'TestTimeSyncTrustedTimeSource.py script not found' |
| 46 | + logging.error(msg) |
| 47 | + raise FileNotFoundError(msg) |
| 48 | + |
| 49 | + def get_base_run_python_cmd(self, run_python_test_path, app_path, app_args, script_path, script_args): |
| 50 | + return f'{str(run_python_test_path)} --app {str(app_path)} --app-args "{app_args}" --script {str(script_path)} --script-args "{script_args}"' |
| 51 | + |
| 52 | + def run_test_section(self, app_args: str, script_args: str, factory_reset_all: bool = False, factory_reset_app: bool = False) -> int: |
| 53 | + # quotes are required here |
| 54 | + cmd = self.get_base_run_python_cmd(self.run_python_test_path, self.app_path, app_args, |
| 55 | + self.script_path, script_args) |
| 56 | + if factory_reset_all: |
| 57 | + cmd = cmd + ' --factoryreset' |
| 58 | + if factory_reset_app: |
| 59 | + cmd = cmd + ' --factoryreset-app-only' |
| 60 | + |
| 61 | + logging.info(f'Running cmd {cmd}') |
| 62 | + |
| 63 | + process = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, shell=True, bufsize=1) |
| 64 | + |
| 65 | + return process.wait() |
| 66 | + |
| 67 | + |
| 68 | +def kill_process(app2_process): |
| 69 | + logging.warning("Stopping app with SIGINT") |
| 70 | + app2_process.send_signal(signal.SIGINT.value) |
| 71 | + app2_process.wait() |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + # in the first round, we're just going to commission the device |
| 76 | + base_app_args = '--discriminator 1234 --KVS kvs1' |
| 77 | + app_args = base_app_args |
| 78 | + base_script_args = '--storage-path admin_storage.json --discriminator 1234 --passcode 20202021' |
| 79 | + script_args = base_script_args + ' --commissioning-method on-network --commission-only' |
| 80 | + |
| 81 | + driver = TestDriver() |
| 82 | + ret = driver.run_test_section(app_args, script_args, factory_reset_all=True) |
| 83 | + if ret != 0: |
| 84 | + return ret |
| 85 | + |
| 86 | + # For this test, we need to have a time source set up already for the simulated no-internal-time source to query. |
| 87 | + # This means it needs to be commissioned onto the same fabric, and the ACLs need to be set up to allow |
| 88 | + # access to the time source cluster. |
| 89 | + # This simulates the second device, so its using a different KVS and nodeid, which will allow both apps to run simultaneously |
| 90 | + app2_args = '--discriminator 1235 --KVS kvs2 --secured-device-port 5580' |
| 91 | + script_args = '--storage-path admin_storage.json --discriminator 1235 --passcode 20202021 --commissioning-method on-network --dut-node-id 2 --tests test_SetupTimeSourceACL' |
| 92 | + |
| 93 | + ret = driver.run_test_section(app2_args, script_args, factory_reset_app=True) |
| 94 | + if ret != 0: |
| 95 | + return ret |
| 96 | + |
| 97 | + # Now we've got something commissioned, we're going to test what happens when it resets, but we're simulating no time. |
| 98 | + # In this case, the commissioner hasn't set the time after the reboot, so there should be no time returned (checked in test) |
| 99 | + app_args = base_app_args + ' --simulate-no-internal-time' |
| 100 | + script_args = base_script_args + ' --tests test_SimulateNoInternalTime' |
| 101 | + |
| 102 | + ret = driver.run_test_section(app_args, script_args) |
| 103 | + if ret != 0: |
| 104 | + return ret |
| 105 | + |
| 106 | + # Make sure we come up with internal time correctly if we don't set that flag |
| 107 | + app_args = base_app_args |
| 108 | + script_args = base_script_args + ' --tests test_HaveInternalTime' |
| 109 | + |
| 110 | + ret = driver.run_test_section(app_args, script_args) |
| 111 | + if ret != 0: |
| 112 | + return ret |
| 113 | + |
| 114 | + # Bring up app2 again, it needs to run for the duration of the next test so app1 has a place to query time |
| 115 | + # App1 will come up, it is simulating having no internal time (confirmed in previous test), but we have |
| 116 | + # set up app2 as the trusted time source, so it should query out to app2 for the time. |
| 117 | + app2_cmd = str(driver.app_path) + ' ' + app2_args |
| 118 | + app2_process = subprocess.Popen(app2_cmd.split(), stdout=sys.stdout, stderr=sys.stderr, bufsize=0) |
| 119 | + |
| 120 | + # Give app2 a second to come up and start advertising |
| 121 | + time.sleep(1) |
| 122 | + |
| 123 | + # This first test ensures that we read from the trusted time source right after it is set. |
| 124 | + app_args = base_app_args + ' --simulate-no-internal-time --trace_decode 1' |
| 125 | + script_args = base_script_args + ' --tests test_SetAndReadFromTrustedTimeSource --int-arg trusted_time_source:2' |
| 126 | + ret = driver.run_test_section(app_args, script_args) |
| 127 | + if ret != 0: |
| 128 | + kill_process(app2_process) |
| 129 | + return ret |
| 130 | + |
| 131 | + # This next test ensures the trusted time source is saved during a reboot |
| 132 | + script_args = base_script_args + ' --tests test_ReadFromTrustedTimeSource' |
| 133 | + ret = driver.run_test_section(app_args, script_args) |
| 134 | + |
| 135 | + kill_process(app2_process) |
| 136 | + sys.exit(ret) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + main() |
0 commit comments