diff options
author | David Benjamin <davidben@chromium.org> | 2015-05-11 22:58:08 +0300 |
---|---|---|
committer | Adam Langley <agl@google.com> | 2015-05-12 00:44:36 +0300 |
commit | 5c694e3fef39ac4c2d23fcaec38fc38836bb2472 (patch) | |
tree | 1a1d92049fb35ee1bf88efe55a151c3835abd786 /crypto/test | |
parent | 074e3d2dfd51b6e7fe51968cd7b6685591b8eda4 (diff) |
Add evp_test, loosely based on upstream's version.
This imports the EVP_PKEY test data of upstream's evptests.txt, but
modified to fit our test framework and with a new test driver. The
remainder of the test data will be imported separately into aead_test
and cipher_test.
Some minor changes to the test format were made to account for test
framework differences. One test has different results since we don't
support RSA signatures with omitted (rather than NULL) parameters.
Otherwise, the biggest difference in test format is that the ad-hoc
result strings are replaced with checking ERR_peek_error.
Change-Id: I758869abbeb843f5f2ac6c1cbd87333baec08ec3
Reviewed-on: https://boringssl-review.googlesource.com/4703
Reviewed-by: Adam Langley <agl@google.com>
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/file_test.cc | 35 | ||||
-rw-r--r-- | crypto/test/file_test.h | 11 | ||||
-rw-r--r-- | crypto/test/scoped_types.h | 1 | ||||
-rw-r--r-- | crypto/test/stl_compat.h | 5 |
4 files changed, 48 insertions, 4 deletions
diff --git a/crypto/test/file_test.cc b/crypto/test/file_test.cc index 907e57b0..12405f20 100644 --- a/crypto/test/file_test.cc +++ b/crypto/test/file_test.cc @@ -19,6 +19,8 @@ #include <stdarg.h> #include <string.h> +#include <openssl/err.h> + #include "stl_compat.h" @@ -184,6 +186,13 @@ bool FileTest::GetAttribute(std::string *out_value, const std::string &key) { return true; } +const std::string &FileTest::GetAttributeOrDie(const std::string &key) { + if (!HasAttribute(key)) { + abort(); + } + return attributes_[key]; +} + static bool FromHexDigit(uint8_t *out, char c) { if ('0' <= c && c <= '9') { *out = c - '0'; @@ -266,7 +275,8 @@ void FileTest::OnKeyUsed(const std::string &key) { unused_attributes_.erase(key); } -int FileTestMain(bool (*run_test)(FileTest *t), const char *path) { +int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg, + const char *path) { FileTest t(path); if (!t.is_open()) { return 1; @@ -281,8 +291,29 @@ int FileTestMain(bool (*run_test)(FileTest *t), const char *path) { break; } - if (!run_test(&t)) { + bool result = run_test(&t, arg); + if (t.HasAttribute("Error")) { + if (result) { + t.PrintLine("Operation unexpectedly succeeded."); + failed = true; + continue; + } + uint32_t err = ERR_peek_error(); + if (ERR_reason_error_string(err) != t.GetAttributeOrDie("Error")) { + t.PrintLine("Unexpected error; wanted '%s', got '%s'.", + t.GetAttributeOrDie("Error").c_str(), + ERR_reason_error_string(err)); + failed = true; + continue; + } + ERR_clear_error(); + } else if (!result) { + // In case the test itself doesn't print output, print something so the + // line number is reported. + t.PrintLine("Test failed"); + ERR_print_errors_fp(stderr); failed = true; + continue; } } diff --git a/crypto/test/file_test.h b/crypto/test/file_test.h index 5ea65c11..7303d8ab 100644 --- a/crypto/test/file_test.h +++ b/crypto/test/file_test.h @@ -104,6 +104,10 @@ class FileTest { // |stderr| otherwise. bool GetAttribute(std::string *out_value, const std::string &key); + // GetAttributeOrDie looks up the attribute with key |key| and aborts if it is + // missing. It only be used after a |HasAttribute| call. + const std::string &GetAttributeOrDie(const std::string &key); + // GetBytes looks up the attribute with key |key| and decodes it as a byte // string. On success, it writes the result to |*out| and returns // true. Otherwise it returns false with an error to |stderr|. The value may @@ -146,14 +150,17 @@ class FileTest { // FileTestMain runs a file-based test out of |path| and returns an exit code // suitable to return out of |main|. |run_test| should return true on pass and -// false on failure. +// false on failure. FileTestMain also implements common handling of the 'Error' +// attribute. A test with that attribute is expected to fail. The value of the +// attribute is the reason string of the expected OpenSSL error code. // // Tests are guaranteed to run serially and may affect global state if need be. // It is legal to use "tests" which, for example, import a private key into a // list of keys. This may be used to initialize a shared set of keys for many // tests. However, if one test fails, the framework will continue to run // subsequent tests. -int FileTestMain(bool (*run_test)(FileTest *t), const char *path); +int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg, + const char *path); #endif /* OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H */ diff --git a/crypto/test/scoped_types.h b/crypto/test/scoped_types.h index 9f5a8db3..eb04c18b 100644 --- a/crypto/test/scoped_types.h +++ b/crypto/test/scoped_types.h @@ -98,6 +98,7 @@ using ScopedEC_GROUP = ScopedOpenSSLType<EC_GROUP, EC_GROUP_free>; using ScopedEC_KEY = ScopedOpenSSLType<EC_KEY, EC_KEY_free>; using ScopedEC_POINT = ScopedOpenSSLType<EC_POINT, EC_POINT_free>; using ScopedEVP_PKEY = ScopedOpenSSLType<EVP_PKEY, EVP_PKEY_free>; +using ScopedEVP_PKEY_CTX = ScopedOpenSSLType<EVP_PKEY_CTX, EVP_PKEY_CTX_free>; using ScopedPKCS8_PRIV_KEY_INFO = ScopedOpenSSLType<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>; using ScopedPKCS12 = ScopedOpenSSLType<PKCS12, PKCS12_free>; diff --git a/crypto/test/stl_compat.h b/crypto/test/stl_compat.h index 9c45a982..1997a45c 100644 --- a/crypto/test/stl_compat.h +++ b/crypto/test/stl_compat.h @@ -32,6 +32,11 @@ static T *vector_data(std::vector<T> *out) { return out->empty() ? nullptr : &(*out)[0]; } +template <class T> +static const T *vector_data(const std::vector<T> *out) { + return out->empty() ? nullptr : &(*out)[0]; +} + // remove_reference is a reimplementation of |std::remove_reference| from C++11. template <class T> struct remove_reference { |