From ca290d870f985e41122f5266acf32b144be62c51 Mon Sep 17 00:00:00 2001 From: Luis Diaz Mas Date: Sun, 26 Aug 2018 14:38:10 +0200 Subject: [PATCH] New tests for base64 encode & decode --- unitTests/test_futils.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/unitTests/test_futils.cpp b/unitTests/test_futils.cpp index 35747b0c..40e3e5a3 100644 --- a/unitTests/test_futils.cpp +++ b/unitTests/test_futils.cpp @@ -105,4 +105,33 @@ TEST(urldecode, decodesGivenUrlInPlace) std::string url ("http%3a%2f%2fwww.geekhideout.com%2furlcode.shtml"); urldecode(url); ASSERT_STREQ(expectedDecodedUrl.c_str(), url.c_str()); -} \ No newline at end of file +} + +TEST(base64encode, encodesValidString) +{ + const std::string original ("This is a unit test"); + const std::string expected ("VGhpcyBpcyBhIHVuaXQgdGVzdA=="); + size_t encodeLength = ((original.size() + 2) / 3) * 4 + 1; + char * result = new char [encodeLength]; + ASSERT_EQ(1, base64encode(original.c_str(), original.size(), result, encodeLength)); + ASSERT_STREQ(expected.c_str(), result); + delete [] result; +} + +TEST(base64encode, doesNotEncodeWithNotBigEnoughResultSize) +{ + const std::string original ("This is a unit test"); + size_t encodeLength = (original.size()); + char * result = new char [encodeLength]; + ASSERT_EQ(0, base64encode(original.c_str(), original.size(), result, encodeLength)); +} + +TEST(base64decode, decodesValidString) +{ + const std::string original ("VGhpcyBpcyBhIHVuaXQgdGVzdA=="); + const std::string expected ("This is a unit test"); + char * result = new char [original.size()]; + ASSERT_EQ(expected.size()+1, base64decode(original.c_str(), result, original.size())); + ASSERT_STREQ(expected.c_str(), result); + delete [] result; +}