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 <ed@tanous.net>
diff --git a/test/http/utility_test.cpp b/test/http/utility_test.cpp
index 5f62d3f..c0b6412 100644
--- a/test/http/utility_test.cpp
+++ b/test/http/utility_test.cpp
@@ -71,6 +71,27 @@
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;