You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

250 rivejä
9.7 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. };
  107. explicit FileTest(std::unique_ptr<LineReader> reader,
  108. std::function<void(const std::string &)> comment_callback);
  109. ~FileTest();
  110. // ReadNext reads the next test from the file. It returns |kReadSuccess| if
  111. // successfully reading a test and |kReadEOF| at the end of the file. On
  112. // error or if the previous test had unconsumed attributes, it returns
  113. // |kReadError|.
  114. ReadResult ReadNext();
  115. // PrintLine is a variant of printf which prepends the line number and appends
  116. // a trailing newline.
  117. void PrintLine(const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3);
  118. unsigned start_line() const { return start_line_; }
  119. // GetType returns the name of the first attribute of the current test.
  120. const std::string &GetType();
  121. // GetParameter returns the value of the first attribute of the current test.
  122. const std::string &GetParameter();
  123. // HasAttribute returns true if the current test has an attribute named |key|.
  124. bool HasAttribute(const std::string &key);
  125. // GetAttribute looks up the attribute with key |key|. It sets |*out_value| to
  126. // the value and returns true if it exists and returns false with an error to
  127. // |stderr| otherwise.
  128. bool GetAttribute(std::string *out_value, const std::string &key);
  129. // GetAttributeOrDie looks up the attribute with key |key| and aborts if it is
  130. // missing. It should only be used after a |HasAttribute| call.
  131. const std::string &GetAttributeOrDie(const std::string &key);
  132. // GetBytes looks up the attribute with key |key| and decodes it as a byte
  133. // string. On success, it writes the result to |*out| and returns
  134. // true. Otherwise it returns false with an error to |stderr|. The value may
  135. // be either a hexadecimal string or a quoted ASCII string. It returns true on
  136. // success and returns false with an error to |stderr| on failure.
  137. bool GetBytes(std::vector<uint8_t> *out, const std::string &key);
  138. // ExpectBytesEqual returns true if |expected| and |actual| are equal.
  139. // Otherwise, it returns false and prints a message to |stderr|.
  140. bool ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
  141. const uint8_t *actual, size_t actual_len);
  142. // AtNewInstructionBlock returns true if the current test was immediately
  143. // preceded by an instruction block.
  144. bool IsAtNewInstructionBlock() const;
  145. // HasInstruction returns true if the current test has an instruction.
  146. bool HasInstruction(const std::string &key);
  147. // GetInstruction looks up the instruction with key |key|. It sets
  148. // |*out_value| to the value (empty string if the instruction has no value)
  149. // and returns true if it exists and returns false with an error to |stderr|
  150. // otherwise.
  151. bool GetInstruction(std::string *out_value, const std::string &key);
  152. // CurrentTestToString returns the file content parsed for the current test.
  153. // If the current test was preceded by an instruction block, the return test
  154. // case is preceded by the instruction block and a single blank line. All
  155. // other blank or comment lines are omitted.
  156. const std::string &CurrentTestToString() const;
  157. // InjectInstruction adds a key value pair to the most recently parsed set of
  158. // instructions.
  159. void InjectInstruction(const std::string &key, const std::string &value);
  160. // SkipCurrent passes the current test case. Unused attributes are ignored.
  161. void SkipCurrent();
  162. private:
  163. void ClearTest();
  164. void ClearInstructions();
  165. void OnKeyUsed(const std::string &key);
  166. void OnInstructionUsed(const std::string &key);
  167. std::unique_ptr<LineReader> reader_;
  168. // line_ is the number of lines read.
  169. unsigned line_ = 0;
  170. // start_line_ is the line number of the first attribute of the test.
  171. unsigned start_line_ = 0;
  172. // type_ is the name of the first attribute of the test.
  173. std::string type_;
  174. // parameter_ is the value of the first attribute.
  175. std::string parameter_;
  176. // attributes_ contains all attributes in the test, including the first.
  177. std::map<std::string, std::string> attributes_;
  178. // instructions_ contains all instructions in scope for the test.
  179. std::map<std::string, std::string> instructions_;
  180. // unused_attributes_ is the set of attributes that have not been queried.
  181. std::set<std::string> unused_attributes_;
  182. // unused_instructions_ is the set of instructions that have not been queried.
  183. std::set<std::string> unused_instructions_;
  184. std::string current_test_;
  185. bool is_at_new_instruction_block_ = false;
  186. // comment_callback_, if set, is a callback function that is called with the
  187. // contents of each comment as they are parsed.
  188. std::function<void(const std::string&)> comment_callback_;
  189. FileTest(const FileTest &) = delete;
  190. FileTest &operator=(const FileTest &) = delete;
  191. };
  192. // FileTestMain runs a file-based test out of |path| and returns an exit code
  193. // suitable to return out of |main|. |run_test| should return true on pass and
  194. // false on failure. FileTestMain also implements common handling of the 'Error'
  195. // attribute. A test with that attribute is expected to fail. The value of the
  196. // attribute is the reason string of the expected OpenSSL error code.
  197. //
  198. // Tests are guaranteed to run serially and may affect global state if need be.
  199. // It is legal to use "tests" which, for example, import a private key into a
  200. // list of keys. This may be used to initialize a shared set of keys for many
  201. // tests. However, if one test fails, the framework will continue to run
  202. // subsequent tests.
  203. int FileTestMain(FileTestFunc run_test, void *arg, const char *path);
  204. // FileTestMain accepts a larger number of options via a struct.
  205. int FileTestMain(const FileTest::Options &opts);
  206. // FileTestGTest behaves like FileTestMain, but for GTest. |path| must be the
  207. // name of a test file embedded in the test binary.
  208. void FileTestGTest(const char *path, std::function<void(FileTest *)> run_test);
  209. #endif // OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H