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.

file_test.cc 7.8 KiB

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