im_test.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 IMTest:
  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('-u', '--paa-trust-store-path', dest='paa_trust_store_path',
  44. help="Path that contains valid and trusted PAA Root Certificates")
  45. args = parser.parse_args(args.split())
  46. self.command_name = args.command
  47. self.nodeid = args.nodeid
  48. self.address = args.address
  49. self.port = args.port
  50. self.setup_payload = args.setup_payload
  51. self.setup_pin_code = args.setup_pin_code
  52. self.discriminator = args.discriminator
  53. self.timeout = args.timeout
  54. logging.basicConfig(level=logging.INFO)
  55. def TestCmdOnnetworkLongImInvoke(self, nodeid, setuppin, discriminator, timeout):
  56. java_command = self.command + ['im', 'onnetwork-long-im-invoke', nodeid, setuppin, discriminator, timeout]
  57. logging.info(f"Execute: {java_command}")
  58. java_process = subprocess.Popen(
  59. java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  60. DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
  61. return java_process.wait()
  62. def TestCmdOnnetworkLongImWrite(self, nodeid, setuppin, discriminator, timeout):
  63. java_command = self.command + ['im', 'onnetwork-long-im-write', nodeid, setuppin, discriminator, timeout]
  64. logging.info(f"Execute: {java_command}")
  65. java_process = subprocess.Popen(
  66. java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  67. DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
  68. return java_process.wait()
  69. def TestCmdOnnetworkLongImRead(self, nodeid, setuppin, discriminator, timeout):
  70. java_command = self.command + ['im', 'onnetwork-long-im-read', nodeid, setuppin, discriminator, timeout]
  71. logging.info(f"Execute: {java_command}")
  72. java_process = subprocess.Popen(
  73. java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  74. DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
  75. return java_process.wait()
  76. def TestCmdOnnetworkLongImSubscribe(self, nodeid, setuppin, discriminator, timeout):
  77. java_command = self.command + ['im', 'onnetwork-long-im-subscribe', nodeid, setuppin, discriminator, timeout]
  78. logging.info(f"Execute: {java_command}")
  79. java_process = subprocess.Popen(
  80. java_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  81. DumpProgramOutputToQueue(self.thread_list, Fore.GREEN + "JAVA " + Style.RESET_ALL, java_process, self.queue)
  82. return java_process.wait()
  83. def RunTest(self):
  84. if self.command_name == 'onnetwork-long-im-invoke':
  85. logging.info("Testing pairing onnetwork-long-im-invoke")
  86. code = self.TestCmdOnnetworkLongImInvoke(self.nodeid, self.setup_pin_code, self.discriminator, self.timeout)
  87. if code != 0:
  88. raise Exception(f"Testing pairing onnetwork-long-im-invoke failed with error {code}")
  89. elif self.command_name == 'onnetwork-long-im-write':
  90. logging.info("Testing pairing onnetwork-long-im-write")
  91. code = self.TestCmdOnnetworkLongImWrite(self.nodeid, self.setup_pin_code, self.discriminator, self.timeout)
  92. if code != 0:
  93. raise Exception(f"Testing pairing onnetwork-long-im-write failed with error {code}")
  94. elif self.command_name == 'onnetwork-long-im-read':
  95. logging.info("Testing pairing onnetwork-long-im-read")
  96. code = self.TestCmdOnnetworkLongImRead(self.nodeid, self.setup_pin_code, self.discriminator, self.timeout)
  97. if code != 0:
  98. raise Exception(f"Testing pairing onnetwork-long-im-read failed with error {code}")
  99. elif self.command_name == 'onnetwork-long-im-subscribe':
  100. logging.info("Testing pairing onnetwork-long-im-subscribe")
  101. code = self.TestCmdOnnetworkLongImSubscribe(self.nodeid, self.setup_pin_code, self.discriminator, self.timeout)
  102. if code != 0:
  103. raise Exception(f"Testing pairing onnetwork-long-im-subscribe failed with error {code}")
  104. else:
  105. raise Exception(f"Unsupported command {self.command_name}")