1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <cxxtest/TestSuite.h>
#include "common/base64.h"
#include "common/memstream.h"
static const char *base64_test_string[] = {
"",
"a",
"abc",
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
("12345678901234567890123456789012345678901234567890123456789012"
"345678901234567890")
};
static const char *base64_test_encoded[] = {
"",
"YQ==",
"YWJj",
"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=",
("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdH"
"V2d3h5ejAxMjM0NTY3ODk="),
("MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Nj"
"c4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTA=")
};
static const char *base64_validate_tests[] = {
"YQ", // missing padding
"!@#$", // characters not in encoding table
"YQ==YWJj", // data after padding
};
#include <common/pack-start.h> // START STRUCT PACKING
struct Base64TestStruct {
uint32 x;
byte y;
uint16 z;
uint32 a;
byte b;
} PACKED_STRUCT;
#include <common/pack-end.h> // END STRUCT PACKING
class Base64TestSuite : public CxxTest::TestSuite {
public:
void test_b64Validate() {
for (int i = 0; i < 6; i++) {
Common::String encoded = base64_test_encoded[i];
// All of these should return true.
TS_ASSERT_EQUALS(Common::b64Validate(encoded), true);
}
for (int i = 0; i < 3; i++) {
Common::String encoded = base64_validate_tests[i];
// All of these should return false.
TS_ASSERT_EQUALS(Common::b64Validate(encoded), false);
}
}
void test_b64EncodeString() {
for (int i = 0; i < 6; i++) {
Common::String string = base64_test_string[i];
Common::String encoded = Common::b64EncodeString(string);
TS_ASSERT_EQUALS(encoded, base64_test_encoded[i]);
}
}
void test_b64EncodeStream() {
for (int i = 0; i < 6; i++) {
Common::MemoryReadStream stream((const byte *)base64_test_string[i], strlen(base64_test_string[i]));
Common::String encoded = Common::b64EncodeStream(stream);
TS_ASSERT_EQUALS(encoded, base64_test_encoded[i]);
}
}
void test_b64EncodeData() {
Base64TestStruct *test = new Base64TestStruct();
test->x = 1;
test->y = 2;
test->z = 3;
test->a = 4;
test->b = 5;
Common::String encoded = Common::b64EncodeData(test, sizeof(Base64TestStruct));
TS_ASSERT_EQUALS(encoded, "AQAAAAIDAAQAAAAF");
delete test;
}
void test_b64DecodeString() {
for (int i = 0; i < 6; i++) {
Common::String encoded = base64_test_encoded[i];
Common::String string = Common::b64DecodeString(encoded);
TS_ASSERT_EQUALS(string, base64_test_string[i]);
}
}
void test_b64DecodeStream() {
for (int i = 0; i < 6; i++) {
Common::String encoded = base64_test_encoded[i];
Common::MemoryReadStream *stream = Common::b64DecodeStream(encoded, strlen(base64_test_string[i]));
TS_ASSERT_EQUALS(stream->size(), strlen(base64_test_string[i]));
char *data = (char *)malloc(stream->size());
stream->read(data, stream->size());
delete stream;
Common::String string(data, strlen(base64_test_string[i]));
TS_ASSERT_EQUALS(string, base64_test_string[i]);
free(data);
}
}
void test_b64DecodeData() {
Base64TestStruct *test = new Base64TestStruct();
Common::String encoded = "AQAAAAIDAAQAAAAF";
bool success = Common::b64DecodeData(encoded, test);
TS_ASSERT_EQUALS(success, true);
TS_ASSERT_EQUALS(test->x, 1);
TS_ASSERT_EQUALS(test->y, 2);
TS_ASSERT_EQUALS(test->z, 3);
TS_ASSERT_EQUALS(test->a, 4);
TS_ASSERT_EQUALS(test->b, 5);
delete test;
}
};
|