Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
Signed-off-by: AssemblyJohn <[email protected]>
  • Loading branch information
AssemblyJohn committed Apr 16, 2024
1 parent da844ca commit 3291f14
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/evse_security/crypto/openssl/openssl_supplier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,26 +896,32 @@ static bool base64_encode(const unsigned char* bytes_str, int bytes_size, std::s
}

EVP_EncodeInit(base64_encode_context_ptr.get());
// evp_encode_ctx_set_flags(base64_encode_context_ptr.get(), EVP_ENCODE_CTX_NO_NEWLINES); // Of course it's not
// public

if (!base64_encode_context_ptr.get()) {
EVLOG_error << "Error during EVP_EncodeInit";
return false;
}

int base64_length = ((bytes_size * 4) / 3) + 1;
char base64_out[base64_length]; // If it causes issues, replace with 'alloca' on different platform
int base64_length = ((bytes_size / 3) * 4) + 2;
// If it causes issues, replace with 'alloca' on different platform
char base64_out[base64_length + 66]; // + 66 bytes for final block
int full_len = 0;

int base64_out_length;
if (EVP_EncodeUpdate(base64_encode_context_ptr.get(), reinterpret_cast<unsigned char*>(base64_out),
&base64_out_length, bytes_str, base64_length) < 0) {
&base64_out_length, bytes_str, bytes_size) < 0) {
EVLOG_error << "Error during EVP_EncodeUpdate";
return false;
}
full_len += base64_out_length;

int encode_final_out;
EVP_EncodeFinal(base64_encode_context_ptr.get(), reinterpret_cast<unsigned char*>(base64_out), &encode_final_out);
EVP_EncodeFinal(base64_encode_context_ptr.get(), reinterpret_cast<unsigned char*>(base64_out) + base64_out_length,
&base64_out_length);
full_len += base64_out_length;

out_encoded.clear();
out_encoded.insert(std::end(out_encoded), base64_out, base64_out + base64_out_length);
out_encoded.assign(base64_out, full_len);

return true;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,22 @@ TEST_F(EvseSecurityTests, verify_expired_csr_deletion) {
ASSERT_FALSE(fs::exists(csr_key_path));
}

TEST_F(EvseSecurityTests, verify_base64) {
std::string test_string1 = "U29tZSBkYXRhIGZvciB0ZXN0IGNhc2VzLiBTb21lIGRhdGEgZm9yIHRlc3QgY2FzZXMuIFNvbWUgZGF0YSBmb3I"
"gdGVzdCBjYXNlcy4gU29tZSBkYXRhIGZvciB0ZXN0IGNhc2VzLg==";

std::string decoded = this->evse_security->base64_decode_to_string(test_string1);
ASSERT_EQ(
decoded,
std::string(
"Some data for test cases. Some data for test cases. Some data for test cases. Some data for test cases."));

std::string out_encoded = this->evse_security->base64_encode_from_string(decoded);
out_encoded.erase(std::remove(out_encoded.begin(), out_encoded.end(), '\n'), out_encoded.cend());

ASSERT_EQ(test_string1, out_encoded);
}

} // namespace evse_security

// FIXME(piet): Add more tests for getRootCertificateHashData (incl. V2GCertificateChain etc.)

0 comments on commit 3291f14

Please sign in to comment.