Test decoding of data which has a length which can be cleanly decoded.
{ enum data = "QUJD"; assert(data.decodeBase64 == "ABC"); } { enum data = "QQ=="; assert(data.decodeBase64 == "A"); } { enum data = "YSBiIGMgZCBlIGYgZyBoIGkgaiBrIGwgbSBuIG8gcCBxIHIgcyB0IHUgdiB3IHggeSB6"; assert(data.decodeBase64 == "a b c d e f g h i j k l m n o p q r s t u v w x y z"); } { enum data = "LCAuIDsgLyBbICcgXSBcID0gLSAwIDkgOCA3IDYgNSA0IDMgMiAxIGAgfiAhIEAgIyAkICUgXiAmICogKCApIF8gKyB8IDogPCA+ID8="; assert(data.decodeBase64 == ", . ; / [ ' ] \\ = - 0 9 8 7 6 5 4 3 2 1 ` ~ ! @ # $ % ^ & * ( ) _ + | : < > ?"); } { enum data = "AAA="; assert(data.decodeBase64 == "\x00\x00"); } { enum data = "AAAABBCC"; assert(data.decodeBase64 == "\x00\x00\x00\x04\x10\x82"); } { enum data = "AA=="; assert(data.decodeBase64 == "\x00"); } { enum data = "AA/="; assert(data.decodeBase64 == "\x00\x0f"); }
Test decoding invalid data
void testFail(const(char)[] input) @safe pure { bool thrown = false; try { ubyte[] decoded = input.decodeBase64; } catch (Exception t) { thrown = true; } assert(thrown); } testFail("===A"); testFail("A="); testFail("AA="); testFail("A=AA"); testFail("AA=A"); testFail("AA=A===="); testFail("=AAA"); testFail("AAA=QUJD"); // This fails because we don't allow extra padding (than what is necessary) testFail("AA======"); // This fails because we don't allow padding before the end of the string (otherwise we'd have a side-channel) testFail("QU==QUJD"); testFail("QU======QUJD"); // Invalid data that's out of the alphabet testFail("!@##@@!@");
Decode a Base64 encoded value, placing the result onto an Appender.