external/boringssl: Sync to 58e449904e248f34bdfc2be7a609c58bcb0257b7.
This includes the following changes:
https://boringssl.googlesource.com/boringssl/+log/2c1523733a71166943e52da11ac2eae82b0227b8..58e449904e248f34bdfc2be7a609c58bcb0257b7
Test: BoringSSL CTS Presubmits
Change-Id: I1a825139c8c7076d09b8a3acc5f09a547a7cbe0d
diff --git a/src/crypto/test/file_test.cc b/src/crypto/test/file_test.cc
index 715907e..676b039 100644
--- a/src/crypto/test/file_test.cc
+++ b/src/crypto/test/file_test.cc
@@ -14,6 +14,7 @@
#include "file_test.h"
+#include <algorithm>
#include <memory>
#include <ctype.h>
@@ -60,26 +61,46 @@
str++;
len--;
}
- while (len > 0 && isspace(str[len-1])) {
+ while (len > 0 && isspace(str[len - 1])) {
len--;
}
return std::string(str, len);
}
+static std::pair<std::string, std::string> ParseKeyValue(const char *str, const size_t len) {
+ const char *delimiter = FindDelimiter(str);
+ std::string key, value;
+ if (delimiter == nullptr) {
+ key = StripSpace(str, len);
+ } else {
+ key = StripSpace(str, delimiter - str);
+ value = StripSpace(delimiter + 1, str + len - delimiter - 1);
+ }
+ return {key, value};
+}
+
FileTest::ReadResult FileTest::ReadNext() {
- // If the previous test had unused attributes, it is an error.
- if (!unused_attributes_.empty()) {
+ // If the previous test had unused attributes or instructions, it is an error.
+ if (!unused_attributes_.empty() && !ignore_unused_attributes_) {
for (const std::string &key : unused_attributes_) {
PrintLine("Unused attribute: %s", key.c_str());
}
return kReadError;
}
+ if (!unused_instructions_.empty() && !ignore_unused_attributes_) {
+ for (const std::string &key : unused_instructions_) {
+ PrintLine("Unused instruction: %s", key.c_str());
+ }
+ return kReadError;
+ }
ClearTest();
- static const size_t kBufLen = 64 + 8192*2;
+ static const size_t kBufLen = 8192 * 4;
std::unique_ptr<char[]> buf(new char[kBufLen]);
+ bool in_instruction_block = false;
+
while (true) {
// Read the next line.
if (fgets(buf.get(), kBufLen, file_) == nullptr) {
@@ -99,21 +120,60 @@
return kReadError;
}
- if (buf[0] == '\n' || buf[0] == '\0') {
+ if (buf[0] == '\n' || buf[0] == '\r' || buf[0] == '\0') {
// Empty lines delimit tests.
if (start_line_ > 0) {
return kReadSuccess;
}
- } else if (buf[0] != '#') { // Comment lines are ignored.
- // Parse the line as an attribute.
- const char *delimiter = FindDelimiter(buf.get());
- if (delimiter == nullptr) {
- fprintf(stderr, "Line %u: Could not parse attribute.\n", line_);
+ if (in_instruction_block) {
+ in_instruction_block = false;
+ // Delimit instruction block from test with a blank line.
+ current_test_ += "\r\n";
+ }
+ } else if (buf[0] == '[') { // Inside an instruction block.
+ if (start_line_ != 0) {
+ // Instructions should be separate blocks.
+ fprintf(stderr, "Line %u is an instruction in a test case.\n", line_);
return kReadError;
}
- std::string key = StripSpace(buf.get(), delimiter - buf.get());
- std::string value = StripSpace(delimiter + 1,
- buf.get() + len - delimiter - 1);
+ if (!in_instruction_block) {
+ ClearInstructions();
+ in_instruction_block = true;
+ }
+
+ // Parse the line as an instruction ("[key = value]" or "[key]").
+ std::string kv = StripSpace(buf.get(), len);
+ if (kv[kv.size() - 1] != ']') {
+ fprintf(stderr, "Line %u, invalid instruction: %s\n", line_,
+ kv.c_str());
+ return kReadError;
+ }
+ current_test_ += kv + "\r\n";
+ kv = std::string(kv.begin() + 1, kv.end() - 1);
+
+ for (;;) {
+ size_t idx = kv.find(",");
+ if (idx == std::string::npos) {
+ idx = kv.size();
+ }
+ std::string key, value;
+ std::tie(key, value) = ParseKeyValue(kv.c_str(), idx);
+ instructions_[key] = value;
+ if (idx == kv.size())
+ break;
+ kv = kv.substr(idx + 1);
+ }
+ } else if (buf[0] != '#') { // Comment lines are ignored.
+ if (in_instruction_block) {
+ // Test cases should be separate blocks.
+ fprintf(stderr, "Line %u is a test case attribute in an instruction block.\n",
+ line_);
+ return kReadError;
+ }
+
+ current_test_ += std::string(buf.get(), len);
+ std::string key, value;
+ std::tie(key, value) = ParseKeyValue(buf.get(), len);
unused_attributes_.insert(key);
attributes_[key] = value;
@@ -122,6 +182,9 @@
type_ = key;
parameter_ = value;
start_line_ = line_;
+ for (const auto &kv : instructions_) {
+ unused_instructions_.insert(kv.first);
+ }
}
}
}
@@ -171,6 +234,26 @@
return attributes_[key];
}
+bool FileTest::HasInstruction(const std::string &key) {
+ OnInstructionUsed(key);
+ return instructions_.count(key) > 0;
+}
+
+bool FileTest::GetInstruction(std::string *out_value, const std::string &key) {
+ OnInstructionUsed(key);
+ auto iter = instructions_.find(key);
+ if (iter == instructions_.end()) {
+ PrintLine("Missing instruction '%s'.", key.c_str());
+ return false;
+ }
+ *out_value = iter->second;
+ return true;
+}
+
+const std::string &FileTest::CurrentTestToString() const {
+ return current_test_;
+}
+
static bool FromHexDigit(uint8_t *out, char c) {
if ('0' <= c && c <= '9') {
*out = c - '0';
@@ -206,7 +289,7 @@
out->reserve(value.size() / 2);
for (size_t i = 0; i < value.size(); i += 2) {
uint8_t hi, lo;
- if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i+1])) {
+ if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i + 1])) {
PrintLine("Error decoding value: %s", value.c_str());
return false;
}
@@ -246,14 +329,28 @@
parameter_.clear();
attributes_.clear();
unused_attributes_.clear();
+ current_test_ = "";
+}
+
+void FileTest::ClearInstructions() {
+ instructions_.clear();
+ unused_attributes_.clear();
}
void FileTest::OnKeyUsed(const std::string &key) {
unused_attributes_.erase(key);
}
-int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
- const char *path) {
+void FileTest::OnInstructionUsed(const std::string &key) {
+ unused_instructions_.erase(key);
+}
+
+void FileTest::SetIgnoreUnusedAttributes(bool ignore) {
+ ignore_unused_attributes_ = ignore;
+}
+
+int FileTestMainSilent(bool (*run_test)(FileTest *t, void *arg), void *arg,
+ const char *path) {
FileTest t(path);
if (!t.is_open()) {
return 1;
@@ -278,8 +375,8 @@
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));
+ t.GetAttributeOrDie("Error").c_str(),
+ ERR_reason_error_string(err));
failed = true;
ERR_clear_error();
continue;
@@ -295,10 +392,14 @@
}
}
- if (failed) {
- return 1;
- }
+ return failed ? 1 : 0;
+}
- printf("PASS\n");
- return 0;
+int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
+ const char *path) {
+ int result = FileTestMainSilent(run_test, arg, path);
+ if (!result) {
+ printf("PASS\n");
+ }
+ return result;
}
diff --git a/src/crypto/test/file_test.h b/src/crypto/test/file_test.h
index a859127..aac9289 100644
--- a/src/crypto/test/file_test.h
+++ b/src/crypto/test/file_test.h
@@ -21,11 +21,11 @@
#include <stdio.h>
OPENSSL_MSVC_PRAGMA(warning(push))
-OPENSSL_MSVC_PRAGMA(warning(disable: 4702))
+OPENSSL_MSVC_PRAGMA(warning(disable : 4702))
-#include <string>
#include <map>
#include <set>
+#include <string>
#include <vector>
OPENSSL_MSVC_PRAGMA(warning(pop))
@@ -33,31 +33,55 @@
// File-based test framework.
//
// This module provides a file-based test framework. The file format is based on
-// that of OpenSSL upstream's evp_test and BoringSSL's aead_test. Each input
-// file is a sequence of attributes and blank lines.
+// that of OpenSSL upstream's evp_test and BoringSSL's aead_test. NIST CAVP test
+// vector files are also supported. Each input file is a sequence of attributes,
+// instructions and blank lines.
//
// Each attribute has the form:
//
// Name = Value
//
+// Instructions are enclosed in square brackets and may appear without a value:
+//
+// [Name = Value]
+//
+// or
+//
+// [Name]
+//
+// Commas in instruction lines are treated as separate instructions. Thus this:
+//
+// [Name1,Name2]
+//
+// is the same as:
+//
+// [Name1]
+// [Name2]
+//
// Either '=' or ':' may be used to delimit the name from the value. Both the
// name and value have leading and trailing spaces stripped.
//
-// Lines beginning with # are ignored.
+// Each file contains a number of instruction blocks and test cases.
//
-// A test is a sequence of one or more attributes followed by a blank line.
-// Blank lines are otherwise ignored. For tests that process multiple kinds of
-// test cases, the first attribute is parsed out as the test's type and
-// parameter. Otherwise, attributes are unordered. The first attribute is also
-// included in the set of attributes, so tests which do not dispatch may ignore
-// this mechanism.
+// An instruction block is a sequence of instructions followed by a blank line.
+// Instructions apply to all test cases following its appearance, until the next
+// instruction block. Instructions are unordered.
+//
+// A test is a sequence of one or more attributes followed by a blank line. For
+// tests that process multiple kinds of test cases, the first attribute is
+// parsed out as the test's type and parameter. Otherwise, attributes are
+// unordered. The first attribute is also included in the set of attributes, so
+// tests which do not dispatch may ignore this mechanism.
+//
+// Additional blank lines and lines beginning with # are ignored.
//
// Functions in this module freely output to |stderr| on failure. Tests should
// also do so, and it is recommended they include the corresponding test's line
// number in any output. |PrintLine| does this automatically.
//
-// Each attribute in a test must be consumed. When a test completes, if any
-// attributes haven't been processed, the framework reports an error.
+// Each attribute in a test and all instructions applying to it must be
+// consumed. When a test completes, if any attributes or insturctions haven't
+// been processed, the framework reports an error.
class FileTest {
@@ -115,9 +139,28 @@
bool ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
const uint8_t *actual, size_t actual_len);
+ // HasInstruction returns true if the current test has an instruction.
+ bool HasInstruction(const std::string &key);
+
+ // GetInstruction looks up the instruction with key |key|. It sets
+ // |*out_value| to the value (empty string if the instruction has no value)
+ // and returns true if it exists and returns false with an error to |stderr|
+ // otherwise.
+ bool GetInstruction(std::string *out_value, const std::string &key);
+
+ // CurrentTestToString returns the file content parsed for the current test.
+ // If the current test was preceded by an instruction block, the return test
+ // case is preceded by the instruction block and a single blank line. All
+ // other blank or comment lines are omitted.
+ const std::string &CurrentTestToString() const;
+
+ void SetIgnoreUnusedAttributes(bool ignore);
+
private:
void ClearTest();
+ void ClearInstructions();
void OnKeyUsed(const std::string &key);
+ void OnInstructionUsed(const std::string &key);
FILE *file_ = nullptr;
// line_ is the number of lines read.
@@ -131,12 +174,21 @@
std::string parameter_;
// attributes_ contains all attributes in the test, including the first.
std::map<std::string, std::string> attributes_;
+ // instructions_ contains all instructions in scope for the test.
+ std::map<std::string, std::string> instructions_;
- // unused_attributes_ is the set of attributes that have been queried.
+ // unused_attributes_ is the set of attributes that have not been queried.
std::set<std::string> unused_attributes_;
- FileTest(const FileTest&) = delete;
- FileTest &operator=(const FileTest&) = delete;
+ // unused_instructions_ is the set of instructions that have not been queried.
+ std::set<std::string> unused_instructions_;
+
+ std::string current_test_;
+
+ bool ignore_unused_attributes_ = false;
+
+ FileTest(const FileTest &) = delete;
+ FileTest &operator=(const FileTest &) = delete;
};
// FileTestMain runs a file-based test out of |path| and returns an exit code
@@ -153,5 +205,9 @@
int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
const char *path);
+// FileTestMainSilent behaves like FileTestMain but does not print a final
+// FAIL/PASS message to stdout.
+int FileTestMainSilent(bool (*run_test)(FileTest *t, void *arg), void *arg,
+ const char *path);
#endif /* OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H */
diff --git a/src/crypto/test/gtest_main.cc b/src/crypto/test/gtest_main.cc
index ea1135c..4071040 100644
--- a/src/crypto/test/gtest_main.cc
+++ b/src/crypto/test/gtest_main.cc
@@ -14,58 +14,11 @@
#include <gtest/gtest.h>
-#include <stdio.h>
+#include "gtest_main.h"
-#include <openssl/err.h>
-#include <openssl/crypto.h>
-
-#if defined(OPENSSL_WINDOWS)
-OPENSSL_MSVC_PRAGMA(warning(push, 3))
-#include <winsock2.h>
-OPENSSL_MSVC_PRAGMA(warning(pop))
-#endif
-
-
-namespace {
-
-class ErrorTestEventListener : public testing::EmptyTestEventListener {
- public:
- ErrorTestEventListener() {}
- ~ErrorTestEventListener() override {}
-
- void OnTestEnd(const testing::TestInfo &test_info) override {
- // If the test failed, print any errors left in the error queue.
- if (test_info.result()->Failed()) {
- ERR_print_errors_fp(stdout);
- }
-
- // Clean up the error queue for the next run.
- ERR_clear_error();
- }
-};
-
-} // namespace
int main(int argc, char **argv) {
- CRYPTO_library_init();
-
-#if defined(OPENSSL_WINDOWS)
- // Initialize Winsock.
- WORD wsa_version = MAKEWORD(2, 2);
- WSADATA wsa_data;
- int wsa_err = WSAStartup(wsa_version, &wsa_data);
- if (wsa_err != 0) {
- fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
- return 1;
- }
- if (wsa_data.wVersion != wsa_version) {
- fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
- return 1;
- }
-#endif
-
testing::InitGoogleTest(&argc, argv);
- testing::UnitTest::GetInstance()->listeners().Append(
- new ErrorTestEventListener);
+ bssl::SetupGoogleTest();
return RUN_ALL_TESTS();
}
diff --git a/src/crypto/test/gtest_main.h b/src/crypto/test/gtest_main.h
new file mode 100644
index 0000000..395b281
--- /dev/null
+++ b/src/crypto/test/gtest_main.h
@@ -0,0 +1,78 @@
+/* Copyright (c) 2017, Google Inc.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+
+#ifndef OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H
+#define OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <gtest/gtest.h>
+
+#include <openssl/crypto.h>
+#include <openssl/err.h>
+
+#if defined(OPENSSL_WINDOWS)
+OPENSSL_MSVC_PRAGMA(warning(push, 3))
+#include <winsock2.h>
+OPENSSL_MSVC_PRAGMA(warning(pop))
+#endif
+
+
+namespace bssl {
+
+class ErrorTestEventListener : public testing::EmptyTestEventListener {
+ public:
+ ErrorTestEventListener() {}
+ ~ErrorTestEventListener() override {}
+
+ void OnTestEnd(const testing::TestInfo &test_info) override {
+ // If the test failed, print any errors left in the error queue.
+ if (test_info.result()->Failed()) {
+ ERR_print_errors_fp(stdout);
+ }
+
+ // Clean up the error queue for the next run.
+ ERR_clear_error();
+ }
+};
+
+// SetupGoogleTest should be called by the test runner after
+// testing::InitGoogleTest has been called and before RUN_ALL_TESTS.
+inline void SetupGoogleTest() {
+ CRYPTO_library_init();
+
+#if defined(OPENSSL_WINDOWS)
+ // Initialize Winsock.
+ WORD wsa_version = MAKEWORD(2, 2);
+ WSADATA wsa_data;
+ int wsa_err = WSAStartup(wsa_version, &wsa_data);
+ if (wsa_err != 0) {
+ fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
+ exit(1);
+ }
+ if (wsa_data.wVersion != wsa_version) {
+ fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
+ exit(1);
+ }
+#endif
+
+ testing::UnitTest::GetInstance()->listeners().Append(
+ new ErrorTestEventListener);
+}
+
+} // namespace bssl
+
+
+#endif /* OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H */