No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

305 líneas
7.8 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 <memory>
  16. #include <ctype.h>
  17. #include <errno.h>
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <openssl/err.h>
  22. #include "../internal.h"
  23. FileTest::FileTest(const char *path) {
  24. file_ = fopen(path, "r");
  25. if (file_ == nullptr) {
  26. fprintf(stderr, "Could not open file %s: %s.\n", path, strerror(errno));
  27. }
  28. }
  29. FileTest::~FileTest() {
  30. if (file_ != nullptr) {
  31. fclose(file_);
  32. }
  33. }
  34. // FindDelimiter returns a pointer to the first '=' or ':' in |str| or nullptr
  35. // if there is none.
  36. static const char *FindDelimiter(const char *str) {
  37. while (*str) {
  38. if (*str == ':' || *str == '=') {
  39. return str;
  40. }
  41. str++;
  42. }
  43. return nullptr;
  44. }
  45. // StripSpace returns a string containing up to |len| characters from |str| with
  46. // leading and trailing whitespace removed.
  47. static std::string StripSpace(const char *str, size_t len) {
  48. // Remove leading space.
  49. while (len > 0 && isspace(*str)) {
  50. str++;
  51. len--;
  52. }
  53. while (len > 0 && isspace(str[len-1])) {
  54. len--;
  55. }
  56. return std::string(str, len);
  57. }
  58. FileTest::ReadResult FileTest::ReadNext() {
  59. // If the previous test had unused attributes, it is an error.
  60. if (!unused_attributes_.empty()) {
  61. for (const std::string &key : unused_attributes_) {
  62. PrintLine("Unused attribute: %s", key.c_str());
  63. }
  64. return kReadError;
  65. }
  66. ClearTest();
  67. static const size_t kBufLen = 64 + 8192*2;
  68. std::unique_ptr<char[]> buf(new char[kBufLen]);
  69. while (true) {
  70. // Read the next line.
  71. if (fgets(buf.get(), kBufLen, file_) == nullptr) {
  72. if (feof(file_)) {
  73. // EOF is a valid terminator for a test.
  74. return start_line_ > 0 ? kReadSuccess : kReadEOF;
  75. }
  76. fprintf(stderr, "Error reading from input.\n");
  77. return kReadError;
  78. }
  79. line_++;
  80. size_t len = strlen(buf.get());
  81. // Check for truncation.
  82. if (len > 0 && buf[len - 1] != '\n' && !feof(file_)) {
  83. fprintf(stderr, "Line %u too long.\n", line_);
  84. return kReadError;
  85. }
  86. if (buf[0] == '\n' || buf[0] == '\0') {
  87. // Empty lines delimit tests.
  88. if (start_line_ > 0) {
  89. return kReadSuccess;
  90. }
  91. } else if (buf[0] != '#') { // Comment lines are ignored.
  92. // Parse the line as an attribute.
  93. const char *delimiter = FindDelimiter(buf.get());
  94. if (delimiter == nullptr) {
  95. fprintf(stderr, "Line %u: Could not parse attribute.\n", line_);
  96. return kReadError;
  97. }
  98. std::string key = StripSpace(buf.get(), delimiter - buf.get());
  99. std::string value = StripSpace(delimiter + 1,
  100. buf.get() + len - delimiter - 1);
  101. unused_attributes_.insert(key);
  102. attributes_[key] = value;
  103. if (start_line_ == 0) {
  104. // This is the start of a test.
  105. type_ = key;
  106. parameter_ = value;
  107. start_line_ = line_;
  108. }
  109. }
  110. }
  111. }
  112. void FileTest::PrintLine(const char *format, ...) {
  113. va_list args;
  114. va_start(args, format);
  115. fprintf(stderr, "Line %u: ", start_line_);
  116. vfprintf(stderr, format, args);
  117. fprintf(stderr, "\n");
  118. va_end(args);
  119. }
  120. const std::string &FileTest::GetType() {
  121. OnKeyUsed(type_);
  122. return type_;
  123. }
  124. const std::string &FileTest::GetParameter() {
  125. OnKeyUsed(type_);
  126. return parameter_;
  127. }
  128. bool FileTest::HasAttribute(const std::string &key) {
  129. OnKeyUsed(key);
  130. return attributes_.count(key) > 0;
  131. }
  132. bool FileTest::GetAttribute(std::string *out_value, const std::string &key) {
  133. OnKeyUsed(key);
  134. auto iter = attributes_.find(key);
  135. if (iter == attributes_.end()) {
  136. PrintLine("Missing attribute '%s'.", key.c_str());
  137. return false;
  138. }
  139. *out_value = iter->second;
  140. return true;
  141. }
  142. const std::string &FileTest::GetAttributeOrDie(const std::string &key) {
  143. if (!HasAttribute(key)) {
  144. abort();
  145. }
  146. return attributes_[key];
  147. }
  148. static bool FromHexDigit(uint8_t *out, char c) {
  149. if ('0' <= c && c <= '9') {
  150. *out = c - '0';
  151. return true;
  152. }
  153. if ('a' <= c && c <= 'f') {
  154. *out = c - 'a' + 10;
  155. return true;
  156. }
  157. if ('A' <= c && c <= 'F') {
  158. *out = c - 'A' + 10;
  159. return true;
  160. }
  161. return false;
  162. }
  163. bool FileTest::GetBytes(std::vector<uint8_t> *out, const std::string &key) {
  164. std::string value;
  165. if (!GetAttribute(&value, key)) {
  166. return false;
  167. }
  168. if (value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') {
  169. out->assign(value.begin() + 1, value.end() - 1);
  170. return true;
  171. }
  172. if (value.size() % 2 != 0) {
  173. PrintLine("Error decoding value: %s", value.c_str());
  174. return false;
  175. }
  176. out->clear();
  177. out->reserve(value.size() / 2);
  178. for (size_t i = 0; i < value.size(); i += 2) {
  179. uint8_t hi, lo;
  180. if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i+1])) {
  181. PrintLine("Error decoding value: %s", value.c_str());
  182. return false;
  183. }
  184. out->push_back((hi << 4) | lo);
  185. }
  186. return true;
  187. }
  188. static std::string EncodeHex(const uint8_t *in, size_t in_len) {
  189. static const char kHexDigits[] = "0123456789abcdef";
  190. std::string ret;
  191. ret.reserve(in_len * 2);
  192. for (size_t i = 0; i < in_len; i++) {
  193. ret += kHexDigits[in[i] >> 4];
  194. ret += kHexDigits[in[i] & 0xf];
  195. }
  196. return ret;
  197. }
  198. bool FileTest::ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
  199. const uint8_t *actual, size_t actual_len) {
  200. if (expected_len == actual_len &&
  201. OPENSSL_memcmp(expected, actual, expected_len) == 0) {
  202. return true;
  203. }
  204. std::string expected_hex = EncodeHex(expected, expected_len);
  205. std::string actual_hex = EncodeHex(actual, actual_len);
  206. PrintLine("Expected: %s", expected_hex.c_str());
  207. PrintLine("Actual: %s", actual_hex.c_str());
  208. return false;
  209. }
  210. void FileTest::ClearTest() {
  211. start_line_ = 0;
  212. type_.clear();
  213. parameter_.clear();
  214. attributes_.clear();
  215. unused_attributes_.clear();
  216. }
  217. void FileTest::OnKeyUsed(const std::string &key) {
  218. unused_attributes_.erase(key);
  219. }
  220. int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
  221. const char *path) {
  222. FileTest t(path);
  223. if (!t.is_open()) {
  224. return 1;
  225. }
  226. bool failed = false;
  227. while (true) {
  228. FileTest::ReadResult ret = t.ReadNext();
  229. if (ret == FileTest::kReadError) {
  230. return 1;
  231. } else if (ret == FileTest::kReadEOF) {
  232. break;
  233. }
  234. bool result = run_test(&t, arg);
  235. if (t.HasAttribute("Error")) {
  236. if (result) {
  237. t.PrintLine("Operation unexpectedly succeeded.");
  238. failed = true;
  239. continue;
  240. }
  241. uint32_t err = ERR_peek_error();
  242. if (ERR_reason_error_string(err) != t.GetAttributeOrDie("Error")) {
  243. t.PrintLine("Unexpected error; wanted '%s', got '%s'.",
  244. t.GetAttributeOrDie("Error").c_str(),
  245. ERR_reason_error_string(err));
  246. failed = true;
  247. ERR_clear_error();
  248. continue;
  249. }
  250. ERR_clear_error();
  251. } else if (!result) {
  252. // In case the test itself doesn't print output, print something so the
  253. // line number is reported.
  254. t.PrintLine("Test failed");
  255. ERR_print_errors_fp(stderr);
  256. failed = true;
  257. continue;
  258. }
  259. }
  260. if (failed) {
  261. return 1;
  262. }
  263. printf("PASS\n");
  264. return 0;
  265. }