Skip to content

Commit

Permalink
test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ecdeye committed Jul 26, 2024
1 parent 6e22c40 commit 5520aec
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 51 deletions.
41 changes: 0 additions & 41 deletions tool-openssl/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#define INTERNAL_H

#include "../tool/internal.h"
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include <fstream>
Expand All @@ -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<unsigned char>(ch));
}));
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(static_cast<unsigned char>(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<char>(tool_output)), std::istreambuf_iterator<char>());
std::ifstream openssl_output(out_path_openssl);
openssl_output_str = std::string((std::istreambuf_iterator<char>(openssl_output)), std::istreambuf_iterator<char>());

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
19 changes: 18 additions & 1 deletion tool-openssl/md5_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(tool_output)), std::istreambuf_iterator<char>());
std::ifstream openssl_output(out_path_openssl);
this->openssl_output_str = std::string((std::istreambuf_iterator<char>(openssl_output)), std::istreambuf_iterator<char>());

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);
Expand Down Expand Up @@ -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);

Expand Down
48 changes: 44 additions & 4 deletions tool-openssl/rsa_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(tool_output)), std::istreambuf_iterator<char>());
std::ifstream openssl_output(out_path_openssl);
this->openssl_output_str = std::string((std::istreambuf_iterator<char>(openssl_output)), std::istreambuf_iterator<char>());

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);
Expand All @@ -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<unsigned char>(ch));
}));
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(static_cast<unsigned char>(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-----";
Expand All @@ -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));
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down
36 changes: 31 additions & 5 deletions tool-openssl/x509_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(tool_output)), std::istreambuf_iterator<char>());
std::ifstream openssl_output(out_path_openssl);
this->openssl_output_str = std::string((std::istreambuf_iterator<char>(openssl_output)), std::istreambuf_iterator<char>());

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);
Expand All @@ -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<unsigned char>(ch));
}));
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(static_cast<unsigned char>(ch));
}).base(), s.end());
return s;
}
// Certificate boundaries
const std::string CERT_BEGIN = "-----BEGIN CERTIFICATE-----";
const std::string CERT_END = "-----END CERTIFICATE-----";
Expand All @@ -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);
}
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
}

0 comments on commit 5520aec

Please sign in to comment.