base64 encoder/decoder: Add parameter to control linefeed handling
[libi2ncommon] / test / stringfunc.cpp
index 604f446..38c8e71 100644 (file)
@@ -882,6 +882,21 @@ BOOST_AUTO_TEST_CASE(base64_large_string_with_zero)
     BOOST_CHECK_EQUAL(large_binary_data, decoded);
 }
 
+BOOST_AUTO_TEST_CASE(base64_large_string_with_zero_encode_linefeeds)
+{
+    // 10 MB data
+    int data_size = 1024 * 1024 * 10;
+
+    string large_binary_data(data_size, 0);
+    BOOST_CHECK_EQUAL(data_size, large_binary_data.size());
+
+    const bool one_line_mode = false;
+    string encoded = base64_encode(large_binary_data, one_line_mode);
+
+    string decoded = base64_decode(encoded, one_line_mode);
+    BOOST_CHECK_EQUAL(large_binary_data, decoded);
+}
+
 BOOST_AUTO_TEST_CASE(base64_decode_garbage)
 {
     std::string data = "Hello World, this is unencoded data";
@@ -891,4 +906,28 @@ BOOST_AUTO_TEST_CASE(base64_decode_garbage)
     BOOST_CHECK_EQUAL(0, decoded.size());
 }
 
+BOOST_AUTO_TEST_CASE(base64_encode_with_linefeeds)
+{
+    const string data = string("Hello World\n")
+                       + "Hello World\n"
+                       + "Hello World\n"
+                       + "Hello World\n"
+                       + "Hello World\n"
+                       + "Hello World\n"
+                       + "Hello World\n";
+
+    const string encoded = base64_encode(data, false);
+
+    const std::string expected = string("SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQK\n")
+                                 + "SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQK\n";
+    BOOST_CHECK_EQUAL(expected, encoded);
+
+    // decode and compare
+    BOOST_CHECK_EQUAL(data, base64_decode(encoded, false));
+
+    // expected empty string when switching on single line base64 mode
+    // (openssl is very strict about this)
+    BOOST_CHECK_EQUAL("", base64_decode(encoded, true));
+}
+
 BOOST_AUTO_TEST_SUITE_END()