|
| 1 | +#include "pch.h" |
| 2 | +#include <kf/Base64.h> |
| 3 | +#include <string_view> |
| 4 | + |
| 5 | +SCENARIO("Base64::decodeLen") |
| 6 | +{ |
| 7 | + WHEN("Encoded string is empty") |
| 8 | + { |
| 9 | + THEN("Decode length is 0") |
| 10 | + { |
| 11 | + REQUIRE(kf::Base64::decodeLen(L"") == 0); |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + WHEN("Encoded string is VA==") |
| 16 | + { |
| 17 | + THEN("Decode length is 1") |
| 18 | + { |
| 19 | + REQUIRE(kf::Base64::decodeLen(L"VA==") == 1); // "T" |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + WHEN("Encoded string is VGU=") |
| 24 | + { |
| 25 | + THEN("Decode length is 2") |
| 26 | + { |
| 27 | + REQUIRE(kf::Base64::decodeLen(L"VGU=") == 2); // "Te" |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + WHEN("Encoded string is VGVz") |
| 32 | + { |
| 33 | + THEN("Decode length is 3") |
| 34 | + { |
| 35 | + REQUIRE(kf::Base64::decodeLen(L"VGVz") == 3); // "Tes" |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + WHEN("Encoded string is VGVzdA==") |
| 40 | + { |
| 41 | + THEN("Decode length is 4") |
| 42 | + { |
| 43 | + REQUIRE(kf::Base64::decodeLen(L"VGVzdA==") == 4); // "Test" |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + WHEN("Encoded string is ==") |
| 48 | + { |
| 49 | + THEN("Decode length is -1") |
| 50 | + { |
| 51 | + REQUIRE(kf::Base64::decodeLen(L"==") == -1); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +SCENARIO("Base64::decode") |
| 58 | +{ |
| 59 | + GIVEN("Encoded string 'VA=='") |
| 60 | + { |
| 61 | + kf::USimpleString input(L"VA=="); // "T" |
| 62 | + constexpr std::wstring_view kExpectedOutput = L"T"; |
| 63 | + constexpr size_t kExpectedLength = kExpectedOutput.size(); |
| 64 | + std::array<wchar_t, kExpectedLength> output; |
| 65 | + |
| 66 | + THEN("Decoded symbol is 'T' and decoded length is 1") |
| 67 | + { |
| 68 | + int written = kf::Base64::decode(input, std::as_writable_bytes(std::span{ output })); |
| 69 | + REQUIRE(written == kExpectedLength); |
| 70 | + |
| 71 | + REQUIRE(std::wstring_view(output.data(), written) == kExpectedOutput); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + GIVEN("Encoded string 'VGU='") |
| 76 | + { |
| 77 | + kf::USimpleString input(L"VGU="); // "Te" |
| 78 | + constexpr std::string_view kExpectedOutput = "Te"; |
| 79 | + constexpr size_t kExpectedLength = kExpectedOutput.size(); |
| 80 | + std::array<char, kExpectedLength> output; |
| 81 | + |
| 82 | + THEN("Decoded symbols are 'Te' and decoded length is 2") |
| 83 | + { |
| 84 | + int written = kf::Base64::decode(input, std::as_writable_bytes(std::span{ output })); |
| 85 | + REQUIRE(written == kExpectedLength); |
| 86 | + REQUIRE(std::string_view(output.data(), written) == kExpectedOutput); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + GIVEN("Encoded string 'VGhpcyBpcyBhIGxvbmcgdGVzdCBzdHJpbmcgZm9yIHVuaXQgdGVzdGluZw=='") |
| 91 | + { |
| 92 | + kf::USimpleString input(L"VGhpcyBpcyBhIGxvbmcgdGVzdCBzdHJpbmcgZm9yIHVuaXQgdGVzdGluZw=="); |
| 93 | + constexpr std::string_view kExpectedOutput = "This is a long test string for unit testing"; |
| 94 | + constexpr size_t kExpectedLength = kExpectedOutput.size(); |
| 95 | + std::array<char, kExpectedLength> output; |
| 96 | + |
| 97 | + THEN("Decoded symbols are 'This is a long test string for unit testing' and decoded length is correct") |
| 98 | + { |
| 99 | + int written = kf::Base64::decode(input, std::as_writable_bytes(std::span{ output })); |
| 100 | + REQUIRE(written == kExpectedLength); |
| 101 | + REQUIRE(std::string_view(output.data(), written) == kExpectedOutput); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + GIVEN("Empty string") |
| 106 | + { |
| 107 | + kf::USimpleString input; |
| 108 | + std::array<char, 2> output; |
| 109 | + |
| 110 | + THEN("Decoded length is 0") |
| 111 | + { |
| 112 | + int written = kf::Base64::decode(input, std::as_writable_bytes(std::span{ output })); |
| 113 | + REQUIRE(written == 0); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + GIVEN("Output buffer isn't big enough") |
| 118 | + { |
| 119 | + kf::USimpleString input(L"VGVzdA=="); |
| 120 | + std::array<char, 2> output; |
| 121 | + |
| 122 | + THEN("Decode returns -1") |
| 123 | + { |
| 124 | + int written = kf::Base64::decode(input, std::as_writable_bytes(std::span{ output })); |
| 125 | + REQUIRE(written == -1); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + GIVEN("Encoded string with negative decode length") |
| 130 | + { |
| 131 | + kf::USimpleString input(L"=="); |
| 132 | + std::array<char, 2> output; |
| 133 | + |
| 134 | + THEN("Decode returns -1") |
| 135 | + { |
| 136 | + int written = kf::Base64::decode(input, std::as_writable_bytes(std::span{ output })); |
| 137 | + REQUIRE(written == -1); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments