commissioning_test.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2022 Project CHIP Authors
  4. # All rights reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import argparse
  19. import logging
  20. import queue
  21. import subprocess
  22. import threading
  23. import typing
  24. from colorama import Fore, Style
  25. from java.base import DumpProgramOutputToQueue
  26. class CommissioningTest:
  27. def __init__(self, thread_list: typing.List[threading.Thread], queue: queue.Queue, cmd: [], args: str):
  28. self.thread_list = thread_list
  29. self.queue = queue
  30. self.command = cmd
  31. parser = argparse.ArgumentParser(description='Process pairing arguments.')
  32. parser.add_argument('command', help="Command name")
  33. parser.add_argument('-t', '--timeout', help="The program will return with timeout after specified seconds", default='200')
  34. parser.add_argument('-a', '--address', help="Address of the device")
  35. parser.add_argument('-p', '--port', help="Port of the remote device", default='5540')
  36. parser.add_argument('-s', '--setup-payload', dest='setup_payload',
  37. help="Setup Payload (manual pairing code or QR code content)")
  38. parser.add_argument('-c', '--setup-pin-code', dest='setup_pin_code',
  39. help=("Setup PIN code which can be used for password-authenticated "
  40. "session establishment (PASE) with the Commissionee"))
  41. parser.add_argument('-n', '--nodeid', help="The Node ID issued to the device", default='1')
  42. parser.add_argument('-d', '--discriminator', help="Discriminator of the device", default='3840')
  43. parser.add_argument('-o', '--discover-once', help="Enable to disable PASE auto retry mechanism", default='false')
  44. parser.add_argument('-u', '--use-only-onnetwork-discovery',
  45. help="Enable when the commissionable device is available on the network", default='false')
  46. parser.add_argument('-r', '--paa-trust-store-path', dest='paa_trust_store_path',
  47. help="Path that contains valid and trusted PAA Root Certificates")
  48. args = parser.parse_args(args.split())
  49. self.command_name = args.command
  50. self.nodeid = args.nodeid
  51. self.address = args.address
  52. self.port = args.port
  53. self.setup_payload = args.setup_payload
  54. self.setup_pin_code = args.setup_pin_code
  55. self.discriminator = args.discriminator
  56. self.discover_once = args.discover_once
  57. self.use_only_onnetwork_discovery = args.use_only_onnetwork_discovery
  58. self.timeout = args.timeout
  59. logging.basicConfig(level=logging.INFO)
  60. def TestCmdOnnetworkLong(self, nodeid, setuppin, discriminator, timeout):
  61. java_command = self.command + ['pairing', 'onnetwork-long', nodeid, setuppin, discriminator, timeout]
  62. logging.info(f"Execute: {java_command}")
  63. java_process = subprocess.Popen(
  64. java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  65. DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
  66. return java_process.wait()
  67. def TestCmdAlreadyDiscovered(self, nodeid, setuppin, address, port, timeout):
  68. java_command = self.command + ['pairing', 'already-discovered', nodeid, setuppin, address, port, timeout]
  69. logging.info(f"Execute: {java_command}")
  70. java_process = subprocess.Popen(
  71. java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  72. DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
  73. return java_process.wait()
  74. def TestCmdAddressPaseOnly(self, nodeid, setuppin, address, port, timeout):
  75. java_command = self.command + ['pairing', 'address-paseonly', nodeid, setuppin, address, port, timeout]
  76. logging.info(f"Execute: {java_command}")
  77. java_process = subprocess.Popen(
  78. java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  79. DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
  80. return java_process.wait()
  81. def TestCmdCode(self, nodeid, setup_payload, discover_once, use_only_onnetwork_discovery, timeout):
  82. java_command = self.command + ['pairing', 'code', nodeid, setup_payload, timeout,
  83. '--discover-once', discover_once, '--use-only-onnetwork-discovery', use_only_onnetwork_discovery]
  84. logging.info(f"Execute: {java_command}")
  85. java_process = subprocess.Popen(
  86. java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  87. DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
  88. return java_process.wait()
  89. def RunTest(self):
  90. if self.command_name == 'onnetwork-long':
  91. logging.info("Testing pairing onnetwork-long")
  92. code = self.TestCmdOnnetworkLong(self.nodeid, self.setup_pin_code, self.discriminator, self.timeout)
  93. if code != 0:
  94. raise Exception(f"Testing pairing onnetwork-long failed with error {code}")
  95. elif self.command_name == 'already-discovered':
  96. logging.info("Testing pairing already-discovered")
  97. code = self.TestCmdAlreadyDiscovered(self.nodeid, self.setup_pin_code, self.address, self.port, self.timeout)
  98. if code != 0:
  99. raise Exception(f"Testing pairing already-discovered failed with error {code}")
  100. elif self.command_name == 'address-paseonly':
  101. logging.info("Testing pairing address-paseonly")
  102. code = self.TestCmdAddressPaseOnly(self.nodeid, self.setup_pin_code, self.address, self.port, self.timeout)
  103. if code != 0:
  104. raise Exception(f"Testing pairing address-paseonly failed with error {code}")
  105. elif self.command_name == 'code':
  106. logging.info("Testing pairing setup-code")
  107. code = self.TestCmdCode(self.nodeid, self.setup_payload, self.discover_once,
  108. self.use_only_onnetwork_discovery, self.timeout)
  109. if code != 0:
  110. raise Exception(f"Testing pairing code failed with error {code}")
  111. else:
  112. raise Exception(f"Unsupported command {self.command_name}")