From 5520aecb8d935664268d1191d88d38a61c1ffba8 Mon Sep 17 00:00:00 2001 From: Deye Date: Fri, 26 Jul 2024 13:56:01 -0700 Subject: [PATCH] test updates --- tool-openssl/internal.h | 41 --------------------------------- tool-openssl/md5_test.cc | 19 +++++++++++++++- tool-openssl/rsa_test.cc | 48 +++++++++++++++++++++++++++++++++++---- tool-openssl/x509_test.cc | 36 +++++++++++++++++++++++++---- 4 files changed, 93 insertions(+), 51 deletions(-) diff --git a/tool-openssl/internal.h b/tool-openssl/internal.h index a84f8cc58b..fe28f1b257 100644 --- a/tool-openssl/internal.h +++ b/tool-openssl/internal.h @@ -5,7 +5,6 @@ #define INTERNAL_H #include "../tool/internal.h" -#include #include #include #include @@ -30,44 +29,4 @@ bool X509Tool(const args_list_t &args); bool rsaTool(const args_list_t &args); bool md5Tool(const args_list_t &args); -// Helper function to trim whitespace from both ends of a string to test comparison output -static inline std::string &trim(std::string &s) { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { - return !std::isspace(static_cast(ch)); - })); - s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { - return !std::isspace(static_cast(ch)); - }).base(), s.end()); - return s; -} - -// Helper function to read file content into a string -inline std::string ReadFileToString(const std::string& file_path) { - std::ifstream file_stream(file_path, std::ios::binary); - if (!file_stream) { - return ""; - } - std::ostringstream buffer; - buffer << file_stream.rdbuf(); - return buffer.str(); -} - -inline void RunCommandsAndCompareOutput(const std::string &tool_command, const std::string &openssl_command, - const std::string &out_path_tool, const std::string &out_path_openssl, - std::string &tool_output_str, std::string &openssl_output_str) { - int tool_result = system(tool_command.c_str()); - ASSERT_EQ(tool_result, 0) << "AWS-LC tool command failed: " << tool_command; - - int openssl_result = system(openssl_command.c_str()); - ASSERT_EQ(openssl_result, 0) << "OpenSSL command failed: " << openssl_command; - - std::ifstream tool_output(out_path_tool); - tool_output_str = std::string((std::istreambuf_iterator(tool_output)), std::istreambuf_iterator()); - std::ifstream openssl_output(out_path_openssl); - openssl_output_str = std::string((std::istreambuf_iterator(openssl_output)), std::istreambuf_iterator()); - - std::cout << "AWS-LC tool output:" << std::endl << tool_output_str << std::endl; - std::cout << "OpenSSL output:" << std::endl << openssl_output_str << std::endl; -} - #endif //INTERNAL_H diff --git a/tool-openssl/md5_test.cc b/tool-openssl/md5_test.cc index 42e7b3d49f..91e9070200 100644 --- a/tool-openssl/md5_test.cc +++ b/tool-openssl/md5_test.cc @@ -51,6 +51,23 @@ class MD5ComparisonTest : public ::testing::Test { } + void RunCommandsAndCompareOutput(const std::string &tool_command, const std::string &openssl_command) { + int tool_result = system(tool_command.c_str()); + ASSERT_EQ(tool_result, 0) << "AWS-LC tool command failed: " << tool_command; + + int openssl_result = system(openssl_command.c_str()); + ASSERT_EQ(openssl_result, 0) << "OpenSSL command failed: " << openssl_command; + + std::ifstream tool_output(out_path_tool); + this->tool_output_str = std::string((std::istreambuf_iterator(tool_output)), std::istreambuf_iterator()); + std::ifstream openssl_output(out_path_openssl); + this->openssl_output_str = std::string((std::istreambuf_iterator(openssl_output)), std::istreambuf_iterator()); + + std::cout << "AWS-LC tool output:" << std::endl << this->tool_output_str << std::endl; + std::cout << "OpenSSL output:" << std::endl << this->openssl_output_str << std::endl; + } + + void TearDown() override { if (tool_executable_path != nullptr && openssl_executable_path != nullptr) { RemoveFile(in_path); @@ -82,7 +99,7 @@ TEST_F(MD5ComparisonTest, MD5ToolCompareOpenSSL) { std::string tool_command = std::string(tool_executable_path) + " md5 < " + input_file + " > " + out_path_tool; std::string openssl_command = std::string(openssl_executable_path) + " md5 < " + input_file + " > " + out_path_openssl; - RunCommandsAndCompareOutput(tool_command, openssl_command, out_path_tool, out_path_openssl, tool_output_str, openssl_output_str); + RunCommandsAndCompareOutput(tool_command, openssl_command); ASSERT_EQ(tool_output_str, openssl_output_str); diff --git a/tool-openssl/rsa_test.cc b/tool-openssl/rsa_test.cc index 71a54db70c..d27146eb6d 100644 --- a/tool-openssl/rsa_test.cc +++ b/tool-openssl/rsa_test.cc @@ -149,6 +149,23 @@ class RSAComparisonTest : public ::testing::Test { ASSERT_TRUE(PEM_write_RSAPrivateKey(in_file.get(), rsa.get(), nullptr, nullptr, 0, nullptr, nullptr)); } + void RunCommandsAndCompareOutput(const std::string &tool_command, const std::string &openssl_command) { + int tool_result = system(tool_command.c_str()); + ASSERT_EQ(tool_result, 0) << "AWS-LC tool command failed: " << tool_command; + + int openssl_result = system(openssl_command.c_str()); + ASSERT_EQ(openssl_result, 0) << "OpenSSL command failed: " << openssl_command; + + std::ifstream tool_output(out_path_tool); + this->tool_output_str = std::string((std::istreambuf_iterator(tool_output)), std::istreambuf_iterator()); + std::ifstream openssl_output(out_path_openssl); + this->openssl_output_str = std::string((std::istreambuf_iterator(openssl_output)), std::istreambuf_iterator()); + + std::cout << "AWS-LC tool output:" << std::endl << this->tool_output_str << std::endl; + std::cout << "OpenSSL output:" << std::endl << this->openssl_output_str << std::endl; + } + + void TearDown() override { if (tool_executable_path != nullptr && openssl_executable_path != nullptr) { RemoveFile(in_path); @@ -167,6 +184,29 @@ class RSAComparisonTest : public ::testing::Test { std::string openssl_output_str; }; +// Helper function to trim whitespace from both ends of a string to test RSA output +static inline std::string &trim(std::string &s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { + return !std::isspace(static_cast(ch)); + })); + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { + return !std::isspace(static_cast(ch)); + }).base(), s.end()); + return s; +} + +// Helper function to read file content into a string +std::string ReadFileToString(const std::string& file_path) { + std::ifstream file_stream(file_path, std::ios::binary); + if (!file_stream) { + return ""; + } + std::ostringstream buffer; + buffer << file_stream.rdbuf(); + return buffer.str(); +} + + // RSA boundaries const std::string RSA_BEGIN = "-----BEGIN RSA PRIVATE KEY-----"; const std::string RSA_END = "-----END RSA PRIVATE KEY-----"; @@ -186,7 +226,7 @@ TEST_F(RSAComparisonTest, RSAToolCompareModulusOpenSSL) { std::string tool_command = std::string(tool_executable_path) + " rsa -in " + in_path + " > " + out_path_tool; std::string openssl_command = std::string(openssl_executable_path) + " rsa -in " + in_path + " > " + out_path_openssl; - RunCommandsAndCompareOutput(tool_command, openssl_command, out_path_tool, out_path_openssl, tool_output_str, openssl_output_str); + RunCommandsAndCompareOutput(tool_command, openssl_command); trim(tool_output_str); ASSERT_TRUE(CheckBoundaries(tool_output_str, RSA_BEGIN, RSA_END, BEGIN, END)); @@ -201,7 +241,7 @@ TEST_F(RSAComparisonTest, RSAToolCompareModulusNooutOpenSSL) { std::string tool_command = std::string(tool_executable_path) + " rsa -in " + in_path + " -modulus -noout > " + out_path_tool; std::string openssl_command = std::string(openssl_executable_path) + " rsa -in " + in_path + " -modulus -noout > " + out_path_openssl; - RunCommandsAndCompareOutput(tool_command, openssl_command, out_path_tool, out_path_openssl, tool_output_str, openssl_output_str); + RunCommandsAndCompareOutput(tool_command, openssl_command); ASSERT_EQ(tool_output_str, openssl_output_str); } @@ -212,7 +252,7 @@ TEST_F(RSAComparisonTest, RSAToolCompareModulusOutOpenSSL) { std::string tool_command = std::string(tool_executable_path) + " rsa -in " + in_path + " -modulus -out " + out_path_tool; std::string openssl_command = std::string(openssl_executable_path) + " rsa -in " + in_path + " -modulus -out " + out_path_openssl; - RunCommandsAndCompareOutput(tool_command, openssl_command, out_path_tool, out_path_openssl, tool_output_str, openssl_output_str); + RunCommandsAndCompareOutput(tool_command, openssl_command); ScopedFILE tool_out_file(fopen(out_path_tool, "rb")); ASSERT_TRUE(tool_out_file); @@ -234,7 +274,7 @@ TEST_F(RSAComparisonTest, RSAToolCompareModulusOutNooutOpenSSL) { std::string tool_command = std::string(tool_executable_path) + " rsa -in " + in_path + " -modulus -out " + out_path_tool + " -noout"; std::string openssl_command = std::string(openssl_executable_path) + " rsa -in " + in_path + " -modulus -out " + out_path_openssl + " -noout"; - RunCommandsAndCompareOutput(tool_command, openssl_command, out_path_tool, out_path_openssl, tool_output_str, openssl_output_str); + RunCommandsAndCompareOutput(tool_command, openssl_command); ScopedFILE tool_out_file(fopen(out_path_tool, "rb")); ASSERT_TRUE(tool_out_file); diff --git a/tool-openssl/x509_test.cc b/tool-openssl/x509_test.cc index 9a038a1731..c063e5dd6c 100644 --- a/tool-openssl/x509_test.cc +++ b/tool-openssl/x509_test.cc @@ -278,6 +278,22 @@ class X509ComparisonTest : public ::testing::Test { ASSERT_TRUE(PEM_write_X509_REQ(csr_file.get(), csr.get())); } + void RunCommandsAndCompareOutput(const std::string &tool_command, const std::string &openssl_command) { + int tool_result = system(tool_command.c_str()); + ASSERT_EQ(tool_result, 0) << "AWS-LC tool command failed: " << tool_command; + + int openssl_result = system(openssl_command.c_str()); + ASSERT_EQ(openssl_result, 0) << "OpenSSL command failed: " << openssl_command; + + std::ifstream tool_output(out_path_tool); + this->tool_output_str = std::string((std::istreambuf_iterator(tool_output)), std::istreambuf_iterator()); + std::ifstream openssl_output(out_path_openssl); + this->openssl_output_str = std::string((std::istreambuf_iterator(openssl_output)), std::istreambuf_iterator()); + + std::cout << "AWS-LC tool output:" << std::endl << this->tool_output_str << std::endl; + std::cout << "OpenSSL output:" << std::endl << this->openssl_output_str << std::endl; + } + void TearDown() override { if (tool_executable_path != nullptr && openssl_executable_path != nullptr) { RemoveFile(in_path); @@ -301,7 +317,16 @@ class X509ComparisonTest : public ::testing::Test { std::string openssl_output_str; }; - +// Helper function to trim whitespace from both ends of a string to test certificate output +static inline std::string &trim(std::string &s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { + return !std::isspace(static_cast(ch)); + })); + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { + return !std::isspace(static_cast(ch)); + }).base(), s.end()); + return s; +} // Certificate boundaries const std::string CERT_BEGIN = "-----BEGIN CERTIFICATE-----"; const std::string CERT_END = "-----END CERTIFICATE-----"; @@ -311,7 +336,7 @@ TEST_F(X509ComparisonTest, X509ToolCompareModulusOpenSSL) { std::string tool_command = std::string(tool_executable_path) + " x509 -in " + in_path + " -modulus > " + out_path_tool; std::string openssl_command = std::string(openssl_executable_path) + " x509 -in " + in_path + " -modulus > " + out_path_openssl; - RunCommandsAndCompareOutput(tool_command, openssl_command, out_path_tool, out_path_openssl, tool_output_str, openssl_output_str); + RunCommandsAndCompareOutput(tool_command, openssl_command); ASSERT_EQ(tool_output_str, openssl_output_str); } @@ -321,7 +346,8 @@ TEST_F(X509ComparisonTest, X509ToolCompareCheckendOpenSSL) { std::string tool_command = std::string(tool_executable_path) + " x509 -in " + in_path + " -checkend 0 > " + out_path_tool; std::string openssl_command = std::string(openssl_executable_path) + " x509 -in " + in_path + " -checkend 0 > " + out_path_openssl; - RunCommandsAndCompareOutput(tool_command, openssl_command, out_path_tool, out_path_openssl, tool_output_str, openssl_output_str); + RunCommandsAndCompareOutput(tool_command, openssl_command); + ASSERT_EQ(tool_output_str, openssl_output_str); } @@ -330,7 +356,7 @@ TEST_F(X509ComparisonTest, X509ToolCompareReqSignkeyDaysOpenSSL) { std::string tool_command = std::string(tool_executable_path) + " x509 -req -in " + csr_path + " -signkey " + signkey_path + " -days 80 -out " + out_path_tool; std::string openssl_command = std::string(openssl_executable_path) + " x509 -req -in " + csr_path + " -signkey " + signkey_path + " -days 80 -out " + out_path_openssl; - RunCommandsAndCompareOutput(tool_command, openssl_command, out_path_tool, out_path_openssl, tool_output_str, openssl_output_str); + RunCommandsAndCompareOutput(tool_command, openssl_command); // Certificates will not be identical, therefore testing that cert header and footer are present trim(tool_output_str); @@ -347,7 +373,7 @@ TEST_F(X509ComparisonTest, X509ToolCompareDatesNooutOpenSSL) { std::string tool_command = std::string(tool_executable_path) + " x509 -in " + in_path + " -dates -noout > " + out_path_tool; std::string openssl_command = std::string(openssl_executable_path) + " x509 -in " + in_path + " -dates -noout > " + out_path_openssl; - RunCommandsAndCompareOutput(tool_command, openssl_command, out_path_tool, out_path_openssl, tool_output_str, openssl_output_str); + RunCommandsAndCompareOutput(tool_command, openssl_command); ASSERT_EQ(tool_output_str, openssl_output_str); }