TestCommand.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2021 Project CHIP Authors
  3. * All rights reserved.
  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. */
  18. #include "TestCommand.h"
  19. CHIP_ERROR TestCommand::RunCommand()
  20. {
  21. return CurrentCommissioner().GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
  22. }
  23. void TestCommand::OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * device)
  24. {
  25. ChipLogProgress(chipTool, " **** Test Setup: Device Connected\n");
  26. auto * command = static_cast<TestCommand *>(context);
  27. VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "Device connected, but cannot run the test, as the context is null"));
  28. command->mDevice = device;
  29. if (command->mPICSFilePath.HasValue())
  30. {
  31. command->PICS.SetValue(PICSBooleanReader::Read(command->mPICSFilePath.Value()));
  32. }
  33. command->NextTest();
  34. }
  35. void TestCommand::OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error)
  36. {
  37. ChipLogProgress(chipTool, " **** Test Setup: Device Connection Failure [deviceId=%" PRIu64 ". Error %" CHIP_ERROR_FORMAT "\n]",
  38. deviceId, error.Format());
  39. auto * command = static_cast<TestCommand *>(context);
  40. VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "Test command context is null"));
  41. command->SetCommandExitStatus(error);
  42. }
  43. void TestCommand::OnWaitForMsFn(chip::System::Layer * systemLayer, void * context)
  44. {
  45. auto * command = static_cast<TestCommand *>(context);
  46. command->NextTest();
  47. }
  48. CHIP_ERROR TestCommand::Wait(chip::System::Clock::Timeout duration)
  49. {
  50. return chip::DeviceLayer::SystemLayer().StartTimer(duration, OnWaitForMsFn, this);
  51. }
  52. CHIP_ERROR TestCommand::Log(const char * message)
  53. {
  54. ChipLogDetail(chipTool, "%s", message);
  55. ReturnErrorOnFailure(ContinueOnChipMainThread());
  56. return CHIP_NO_ERROR;
  57. }
  58. CHIP_ERROR TestCommand::UserPrompt(const char * message)
  59. {
  60. ChipLogDetail(chipTool, "USER_PROMPT: %s", message);
  61. ReturnErrorOnFailure(ContinueOnChipMainThread());
  62. return CHIP_NO_ERROR;
  63. }
  64. void TestCommand::Exit(std::string message)
  65. {
  66. ChipLogError(chipTool, " ***** Test Failure: %s\n", message.c_str());
  67. SetCommandExitStatus(CHIP_ERROR_INTERNAL);
  68. }
  69. void TestCommand::ThrowFailureResponse()
  70. {
  71. Exit("Expecting success response but got a failure response");
  72. }
  73. void TestCommand::ThrowSuccessResponse()
  74. {
  75. Exit("Expecting failure response but got a success response");
  76. }
  77. bool TestCommand::CheckConstraintType(const char * itemName, const char * current, const char * expected)
  78. {
  79. ChipLogError(chipTool, "Warning: %s type checking is not implemented yet. Expected type: '%s'", itemName, expected);
  80. return true;
  81. }
  82. bool TestCommand::CheckConstraintFormat(const char * itemName, const char * current, const char * expected)
  83. {
  84. ChipLogError(chipTool, "Warning: %s format checking is not implemented yet. Expected format: '%s'", itemName, expected);
  85. return true;
  86. }
  87. bool TestCommand::CheckConstraintStartsWith(const char * itemName, const chip::Span<const char> current, const char * expected)
  88. {
  89. std::string value(current.data(), current.size());
  90. if (value.rfind(expected, 0) != 0)
  91. {
  92. Exit(std::string(itemName) + " (\"" + value + "\") does not starts with: \"" + std::string(expected) + "\"");
  93. return false;
  94. }
  95. return true;
  96. }
  97. bool TestCommand::CheckConstraintEndsWith(const char * itemName, const chip::Span<const char> current, const char * expected)
  98. {
  99. std::string value(current.data(), current.size());
  100. if (value.find(expected, value.size() - strlen(expected)) == std::string::npos)
  101. {
  102. Exit(std::string(itemName) + " (\"" + value + "\") does not ends with: \"" + std::string(expected) + "\"");
  103. return false;
  104. }
  105. return true;
  106. }
  107. bool TestCommand::CheckConstraintMinLength(const char * itemName, uint64_t current, uint64_t expected)
  108. {
  109. if (current < expected)
  110. {
  111. Exit(std::string(itemName) + " length < minLength: " + std::to_string(current) + " < " + std::to_string(expected));
  112. return false;
  113. }
  114. return true;
  115. }
  116. bool TestCommand::CheckConstraintMaxLength(const char * itemName, uint64_t current, uint64_t expected)
  117. {
  118. if (current > expected)
  119. {
  120. Exit(std::string(itemName) + " length > minLength: " + std::to_string(current) + " > " + std::to_string(expected));
  121. return false;
  122. }
  123. return true;
  124. }
  125. bool TestCommand::CheckValueAsString(const char * itemName, chip::ByteSpan current, chip::ByteSpan expected)
  126. {
  127. if (!current.data_equal(expected))
  128. {
  129. Exit(std::string(itemName) + " value mismatch, expecting " +
  130. std::string(chip::Uint8::to_const_char(expected.data()), expected.size()));
  131. return false;
  132. }
  133. return true;
  134. }
  135. bool TestCommand::CheckValueAsString(const char * itemName, chip::CharSpan current, chip::CharSpan expected)
  136. {
  137. if (!current.data_equal(expected))
  138. {
  139. Exit(std::string(itemName) + " value mismatch, expected '" + std::string(expected.data(), expected.size()) + "' but got '" +
  140. std::string(current.data(), current.size()) + "'");
  141. return false;
  142. }
  143. return true;
  144. }
  145. bool TestCommand::ShouldSkip(const char * expression)
  146. {
  147. // If there is no PICS configuration file, considers that nothing should be skipped.
  148. if (!PICS.HasValue())
  149. {
  150. return false;
  151. }
  152. std::map<std::string, bool> pics(PICS.Value());
  153. bool shouldSkip = !PICSBooleanExpressionParser::Eval(expression, pics);
  154. if (shouldSkip)
  155. {
  156. ChipLogProgress(chipTool, " **** Skipping: %s == false\n", expression);
  157. ContinueOnChipMainThread();
  158. }
  159. return shouldSkip;
  160. }