Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

328 linhas
8.4 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include "file_test.h"
  15. #include <ctype.h>
  16. #include <errno.h>
  17. #include <stdarg.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <openssl/err.h>
  21. #include "stl_compat.h"
  22. FileTest::FileTest(const char *path) {
  23. file_ = fopen(path, "r");
  24. if (file_ == nullptr) {
  25. fprintf(stderr, "Could not open file %s: %s.\n", path, strerror(errno));
  26. }
  27. }
  28. FileTest::~FileTest() {
  29. if (file_ != nullptr) {
  30. fclose(file_);
  31. }
  32. }
  33. // FindDelimiter returns a pointer to the first '=' or ':' in |str| or nullptr
  34. // if there is none.
  35. static const char *FindDelimiter(const char *str) {
  36. while (*str) {
  37. if (*str == ':' || *str == '=') {
  38. return str;
  39. }
  40. str++;
  41. }
  42. return nullptr;
  43. }
  44. // StripSpace returns a string containing up to |len| characters from |str| with
  45. // leading and trailing whitespace removed.
  46. static std::string StripSpace(const char *str, size_t len) {
  47. // Remove leading space.
  48. while (len > 0 && isspace(*str)) {
  49. str++;
  50. len--;
  51. }
  52. while (len > 0 && isspace(str[len-1])) {
  53. len--;
  54. }
  55. return std::string(str, len);
  56. }
  57. FileTest::ReadResult FileTest::ReadNext() {
  58. // If the previous test had unused attributes or block, it is an error.
  59. if (!unused_attributes_.empty()) {
  60. for (const std::string &key : unused_attributes_) {
  61. PrintLine("Unused attribute: %s", key.c_str());
  62. }
  63. return kReadError;
  64. }
  65. if (!block_.empty() && !used_block_) {
  66. PrintLine("Unused block");
  67. return kReadError;
  68. }
  69. ClearTest();
  70. bool in_block = false;
  71. while (true) {
  72. // Read the next line.
  73. char buf[4096];
  74. if (fgets(buf, sizeof(buf), file_) == nullptr) {
  75. if (feof(file_)) {
  76. if (in_block) {
  77. fprintf(stderr, "Unterminated block.\n");
  78. return kReadError;
  79. }
  80. // EOF is a valid terminator for a test.
  81. return start_line_ > 0 ? kReadSuccess : kReadEOF;
  82. }
  83. fprintf(stderr, "Error reading from input.\n");
  84. return kReadError;
  85. }
  86. line_++;
  87. size_t len = strlen(buf);
  88. // Check for truncation.
  89. if (len > 0 && buf[len - 1] != '\n' && !feof(file_)) {
  90. fprintf(stderr, "Line %u too long.\n", line_);
  91. return kReadError;
  92. }
  93. bool is_delimiter = strncmp(buf, "---", 3) == 0;
  94. if (in_block) {
  95. block_ += buf;
  96. if (is_delimiter) {
  97. // Ending the block completes the test.
  98. return kReadSuccess;
  99. }
  100. } else if (is_delimiter) {
  101. if (start_line_ == 0) {
  102. fprintf(stderr, "Line %u: Unexpected block.\n", line_);
  103. return kReadError;
  104. }
  105. in_block = true;
  106. block_ += buf;
  107. } else if (buf[0] == '\n' || buf[0] == '\0') {
  108. // Empty lines delimit tests.
  109. if (start_line_ > 0) {
  110. return kReadSuccess;
  111. }
  112. } else if (buf[0] != '#') { // Comment lines are ignored.
  113. // Parse the line as an attribute.
  114. const char *delimiter = FindDelimiter(buf);
  115. if (delimiter == nullptr) {
  116. fprintf(stderr, "Line %u: Could not parse attribute.\n", line_);
  117. }
  118. std::string key = StripSpace(buf, delimiter - buf);
  119. std::string value = StripSpace(delimiter + 1,
  120. buf + len - delimiter - 1);
  121. unused_attributes_.insert(key);
  122. attributes_[key] = value;
  123. if (start_line_ == 0) {
  124. // This is the start of a test.
  125. type_ = key;
  126. parameter_ = value;
  127. start_line_ = line_;
  128. }
  129. }
  130. }
  131. }
  132. void FileTest::PrintLine(const char *format, ...) {
  133. va_list args;
  134. va_start(args, format);
  135. fprintf(stderr, "Line %u: ", start_line_);
  136. vfprintf(stderr, format, args);
  137. fprintf(stderr, "\n");
  138. va_end(args);
  139. }
  140. const std::string &FileTest::GetType() {
  141. OnKeyUsed(type_);
  142. return type_;
  143. }
  144. const std::string &FileTest::GetParameter() {
  145. OnKeyUsed(type_);
  146. return parameter_;
  147. }
  148. const std::string &FileTest::GetBlock() {
  149. used_block_ = true;
  150. return block_;
  151. }
  152. bool FileTest::HasAttribute(const std::string &key) {
  153. OnKeyUsed(key);
  154. return attributes_.count(key) > 0;
  155. }
  156. bool FileTest::GetAttribute(std::string *out_value, const std::string &key) {
  157. OnKeyUsed(key);
  158. auto iter = attributes_.find(key);
  159. if (iter == attributes_.end()) {
  160. PrintLine("Missing attribute '%s'.", key.c_str());
  161. return false;
  162. }
  163. *out_value = iter->second;
  164. return true;
  165. }
  166. const std::string &FileTest::GetAttributeOrDie(const std::string &key) {
  167. if (!HasAttribute(key)) {
  168. abort();
  169. }
  170. return attributes_[key];
  171. }
  172. static bool FromHexDigit(uint8_t *out, char c) {
  173. if ('0' <= c && c <= '9') {
  174. *out = c - '0';
  175. return true;
  176. }
  177. if ('a' <= c && c <= 'f') {
  178. *out = c - 'a' + 10;
  179. return true;
  180. }
  181. if ('A' <= c && c <= 'F') {
  182. *out = c - 'A' + 10;
  183. return true;
  184. }
  185. return false;
  186. }
  187. bool FileTest::GetBytes(std::vector<uint8_t> *out, const std::string &key) {
  188. std::string value;
  189. if (!GetAttribute(&value, key)) {
  190. return false;
  191. }
  192. if (value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') {
  193. out->assign(value.begin() + 1, value.end() - 1);
  194. return true;
  195. }
  196. if (value.size() % 2 != 0) {
  197. PrintLine("Error decoding value: %s", value.c_str());
  198. return false;
  199. }
  200. out->reserve(value.size() / 2);
  201. for (size_t i = 0; i < value.size(); i += 2) {
  202. uint8_t hi, lo;
  203. if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i+1])) {
  204. PrintLine("Error decoding value: %s", value.c_str());
  205. return false;
  206. }
  207. out->push_back((hi << 4) | lo);
  208. }
  209. return true;
  210. }
  211. static std::string EncodeHex(const uint8_t *in, size_t in_len) {
  212. static const char kHexDigits[] = "0123456789abcdef";
  213. std::string ret;
  214. ret.reserve(in_len * 2);
  215. for (size_t i = 0; i < in_len; i++) {
  216. ret += kHexDigits[in[i] >> 4];
  217. ret += kHexDigits[in[i] & 0xf];
  218. }
  219. return ret;
  220. }
  221. bool FileTest::ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
  222. const uint8_t *actual, size_t actual_len) {
  223. if (expected_len == actual_len &&
  224. memcmp(expected, actual, expected_len) == 0) {
  225. return true;
  226. }
  227. std::string expected_hex = EncodeHex(expected, expected_len);
  228. std::string actual_hex = EncodeHex(actual, actual_len);
  229. PrintLine("Expected: %s", expected_hex.c_str());
  230. PrintLine("Actual: %s", actual_hex.c_str());
  231. return false;
  232. }
  233. void FileTest::ClearTest() {
  234. start_line_ = 0;
  235. type_.clear();
  236. parameter_.clear();
  237. attributes_.clear();
  238. block_.clear();
  239. unused_attributes_.clear();
  240. used_block_ = false;
  241. }
  242. void FileTest::OnKeyUsed(const std::string &key) {
  243. unused_attributes_.erase(key);
  244. }
  245. int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
  246. const char *path) {
  247. FileTest t(path);
  248. if (!t.is_open()) {
  249. return 1;
  250. }
  251. bool failed = false;
  252. while (true) {
  253. FileTest::ReadResult ret = t.ReadNext();
  254. if (ret == FileTest::kReadError) {
  255. return 1;
  256. } else if (ret == FileTest::kReadEOF) {
  257. break;
  258. }
  259. bool result = run_test(&t, arg);
  260. if (t.HasAttribute("Error")) {
  261. if (result) {
  262. t.PrintLine("Operation unexpectedly succeeded.");
  263. failed = true;
  264. continue;
  265. }
  266. uint32_t err = ERR_peek_error();
  267. if (ERR_reason_error_string(err) != t.GetAttributeOrDie("Error")) {
  268. t.PrintLine("Unexpected error; wanted '%s', got '%s'.",
  269. t.GetAttributeOrDie("Error").c_str(),
  270. ERR_reason_error_string(err));
  271. failed = true;
  272. continue;
  273. }
  274. ERR_clear_error();
  275. } else if (!result) {
  276. // In case the test itself doesn't print output, print something so the
  277. // line number is reported.
  278. t.PrintLine("Test failed");
  279. ERR_print_errors_fp(stderr);
  280. failed = true;
  281. continue;
  282. }
  283. }
  284. if (failed) {
  285. return 1;
  286. }
  287. printf("PASS\n");
  288. return 0;
  289. }