Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

272 строки
11 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. #ifndef OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H
  15. #define OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H
  16. #include <openssl/base.h>
  17. #include <stdint.h>
  18. OPENSSL_MSVC_PRAGMA(warning(push))
  19. OPENSSL_MSVC_PRAGMA(warning(disable : 4702))
  20. #include <functional>
  21. #include <map>
  22. #include <memory>
  23. #include <set>
  24. #include <string>
  25. #include <vector>
  26. OPENSSL_MSVC_PRAGMA(warning(pop))
  27. // File-based test framework.
  28. //
  29. // This module provides a file-based test framework. The file format is based on
  30. // that of OpenSSL upstream's evp_test and BoringSSL's aead_test. NIST CAVP test
  31. // vector files are also supported. Each input file is a sequence of attributes,
  32. // instructions and blank lines.
  33. //
  34. // Each attribute has the form:
  35. //
  36. // Name = Value
  37. //
  38. // Instructions are enclosed in square brackets and may appear without a value:
  39. //
  40. // [Name = Value]
  41. //
  42. // or
  43. //
  44. // [Name]
  45. //
  46. // Commas in instruction lines are treated as separate instructions. Thus this:
  47. //
  48. // [Name1,Name2]
  49. //
  50. // is the same as:
  51. //
  52. // [Name1]
  53. // [Name2]
  54. //
  55. // Either '=' or ':' may be used to delimit the name from the value. Both the
  56. // name and value have leading and trailing spaces stripped.
  57. //
  58. // Each file contains a number of instruction blocks and test cases.
  59. //
  60. // An instruction block is a sequence of instructions followed by a blank line.
  61. // Instructions apply to all test cases following its appearance, until the next
  62. // instruction block. Instructions are unordered.
  63. //
  64. // A test is a sequence of one or more attributes followed by a blank line. For
  65. // tests that process multiple kinds of test cases, the first attribute is
  66. // parsed out as the test's type and parameter. Otherwise, attributes are
  67. // unordered. The first attribute is also included in the set of attributes, so
  68. // tests which do not dispatch may ignore this mechanism.
  69. //
  70. // Additional blank lines and lines beginning with # are ignored.
  71. //
  72. // Functions in this module freely output to |stderr| on failure. Tests should
  73. // also do so, and it is recommended they include the corresponding test's line
  74. // number in any output. |PrintLine| does this automatically.
  75. //
  76. // Each attribute in a test and all instructions applying to it must be
  77. // consumed. When a test completes, if any attributes or insturctions haven't
  78. // been processed, the framework reports an error.
  79. class FileTest;
  80. typedef bool (*FileTestFunc)(FileTest *t, void *arg);
  81. class FileTest {
  82. public:
  83. enum ReadResult {
  84. kReadSuccess,
  85. kReadEOF,
  86. kReadError,
  87. };
  88. class LineReader {
  89. public:
  90. virtual ~LineReader() {}
  91. virtual ReadResult ReadLine(char *out, size_t len) = 0;
  92. };
  93. struct Options {
  94. // path is the path to the input file.
  95. const char *path = nullptr;
  96. // callback is called for each test. It should get the parameters from this
  97. // object and signal any errors by returning false.
  98. FileTestFunc callback = nullptr;
  99. // arg is an opaque pointer that is passed to |callback|.
  100. void *arg = nullptr;
  101. // silent suppressed the "PASS" string that is otherwise printed after
  102. // successful runs.
  103. bool silent = false;
  104. // comment_callback is called after each comment in the input is parsed.
  105. std::function<void(const std::string&)> comment_callback;
  106. // is_kas_test is true if a NIST “KAS” test is being parsed. These tests
  107. // are inconsistent with the other NIST files to such a degree that they
  108. // need their own boolean.
  109. bool is_kas_test = false;
  110. };
  111. explicit FileTest(std::unique_ptr<LineReader> reader,
  112. std::function<void(const std::string &)> comment_callback,
  113. bool is_kas_test);
  114. ~FileTest();
  115. // ReadNext reads the next test from the file. It returns |kReadSuccess| if
  116. // successfully reading a test and |kReadEOF| at the end of the file. On
  117. // error or if the previous test had unconsumed attributes, it returns
  118. // |kReadError|.
  119. ReadResult ReadNext();
  120. // PrintLine is a variant of printf which prepends the line number and appends
  121. // a trailing newline.
  122. void PrintLine(const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3);
  123. unsigned start_line() const { return start_line_; }
  124. // GetType returns the name of the first attribute of the current test.
  125. const std::string &GetType();
  126. // GetParameter returns the value of the first attribute of the current test.
  127. const std::string &GetParameter();
  128. // HasAttribute returns true if the current test has an attribute named |key|.
  129. bool HasAttribute(const std::string &key);
  130. // GetAttribute looks up the attribute with key |key|. It sets |*out_value| to
  131. // the value and returns true if it exists and returns false with an error to
  132. // |stderr| otherwise.
  133. bool GetAttribute(std::string *out_value, const std::string &key);
  134. // GetAttributeOrDie looks up the attribute with key |key| and aborts if it is
  135. // missing. It should only be used after a |HasAttribute| call.
  136. const std::string &GetAttributeOrDie(const std::string &key);
  137. // IgnoreAttribute marks the attribute with key |key| as used.
  138. void IgnoreAttribute(const std::string &key) { HasAttribute(key); }
  139. // GetBytes looks up the attribute with key |key| and decodes it as a byte
  140. // string. On success, it writes the result to |*out| and returns
  141. // true. Otherwise it returns false with an error to |stderr|. The value may
  142. // be either a hexadecimal string or a quoted ASCII string. It returns true on
  143. // success and returns false with an error to |stderr| on failure.
  144. bool GetBytes(std::vector<uint8_t> *out, const std::string &key);
  145. // ExpectBytesEqual returns true if |expected| and |actual| are equal.
  146. // Otherwise, it returns false and prints a message to |stderr|.
  147. bool ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
  148. const uint8_t *actual, size_t actual_len);
  149. // AtNewInstructionBlock returns true if the current test was immediately
  150. // preceded by an instruction block.
  151. bool IsAtNewInstructionBlock() const;
  152. // HasInstruction returns true if the current test has an instruction.
  153. bool HasInstruction(const std::string &key);
  154. // IgnoreInstruction marks the instruction with key |key| as used.
  155. void IgnoreInstruction(const std::string &key) { HasInstruction(key); }
  156. // GetInstruction looks up the instruction with key |key|. It sets
  157. // |*out_value| to the value (empty string if the instruction has no value)
  158. // and returns true if it exists and returns false with an error to |stderr|
  159. // otherwise.
  160. bool GetInstruction(std::string *out_value, const std::string &key);
  161. // GetInstructionOrDie looks up the instruction with key |key| and aborts if
  162. // it is missing. It should only be used after a |HasInstruction| call.
  163. const std::string &GetInstructionOrDie(const std::string &key);
  164. // GetInstructionBytes behaves like GetBytes, but looks up the corresponding
  165. // instruction.
  166. bool GetInstructionBytes(std::vector<uint8_t> *out, const std::string &key);
  167. // CurrentTestToString returns the file content parsed for the current test.
  168. // If the current test was preceded by an instruction block, the return test
  169. // case is preceded by the instruction block and a single blank line. All
  170. // other blank or comment lines are omitted.
  171. const std::string &CurrentTestToString() const;
  172. // InjectInstruction adds a key value pair to the most recently parsed set of
  173. // instructions.
  174. void InjectInstruction(const std::string &key, const std::string &value);
  175. // SkipCurrent passes the current test case. Unused attributes are ignored.
  176. void SkipCurrent();
  177. private:
  178. void ClearTest();
  179. void ClearInstructions();
  180. void OnKeyUsed(const std::string &key);
  181. void OnInstructionUsed(const std::string &key);
  182. bool ConvertToBytes(std::vector<uint8_t> *out, const std::string &value);
  183. std::unique_ptr<LineReader> reader_;
  184. // line_ is the number of lines read.
  185. unsigned line_ = 0;
  186. // start_line_ is the line number of the first attribute of the test.
  187. unsigned start_line_ = 0;
  188. // type_ is the name of the first attribute of the test.
  189. std::string type_;
  190. // parameter_ is the value of the first attribute.
  191. std::string parameter_;
  192. // attributes_ contains all attributes in the test, including the first.
  193. std::map<std::string, std::string> attributes_;
  194. // instructions_ contains all instructions in scope for the test.
  195. std::map<std::string, std::string> instructions_;
  196. // unused_attributes_ is the set of attributes that have not been queried.
  197. std::set<std::string> unused_attributes_;
  198. // unused_instructions_ is the set of instructions that have not been queried.
  199. std::set<std::string> unused_instructions_;
  200. std::string current_test_;
  201. bool is_at_new_instruction_block_ = false;
  202. bool seen_non_comment_ = false;
  203. bool is_kas_test_ = false;
  204. // comment_callback_, if set, is a callback function that is called with the
  205. // contents of each comment as they are parsed.
  206. std::function<void(const std::string&)> comment_callback_;
  207. FileTest(const FileTest &) = delete;
  208. FileTest &operator=(const FileTest &) = delete;
  209. };
  210. // FileTestMain runs a file-based test out of |path| and returns an exit code
  211. // suitable to return out of |main|. |run_test| should return true on pass and
  212. // false on failure. FileTestMain also implements common handling of the 'Error'
  213. // attribute. A test with that attribute is expected to fail. The value of the
  214. // attribute is the reason string of the expected OpenSSL error code.
  215. //
  216. // Tests are guaranteed to run serially and may affect global state if need be.
  217. // It is legal to use "tests" which, for example, import a private key into a
  218. // list of keys. This may be used to initialize a shared set of keys for many
  219. // tests. However, if one test fails, the framework will continue to run
  220. // subsequent tests.
  221. int FileTestMain(FileTestFunc run_test, void *arg, const char *path);
  222. // FileTestMain accepts a larger number of options via a struct.
  223. int FileTestMain(const FileTest::Options &opts);
  224. // FileTestGTest behaves like FileTestMain, but for GTest. |path| must be the
  225. // name of a test file embedded in the test binary.
  226. void FileTestGTest(const char *path, std::function<void(FileTest *)> run_test);
  227. #endif // OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H