diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.com> | 2014-03-23 13:05:03 +0100 |
---|---|---|
committer | Tomaž Vajngerl <tomaz.vajngerl@collabora.com> | 2014-03-23 13:11:59 +0100 |
commit | 7d3e8c5217250d11b4bf4fcdf74a3791be69ab53 (patch) | |
tree | e5f886d9932903f366e456398ad6aee70b751c95 /sal/qa | |
parent | 53cbca6ee1b8e72144310147c88585a4f4b854c8 (diff) |
sal: Rewrite digest tests to be simpler, enable them in check
Change-Id: I610e615be498237e5cd9dd963bca60c7f8f62aac
Diffstat (limited to 'sal/qa')
-rw-r--r-- | sal/qa/rtl/digest/rtl_digest.cxx | 1491 |
1 files changed, 223 insertions, 1268 deletions
diff --git a/sal/qa/rtl/digest/rtl_digest.cxx b/sal/qa/rtl/digest/rtl_digest.cxx index b41929236603..1ae3d27e7b15 100644 --- a/sal/qa/rtl/digest/rtl_digest.cxx +++ b/sal/qa/rtl/digest/rtl_digest.cxx @@ -23,6 +23,8 @@ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/plugin/TestPlugIn.h> +#include <boost/scoped_array.hpp> + #include <rtl/digest.h> #include <rtl/ustring.hxx> #include <rtl/ustrbuf.hxx> @@ -30,1410 +32,363 @@ #include <string.h> -// sample, how to use digest +using namespace rtl; -rtl::OUString CreateMD5FromString( const rtl::OUString& aMsg ) +namespace { - // PRE: aStr "file" - // BACK: Str "ababab....0f" Hexcode String - - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - if ( handle > 0 ) - { - const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr(); - sal_uInt32 nSize = ( aMsg.getLength() * sizeof( sal_Unicode )); - sal_uInt32 nMD5KeyLen = rtl_digest_queryLength( handle ); - sal_uInt8* pMD5KeyBuffer = new sal_uInt8[ nMD5KeyLen ]; - - rtl_digest_init( handle, pData, nSize ); - rtl_digest_update( handle, pData, nSize ); - rtl_digest_get( handle, pMD5KeyBuffer, nMD5KeyLen ); - rtl_digest_destroy( handle ); - - // Create hex-value string from the MD5 value to keep the string size minimal - rtl::OUStringBuffer aBuffer( nMD5KeyLen * 2 + 1 ); - for ( sal_uInt32 i = 0; i < nMD5KeyLen; ++i ) - aBuffer.append( (sal_Int32)pMD5KeyBuffer[i], 16 ); - - delete [] pMD5KeyBuffer; - return aBuffer.makeStringAndClear(); - } - - return rtl::OUString(); -} +const OString sSampleString = "This is a sample sentence, which we use to check some crypto functions in sal."; +const OString sSampleString_only_one_diff = "This is a sample sentence. which we use to check some crypto functions in sal."; -namespace rtl_digest +const rtlDigestAlgorithm constDigestAlgorithms[] = { - - rtl::OString sSampleString = "This is a sample sentence, which we use to check some crypto functions in sal."; - rtl::OString sSampleString_MD2 = "647ee6c9d4aa5fdd374ed9d7a156acbf"; - rtl::OString sSampleString_MD5 = "b16b903e6fc0b62ae389013ed93fe531"; - rtl::OString sSampleString_SHA = "eab2814429b2613301c8a077b806af3680548914"; - rtl::OString sSampleString_SHA1 = "2bc5bdb7506a2cdc2fd27fc8b9889343012d5008"; - rtl::OString sSampleString_HMAC_MD5 = "dd9cba48c972fba0a882baa72b079674"; - rtl::OString sSampleString_HMAC_SHA1 = "5d7f43ce6abd1de4438d7e69e01495864490cf3e"; - - rtl::OString sSampleString_only_one_diff = "This is a sample sentence. which we use to check some crypto functions in sal."; - -class create : public CppUnit::TestFixture + rtl_Digest_AlgorithmMD2, + rtl_Digest_AlgorithmMD5, + rtl_Digest_AlgorithmSHA, + rtl_Digest_AlgorithmSHA1, + rtl_Digest_AlgorithmHMAC_MD5, + rtl_Digest_AlgorithmHMAC_SHA1, +}; + +const sal_uInt32 constDigestAlgorithmLengths[] = { -public: - // initialise your test code values here. - void setUp() - { - } - - void tearDown() - { - } - - void create_001() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", handle != 0); - rtl_digest_destroy( handle ); - } - void create_002() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", handle != 0); - rtl_digest_destroy( handle ); - } - void create_003() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmSHA", handle != 0); - rtl_digest_destroy( handle ); - } - void create_004() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmSHA1", handle != 0); - rtl_digest_destroy( handle ); - } - void create_005() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmHMAC_MD5", handle != 0); - rtl_digest_destroy( handle ); - } - void create_006() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmHMAC_SHA1", handle != 0); - rtl_digest_destroy( handle ); - } - - void create_007() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid ); - printf("Handle is %p\n", handle); - CPPUNIT_ASSERT_MESSAGE("create with NULL", handle == 0); - rtl_digest_destroy( handle ); - } - - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(create); - CPPUNIT_TEST(create_001); - CPPUNIT_TEST(create_002); - CPPUNIT_TEST(create_003); - CPPUNIT_TEST(create_004); - CPPUNIT_TEST(create_005); - CPPUNIT_TEST(create_006); - CPPUNIT_TEST(create_007); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - - - - - -class createMD5 : public CppUnit::TestFixture + RTL_DIGEST_LENGTH_MD2, + RTL_DIGEST_LENGTH_MD5, + RTL_DIGEST_LENGTH_SHA, + RTL_DIGEST_LENGTH_SHA1, + RTL_DIGEST_LENGTH_HMAC_MD5, + RTL_DIGEST_LENGTH_HMAC_SHA1, +}; + +const OString constSampleStringSums[] = { -public: - // initialise your test code values here. - void setUp() - { - } - - void tearDown() - { - } - - void createMD5_001() - { - rtlDigest handle = rtl_digest_createMD5(); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmMD5 == aAlgo); - - rtl_digest_destroy( handle ); - } - CPPUNIT_TEST_SUITE(createMD5); - CPPUNIT_TEST(createMD5_001); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - - - -class createMD2 : public CppUnit::TestFixture + "647ee6c9d4aa5fdd374ed9d7a156acbf", + "b16b903e6fc0b62ae389013ed93fe531", + "eab2814429b2613301c8a077b806af3680548914", + "2bc5bdb7506a2cdc2fd27fc8b9889343012d5008", + "0b1b0e1a6f2e4420326354b031063605", + "1998c6a556915be76451bfb587fa7c34d849936e" +}; + +// Create hex-value string from the digest value to keep the string size minimal +OString createHex(sal_uInt8* pKeyBuffer, sal_uInt32 nKeyLen) { -public: - // initialise your test code values here. - void setUp() + OStringBuffer aBuffer(nKeyLen * 2 + 1); + for (sal_uInt32 i = 0; i < nKeyLen; ++i) { + sal_Int32 nValue = (sal_Int32) pKeyBuffer[i]; + if (nValue < 16) + aBuffer.append('0'); + aBuffer.append(nValue, 16); } + return aBuffer.makeStringAndClear(); +} - void tearDown() - { - } - - void createMD2_001() - { - rtlDigest handle = rtl_digest_createMD2( ); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmMD2 == aAlgo); - - rtl_digest_destroy( handle ); - } - CPPUNIT_TEST_SUITE(createMD2); - CPPUNIT_TEST(createMD2_001); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - - -class createSHA : public CppUnit::TestFixture +OString getDigest(const OString& aMessage, rtlDigestAlgorithm aAlgorithm) { -public: - // initialise your test code values here. - void setUp() - { - } + rtlDigest handle = rtl_digest_create(aAlgorithm); - void tearDown() - { - } - - void createSHA_001() - { - rtlDigest handle = rtl_digest_createSHA( ); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmSHA == aAlgo); - - rtl_digest_destroy( handle ); - } - CPPUNIT_TEST_SUITE(createSHA); - CPPUNIT_TEST(createSHA_001); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - -class createSHA1 : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { - } + const sal_uInt8* pData = (const sal_uInt8*) aMessage.getStr(); + sal_uInt32 nSize = aMessage.getLength(); - void tearDown() - { - } + rtl_digest_init(handle, pData, nSize); + rtl_digest_update(handle, pData, nSize); - void createSHA1_001() - { - rtlDigest handle = rtl_digest_createSHA1(); + sal_uInt32 nKeyLen = rtl_digest_queryLength(handle); + boost::scoped_array<sal_uInt8> pKeyBuffer(new sal_uInt8[nKeyLen]); - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmSHA1 == aAlgo); - - rtl_digest_destroy( handle ); - } - CPPUNIT_TEST_SUITE(createSHA1); - CPPUNIT_TEST(createSHA1_001); - CPPUNIT_TEST_SUITE_END(); -}; // class create + rtl_digest_get(handle, pKeyBuffer.get(), nKeyLen); + OString aSum = createHex(pKeyBuffer.get(), nKeyLen); + rtl_digest_destroy( handle ); + return aSum; +} -class createHMAC_MD5 : public CppUnit::TestFixture +class DigestTest : public CppUnit::TestFixture { public: - // initialise your test code values here. - void setUp() - { - } - - void tearDown() + void testCreate() { - } + int aAlgorithmSize = sizeof(constDigestAlgorithms) / sizeof(constDigestAlgorithms[0]); - void createHMAC_MD5_001() + for (int i = 0; i < aAlgorithmSize; i++) { - rtlDigest handle = rtl_digest_createHMAC_MD5(); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmHMAC_MD5 == aAlgo); - + rtlDigest handle = rtl_digest_create( constDigestAlgorithms[i] ); + CPPUNIT_ASSERT_MESSAGE("create digest", handle != NULL); rtl_digest_destroy( handle ); } - CPPUNIT_TEST_SUITE(createHMAC_MD5); - CPPUNIT_TEST(createHMAC_MD5_001); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - -class createHMAC_SHA1 : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { - } - void tearDown() - { - } - - void createHMAC_SHA1_001() - { - rtlDigest handle = rtl_digest_createHMAC_SHA1(); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmHMAC_SHA1 == aAlgo); - - rtl_digest_destroy( handle ); - } - - - CPPUNIT_TEST_SUITE(createHMAC_SHA1); - CPPUNIT_TEST(createHMAC_SHA1_001); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - - -class queryAlgorithm : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { + rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid ); + CPPUNIT_ASSERT_MESSAGE("create invalid digest", handle == NULL); + rtl_digest_destroy( handle ); } - void tearDown() + void testQuery() { - } - - void query_001() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmMD5 == aAlgo); - - rtl_digest_destroy( handle ); - } - void query_002() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmMD2 == aAlgo); + int aAlgorithmSize = sizeof(constDigestAlgorithms) / sizeof(constDigestAlgorithms[0]); - rtl_digest_destroy( handle ); - } - void query_003() + for (int i = 0; i < aAlgorithmSize; i++) { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA ); - + rtlDigest handle = rtl_digest_create(constDigestAlgorithms[i]); rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmSHA == aAlgo); - + CPPUNIT_ASSERT_MESSAGE("query handle", constDigestAlgorithms[i] == aAlgo); rtl_digest_destroy( handle ); } - void query_004() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 ); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmSHA1 == aAlgo); - - rtl_digest_destroy( handle ); - } - void query_005() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 ); - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmHMAC_MD5 == aAlgo); - - rtl_digest_destroy( handle ); - } - void query_006() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 ); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmHMAC_SHA1 == aAlgo); - - rtl_digest_destroy( handle ); - } - void query_007() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid ); - - rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); - CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmInvalid == aAlgo); - - rtl_digest_destroy( handle ); - } - - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(queryAlgorithm); - CPPUNIT_TEST( query_001 ); - CPPUNIT_TEST( query_002 ); - CPPUNIT_TEST( query_003 ); - CPPUNIT_TEST( query_004 ); - CPPUNIT_TEST( query_005 ); - CPPUNIT_TEST( query_006 ); - CPPUNIT_TEST( query_007 ); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - - -class queryLength : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { } - void tearDown() + void testQueryLength() { - } - - void queryLength_MD5() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - - sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); - // printf("nAlgoLength:=%d\n", nAlgoLength); - CPPUNIT_ASSERT_MESSAGE("query Length", RTL_DIGEST_LENGTH_MD5 == nAlgoLength); - - rtl_digest_destroy( handle ); - } - void queryLength_MD2() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); - - sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); - // printf("nAlgoLength:=%d\n", nAlgoLength); - CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_MD2 == nAlgoLength); - - rtl_digest_destroy( handle ); - } - void queryLength_SHA() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA ); - - sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); - // printf("nAlgoLength:=%d\n", nAlgoLength); - CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_SHA == nAlgoLength); - - rtl_digest_destroy( handle ); - } - void queryLength_SHA1() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 ); - - sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); - // printf("nAlgoLength:=%d\n", nAlgoLength); - CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_SHA1 == nAlgoLength); - - rtl_digest_destroy( handle ); - } - void queryLength_HMAC_MD5() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 ); - - sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); - // printf("nAlgoLength:=%d\n", nAlgoLength); - CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_HMAC_MD5 == nAlgoLength); - - rtl_digest_destroy( handle ); - } - void queryLength_HMAC_SHA1() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 ); - - sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); - // printf("nAlgoLength:=%d\n", nAlgoLength); - CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_HMAC_SHA1 == nAlgoLength); - - rtl_digest_destroy( handle ); - } + int aAlgorithmSize = sizeof(constDigestAlgorithms) / sizeof(constDigestAlgorithms[0]); + rtlDigest handle; + sal_uInt32 nAlgoLength; - void queryLength_Illegal() + for (int i = 0; i < aAlgorithmSize; i++) { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid ); - - sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); - // printf("nAlgoLength:=%d\n", nAlgoLength); - CPPUNIT_ASSERT_MESSAGE("query length", 0 == nAlgoLength); - + handle = rtl_digest_create(constDigestAlgorithms[i]); + nAlgoLength = rtl_digest_queryLength(handle); + CPPUNIT_ASSERT_MESSAGE("query Length", constDigestAlgorithmLengths[i] == nAlgoLength); rtl_digest_destroy( handle ); } - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(queryLength); - CPPUNIT_TEST( queryLength_MD2 ); - CPPUNIT_TEST( queryLength_MD5 ); - CPPUNIT_TEST( queryLength_SHA ); - CPPUNIT_TEST( queryLength_SHA1 ); - CPPUNIT_TEST( queryLength_HMAC_MD5 ); - CPPUNIT_TEST( queryLength_HMAC_SHA1 ); - CPPUNIT_TEST( queryLength_Illegal ); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - - -rtl::OString createHex(sal_uInt8 *_pMD5KeyBuffer, sal_uInt32 _nMD5KeyLen) -{ - // Create hex-value string from the MD5 value to keep the string size minimal - rtl::OStringBuffer aBuffer( _nMD5KeyLen * 2 + 1 ); - for ( sal_uInt32 i = 0; i < _nMD5KeyLen; ++i ) - { - sal_Int32 nValue = (sal_Int32)_pMD5KeyBuffer[i]; - if (nValue < 16) // maximul hex value for 1 byte - aBuffer.append( static_cast<sal_Int32>(0), static_cast<sal_Int16>(16) /* radix */ ); - aBuffer.append( nValue, 16 /* radix */ ); - } - - return aBuffer.makeStringAndClear(); -} - - -class init : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { + handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid ); + nAlgoLength = rtl_digest_queryLength(handle); + CPPUNIT_ASSERT_MESSAGE("query length", 0 == nAlgoLength); + rtl_digest_destroy( handle ); } - void tearDown() + void testInit() { - } - - void init_000() - { - rtlDigest handle = NULL; - - rtlDigestError aError = rtl_digest_init(handle, NULL, 0); - - CPPUNIT_ASSERT_MESSAGE("init(NULL, 0, 0)", aError == rtl_Digest_E_Argument); - } - - void init_001() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - - rtlDigestError aError = rtl_digest_init(handle, NULL, 0); - - CPPUNIT_ASSERT_MESSAGE("init(handle, 0, 0)", aError == rtl_Digest_E_None); - - rtl_digest_destroy( handle ); - } - - - void init_MD2() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); - - rtl::OString aMsg = sSampleString; - const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); - sal_uInt32 nSize = ( aMsg.getLength() ); - - rtlDigestError aError = rtl_digest_init(handle, pData, nSize); - - CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); - - rtl_digest_update( handle, pData, nSize ); - - sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); - sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; - - rtl_digest_get( handle, pKeyBuffer, nKeyLen ); - rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); - delete [] pKeyBuffer; - - printf("MD2 Sum: %s\n", aSum.getStr()); - // LLA: how to check right values - // samples? - - rtl_digest_destroy( handle ); - } + rtlDigestError aError; + rtlDigest handle; - void init_MD5() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - - rtl::OString aMsg = sSampleString; - const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); - sal_uInt32 nSize = ( aMsg.getLength() ); - - rtlDigestError aError = rtl_digest_init(handle, pData, nSize); - - CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); - - rtl_digest_update( handle, pData, nSize ); - - sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); - sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; - - rtl_digest_get( handle, pKeyBuffer, nKeyLen ); - rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); - delete [] pKeyBuffer; - - printf("MD5 Sum: %s\n", aSum.getStr()); - // LLA: how to check right values - // samples? - - rtl_digest_destroy( handle ); - } - - void init_SHA() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA ); - - rtl::OString aMsg = sSampleString; - const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); - sal_uInt32 nSize = ( aMsg.getLength() ); - - rtlDigestError aError = rtl_digest_init(handle, pData, nSize); - - CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); - - rtl_digest_update( handle, pData, nSize ); - - sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); - sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; - - rtl_digest_get( handle, pKeyBuffer, nKeyLen ); - rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); - delete [] pKeyBuffer; - - printf("SHA Sum: %s\n", aSum.getStr()); - // LLA: how to check right values - // samples? - - rtl_digest_destroy( handle ); - } - void init_SHA1() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 ); - - rtl::OString aMsg = sSampleString; - const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); - sal_uInt32 nSize = ( aMsg.getLength() ); - - rtlDigestError aError = rtl_digest_init(handle, pData, nSize); - - CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); - - rtl_digest_update( handle, pData, nSize ); - - sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); - sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; - - rtl_digest_get( handle, pKeyBuffer, nKeyLen ); - rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); - delete [] pKeyBuffer; - - printf("SHA1 Sum: %s\n", aSum.getStr()); - // LLA: how to check right values - // samples? + handle = NULL; + aError = rtl_digest_init(handle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("init(NULL, 0, 0)", aError == rtl_Digest_E_Argument); - rtl_digest_destroy( handle ); - } - void init_HMAC_MD5() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 ); - - rtl::OString aMsg = sSampleString; - const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); - sal_uInt32 nSize = ( aMsg.getLength() ); - - sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); - CPPUNIT_ASSERT_MESSAGE( "Keylen must be greater 0", nKeyLen ); - - sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; - CPPUNIT_ASSERT( pKeyBuffer ); - memset(pKeyBuffer, 0, nKeyLen); - - rtlDigestError aError = rtl_digest_init(handle, pKeyBuffer, nKeyLen ); - - CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); - - rtl_digest_update( handle, pData, nSize ); - - rtl_digest_get( handle, pKeyBuffer, nKeyLen ); - rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); - delete [] pKeyBuffer; + handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); + aError = rtl_digest_init(handle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("init(handle, 0, 0)", aError == rtl_Digest_E_None); + rtl_digest_destroy( handle ); - printf("HMAC_MD5 Sum: %s\n", aSum.getStr()); - // LLA: how to check right values - // samples? + int aAlgorithmSize = sizeof(constDigestAlgorithms) / sizeof(constDigestAlgorithms[0]); - rtl_digest_destroy( handle ); - } - void init_HMAC_SHA1() + for (int i = 0; i < aAlgorithmSize; i++) { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 ); - - rtl::OString aMsg = sSampleString; - const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); - sal_uInt32 nSize = ( aMsg.getLength() ); - - sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); - CPPUNIT_ASSERT_MESSAGE( "Keylen must be greater 0", nKeyLen ); - - sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; - CPPUNIT_ASSERT( pKeyBuffer ); - memset(pKeyBuffer, 0, nKeyLen); + handle = rtl_digest_create(constDigestAlgorithms[i]); - rtlDigestError aError = rtl_digest_init(handle, pKeyBuffer, nKeyLen ); + OString aMessage = sSampleString; + const sal_uInt8* pData = (const sal_uInt8*) aMessage.getStr(); + sal_uInt32 nSize = aMessage.getLength(); + aError = rtl_digest_init(handle, pData, nSize); CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); - rtl_digest_update( handle, pData, nSize ); + rtl_digest_update(handle, pData, nSize); - rtl_digest_get( handle, pKeyBuffer, nKeyLen ); - rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); - delete [] pKeyBuffer; + sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); + boost::scoped_array<sal_uInt8> pKeyBuffer(new sal_uInt8[nKeyLen]); - printf("HMAC_SHA1 Sum: %s\n", aSum.getStr()); - // LLA: how to check right values - // samples? + rtl_digest_get( handle, pKeyBuffer.get(), nKeyLen ); + OString aSum = createHex(pKeyBuffer.get(), nKeyLen); rtl_digest_destroy( handle ); } - - - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(init); - CPPUNIT_TEST( init_000 ); - CPPUNIT_TEST( init_001 ); - CPPUNIT_TEST( init_MD2 ); - CPPUNIT_TEST( init_MD5 ); - CPPUNIT_TEST( init_SHA ); - CPPUNIT_TEST( init_SHA1 ); - CPPUNIT_TEST( init_HMAC_MD5 ); - CPPUNIT_TEST( init_HMAC_SHA1 ); - CPPUNIT_TEST_SUITE_END(); -}; // class init - - - -rtl::OString getMD5Sum(rtl::OString const& _aMsg ) -{ - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - - const sal_uInt8 *pData = (const sal_uInt8*)_aMsg.getStr(); - sal_uInt32 nSize = ( _aMsg.getLength() ); - - rtl_digest_init(handle, pData, nSize); - rtl_digest_update( handle, pData, nSize ); - - sal_uInt32 nMD5KeyLen = rtl_digest_queryLength( handle ); - sal_uInt8 *pMD5KeyBuffer = new sal_uInt8[ nMD5KeyLen ]; - - rtl_digest_get( handle, pMD5KeyBuffer, nMD5KeyLen ); - rtl::OString aMD5Sum = createHex(pMD5KeyBuffer, nMD5KeyLen); - delete [] pMD5KeyBuffer; - - rtl_digest_destroy( handle ); - return aMD5Sum; -} - - - -class equalTests : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { } - void tearDown() + void testEqual() { - } - - - void equal_001() { - rtl::OString aMsg1 = sSampleString; - rtl::OString aMsg2 = sSampleString; + OString aMsg1 = sSampleString; + OString aMsg2 = sSampleString; - rtl::OString aMsgMD5Sum1 = getMD5Sum(aMsg1); - rtl::OString aMsgMD5Sum2 = getMD5Sum(aMsg2); + OString aSum1 = getDigest(aMsg1, rtl_Digest_AlgorithmMD5); + OString aSum2 = getDigest(aMsg2, rtl_Digest_AlgorithmMD5); - CPPUNIT_ASSERT_MESSAGE("md5sum must have a length", aMsgMD5Sum1.getLength() == 32 && aMsgMD5Sum2.getLength() == 32 ); - CPPUNIT_ASSERT_MESSAGE("source is the same, dest must be also the same", aMsgMD5Sum1.equals(aMsgMD5Sum2) == sal_True); + CPPUNIT_ASSERT_MESSAGE("md5sum must have a length", aSum1.getLength() == 32 && aSum2.getLength() == 32 ); + CPPUNIT_ASSERT_MESSAGE("source is the same, dest must be also the same", aSum1.equals(aSum2)); } - void equal_002() { - rtl::OString aMsg1 = sSampleString; - rtl::OString aMsg2 = sSampleString_only_one_diff; + OString aMsg1 = sSampleString; + OString aMsg2 = sSampleString_only_one_diff; - rtl::OString aMsgMD5Sum1 = getMD5Sum(aMsg1); - rtl::OString aMsgMD5Sum2 = getMD5Sum(aMsg2); + OString aSum1 = getDigest(aMsg1, rtl_Digest_AlgorithmMD5); + OString aSum2 = getDigest(aMsg2, rtl_Digest_AlgorithmMD5); - CPPUNIT_ASSERT_MESSAGE("md5sum must have a length", aMsgMD5Sum1.getLength() == 32 && aMsgMD5Sum2.getLength() == 32 ); - CPPUNIT_ASSERT_MESSAGE("differ only in one char", aMsgMD5Sum1.equals(aMsgMD5Sum2) == sal_False); + CPPUNIT_ASSERT_MESSAGE("md5sum must have a length", aSum1.getLength() == 32 && aSum2.getLength() == 32 ); + CPPUNIT_ASSERT_MESSAGE("differ only in one char", !aSum1.equals(aSum2)); } - - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(equalTests); - CPPUNIT_TEST( equal_001 ); - CPPUNIT_TEST( equal_002 ); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - - -class digest_MD2 : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { } - void tearDown() + void testCheckSum() { - } - + int aAlgorithmSize = sizeof(constDigestAlgorithms) / sizeof(constDigestAlgorithms[0]); - void MD2_001() + for (int i = 0; i < aAlgorithmSize; i++) { - rtl::OString aMsg1 = sSampleString; - - sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_MD2 ]; - CPPUNIT_ASSERT( pBuffer ); - memset(pBuffer, 0, RTL_DIGEST_LENGTH_MD2 ); - - sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); - sal_Int32 nLen = aMsg1.getLength(); - - rtlDigestError aError = rtl_digest_MD2(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_MD2); - - CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); - - rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_MD2); - printf("Decrypt MD2: %s\n", aStr.getStr()); - CPPUNIT_ASSERT_MESSAGE("checksum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_MD2) ); - - delete [] pBuffer; + OString aSum = getDigest(sSampleString, constDigestAlgorithms[i]); + CPPUNIT_ASSERT_EQUAL_MESSAGE("Checksum of sample string is wrong.", constSampleStringSums[i], aSum); } - - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(digest_MD2); - CPPUNIT_TEST( MD2_001 ); - CPPUNIT_TEST_SUITE_END(); -}; // class create - -class digest_MD5 : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { } - void tearDown() + OString runCheckPBKDF2(OString& sPassword, bool bClearSalt, sal_uInt32 nCount) { - } + sal_uInt32 nKeyLen = RTL_DIGEST_LENGTH_HMAC_SHA1; + boost::scoped_array<sal_uInt8> pKeyBuffer(new sal_uInt8[nKeyLen]); - void MD5_001() - { - rtl::OString aMsg1 = sSampleString; + memset(pKeyBuffer.get(), 0, nKeyLen); - sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_MD5 ]; - CPPUNIT_ASSERT( pBuffer ); - memset(pBuffer, 0, RTL_DIGEST_LENGTH_MD5 ); + sal_uInt8* pPassword = (sal_uInt8*)sPassword.getStr(); + sal_Int32 nPasswordLen = sPassword.getLength(); - sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); - sal_Int32 nLen = aMsg1.getLength(); + sal_uInt32 nSaltDataLen = RTL_DIGEST_LENGTH_HMAC_SHA1; + boost::scoped_array<sal_uInt8> pSaltData(new sal_uInt8[nSaltDataLen]); + memset(pSaltData.get(), 0, nSaltDataLen); - rtlDigestError aError = rtl_digest_MD5(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_MD5); - - CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); - - rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_MD5); - printf("Decrypt MD5: %s\n", aStr.getStr()); - CPPUNIT_ASSERT_MESSAGE("checksum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_MD5) ); - - delete [] pBuffer; - } - - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(digest_MD5); - CPPUNIT_TEST( MD5_001 ); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - -class digest_SHA : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { - } - - void tearDown() - { - } - - - void SHA_001() + if (!bClearSalt) { - rtl::OString aMsg1 = sSampleString; - - sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_SHA ]; - CPPUNIT_ASSERT( pBuffer ); - memset(pBuffer, 0, RTL_DIGEST_LENGTH_SHA); - - sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); - sal_Int32 nLen = aMsg1.getLength(); - - rtlDigestError aError = rtl_digest_SHA(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_SHA); - - CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); - - rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_SHA); - printf("Decrypt SHA: %s\n", aStr.getStr()); - CPPUNIT_ASSERT_MESSAGE("checksum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_SHA) ); - - delete [] pBuffer; + // wilful contamination + pSaltData[0] = 1; } - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. + rtlDigestError aError = rtl_digest_PBKDF2(pKeyBuffer.get(), nKeyLen, pPassword, nPasswordLen, pSaltData.get(), nSaltDataLen, nCount); - CPPUNIT_TEST_SUITE(digest_SHA); - CPPUNIT_TEST( SHA_001 ); - CPPUNIT_TEST_SUITE_END(); -}; // class create + CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); + rtl::OString aKey = createHex(pKeyBuffer.get(), nKeyLen); -class digest_SHA1 : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { - } + // OString sSalt = createHex(pSaltData, nSaltDataLen); + // printf("Salt: %s\n", sSalt.getStr()); - void tearDown() - { + // CPPUNIT_ASSERT_MESSAGE("md5sum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_PBKDF2) ); + return aKey; } - - void SHA1_001() - { - rtl::OString aMsg1 = sSampleString; - - sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_SHA1 ]; - CPPUNIT_ASSERT( pBuffer ); - memset(pBuffer, 0, RTL_DIGEST_LENGTH_SHA1); - - sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); - sal_Int32 nLen = aMsg1.getLength(); - - rtlDigestError aError = rtl_digest_SHA1(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_SHA1); - - CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); - - rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_SHA1); - printf("Decrypt SHA1: %s\n", aStr.getStr()); - CPPUNIT_ASSERT_MESSAGE("checksum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_SHA1) ); - - delete [] pBuffer; - } - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(digest_SHA1); - CPPUNIT_TEST( SHA1_001 ); - CPPUNIT_TEST_SUITE_END(); -}; // class create - -class digest_HMAC_MD5 : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() + void testPBKDF2() { - } + OString aPassword = "Password"; - void tearDown() - { + // all permutations + runCheckPBKDF2(aPassword, false, 1); + runCheckPBKDF2(aPassword, false, 2); + runCheckPBKDF2(aPassword, true, 1); + runCheckPBKDF2(aPassword, true, 2); + runCheckPBKDF2(aPassword, false, 3); + runCheckPBKDF2(aPassword, false, 4); + runCheckPBKDF2(aPassword, true, 3); + runCheckPBKDF2(aPassword, true, 4); } - - void HMAC_MD5_001() - { - rtl::OString aMsg1 = sSampleString; - - sal_uInt8 *pKeyBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_MD5 ]; - CPPUNIT_ASSERT( pKeyBuffer ); - memset(pKeyBuffer, 0, RTL_DIGEST_LENGTH_HMAC_MD5); - - sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_MD5 ]; - CPPUNIT_ASSERT( pBuffer ); - memset(pBuffer, 0, RTL_DIGEST_LENGTH_HMAC_MD5); - - sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); - sal_Int32 nLen = aMsg1.getLength(); - - rtlDigestError aError = rtl_digest_HMAC_MD5(pKeyBuffer, RTL_DIGEST_LENGTH_HMAC_MD5, pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_HMAC_MD5); - - CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); - - rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_HMAC_MD5); - printf("Decrypt HMAC_MD5: %s\n", aStr.getStr()); - CPPUNIT_ASSERT_MESSAGE("md5sum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_HMAC_MD5) ); - - delete [] pBuffer; - } - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(digest_HMAC_MD5); - CPPUNIT_TEST( HMAC_MD5_001 ); - CPPUNIT_TEST_SUITE_END(); -}; // class create - -class digest_HMAC_SHA1 : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() + void testUpdate() { - } + rtlDigestError aError; + rtlDigest aHandle; - void tearDown() - { - } + aHandle = NULL; + aError = rtl_digest_update(aHandle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument); + aHandle = NULL; + aError = rtl_digest_updateMD2(aHandle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument); - void HMAC_SHA1_001() - { - rtl::OString aMsg1 = sSampleString; + aError = rtl_digest_updateMD5(aHandle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument); - sal_uInt8 *pKeyBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_SHA1 ]; - CPPUNIT_ASSERT( pKeyBuffer ); - memset(pKeyBuffer, 0, RTL_DIGEST_LENGTH_HMAC_SHA1); + aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); + CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != 0); - sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_SHA1 ]; - CPPUNIT_ASSERT( pBuffer ); - memset(pBuffer, 0, RTL_DIGEST_LENGTH_HMAC_SHA1); + const sal_uInt8* pData = (const sal_uInt8*)sSampleString.getStr(); + sal_uInt32 nSize = sSampleString.getLength(); - sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); - sal_Int32 nLen = aMsg1.getLength(); + aError = rtl_digest_updateMD2(aHandle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("handle parameter 'pData' wrong", aError == rtl_Digest_E_Argument); - rtlDigestError aError = rtl_digest_HMAC_SHA1(pKeyBuffer, RTL_DIGEST_LENGTH_HMAC_SHA1, pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_HMAC_SHA1); + aError = rtl_digest_updateMD2(aHandle, pData, 0); + CPPUNIT_ASSERT_MESSAGE("handle parameter 'nSize' wrong", aError == rtl_Digest_E_None); - CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); + rtl_digest_destroyMD2(aHandle); - rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_HMAC_SHA1); - printf("Decrypt HMAC_SHA1: %s\n", aStr.getStr()); - CPPUNIT_ASSERT_MESSAGE("md5sum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_HMAC_SHA1) ); + // use wrong Algorithm!!! This is volitional! + aHandle = rtl_digest_create(rtl_Digest_AlgorithmMD2); + CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != 0); - delete [] pBuffer; - } + aError = rtl_digest_updateMD5(aHandle, pData, nSize); + CPPUNIT_ASSERT_MESSAGE("handle parameter 'handle' wrong", aError == rtl_Digest_E_Algorithm); - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. + rtl_digest_destroyMD5(aHandle); - CPPUNIT_TEST_SUITE(digest_HMAC_SHA1); - CPPUNIT_TEST( HMAC_SHA1_001 ); - CPPUNIT_TEST_SUITE_END(); -}; // class create + aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); + CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", aHandle != 0); -class digest_PBKDF2 : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { - } - - void tearDown() - { - } - - - rtl::OString /* key */ run_check_PBKDF2(rtl::OString const& _sPassword, bool _bClearSalt, sal_uInt32 _nCount) - { - sal_uInt32 nKeyLen = RTL_DIGEST_LENGTH_HMAC_SHA1; - sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; - CPPUNIT_ASSERT( pKeyBuffer ); - memset(pKeyBuffer, 0, nKeyLen); - - sal_uInt8 *pPassword = (sal_uInt8*)_sPassword.getStr(); - sal_Int32 nPasswordLen = _sPassword.getLength(); - - sal_uInt32 nSaltDataLen = RTL_DIGEST_LENGTH_HMAC_SHA1; - sal_uInt8 *pSaltData = new sal_uInt8[ nSaltDataLen ]; - CPPUNIT_ASSERT( pSaltData ); - memset(pSaltData, 0, nSaltDataLen); - - if (! _bClearSalt) - { - // wilful contamination - pSaltData[0] = 1; - } - - rtlDigestError aError = rtl_digest_PBKDF2(pKeyBuffer, nKeyLen, pPassword, nPasswordLen, pSaltData, nSaltDataLen, _nCount); - - CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); - - rtl::OString aKey = createHex(pKeyBuffer, nKeyLen); - printf("Key: %s\n", aKey.getStr()); - - // rtl::OString sSalt = createHex(pSaltData, nSaltDataLen); - // printf("Salt: %s\n", sSalt.getStr()); + aError = rtl_digest_updateMD5(aHandle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("handle parameter 'pData' wrong", aError == rtl_Digest_E_Argument); - // CPPUNIT_ASSERT_MESSAGE("md5sum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_PBKDF2) ); + aError = rtl_digest_updateMD5(aHandle, pData, 0); + CPPUNIT_ASSERT_MESSAGE("handle parameter 'nSize' wrong", aError == rtl_Digest_E_None); - delete [] pSaltData; - delete [] pKeyBuffer; - return aKey; - } - - void PBKDF2_001() - { - rtl::OString aPassword = "Password"; - - // all permutations - run_check_PBKDF2(aPassword, false, 1); - run_check_PBKDF2(aPassword, false, 2); - run_check_PBKDF2(aPassword, true, 1); - run_check_PBKDF2(aPassword, true, 2); - run_check_PBKDF2(aPassword, false, 3); - run_check_PBKDF2(aPassword, false, 4); - run_check_PBKDF2(aPassword, true, 3); - run_check_PBKDF2(aPassword, true, 4); - } - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(digest_PBKDF2); - CPPUNIT_TEST( PBKDF2_001 ); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - -class update : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { + rtl_digest_destroyMD5(aHandle); } - void tearDown() + void testGet() { - } - - void update_000() - { - rtlDigest aHandle = NULL; - rtlDigestError aError = rtl_digest_update(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); - } - - void updateMD2_000() - { - rtlDigest aHandle = NULL; - rtlDigestError aError = rtl_digest_updateMD2(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); - } - - void updateMD2_001() - { - rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != 0); - - rtl::OString aMsg = sSampleString; - const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr(); - - rtlDigestError aError = rtl_digest_updateMD2(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("handle parameter 'pData' wrong", aError == rtl_Digest_E_Argument ); - - /* rtlDigestError */ aError = rtl_digest_updateMD2(aHandle, pData, 0); - CPPUNIT_ASSERT_MESSAGE("handle parameter 'nSize' wrong", aError == rtl_Digest_E_None ); - - rtl_digest_destroyMD2(aHandle); - } - void updateMD5_000() - { - rtlDigest aHandle = NULL; - rtlDigestError aError = rtl_digest_updateMD5(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); - } - - void updateMD5_001() - { - // use wrong Algorithm!!! This is volitional! - rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != 0); - - rtl::OString aMsg = sSampleString; - const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr(); - sal_uInt32 nSize = ( aMsg.getLength() ); - - rtlDigestError aError = rtl_digest_updateMD5(aHandle, pData, nSize); - CPPUNIT_ASSERT_MESSAGE("handle parameter 'handle' wrong", aError == rtl_Digest_E_Algorithm ); - - rtl_digest_destroyMD5(aHandle); - } + rtlDigest aHandle; + rtlDigestError aError; - void updateMD5_002() - { - rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", aHandle != 0); + aHandle = NULL; + aError = rtl_digest_get(aHandle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument); - rtl::OString aMsg = sSampleString; - const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr(); + aHandle = NULL; + aError = rtl_digest_getMD5(aHandle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument); - rtlDigestError aError = rtl_digest_updateMD5(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("handle parameter 'pData' wrong", aError == rtl_Digest_E_Argument ); + // test with wrong algorithm + aHandle = rtl_digest_create(rtl_Digest_AlgorithmMD2); + CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != 0); - /* rtlDigestError */ aError = rtl_digest_updateMD5(aHandle, pData, 0); - CPPUNIT_ASSERT_MESSAGE("handle parameter 'nSize' wrong", aError == rtl_Digest_E_None ); + sal_uInt32 nKeyLen = rtl_digest_queryLength(aHandle); + boost::scoped_array<sal_uInt8> pKeyBuffer(new sal_uInt8[nKeyLen]); - rtl_digest_destroyMD5(aHandle); - } + aError = rtl_digest_getMD5(aHandle, NULL, 0); + CPPUNIT_ASSERT_MESSAGE("handle 2. parameter wrong", aError == rtl_Digest_E_Argument); - void updateSHA_000() - { - rtlDigest aHandle = NULL; - rtlDigestError aError = rtl_digest_updateSHA(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); - } + aError = rtl_digest_getMD5(aHandle, pKeyBuffer.get(), 0); + CPPUNIT_ASSERT_MESSAGE("handle parameter 'handle' wrong", aError == rtl_Digest_E_Algorithm); - void updateSHA1_000() - { - rtlDigest aHandle = NULL; - rtlDigestError aError = rtl_digest_updateSHA1(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); - } + rtl_digest_destroyMD2(aHandle); - void updateHMAC_MD5_000() - { - rtlDigest aHandle = NULL; - rtlDigestError aError = rtl_digest_updateHMAC_MD5(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); - } + aHandle = rtl_digest_create(rtl_Digest_AlgorithmMD5); + CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", aHandle != 0); - void updateHMAC_SHA1_000() - { - rtlDigest aHandle = NULL; - rtlDigestError aError = rtl_digest_updateHMAC_SHA1(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); - } - - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(update); - CPPUNIT_TEST(update_000); - CPPUNIT_TEST(updateMD5_000); - CPPUNIT_TEST(updateMD5_001); - CPPUNIT_TEST(updateMD5_002); - CPPUNIT_TEST(updateMD5_000); - CPPUNIT_TEST(updateSHA_000); - CPPUNIT_TEST(updateSHA1_000); - CPPUNIT_TEST(updateHMAC_MD5_000); - CPPUNIT_TEST(updateHMAC_SHA1_000); - CPPUNIT_TEST_SUITE_END(); -}; // class create + aError = rtl_digest_getMD5(aHandle, NULL, nKeyLen); + CPPUNIT_ASSERT_MESSAGE("handle parameter 'pData' wrong", aError == rtl_Digest_E_Argument ); + aError = rtl_digest_getMD5(aHandle, pKeyBuffer.get(), 0); + CPPUNIT_ASSERT_MESSAGE("handle parameter 'nSize' wrong", aError == rtl_Digest_E_BufferSize ); -class get : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { + rtl_digest_destroyMD5(aHandle); } - void tearDown() - { - } - void get_000() - { - rtlDigest aHandle = NULL; - rtlDigestError aError = rtl_digest_get(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); - } - void getMD5_000() - { - rtlDigest aHandle = NULL; - rtlDigestError aError = rtl_digest_getMD5(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); - } - void getMD5_001() - { - // test with wrong algorithm - rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != 0); - - sal_uInt32 nKeyLen = rtl_digest_queryLength( aHandle ); - sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; - - rtlDigestError aError = rtl_digest_getMD5(aHandle, NULL, 0); - CPPUNIT_ASSERT_MESSAGE("handle 2. parameter wrong", aError == rtl_Digest_E_Argument ); - - /* rtlDigestError */ aError = rtl_digest_getMD5(aHandle, pKeyBuffer, 0); - CPPUNIT_ASSERT_MESSAGE("handle parameter 'handle' wrong", aError == rtl_Digest_E_Algorithm ); - - rtl_digest_destroyMD2(aHandle); - } - - void getMD5_002() - { - rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", aHandle != 0); - - sal_uInt32 nKeyLen = rtl_digest_queryLength( aHandle ); - sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; - - rtlDigestError aError = rtl_digest_getMD5(aHandle, NULL /* pKeyBuffer */ , nKeyLen); - CPPUNIT_ASSERT_MESSAGE("handle parameter 'pData' wrong", aError == rtl_Digest_E_Argument ); - - /* rtlDigestError */ aError = rtl_digest_getMD5(aHandle, pKeyBuffer, 0); - CPPUNIT_ASSERT_MESSAGE("handle parameter 'nSize' wrong", aError == rtl_Digest_E_BufferSize ); - - rtl_digest_destroyMD5(aHandle); - delete [] pKeyBuffer; - } + CPPUNIT_TEST_SUITE(DigestTest); + CPPUNIT_TEST(testCreate); + CPPUNIT_TEST(testQuery); + CPPUNIT_TEST(testQueryLength); + CPPUNIT_TEST(testInit); + CPPUNIT_TEST(testEqual); + CPPUNIT_TEST(testCheckSum); + CPPUNIT_TEST(testPBKDF2); + CPPUNIT_TEST(testUpdate); + CPPUNIT_TEST(testGet); - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(get); - CPPUNIT_TEST(get_000); - CPPUNIT_TEST(getMD5_000); - CPPUNIT_TEST(getMD5_001); - CPPUNIT_TEST(getMD5_002); CPPUNIT_TEST_SUITE_END(); -}; // class create - - -class destroy : public CppUnit::TestFixture -{ -public: - // initialise your test code values here. - void setUp() - { - } - - void tearDown() - { - } - - void destroy_001() - { - rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); - CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", handle != 0); +}; - // not really testable - // LLA: good will test. - rtl_digest_destroy( handle ); - } +CPPUNIT_TEST_SUITE_REGISTRATION(DigestTest); - // Change the following lines only, if you add, remove or rename - // member functions of the current class, - // because these macros are need by auto register mechanism. - - CPPUNIT_TEST_SUITE(destroy); - CPPUNIT_TEST(destroy_001); - CPPUNIT_TEST_SUITE_END(); -}; // class create - - -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::create); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::createMD2); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::createMD5); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::createSHA); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::createSHA1); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::createHMAC_MD5); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::createHMAC_SHA1); - -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::destroy); - -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::queryAlgorithm); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::queryLength); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::init); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::equalTests); - -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::digest_MD2); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::digest_MD5); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::digest_SHA); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::digest_SHA1); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::digest_HMAC_MD5); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::digest_HMAC_SHA1); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::digest_PBKDF2); - -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::update); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::get); -CPPUNIT_TEST_SUITE_REGISTRATION(rtl_digest::destroy); } // namespace rtl_digest CPPUNIT_PLUGIN_IMPLEMENT(); |