TestTimeSyncTrustedTimeSourceRunner.py 6.1 KB

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