Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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