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.
 
 
 
 
 
 

167 lines
6.6 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 <stdint.h>
  17. #include <stdio.h>
  18. #include <string>
  19. #include <map>
  20. #include <set>
  21. #include <vector>
  22. // File-based test framework.
  23. //
  24. // This module provides a file-based test framework. The file format is based on
  25. // that of OpenSSL upstream's evp_test and BoringSSL's aead_test. Each input
  26. // file is a sequence of attributes, blocks, and blank lines.
  27. //
  28. // Each attribute has the form:
  29. //
  30. // Name = Value
  31. //
  32. // Either '=' or ':' may be used to delimit the name from the value. Both the
  33. // name and value have leading and trailing spaces stripped.
  34. //
  35. // Blocks are delimited by lines beginning with three hyphens, "---". One such
  36. // line begins a block and another ends it. Blocks are intended as a convenient
  37. // way to embed PEM data and include their delimiters.
  38. //
  39. // Outside a block, lines beginning with # are ignored.
  40. //
  41. // A test is a sequence of one or more attributes followed by a block or blank
  42. // line. Blank lines are otherwise ignored. For tests that process multiple
  43. // kinds of test cases, the first attribute is parsed out as the test's type and
  44. // parameter. Otherwise, attributes are unordered. The first attribute is also
  45. // included in the set of attributes, so tests which do not dispatch may ignore
  46. // this mechanism.
  47. //
  48. // Functions in this module freely output to |stderr| on failure. Tests should
  49. // also do so, and it is recommended they include the corresponding test's line
  50. // number in any output. |PrintLine| does this automatically.
  51. //
  52. // Each attribute in a test must be consumed. When a test completes, if any
  53. // attributes haven't been processed, the framework reports an error.
  54. class FileTest {
  55. public:
  56. explicit FileTest(const char *path);
  57. ~FileTest();
  58. // is_open returns true if the file was successfully opened.
  59. bool is_open() const { return file_ != nullptr; }
  60. enum ReadResult {
  61. kReadSuccess,
  62. kReadEOF,
  63. kReadError,
  64. };
  65. // ReadNext reads the next test from the file. It returns |kReadSuccess| if
  66. // successfully reading a test and |kReadEOF| at the end of the file. On
  67. // error or if the previous test had unconsumed attributes, it returns
  68. // |kReadError|.
  69. ReadResult ReadNext();
  70. // PrintLine is a variant of printf which prepends the line number and appends
  71. // a trailing newline.
  72. void PrintLine(const char *format, ...)
  73. #ifdef __GNUC__
  74. __attribute__((__format__(__printf__, 2, 3)))
  75. #endif
  76. ;
  77. unsigned start_line() const { return start_line_; }
  78. // GetType returns the name of the first attribute of the current test.
  79. const std::string &GetType();
  80. // GetParameter returns the value of the first attribute of the current test.
  81. const std::string &GetParameter();
  82. // GetBlock returns the optional block of the current test, or the empty
  83. // if there was no block.
  84. const std::string &GetBlock();
  85. // HasAttribute returns true if the current test has an attribute named |key|.
  86. bool HasAttribute(const std::string &key);
  87. // GetAttribute looks up the attribute with key |key|. It sets |*out_value| to
  88. // the value and returns true if it exists and returns false with an error to
  89. // |stderr| otherwise.
  90. bool GetAttribute(std::string *out_value, const std::string &key);
  91. // GetAttributeOrDie looks up the attribute with key |key| and aborts if it is
  92. // missing. It only be used after a |HasAttribute| call.
  93. const std::string &GetAttributeOrDie(const std::string &key);
  94. // GetBytes looks up the attribute with key |key| and decodes it as a byte
  95. // string. On success, it writes the result to |*out| and returns
  96. // true. Otherwise it returns false with an error to |stderr|. The value may
  97. // be either a hexadecimal string or a quoted ASCII string. It returns true on
  98. // success and returns false with an error to |stderr| on failure.
  99. bool GetBytes(std::vector<uint8_t> *out, const std::string &key);
  100. // ExpectBytesEqual returns true if |expected| and |actual| are equal.
  101. // Otherwise, it returns false and prints a message to |stderr|.
  102. bool ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
  103. const uint8_t *actual, size_t actual_len);
  104. private:
  105. void ClearTest();
  106. void OnKeyUsed(const std::string &key);
  107. FILE *file_ = nullptr;
  108. // line_ is the number of lines read.
  109. unsigned line_ = 0;
  110. // start_line_ is the line number of the first attribute of the test.
  111. unsigned start_line_ = 0;
  112. // type_ is the name of the first attribute of the test.
  113. std::string type_;
  114. // parameter_ is the value of the first attribute.
  115. std::string parameter_;
  116. // attributes_ contains all attributes in the test, including the first.
  117. std::map<std::string, std::string> attributes_;
  118. // block_, if non-empty, is the test's optional trailing block.
  119. std::string block_;
  120. // unused_attributes_ is the set of attributes that have been queried.
  121. std::set<std::string> unused_attributes_;
  122. // used_block_ is true if the block has been queried.
  123. bool used_block_ = false;
  124. FileTest(const FileTest&) = delete;
  125. FileTest &operator=(const FileTest&) = delete;
  126. };
  127. // FileTestMain runs a file-based test out of |path| and returns an exit code
  128. // suitable to return out of |main|. |run_test| should return true on pass and
  129. // false on failure. FileTestMain also implements common handling of the 'Error'
  130. // attribute. A test with that attribute is expected to fail. The value of the
  131. // attribute is the reason string of the expected OpenSSL error code.
  132. //
  133. // Tests are guaranteed to run serially and may affect global state if need be.
  134. // It is legal to use "tests" which, for example, import a private key into a
  135. // list of keys. This may be used to initialize a shared set of keys for many
  136. // tests. However, if one test fails, the framework will continue to run
  137. // subsequent tests.
  138. int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
  139. const char *path);
  140. #endif /* OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H */