| 1 |
pure @safe unittest |
| 2 |
{ |
| 3 |
import std.base64; |
| 4 |
|
| 5 |
ubyte[] data = [0x83, 0xd7, 0x30, 0x7a, 0x01, 0x3f]; |
| 6 |
assert(Base64.encode(data) == "g9cwegE/"); |
| 7 |
assert(Base64.decode("g9cwegE/") == data); |
| 8 |
} |
| 9 |
|
| 10 |
pure @safe unittest |
| 11 |
{ |
| 12 |
import std.base64; |
| 13 |
|
| 14 |
ubyte[] data = [0x83, 0xd7, 0x30, 0x7a, 0x01, 0x3f]; |
| 15 |
assert(Base64URL.encode(data) == "g9cwegE_"); |
| 16 |
assert(Base64URL.decode("g9cwegE_") == data); |
| 17 |
} |
| 18 |
|
| 19 |
pure @safe unittest |
| 20 |
{ |
| 21 |
import std.base64; |
| 22 |
|
| 23 |
ubyte[] data = [0x83, 0xd7, 0x30, 0x7b, 0xef]; |
| 24 |
assert(Base64URLNoPadding.encode(data) == "g9cwe-8"); |
| 25 |
assert(Base64URLNoPadding.decode("g9cwe-8") == data); |
| 26 |
} |
| 27 |
|
| 28 |
@safe unittest |
| 29 |
{ |
| 30 |
import std.base64; |
| 31 |
|
| 32 |
ubyte[] data = [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]; |
| 33 |
|
| 34 |
// Allocate a buffer large enough to hold the encoded string. |
| 35 |
auto buf = new char[Base64.encodeLength(data.length)]; |
| 36 |
|
| 37 |
Base64.encode(data, buf); |
| 38 |
assert(buf == "Gis8TV1u"); |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
@nogc nothrow @safe unittest |
| 43 |
{ |
| 44 |
import std.base64; |
| 45 |
|
| 46 |
ubyte[6] data = [0x83, 0xd7, 0x30, 0x7a, 0x01, 0x3f]; |
| 47 |
char[32] buffer; // much bigger than necessary |
| 48 |
|
| 49 |
// Just to be sure... |
| 50 |
auto encodedLength = Base64.encodeLength(data.length); |
| 51 |
assert(buffer.length >= encodedLength); |
| 52 |
|
| 53 |
// encode() returns a slice to the provided buffer. |
| 54 |
auto encoded = Base64.encode(data[], buffer[]); |
| 55 |
assert(encoded is buffer[0 .. encodedLength]); |
| 56 |
assert(encoded == "g9cwegE/"); |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
@safe pure nothrow unittest |
| 61 |
{ |
| 62 |
import std.base64; |
| 63 |
|
| 64 |
import std.array : appender; |
| 65 |
|
| 66 |
auto output = appender!string(); |
| 67 |
ubyte[] data = [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]; |
| 68 |
|
| 69 |
// This overload of encode() returns the number of calls to the output |
| 70 |
// range's put method. |
| 71 |
assert(Base64.encode(data, output) == 8); |
| 72 |
assert(output.data == "Gis8TV1u"); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
@safe unittest |
| 77 |
{ |
| 78 |
import std.base64; |
| 79 |
|
| 80 |
ubyte[] data = [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]; |
| 81 |
assert(Base64.encode(data) == "Gis8TV1u"); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
@safe unittest |
| 86 |
{ |
| 87 |
import std.base64; |
| 88 |
|
| 89 |
auto encoded = "Gis8TV1u"; |
| 90 |
|
| 91 |
// Allocate a sufficiently large buffer to hold to decoded result. |
| 92 |
auto buffer = new ubyte[Base64.decodeLength(encoded.length)]; |
| 93 |
|
| 94 |
Base64.decode(encoded, buffer); |
| 95 |
assert(buffer == [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
@safe unittest |
| 100 |
{ |
| 101 |
import std.base64; |
| 102 |
|
| 103 |
auto encoded = "Gis8TV1u"; |
| 104 |
ubyte[32] buffer; // much bigger than necessary |
| 105 |
|
| 106 |
// Just to be sure... |
| 107 |
auto decodedLength = Base64.decodeLength(encoded.length); |
| 108 |
assert(buffer.length >= decodedLength); |
| 109 |
|
| 110 |
// decode() returns a slice of the given buffer. |
| 111 |
auto decoded = Base64.decode(encoded, buffer[]); |
| 112 |
assert(decoded is buffer[0 .. decodedLength]); |
| 113 |
assert(decoded == [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]); |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
@system unittest |
| 118 |
{ |
| 119 |
import std.base64; |
| 120 |
|
| 121 |
struct OutputRange |
| 122 |
{ |
| 123 |
ubyte[] result; |
| 124 |
void put(ubyte b) { result ~= b; } |
| 125 |
} |
| 126 |
OutputRange output; |
| 127 |
|
| 128 |
// This overload of decode() returns the number of calls to put(). |
| 129 |
assert(Base64.decode("Gis8TV1u", output) == 6); |
| 130 |
assert(output.result == [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]); |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
@safe unittest |
| 135 |
{ |
| 136 |
import std.base64; |
| 137 |
|
| 138 |
auto data = "Gis8TV1u"; |
| 139 |
assert(Base64.decode(data) == [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
@safe pure unittest |
| 144 |
{ |
| 145 |
import std.base64; |
| 146 |
|
| 147 |
import std.algorithm.comparison : equal; |
| 148 |
string encoded = |
| 149 |
"VGhvdSBzaGFsdCBuZXZlciBjb250aW51ZSBhZnRlciBhc3NlcnRpbmcgbnVsbA=="; |
| 150 |
|
| 151 |
assert(Base64.decoder(encoded) |
| 152 |
.equal("Thou shalt never continue after asserting null")); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
@safe unittest |
| 157 |
{ |
| 158 |
import std.base64; |
| 159 |
|
| 160 |
import std.string : representation; |
| 161 |
|
| 162 |
// pre-defined: alias Base64 = Base64Impl!('+', '/'); |
| 163 |
ubyte[] emptyArr; |
| 164 |
assert(Base64.encode(emptyArr) == ""); |
| 165 |
assert(Base64.encode("f".representation) == "Zg=="); |
| 166 |
assert(Base64.encode("foo".representation) == "Zm9v"); |
| 167 |
|
| 168 |
alias Base64Re = Base64Impl!('!', '=', Base64.NoPadding); |
| 169 |
assert(Base64Re.encode("f".representation) == "Zg"); |
| 170 |
assert(Base64Re.encode("foo".representation) == "Zm9v"); |
| 171 |
} |
| 172 |
|
| 173 |
@safe unittest |
| 174 |
{ |
| 175 |
import std.base64; |
| 176 |
|
| 177 |
import std.exception : assertThrown; |
| 178 |
assertThrown!Base64Exception(Base64.decode("ab|c")); |
| 179 |
} |
| 180 |
|