changeset 483:3f29e6c05983

Base64: add new test because AB=Y is not allowed
author David Demelier <markand@malikania.fr>
date Thu, 12 Nov 2015 08:57:21 +0100
parents eb6163fb1ee6
children 3ee3f3e53ee3
files C++/modules/Base64/Base64.h C++/tests/Base64/main.cpp
diffstat 2 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/C++/modules/Base64/Base64.h	Wed Nov 11 22:22:40 2015 +0100
+++ b/C++/modules/Base64/Base64.h	Thu Nov 12 08:57:21 2015 +0100
@@ -129,6 +129,11 @@
 			*output++ = static_cast<unsigned char>(((inputbuf[1] << 4) & 0xf0) | ((inputbuf[2] >> 2) & 0x0f));
 		}
 		if (inputbuf[3] != -1) {
+			/* "XY=Z" is not allowed */
+			if (inputbuf[2] == -1) {
+				throw std::invalid_argument{"invalid base64 string"};
+			}
+
 			*output++ = static_cast<unsigned char>(((inputbuf[2] << 6) & 0xc0) | (inputbuf[3] & 0x3f));
 		}
 	}
--- a/C++/tests/Base64/main.cpp	Wed Nov 11 22:22:40 2015 +0100
+++ b/C++/tests/Base64/main.cpp	Thu Nov 12 08:57:21 2015 +0100
@@ -222,6 +222,15 @@
 	} catch (...) {}
 }
 
+TEST(Failure, wrong4)
+{
+	try {
+		base64::decode("AB=C");
+
+		FAIL() << "exception expected";
+	} catch (...) {}
+}
+
 int main(int argc, char **argv)
 {
 	testing::InitGoogleTest(&argc, argv);