diff options
Diffstat (limited to 'Swiften/StringCodecs')
-rw-r--r-- | Swiften/StringCodecs/Base64.cpp | 150 | ||||
-rw-r--r-- | Swiften/StringCodecs/Base64.h | 12 | ||||
-rw-r--r-- | Swiften/StringCodecs/Hexify.cpp | 76 | ||||
-rw-r--r-- | Swiften/StringCodecs/Hexify.h | 12 | ||||
-rw-r--r-- | Swiften/StringCodecs/PBKDF2.h | 32 | ||||
-rw-r--r-- | Swiften/StringCodecs/SHA1_Windows.cpp | 128 | ||||
-rw-r--r-- | Swiften/StringCodecs/SHA1_Windows.h | 38 | ||||
-rw-r--r-- | Swiften/StringCodecs/UnitTest/Base64Test.cpp | 94 | ||||
-rw-r--r-- | Swiften/StringCodecs/UnitTest/HexifyTest.cpp | 38 | ||||
-rw-r--r-- | Swiften/StringCodecs/UnitTest/PBKDF2Test.cpp | 48 |
10 files changed, 314 insertions, 314 deletions
diff --git a/Swiften/StringCodecs/Base64.cpp b/Swiften/StringCodecs/Base64.cpp index 4e6ac8c..6eeaca9 100644 --- a/Swiften/StringCodecs/Base64.cpp +++ b/Swiften/StringCodecs/Base64.cpp @@ -11,91 +11,91 @@ using namespace Swift; namespace { - const char* encodeMap = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - const unsigned char decodeMap[255] = { - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 62, 255, 255, 255, 63, - 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 255, 255, 255, 255, 255, 255, - 255, 0, 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, 255, 255, 255, 255, 255, - 255, 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, 255, 255, 255, 255, 255 - }; + const char* encodeMap = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + const unsigned char decodeMap[255] = { + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 62, 255, 255, 255, 63, + 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 255, 255, 255, 255, 255, 255, + 255, 0, 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, 255, 255, 255, 255, 255, + 255, 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, 255, 255, 255, 255, 255 + }; - template<typename ResultType, typename InputType> - ResultType encodeDetail(const InputType& input) { - ResultType result; - size_t i = 0; - for (; i < (input.size()/3)*3; i += 3) { - unsigned int c = input[i+2] | (input[i+1]<<8) | (input[i]<<16); - result.push_back(encodeMap[(c&0xFC0000)>>18]); - result.push_back(encodeMap[(c&0x3F000)>>12]); - result.push_back(encodeMap[(c&0xFC0)>>6]); - result.push_back(encodeMap[c&0x3F]); - } - if (input.size() % 3 == 2) { - unsigned int c = (input[i+1]<<8) | (input[i]<<16); - result.push_back(encodeMap[(c&0xFC0000)>>18]); - result.push_back(encodeMap[(c&0x3F000)>>12]); - result.push_back(encodeMap[(c&0xFC0)>>6]); - result.push_back('='); - } - else if (input.size() % 3 == 1) { - unsigned int c = input[i]<<16; - result.push_back(encodeMap[(c&0xFC0000)>>18]); - result.push_back(encodeMap[(c&0x3F000)>>12]); - result.push_back('='); - result.push_back('='); - } - return result; - } + template<typename ResultType, typename InputType> + ResultType encodeDetail(const InputType& input) { + ResultType result; + size_t i = 0; + for (; i < (input.size()/3)*3; i += 3) { + unsigned int c = input[i+2] | (input[i+1]<<8) | (input[i]<<16); + result.push_back(encodeMap[(c&0xFC0000)>>18]); + result.push_back(encodeMap[(c&0x3F000)>>12]); + result.push_back(encodeMap[(c&0xFC0)>>6]); + result.push_back(encodeMap[c&0x3F]); + } + if (input.size() % 3 == 2) { + unsigned int c = (input[i+1]<<8) | (input[i]<<16); + result.push_back(encodeMap[(c&0xFC0000)>>18]); + result.push_back(encodeMap[(c&0x3F000)>>12]); + result.push_back(encodeMap[(c&0xFC0)>>6]); + result.push_back('='); + } + else if (input.size() % 3 == 1) { + unsigned int c = input[i]<<16; + result.push_back(encodeMap[(c&0xFC0000)>>18]); + result.push_back(encodeMap[(c&0x3F000)>>12]); + result.push_back('='); + result.push_back('='); + } + return result; + } } std::string Base64::encode(const ByteArray& s) { - return encodeDetail<std::string>(s); + return encodeDetail<std::string>(s); } SafeByteArray Base64::encode(const SafeByteArray& s) { - return encodeDetail<SafeByteArray>(s); + return encodeDetail<SafeByteArray>(s); } ByteArray Base64::decode(const std::string& input) { - ByteArray result; + ByteArray result; - if (input.size() % 4) { - return ByteArray(); - } - for (size_t i = 0; i < input.size(); i += 4) { - unsigned char c1 = input[i+0]; - unsigned char c2 = input[i+1]; - unsigned char c3 = input[i+2]; - unsigned char c4 = input[i+3]; - if (c3 == '=') { - unsigned int c = (((decodeMap[c1]<<6)|decodeMap[c2])&0xFF0)>>4; - result.push_back(c); - } - else if (c4 == '=') { - unsigned int c = (((decodeMap[c1]<<12)|(decodeMap[c2]<<6)|decodeMap[c3])&0x3FFFC)>>2; - result.push_back((c&0xFF00) >> 8); - result.push_back(c&0xFF); - } - else { - unsigned int c = (decodeMap[c1]<<18) | (decodeMap[c2]<<12) | (decodeMap[c3]<<6) | decodeMap[c4]; - result.push_back((c&0xFF0000) >> 16); - result.push_back((c&0xFF00) >> 8); - result.push_back(c&0xFF); - } - } - return result; + if (input.size() % 4) { + return ByteArray(); + } + for (size_t i = 0; i < input.size(); i += 4) { + unsigned char c1 = input[i+0]; + unsigned char c2 = input[i+1]; + unsigned char c3 = input[i+2]; + unsigned char c4 = input[i+3]; + if (c3 == '=') { + unsigned int c = (((decodeMap[c1]<<6)|decodeMap[c2])&0xFF0)>>4; + result.push_back(c); + } + else if (c4 == '=') { + unsigned int c = (((decodeMap[c1]<<12)|(decodeMap[c2]<<6)|decodeMap[c3])&0x3FFFC)>>2; + result.push_back((c&0xFF00) >> 8); + result.push_back(c&0xFF); + } + else { + unsigned int c = (decodeMap[c1]<<18) | (decodeMap[c2]<<12) | (decodeMap[c3]<<6) | decodeMap[c4]; + result.push_back((c&0xFF0000) >> 16); + result.push_back((c&0xFF00) >> 8); + result.push_back(c&0xFF); + } + } + return result; } diff --git a/Swiften/StringCodecs/Base64.h b/Swiften/StringCodecs/Base64.h index 416f48c..e1be4bc 100644 --- a/Swiften/StringCodecs/Base64.h +++ b/Swiften/StringCodecs/Base64.h @@ -14,11 +14,11 @@ #include <Swiften/Base/SafeByteArray.h> namespace Swift { - class SWIFTEN_API Base64 { - public: - static std::string encode(const ByteArray& s); - static SafeByteArray encode(const SafeByteArray& s); + class SWIFTEN_API Base64 { + public: + static std::string encode(const ByteArray& s); + static SafeByteArray encode(const SafeByteArray& s); - static ByteArray decode(const std::string &s); - }; + static ByteArray decode(const std::string &s); + }; } diff --git a/Swiften/StringCodecs/Hexify.cpp b/Swiften/StringCodecs/Hexify.cpp index 832d5b7..f4e4ecc 100644 --- a/Swiften/StringCodecs/Hexify.cpp +++ b/Swiften/StringCodecs/Hexify.cpp @@ -18,56 +18,56 @@ namespace Swift { std::string Hexify::hexify(unsigned char byte) { - std::ostringstream result; - result << std::hex << std::setw(2) << std::setfill('0') << boost::numeric_cast<unsigned int>(byte); - return std::string(result.str()); + std::ostringstream result; + result << std::hex << std::setw(2) << std::setfill('0') << boost::numeric_cast<unsigned int>(byte); + return std::string(result.str()); } std::string Hexify::hexify(const ByteArray& data) { - std::ostringstream result; - result << std::hex; + std::ostringstream result; + result << std::hex; - for (unsigned int i = 0; i < data.size(); ++i) { - result << std::setw(2) << std::setfill('0') << boost::numeric_cast<unsigned int>(static_cast<unsigned char>(data[i])); - } - return std::string(result.str()); + for (unsigned int i = 0; i < data.size(); ++i) { + result << std::setw(2) << std::setfill('0') << boost::numeric_cast<unsigned int>(static_cast<unsigned char>(data[i])); + } + return std::string(result.str()); } static const unsigned char map[256] = { - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 0, 1, 2, 3, 4, 5, 6, 7, - 8, 9, 255, 255, 255, 255, 255, 255, - 255, 10, 11, 12, 13, 14, 15, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 10, 11, 12, 13, 14, 15, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255 + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 255, 255, 255, 255, 255, 255, + 255, 10, 11, 12, 13, 14, 15, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 10, 11, 12, 13, 14, 15, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255 }; ByteArray Hexify::unhexify(const std::string& in) { - if (in.size() % 2) { - return ByteArray(); - } + if (in.size() % 2) { + return ByteArray(); + } - ByteArray result(in.size() / 2); - for (size_t pos = 0; pos < in.size() - 1; pos += 2) { - unsigned char a = map[static_cast<size_t>(in[pos])]; - unsigned char b = map[static_cast<size_t>(in[pos+1])]; - if (a == 255 || b == 255) { - return ByteArray(); - } - result[pos/2] = (a<<4) | b; - } - return result; + ByteArray result(in.size() / 2); + for (size_t pos = 0; pos < in.size() - 1; pos += 2) { + unsigned char a = map[static_cast<size_t>(in[pos])]; + unsigned char b = map[static_cast<size_t>(in[pos+1])]; + if (a == 255 || b == 255) { + return ByteArray(); + } + result[pos/2] = (a<<4) | b; + } + return result; } } diff --git a/Swiften/StringCodecs/Hexify.h b/Swiften/StringCodecs/Hexify.h index b29864c..20027d3 100644 --- a/Swiften/StringCodecs/Hexify.h +++ b/Swiften/StringCodecs/Hexify.h @@ -10,10 +10,10 @@ #include <Swiften/Base/ByteArray.h> namespace Swift { - class SWIFTEN_API Hexify { - public: - static std::string hexify(unsigned char byte); - static std::string hexify(const ByteArray& data); - static ByteArray unhexify(const std::string& hexstring); - }; + class SWIFTEN_API Hexify { + public: + static std::string hexify(unsigned char byte); + static std::string hexify(const ByteArray& data); + static ByteArray unhexify(const std::string& hexstring); + }; } diff --git a/Swiften/StringCodecs/PBKDF2.h b/Swiften/StringCodecs/PBKDF2.h index 76a9656..2177b5f 100644 --- a/Swiften/StringCodecs/PBKDF2.h +++ b/Swiften/StringCodecs/PBKDF2.h @@ -12,20 +12,20 @@ #include <Swiften/Crypto/CryptoProvider.h> namespace Swift { - class SWIFTEN_API PBKDF2 { - public: - static ByteArray encode(const SafeByteArray& password, const ByteArray& salt, int iterations, CryptoProvider* crypto) { - ByteArray u = crypto->getHMACSHA1(password, concat(salt, createByteArray("\0\0\0\1", 4))); - ByteArray result(u); - int i = 1; - while (i < iterations) { - u = crypto->getHMACSHA1(password, u); - for (unsigned int j = 0; j < u.size(); ++j) { - result[j] ^= u[j]; - } - ++i; - } - return result; - } - }; + class SWIFTEN_API PBKDF2 { + public: + static ByteArray encode(const SafeByteArray& password, const ByteArray& salt, int iterations, CryptoProvider* crypto) { + ByteArray u = crypto->getHMACSHA1(password, concat(salt, createByteArray("\0\0\0\1", 4))); + ByteArray result(u); + int i = 1; + while (i < iterations) { + u = crypto->getHMACSHA1(password, u); + for (unsigned int j = 0; j < u.size(); ++j) { + result[j] ^= u[j]; + } + ++i; + } + return result; + } + }; } diff --git a/Swiften/StringCodecs/SHA1_Windows.cpp b/Swiften/StringCodecs/SHA1_Windows.cpp index f883d13..ff02012 100644 --- a/Swiften/StringCodecs/SHA1_Windows.cpp +++ b/Swiften/StringCodecs/SHA1_Windows.cpp @@ -10,95 +10,95 @@ #include <Swiften/StringCodecs/SHA1_Windows.h> namespace { - HCRYPTPROV context = 0; - - struct ContextDeleter { - ~ContextDeleter() { - if (context) { - CryptReleaseContext(context, 0); - context = 0; - } - } - } contextDeleter; + HCRYPTPROV context = 0; + + struct ContextDeleter { + ~ContextDeleter() { + if (context) { + CryptReleaseContext(context, 0); + context = 0; + } + } + } contextDeleter; } namespace Swift { SHA1::SHA1() : hash(NULL) { - if (!context) { - if (!CryptAcquireContext(&context, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { -// DWORD error = GetLastError(); -// switch (error) { -// std::cerr << (long)error << std::endl; -// } -// assert(false); - context = 0; - } - } - - if (!CryptCreateHash(context, CALG_SHA1, 0, 0, &hash)) { - hash = NULL; - } + if (!context) { + if (!CryptAcquireContext(&context, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { +// DWORD error = GetLastError(); +// switch (error) { +// std::cerr << (long)error << std::endl; +// } +// assert(false); + context = 0; + } + } + + if (!CryptCreateHash(context, CALG_SHA1, 0, 0, &hash)) { + hash = NULL; + } } SHA1::~SHA1() { - if (hash) { - CryptDestroyHash(hash); - } + if (hash) { + CryptDestroyHash(hash); + } } SHA1& SHA1::update(const std::vector<unsigned char>& data) { - return update(vecptr(data), data.size()); + return update(vecptr(data), data.size()); } SHA1& SHA1::update(const unsigned char* data, size_t dataSize) { - if (!hash || !context) { - return *this; - } - if (!CryptHashData(hash, const_cast<BYTE*>(data), dataSize, 0)) { -// DWORD error = GetLastError(); -// switch (error) { -// std::cerr << (long)error << std::endl; -// } -// assert(false); -// } - } - return *this; + if (!hash || !context) { + return *this; + } + if (!CryptHashData(hash, const_cast<BYTE*>(data), dataSize, 0)) { +// DWORD error = GetLastError(); +// switch (error) { +// std::cerr << (long)error << std::endl; +// } +// assert(false); +// } + } + return *this; } std::vector<unsigned char> SHA1::getHash() const { - if (!hash || !context) { - return std::vector<unsigned char>(); - } - std::vector<unsigned char> result; - DWORD hashLength = sizeof(DWORD); - DWORD hashSize; - CryptGetHashParam(hash, HP_HASHSIZE, reinterpret_cast<BYTE*>(&hashSize), &hashLength, 0); - result.resize(static_cast<size_t>(hashSize)); - if (!CryptGetHashParam(hash, HP_HASHVAL, vecptr(result), &hashSize, 0)) { -// DWORD error = GetLastError(); -// switch (error) { -// std::cerr << (long)error << std::endl; -// } -// assert(false); - return std::vector<unsigned char>(); - } - result.resize(static_cast<size_t>(hashSize)); - return result; + if (!hash || !context) { + return std::vector<unsigned char>(); + } + std::vector<unsigned char> result; + DWORD hashLength = sizeof(DWORD); + DWORD hashSize; + CryptGetHashParam(hash, HP_HASHSIZE, reinterpret_cast<BYTE*>(&hashSize), &hashLength, 0); + result.resize(static_cast<size_t>(hashSize)); + if (!CryptGetHashParam(hash, HP_HASHVAL, vecptr(result), &hashSize, 0)) { +// DWORD error = GetLastError(); +// switch (error) { +// std::cerr << (long)error << std::endl; +// } +// assert(false); + return std::vector<unsigned char>(); + } + result.resize(static_cast<size_t>(hashSize)); + return result; } ByteArray SHA1::getHash(const ByteArray& data) { - SHA1 hash; - hash.update(vecptr(data), data.size()); - return hash.getHash(); + SHA1 hash; + hash.update(vecptr(data), data.size()); + return hash.getHash(); } ByteArray SHA1::getHash(const SafeByteArray& data) { - SHA1 hash; - hash.update(vecptr(data), data.size()); - return hash.getHash(); + SHA1 hash; + hash.update(vecptr(data), data.size()); + return hash.getHash(); } } diff --git a/Swiften/StringCodecs/SHA1_Windows.h b/Swiften/StringCodecs/SHA1_Windows.h index 29ec78b..b94de21 100644 --- a/Swiften/StringCodecs/SHA1_Windows.h +++ b/Swiften/StringCodecs/SHA1_Windows.h @@ -16,29 +16,29 @@ #include <Swiften/Base/SafeByteArray.h> namespace Swift { - class SWIFTEN_API SHA1 { - public: - SHA1(); - ~SHA1(); + class SWIFTEN_API SHA1 { + public: + SHA1(); + ~SHA1(); - SHA1& update(const std::vector<unsigned char>& data); - std::vector<unsigned char> getHash() const; + SHA1& update(const std::vector<unsigned char>& data); + std::vector<unsigned char> getHash() const; - static ByteArray getHash(const ByteArray& data); - static ByteArray getHash(const SafeByteArray& data); + static ByteArray getHash(const ByteArray& data); + static ByteArray getHash(const SafeByteArray& data); - ByteArray operator()(const SafeByteArray& data) { - return getHash(data); - } + ByteArray operator()(const SafeByteArray& data) { + return getHash(data); + } - ByteArray operator()(const ByteArray& data) { - return getHash(data); - } + ByteArray operator()(const ByteArray& data) { + return getHash(data); + } - private: - SHA1& update(const unsigned char* data, size_t dataSize); + private: + SHA1& update(const unsigned char* data, size_t dataSize); - private: - HCRYPTHASH hash; - }; + private: + HCRYPTHASH hash; + }; } diff --git a/Swiften/StringCodecs/UnitTest/Base64Test.cpp b/Swiften/StringCodecs/UnitTest/Base64Test.cpp index d626090..272514f 100644 --- a/Swiften/StringCodecs/UnitTest/Base64Test.cpp +++ b/Swiften/StringCodecs/UnitTest/Base64Test.cpp @@ -15,53 +15,53 @@ using namespace Swift; class Base64Test : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(Base64Test); - CPPUNIT_TEST(testEncodeDecodeAllChars); - CPPUNIT_TEST(testEncodeDecodeOneBytePadding); - CPPUNIT_TEST(testEncodeDecodeTwoBytesPadding); - CPPUNIT_TEST(testEncode_NoData); - CPPUNIT_TEST(testDecode_NoData); - CPPUNIT_TEST_SUITE_END(); - - public: - void testEncodeDecodeAllChars() { - ByteArray input; - for (unsigned char i = 0; i < 255; ++i) { - input.push_back(i); - } - std::string result(Base64::encode(input)); - - CPPUNIT_ASSERT_EQUAL(std::string("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+"), result); - CPPUNIT_ASSERT_EQUAL(input, Base64::decode(result)); - } - - void testEncodeDecodeOneBytePadding() { - ByteArray input = createByteArray("ABCDE", 5); - - std::string result = Base64::encode(input); - - CPPUNIT_ASSERT_EQUAL(std::string("QUJDREU="), result); - CPPUNIT_ASSERT_EQUAL(input, Base64::decode(result)); - } - - void testEncodeDecodeTwoBytesPadding() { - ByteArray input = createByteArray("ABCD", 4); - - std::string result = Base64::encode(input); - - CPPUNIT_ASSERT_EQUAL(std::string("QUJDRA=="), result); - CPPUNIT_ASSERT_EQUAL(input, Base64::decode(result)); - } - - void testEncode_NoData() { - std::string result(Base64::encode(ByteArray())); - CPPUNIT_ASSERT_EQUAL(std::string(""), result); - } - - void testDecode_NoData() { - ByteArray result(Base64::decode("")); - CPPUNIT_ASSERT_EQUAL(ByteArray(), result); - } + CPPUNIT_TEST_SUITE(Base64Test); + CPPUNIT_TEST(testEncodeDecodeAllChars); + CPPUNIT_TEST(testEncodeDecodeOneBytePadding); + CPPUNIT_TEST(testEncodeDecodeTwoBytesPadding); + CPPUNIT_TEST(testEncode_NoData); + CPPUNIT_TEST(testDecode_NoData); + CPPUNIT_TEST_SUITE_END(); + + public: + void testEncodeDecodeAllChars() { + ByteArray input; + for (unsigned char i = 0; i < 255; ++i) { + input.push_back(i); + } + std::string result(Base64::encode(input)); + + CPPUNIT_ASSERT_EQUAL(std::string("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+"), result); + CPPUNIT_ASSERT_EQUAL(input, Base64::decode(result)); + } + + void testEncodeDecodeOneBytePadding() { + ByteArray input = createByteArray("ABCDE", 5); + + std::string result = Base64::encode(input); + + CPPUNIT_ASSERT_EQUAL(std::string("QUJDREU="), result); + CPPUNIT_ASSERT_EQUAL(input, Base64::decode(result)); + } + + void testEncodeDecodeTwoBytesPadding() { + ByteArray input = createByteArray("ABCD", 4); + + std::string result = Base64::encode(input); + + CPPUNIT_ASSERT_EQUAL(std::string("QUJDRA=="), result); + CPPUNIT_ASSERT_EQUAL(input, Base64::decode(result)); + } + + void testEncode_NoData() { + std::string result(Base64::encode(ByteArray())); + CPPUNIT_ASSERT_EQUAL(std::string(""), result); + } + + void testDecode_NoData() { + ByteArray result(Base64::decode("")); + CPPUNIT_ASSERT_EQUAL(ByteArray(), result); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(Base64Test); diff --git a/Swiften/StringCodecs/UnitTest/HexifyTest.cpp b/Swiften/StringCodecs/UnitTest/HexifyTest.cpp index 57f4fe8..7d349fa 100644 --- a/Swiften/StringCodecs/UnitTest/HexifyTest.cpp +++ b/Swiften/StringCodecs/UnitTest/HexifyTest.cpp @@ -15,25 +15,25 @@ using namespace Swift; class HexifyTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(HexifyTest); - CPPUNIT_TEST(testHexify); - CPPUNIT_TEST(testHexify_Byte); - CPPUNIT_TEST(testUnhexify); - CPPUNIT_TEST_SUITE_END(); - - public: - void testHexify() { - CPPUNIT_ASSERT_EQUAL(std::string("4206b23ca6b0a643d20d89b04ff58cf78b8096ed"), Hexify::hexify(createByteArray("\x42\x06\xb2\x3c\xa6\xb0\xa6\x43\xd2\x0d\x89\xb0\x4f\xf5\x8c\xf7\x8b\x80\x96\xed"))); - } - - void testHexify_Byte() { - CPPUNIT_ASSERT_EQUAL(std::string("b2"), Hexify::hexify(0xb2)); - } - - void testUnhexify() { - CPPUNIT_ASSERT_EQUAL(std::string("ffaf02"), Hexify::hexify(Hexify::unhexify("ffaf02"))); - CPPUNIT_ASSERT(createByteArray("\x01\x23\xf2", 3) == Hexify::unhexify("0123f2")); - } + CPPUNIT_TEST_SUITE(HexifyTest); + CPPUNIT_TEST(testHexify); + CPPUNIT_TEST(testHexify_Byte); + CPPUNIT_TEST(testUnhexify); + CPPUNIT_TEST_SUITE_END(); + + public: + void testHexify() { + CPPUNIT_ASSERT_EQUAL(std::string("4206b23ca6b0a643d20d89b04ff58cf78b8096ed"), Hexify::hexify(createByteArray("\x42\x06\xb2\x3c\xa6\xb0\xa6\x43\xd2\x0d\x89\xb0\x4f\xf5\x8c\xf7\x8b\x80\x96\xed"))); + } + + void testHexify_Byte() { + CPPUNIT_ASSERT_EQUAL(std::string("b2"), Hexify::hexify(0xb2)); + } + + void testUnhexify() { + CPPUNIT_ASSERT_EQUAL(std::string("ffaf02"), Hexify::hexify(Hexify::unhexify("ffaf02"))); + CPPUNIT_ASSERT(createByteArray("\x01\x23\xf2", 3) == Hexify::unhexify("0123f2")); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(HexifyTest); diff --git a/Swiften/StringCodecs/UnitTest/PBKDF2Test.cpp b/Swiften/StringCodecs/UnitTest/PBKDF2Test.cpp index b75f6fb..343c170 100644 --- a/Swiften/StringCodecs/UnitTest/PBKDF2Test.cpp +++ b/Swiften/StringCodecs/UnitTest/PBKDF2Test.cpp @@ -17,37 +17,37 @@ using namespace Swift; class PBKDF2Test : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PBKDF2Test); - CPPUNIT_TEST(testGetResult_I1); - CPPUNIT_TEST(testGetResult_I2); - CPPUNIT_TEST(testGetResult_I4096); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(PBKDF2Test); + CPPUNIT_TEST(testGetResult_I1); + CPPUNIT_TEST(testGetResult_I2); + CPPUNIT_TEST(testGetResult_I4096); + CPPUNIT_TEST_SUITE_END(); - public: - void setUp() { - crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create()); - } + public: + void setUp() { + crypto = boost::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create()); + } - void testGetResult_I1() { - ByteArray result(PBKDF2::encode(createSafeByteArray("password"), createByteArray("salt"), 1, crypto.get())); + void testGetResult_I1() { + ByteArray result(PBKDF2::encode(createSafeByteArray("password"), createByteArray("salt"), 1, crypto.get())); - CPPUNIT_ASSERT_EQUAL(createByteArray("\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6"), result); - } + CPPUNIT_ASSERT_EQUAL(createByteArray("\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6"), result); + } - void testGetResult_I2() { - ByteArray result(PBKDF2::encode(createSafeByteArray("password"), createByteArray("salt"), 2, crypto.get())); + void testGetResult_I2() { + ByteArray result(PBKDF2::encode(createSafeByteArray("password"), createByteArray("salt"), 2, crypto.get())); - CPPUNIT_ASSERT_EQUAL(createByteArray("\xea\x6c\x1\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57"), result); - } + CPPUNIT_ASSERT_EQUAL(createByteArray("\xea\x6c\x1\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57"), result); + } - void testGetResult_I4096() { - ByteArray result(PBKDF2::encode(createSafeByteArray("password"), createByteArray("salt"), 4096, crypto.get())); + void testGetResult_I4096() { + ByteArray result(PBKDF2::encode(createSafeByteArray("password"), createByteArray("salt"), 4096, crypto.get())); - CPPUNIT_ASSERT_EQUAL(createByteArray("\x4b\x00\x79\x1\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1", 20), result); - } - - private: - boost::shared_ptr<CryptoProvider> crypto; + CPPUNIT_ASSERT_EQUAL(createByteArray("\x4b\x00\x79\x1\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1", 20), result); + } + + private: + boost::shared_ptr<CryptoProvider> crypto; }; CPPUNIT_TEST_SUITE_REGISTRATION(PBKDF2Test); |