From d830ff5aa48b2c39aac411162873239b235b0858 Mon Sep 17 00:00:00 2001 From: Adriana Kobylak Date: Wed, 27 Jan 2021 14:15:27 -0600 Subject: http: utility: Add base64encode Add the base64encode() function to be used to encode binary data to offload out of the BMC. Based on crow/utility.h, reworked for readability. Tested: Added unit test cases. Also verified data encoded with this function was the same as the original binary when using a decoder. Change-Id: I0a27ffb0090c4613e296af33d11e2e2657957167 Signed-off-by: Adriana Kobylak --- http/ut/utility_test.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'http/ut/utility_test.cpp') diff --git a/http/ut/utility_test.cpp b/http/ut/utility_test.cpp index a81adf5198..191a16af86 100644 --- a/http/ut/utility_test.cpp +++ b/http/ut/utility_test.cpp @@ -16,3 +16,43 @@ TEST(Utility, Base64DecodeNonAscii) std::string result; EXPECT_FALSE(crow::utility::base64Decode(junkString, result)); } + +TEST(Utility, Base64EncodeString) +{ + using namespace std::string_literals; + std::string encoded; + + encoded = crow::utility::base64encode(""); + EXPECT_EQ(encoded, ""); + + encoded = crow::utility::base64encode("f"); + EXPECT_EQ(encoded, "Zg=="); + + encoded = crow::utility::base64encode("f0"); + EXPECT_EQ(encoded, "ZjA="); + + encoded = crow::utility::base64encode("f0\0"s); + EXPECT_EQ(encoded, "ZjAA"); + + encoded = crow::utility::base64encode("f0\0 "s); + EXPECT_EQ(encoded, "ZjAAIA=="); + + encoded = crow::utility::base64encode("f0\0 B"s); + EXPECT_EQ(encoded, "ZjAAIEI="); + + encoded = crow::utility::base64encode("f0\0 Ba"s); + EXPECT_EQ(encoded, "ZjAAIEJh"); + + encoded = crow::utility::base64encode("f0\0 Bar"s); + EXPECT_EQ(encoded, "ZjAAIEJhcg=="); +} + +TEST(Utility, Base64EncodeDecodeString) +{ + using namespace std::string_literals; + std::string data("Data fr\0m 90 reading a \nFile"s); + std::string encoded = crow::utility::base64encode(data); + std::string decoded; + EXPECT_TRUE(crow::utility::base64Decode(encoded, decoded)); + EXPECT_EQ(data, decoded); +} -- cgit v1.2.3