From ee192c065cc658b2489d04c791e2fb956a331699 Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Wed, 13 Dec 2023 10:49:58 -0800 Subject: Make base64 encoder incremental As part of https://gerrit.openbmc.org/c/openbmc/bmcweb/+/67667, it would be desirable if we could incrementally encode base64 in chunks. Given that base64 encoding requires encoding 3 characters to 4, there's a possibility that a chunk might not be mod 3 length. This commit moves the base64 encoder into a class that can run incrementally. Tested: Unit tests pass. More tests in next commit. Change-Id: Ic7da3fd4db865c99fcbd96ae06fdecb87628f94c Signed-off-by: Ed Tanous --- test/http/utility_test.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test') diff --git a/test/http/utility_test.cpp b/test/http/utility_test.cpp index 5f62d3f3d1..c0b6412af2 100644 --- a/test/http/utility_test.cpp +++ b/test/http/utility_test.cpp @@ -71,6 +71,27 @@ TEST(Utility, Base64EncodeString) EXPECT_EQ(encoded, "ZjAAIEJhcg=="); } +TEST(Utility, Base64Encoder) +{ + using namespace std::string_literals; + std::string data = "f0\0 Bar"s; + for (size_t chunkSize = 1; chunkSize < 6; chunkSize++) + { + std::string_view testString(data); + std::string out; + Base64Encoder encoder; + while (!testString.empty()) + { + size_t thisChunk = std::min(testString.size(), chunkSize); + encoder.encode(testString.substr(0, thisChunk), out); + testString.remove_prefix(thisChunk); + } + + encoder.finalize(out); + EXPECT_EQ(out, "ZjAAIEJhcg=="); + } +} + TEST(Utility, Base64EncodeDecodeString) { using namespace std::string_literals; -- cgit v1.2.3