1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 29 // MARKER(update_precomp.py): autogen include statement, do not remove 30 #include "precompiled_sal.hxx" 31 #include <testshl/simpleheader.hxx> 32 33 #include <rtl/digest.h> 34 #include <rtl/ustring.hxx> 35 #include <rtl/ustrbuf.hxx> 36 #include <rtl/strbuf.hxx> 37 38 // sample, how to use digest 39 40 rtl::OUString CreateMD5FromString( const rtl::OUString& aMsg ) 41 { 42 // PRE: aStr "file" 43 // BACK: Str "ababab....0f" Hexcode String 44 45 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 46 if ( handle > 0 ) 47 { 48 const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr(); 49 sal_uInt32 nSize = ( aMsg.getLength() * sizeof( sal_Unicode )); 50 sal_uInt32 nMD5KeyLen = rtl_digest_queryLength( handle ); 51 sal_uInt8* pMD5KeyBuffer = new sal_uInt8[ nMD5KeyLen ]; 52 53 rtl_digest_init( handle, pData, nSize ); 54 rtl_digest_update( handle, pData, nSize ); 55 rtl_digest_get( handle, pMD5KeyBuffer, nMD5KeyLen ); 56 rtl_digest_destroy( handle ); 57 58 // Create hex-value string from the MD5 value to keep the string size minimal 59 rtl::OUStringBuffer aBuffer( nMD5KeyLen * 2 + 1 ); 60 for ( sal_uInt32 i = 0; i < nMD5KeyLen; i++ ) 61 aBuffer.append( (sal_Int32)pMD5KeyBuffer[i], 16 ); 62 63 delete [] pMD5KeyBuffer; 64 return aBuffer.makeStringAndClear(); 65 } 66 67 return rtl::OUString(); 68 } 69 70 // ----------------------------------------------------------------------------- 71 namespace rtl_digest 72 { 73 74 rtl::OString sSampleString = "This is a sample sentence, which we use to check some crypto functions in sal."; 75 rtl::OString sSampleString_MD2 = "647ee6c9d4aa5fdd374ed9d7a156acbf"; 76 rtl::OString sSampleString_MD5 = "b16b903e6fc0b62ae389013ed93fe531"; 77 rtl::OString sSampleString_SHA = "eab2814429b2613301c8a077b806af3680548914"; 78 rtl::OString sSampleString_SHA1 = "2bc5bdb7506a2cdc2fd27fc8b9889343012d5008"; 79 rtl::OString sSampleString_HMAC_MD5 = "dd9cba48c972fba0a882baa72b079674"; 80 rtl::OString sSampleString_HMAC_SHA1 = "5d7f43ce6abd1de4438d7e69e01495864490cf3e"; 81 82 rtl::OString sSampleString_only_one_diff = "This is a sample sentence. which we use to check some crypto functions in sal."; 83 84 class create : public CppUnit::TestFixture 85 { 86 public: 87 // initialise your test code values here. 88 void setUp() 89 { 90 } 91 92 void tearDown() 93 { 94 } 95 96 void create_001() 97 { 98 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 99 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", handle != 0); 100 rtl_digest_destroy( handle ); 101 } 102 void create_002() 103 { 104 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); 105 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", handle != 0); 106 rtl_digest_destroy( handle ); 107 } 108 void create_003() 109 { 110 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA ); 111 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmSHA", handle != 0); 112 rtl_digest_destroy( handle ); 113 } 114 void create_004() 115 { 116 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 ); 117 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmSHA1", handle != 0); 118 rtl_digest_destroy( handle ); 119 } 120 void create_005() 121 { 122 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 ); 123 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmHMAC_MD5", handle != 0); 124 rtl_digest_destroy( handle ); 125 } 126 void create_006() 127 { 128 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 ); 129 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmHMAC_SHA1", handle != 0); 130 rtl_digest_destroy( handle ); 131 } 132 133 void create_007() 134 { 135 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid ); 136 t_print("Handle is %x\n", handle); 137 CPPUNIT_ASSERT_MESSAGE("create with NULL", handle == 0); 138 rtl_digest_destroy( handle ); 139 } 140 141 // Change the following lines only, if you add, remove or rename 142 // member functions of the current class, 143 // because these macros are need by auto register mechanism. 144 145 CPPUNIT_TEST_SUITE(create); 146 CPPUNIT_TEST(create_001); 147 CPPUNIT_TEST(create_002); 148 CPPUNIT_TEST(create_003); 149 CPPUNIT_TEST(create_004); 150 CPPUNIT_TEST(create_005); 151 CPPUNIT_TEST(create_006); 152 CPPUNIT_TEST(create_007); 153 CPPUNIT_TEST_SUITE_END(); 154 }; // class create 155 156 157 158 159 // ----------------------------------------------------------------------------- 160 161 class createMD5 : public CppUnit::TestFixture 162 { 163 public: 164 // initialise your test code values here. 165 void setUp() 166 { 167 } 168 169 void tearDown() 170 { 171 } 172 173 void createMD5_001() 174 { 175 rtlDigest handle = rtl_digest_createMD5(); 176 177 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 178 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmMD5 == aAlgo); 179 180 rtl_digest_destroy( handle ); 181 } 182 CPPUNIT_TEST_SUITE(createMD5); 183 CPPUNIT_TEST(createMD5_001); 184 CPPUNIT_TEST_SUITE_END(); 185 }; // class create 186 187 188 // ----------------------------------------------------------------------------- 189 190 class createMD2 : public CppUnit::TestFixture 191 { 192 public: 193 // initialise your test code values here. 194 void setUp() 195 { 196 } 197 198 void tearDown() 199 { 200 } 201 202 void createMD2_001() 203 { 204 rtlDigest handle = rtl_digest_createMD2( ); 205 206 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 207 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmMD2 == aAlgo); 208 209 rtl_digest_destroy( handle ); 210 } 211 CPPUNIT_TEST_SUITE(createMD2); 212 CPPUNIT_TEST(createMD2_001); 213 CPPUNIT_TEST_SUITE_END(); 214 }; // class create 215 216 // ----------------------------------------------------------------------------- 217 218 class createSHA : public CppUnit::TestFixture 219 { 220 public: 221 // initialise your test code values here. 222 void setUp() 223 { 224 } 225 226 void tearDown() 227 { 228 } 229 230 void createSHA_001() 231 { 232 rtlDigest handle = rtl_digest_createSHA( ); 233 234 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 235 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmSHA == aAlgo); 236 237 rtl_digest_destroy( handle ); 238 } 239 CPPUNIT_TEST_SUITE(createSHA); 240 CPPUNIT_TEST(createSHA_001); 241 CPPUNIT_TEST_SUITE_END(); 242 }; // class create 243 // ----------------------------------------------------------------------------- 244 245 class createSHA1 : public CppUnit::TestFixture 246 { 247 public: 248 // initialise your test code values here. 249 void setUp() 250 { 251 } 252 253 void tearDown() 254 { 255 } 256 257 void createSHA1_001() 258 { 259 rtlDigest handle = rtl_digest_createSHA1(); 260 261 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 262 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmSHA1 == aAlgo); 263 264 rtl_digest_destroy( handle ); 265 } 266 CPPUNIT_TEST_SUITE(createSHA1); 267 CPPUNIT_TEST(createSHA1_001); 268 CPPUNIT_TEST_SUITE_END(); 269 }; // class create 270 // ----------------------------------------------------------------------------- 271 272 class createHMAC_MD5 : public CppUnit::TestFixture 273 { 274 public: 275 // initialise your test code values here. 276 void setUp() 277 { 278 } 279 280 void tearDown() 281 { 282 } 283 284 void createHMAC_MD5_001() 285 { 286 rtlDigest handle = rtl_digest_createHMAC_MD5(); 287 288 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 289 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmHMAC_MD5 == aAlgo); 290 291 rtl_digest_destroy( handle ); 292 } 293 CPPUNIT_TEST_SUITE(createHMAC_MD5); 294 CPPUNIT_TEST(createHMAC_MD5_001); 295 CPPUNIT_TEST_SUITE_END(); 296 }; // class create 297 // ----------------------------------------------------------------------------- 298 299 class createHMAC_SHA1 : public CppUnit::TestFixture 300 { 301 public: 302 // initialise your test code values here. 303 void setUp() 304 { 305 } 306 307 void tearDown() 308 { 309 } 310 311 void createHMAC_SHA1_001() 312 { 313 rtlDigest handle = rtl_digest_createHMAC_SHA1(); 314 315 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 316 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmHMAC_SHA1 == aAlgo); 317 318 rtl_digest_destroy( handle ); 319 } 320 321 322 CPPUNIT_TEST_SUITE(createHMAC_SHA1); 323 CPPUNIT_TEST(createHMAC_SHA1_001); 324 CPPUNIT_TEST_SUITE_END(); 325 }; // class create 326 327 // ----------------------------------------------------------------------------- 328 329 class queryAlgorithm : public CppUnit::TestFixture 330 { 331 public: 332 // initialise your test code values here. 333 void setUp() 334 { 335 } 336 337 void tearDown() 338 { 339 } 340 341 void query_001() 342 { 343 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 344 345 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 346 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmMD5 == aAlgo); 347 348 rtl_digest_destroy( handle ); 349 } 350 void query_002() 351 { 352 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); 353 354 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 355 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmMD2 == aAlgo); 356 357 rtl_digest_destroy( handle ); 358 } 359 void query_003() 360 { 361 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA ); 362 363 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 364 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmSHA == aAlgo); 365 366 rtl_digest_destroy( handle ); 367 } 368 void query_004() 369 { 370 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 ); 371 372 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 373 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmSHA1 == aAlgo); 374 375 rtl_digest_destroy( handle ); 376 } 377 void query_005() 378 { 379 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 ); 380 381 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 382 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmHMAC_MD5 == aAlgo); 383 384 rtl_digest_destroy( handle ); 385 } 386 void query_006() 387 { 388 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 ); 389 390 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 391 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmHMAC_SHA1 == aAlgo); 392 393 rtl_digest_destroy( handle ); 394 } 395 void query_007() 396 { 397 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid ); 398 399 rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle); 400 CPPUNIT_ASSERT_MESSAGE("query handle", rtl_Digest_AlgorithmInvalid == aAlgo); 401 402 rtl_digest_destroy( handle ); 403 } 404 405 // Change the following lines only, if you add, remove or rename 406 // member functions of the current class, 407 // because these macros are need by auto register mechanism. 408 409 CPPUNIT_TEST_SUITE(queryAlgorithm); 410 CPPUNIT_TEST( query_001 ); 411 CPPUNIT_TEST( query_002 ); 412 CPPUNIT_TEST( query_003 ); 413 CPPUNIT_TEST( query_004 ); 414 CPPUNIT_TEST( query_005 ); 415 CPPUNIT_TEST( query_006 ); 416 CPPUNIT_TEST( query_007 ); 417 CPPUNIT_TEST_SUITE_END(); 418 }; // class create 419 420 421 // ----------------------------------------------------------------------------- 422 class queryLength : public CppUnit::TestFixture 423 { 424 public: 425 // initialise your test code values here. 426 void setUp() 427 { 428 } 429 430 void tearDown() 431 { 432 } 433 434 void queryLength_MD5() 435 { 436 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 437 438 sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); 439 // t_print("nAlgoLength:=%d\n", nAlgoLength); 440 CPPUNIT_ASSERT_MESSAGE("query Length", RTL_DIGEST_LENGTH_MD5 == nAlgoLength); 441 442 rtl_digest_destroy( handle ); 443 } 444 void queryLength_MD2() 445 { 446 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); 447 448 sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); 449 // t_print("nAlgoLength:=%d\n", nAlgoLength); 450 CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_MD2 == nAlgoLength); 451 452 rtl_digest_destroy( handle ); 453 } 454 void queryLength_SHA() 455 { 456 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA ); 457 458 sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); 459 // t_print("nAlgoLength:=%d\n", nAlgoLength); 460 CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_SHA == nAlgoLength); 461 462 rtl_digest_destroy( handle ); 463 } 464 void queryLength_SHA1() 465 { 466 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 ); 467 468 sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); 469 // t_print("nAlgoLength:=%d\n", nAlgoLength); 470 CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_SHA1 == nAlgoLength); 471 472 rtl_digest_destroy( handle ); 473 } 474 void queryLength_HMAC_MD5() 475 { 476 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 ); 477 478 sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); 479 // t_print("nAlgoLength:=%d\n", nAlgoLength); 480 CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_HMAC_MD5 == nAlgoLength); 481 482 rtl_digest_destroy( handle ); 483 } 484 void queryLength_HMAC_SHA1() 485 { 486 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 ); 487 488 sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); 489 // t_print("nAlgoLength:=%d\n", nAlgoLength); 490 CPPUNIT_ASSERT_MESSAGE("query length", RTL_DIGEST_LENGTH_HMAC_SHA1 == nAlgoLength); 491 492 rtl_digest_destroy( handle ); 493 } 494 495 void queryLength_Illegal() 496 { 497 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid ); 498 499 sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle); 500 // t_print("nAlgoLength:=%d\n", nAlgoLength); 501 CPPUNIT_ASSERT_MESSAGE("query length", 0 == nAlgoLength); 502 503 rtl_digest_destroy( handle ); 504 } 505 506 // Change the following lines only, if you add, remove or rename 507 // member functions of the current class, 508 // because these macros are need by auto register mechanism. 509 510 CPPUNIT_TEST_SUITE(queryLength); 511 CPPUNIT_TEST( queryLength_MD2 ); 512 CPPUNIT_TEST( queryLength_MD5 ); 513 CPPUNIT_TEST( queryLength_SHA ); 514 CPPUNIT_TEST( queryLength_SHA1 ); 515 CPPUNIT_TEST( queryLength_HMAC_MD5 ); 516 CPPUNIT_TEST( queryLength_HMAC_SHA1 ); 517 CPPUNIT_TEST( queryLength_Illegal ); 518 CPPUNIT_TEST_SUITE_END(); 519 }; // class create 520 521 // ----------------------------------------------------------------------------- 522 523 rtl::OString createHex(sal_uInt8 *_pMD5KeyBuffer, sal_uInt32 _nMD5KeyLen) 524 { 525 // Create hex-value string from the MD5 value to keep the string size minimal 526 rtl::OStringBuffer aBuffer( _nMD5KeyLen * 2 + 1 ); 527 for ( sal_uInt32 i = 0; i < _nMD5KeyLen; i++ ) 528 { 529 sal_Int32 nValue = (sal_Int32)_pMD5KeyBuffer[i]; 530 if (nValue < 16) // maximul hex value for 1 byte 531 { 532 aBuffer.append( sal_Int32(0), 16 /* radix */ ); 533 } 534 aBuffer.append( nValue, 16 /* radix */ ); 535 } 536 537 return aBuffer.makeStringAndClear(); 538 } 539 540 541 // ----------------------------------------------------------------------------- 542 class init : public CppUnit::TestFixture 543 { 544 public: 545 // initialise your test code values here. 546 void setUp() 547 { 548 } 549 550 void tearDown() 551 { 552 } 553 554 void init_000() 555 { 556 rtlDigest handle = NULL; 557 558 rtlDigestError aError = rtl_digest_init(handle, NULL, 0); 559 560 CPPUNIT_ASSERT_MESSAGE("init(NULL, 0, 0)", aError == rtl_Digest_E_Argument); 561 } 562 563 void init_001() 564 { 565 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 566 567 rtlDigestError aError = rtl_digest_init(handle, NULL, 0); 568 569 CPPUNIT_ASSERT_MESSAGE("init(handle, 0, 0)", aError == rtl_Digest_E_None); 570 571 rtl_digest_destroy( handle ); 572 } 573 574 // ------------------------------------ 575 void init_MD2() 576 { 577 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); 578 579 rtl::OString aMsg = sSampleString; 580 const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); 581 sal_uInt32 nSize = ( aMsg.getLength() ); 582 583 rtlDigestError aError = rtl_digest_init(handle, pData, nSize); 584 585 CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); 586 587 rtl_digest_update( handle, pData, nSize ); 588 589 sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); 590 sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; 591 592 rtl_digest_get( handle, pKeyBuffer, nKeyLen ); 593 rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); 594 delete [] pKeyBuffer; 595 596 t_print("MD2 Sum: %s\n", aSum.getStr()); 597 // LLA: how to check right values 598 // samples? 599 600 rtl_digest_destroy( handle ); 601 } 602 603 void init_MD5() 604 { 605 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 606 607 rtl::OString aMsg = sSampleString; 608 const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); 609 sal_uInt32 nSize = ( aMsg.getLength() ); 610 611 rtlDigestError aError = rtl_digest_init(handle, pData, nSize); 612 613 CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); 614 615 rtl_digest_update( handle, pData, nSize ); 616 617 sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); 618 sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; 619 620 rtl_digest_get( handle, pKeyBuffer, nKeyLen ); 621 rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); 622 delete [] pKeyBuffer; 623 624 t_print("MD5 Sum: %s\n", aSum.getStr()); 625 // LLA: how to check right values 626 // samples? 627 628 rtl_digest_destroy( handle ); 629 } 630 631 void init_SHA() 632 { 633 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA ); 634 635 rtl::OString aMsg = sSampleString; 636 const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); 637 sal_uInt32 nSize = ( aMsg.getLength() ); 638 639 rtlDigestError aError = rtl_digest_init(handle, pData, nSize); 640 641 CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); 642 643 rtl_digest_update( handle, pData, nSize ); 644 645 sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); 646 sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; 647 648 rtl_digest_get( handle, pKeyBuffer, nKeyLen ); 649 rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); 650 delete [] pKeyBuffer; 651 652 t_print("SHA Sum: %s\n", aSum.getStr()); 653 // LLA: how to check right values 654 // samples? 655 656 rtl_digest_destroy( handle ); 657 } 658 void init_SHA1() 659 { 660 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 ); 661 662 rtl::OString aMsg = sSampleString; 663 const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); 664 sal_uInt32 nSize = ( aMsg.getLength() ); 665 666 rtlDigestError aError = rtl_digest_init(handle, pData, nSize); 667 668 CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); 669 670 rtl_digest_update( handle, pData, nSize ); 671 672 sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); 673 sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; 674 675 rtl_digest_get( handle, pKeyBuffer, nKeyLen ); 676 rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); 677 delete [] pKeyBuffer; 678 679 t_print("SHA1 Sum: %s\n", aSum.getStr()); 680 // LLA: how to check right values 681 // samples? 682 683 rtl_digest_destroy( handle ); 684 } 685 void init_HMAC_MD5() 686 { 687 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 ); 688 689 rtl::OString aMsg = sSampleString; 690 const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); 691 sal_uInt32 nSize = ( aMsg.getLength() ); 692 693 sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); 694 CPPUNIT_ASSERT_MESSAGE( "Keylen must be greater 0", nKeyLen ); 695 696 sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; 697 CPPUNIT_ASSERT( pKeyBuffer ); 698 memset(pKeyBuffer, 0, nKeyLen); 699 700 rtlDigestError aError = rtl_digest_init(handle, pKeyBuffer, nKeyLen ); 701 702 CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); 703 704 rtl_digest_update( handle, pData, nSize ); 705 706 rtl_digest_get( handle, pKeyBuffer, nKeyLen ); 707 rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); 708 delete [] pKeyBuffer; 709 710 t_print("HMAC_MD5 Sum: %s\n", aSum.getStr()); 711 // LLA: how to check right values 712 // samples? 713 714 rtl_digest_destroy( handle ); 715 } 716 void init_HMAC_SHA1() 717 { 718 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 ); 719 720 rtl::OString aMsg = sSampleString; 721 const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr(); 722 sal_uInt32 nSize = ( aMsg.getLength() ); 723 724 sal_uInt32 nKeyLen = rtl_digest_queryLength( handle ); 725 CPPUNIT_ASSERT_MESSAGE( "Keylen must be greater 0", nKeyLen ); 726 727 sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; 728 CPPUNIT_ASSERT( pKeyBuffer ); 729 memset(pKeyBuffer, 0, nKeyLen); 730 731 rtlDigestError aError = rtl_digest_init(handle, pKeyBuffer, nKeyLen ); 732 733 CPPUNIT_ASSERT_MESSAGE("init(handle, pData, nSize)", aError == rtl_Digest_E_None); 734 735 rtl_digest_update( handle, pData, nSize ); 736 737 rtl_digest_get( handle, pKeyBuffer, nKeyLen ); 738 rtl::OString aSum = createHex(pKeyBuffer, nKeyLen); 739 delete [] pKeyBuffer; 740 741 t_print("HMAC_SHA1 Sum: %s\n", aSum.getStr()); 742 // LLA: how to check right values 743 // samples? 744 745 rtl_digest_destroy( handle ); 746 } 747 748 749 // Change the following lines only, if you add, remove or rename 750 // member functions of the current class, 751 // because these macros are need by auto register mechanism. 752 753 CPPUNIT_TEST_SUITE(init); 754 CPPUNIT_TEST( init_000 ); 755 CPPUNIT_TEST( init_001 ); 756 CPPUNIT_TEST( init_MD2 ); 757 CPPUNIT_TEST( init_MD5 ); 758 CPPUNIT_TEST( init_SHA ); 759 CPPUNIT_TEST( init_SHA1 ); 760 CPPUNIT_TEST( init_HMAC_MD5 ); 761 CPPUNIT_TEST( init_HMAC_SHA1 ); 762 CPPUNIT_TEST_SUITE_END(); 763 }; // class init 764 765 // ------------------------------------ 766 767 rtl::OString getMD5Sum(rtl::OString const& _aMsg ) 768 { 769 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 770 771 const sal_uInt8 *pData = (const sal_uInt8*)_aMsg.getStr(); 772 sal_uInt32 nSize = ( _aMsg.getLength() ); 773 774 rtl_digest_init(handle, pData, nSize); 775 rtl_digest_update( handle, pData, nSize ); 776 777 sal_uInt32 nMD5KeyLen = rtl_digest_queryLength( handle ); 778 sal_uInt8 *pMD5KeyBuffer = new sal_uInt8[ nMD5KeyLen ]; 779 780 rtl_digest_get( handle, pMD5KeyBuffer, nMD5KeyLen ); 781 rtl::OString aMD5Sum = createHex(pMD5KeyBuffer, nMD5KeyLen); 782 delete [] pMD5KeyBuffer; 783 784 rtl_digest_destroy( handle ); 785 return aMD5Sum; 786 } 787 788 // ----------------------------------------------------------------------------- 789 790 class equalTests : public CppUnit::TestFixture 791 { 792 public: 793 // initialise your test code values here. 794 void setUp() 795 { 796 } 797 798 void tearDown() 799 { 800 } 801 802 // ------------------------------------ 803 void equal_001() 804 { 805 rtl::OString aMsg1 = sSampleString; 806 rtl::OString aMsg2 = sSampleString; 807 808 rtl::OString aMsgMD5Sum1 = getMD5Sum(aMsg1); 809 rtl::OString aMsgMD5Sum2 = getMD5Sum(aMsg2); 810 811 CPPUNIT_ASSERT_MESSAGE("md5sum must have a length", aMsgMD5Sum1.getLength() == 32 && aMsgMD5Sum2.getLength() == 32 ); 812 CPPUNIT_ASSERT_MESSAGE("source is the same, dest must be also the same", aMsgMD5Sum1.equals(aMsgMD5Sum2) == sal_True); 813 } 814 // ------------------------------------ 815 void equal_002() 816 { 817 rtl::OString aMsg1 = sSampleString; 818 rtl::OString aMsg2 = sSampleString_only_one_diff; 819 820 rtl::OString aMsgMD5Sum1 = getMD5Sum(aMsg1); 821 rtl::OString aMsgMD5Sum2 = getMD5Sum(aMsg2); 822 823 CPPUNIT_ASSERT_MESSAGE("md5sum must have a length", aMsgMD5Sum1.getLength() == 32 && aMsgMD5Sum2.getLength() == 32 ); 824 CPPUNIT_ASSERT_MESSAGE("differ only in one char", aMsgMD5Sum1.equals(aMsgMD5Sum2) == sal_False); 825 } 826 827 // Change the following lines only, if you add, remove or rename 828 // member functions of the current class, 829 // because these macros are need by auto register mechanism. 830 831 CPPUNIT_TEST_SUITE(equalTests); 832 CPPUNIT_TEST( equal_001 ); 833 CPPUNIT_TEST( equal_002 ); 834 CPPUNIT_TEST_SUITE_END(); 835 }; // class create 836 837 838 // ----------------------------------------------------------------------------- 839 class digest_MD2 : public CppUnit::TestFixture 840 { 841 public: 842 // initialise your test code values here. 843 void setUp() 844 { 845 } 846 847 void tearDown() 848 { 849 } 850 851 // ------------------------------------ 852 void MD2_001() 853 { 854 rtl::OString aMsg1 = sSampleString; 855 856 sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_MD2 ]; 857 CPPUNIT_ASSERT( pBuffer ); 858 memset(pBuffer, 0, RTL_DIGEST_LENGTH_MD2 ); 859 860 sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); 861 sal_Int32 nLen = aMsg1.getLength(); 862 863 rtlDigestError aError = rtl_digest_MD2(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_MD2); 864 865 CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); 866 867 rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_MD2); 868 t_print("Decrypt MD2: %s\n", aStr.getStr()); 869 CPPUNIT_ASSERT_MESSAGE("checksum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_MD2) ); 870 871 delete [] pBuffer; 872 } 873 874 // Change the following lines only, if you add, remove or rename 875 // member functions of the current class, 876 // because these macros are need by auto register mechanism. 877 878 CPPUNIT_TEST_SUITE(digest_MD2); 879 CPPUNIT_TEST( MD2_001 ); 880 CPPUNIT_TEST_SUITE_END(); 881 }; // class create 882 // ----------------------------------------------------------------------------- 883 class digest_MD5 : public CppUnit::TestFixture 884 { 885 public: 886 // initialise your test code values here. 887 void setUp() 888 { 889 } 890 891 void tearDown() 892 { 893 } 894 // ------------------------------------ 895 void MD5_001() 896 { 897 rtl::OString aMsg1 = sSampleString; 898 899 sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_MD5 ]; 900 CPPUNIT_ASSERT( pBuffer ); 901 memset(pBuffer, 0, RTL_DIGEST_LENGTH_MD5 ); 902 903 sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); 904 sal_Int32 nLen = aMsg1.getLength(); 905 906 rtlDigestError aError = rtl_digest_MD5(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_MD5); 907 908 CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); 909 910 rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_MD5); 911 t_print("Decrypt MD5: %s\n", aStr.getStr()); 912 CPPUNIT_ASSERT_MESSAGE("checksum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_MD5) ); 913 914 delete [] pBuffer; 915 } 916 917 // Change the following lines only, if you add, remove or rename 918 // member functions of the current class, 919 // because these macros are need by auto register mechanism. 920 921 CPPUNIT_TEST_SUITE(digest_MD5); 922 CPPUNIT_TEST( MD5_001 ); 923 CPPUNIT_TEST_SUITE_END(); 924 }; // class create 925 926 // ----------------------------------------------------------------------------- 927 class digest_SHA : public CppUnit::TestFixture 928 { 929 public: 930 // initialise your test code values here. 931 void setUp() 932 { 933 } 934 935 void tearDown() 936 { 937 } 938 939 // ------------------------------------ 940 void SHA_001() 941 { 942 rtl::OString aMsg1 = sSampleString; 943 944 sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_SHA ]; 945 CPPUNIT_ASSERT( pBuffer ); 946 memset(pBuffer, 0, RTL_DIGEST_LENGTH_SHA); 947 948 sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); 949 sal_Int32 nLen = aMsg1.getLength(); 950 951 rtlDigestError aError = rtl_digest_SHA(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_SHA); 952 953 CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); 954 955 rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_SHA); 956 t_print("Decrypt SHA: %s\n", aStr.getStr()); 957 CPPUNIT_ASSERT_MESSAGE("checksum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_SHA) ); 958 959 delete [] pBuffer; 960 } 961 962 // Change the following lines only, if you add, remove or rename 963 // member functions of the current class, 964 // because these macros are need by auto register mechanism. 965 966 CPPUNIT_TEST_SUITE(digest_SHA); 967 CPPUNIT_TEST( SHA_001 ); 968 CPPUNIT_TEST_SUITE_END(); 969 }; // class create 970 971 // ----------------------------------------------------------------------------- 972 class digest_SHA1 : public CppUnit::TestFixture 973 { 974 public: 975 // initialise your test code values here. 976 void setUp() 977 { 978 } 979 980 void tearDown() 981 { 982 } 983 984 // ------------------------------------ 985 void SHA1_001() 986 { 987 rtl::OString aMsg1 = sSampleString; 988 989 sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_SHA1 ]; 990 CPPUNIT_ASSERT( pBuffer ); 991 memset(pBuffer, 0, RTL_DIGEST_LENGTH_SHA1); 992 993 sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); 994 sal_Int32 nLen = aMsg1.getLength(); 995 996 rtlDigestError aError = rtl_digest_SHA1(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_SHA1); 997 998 CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); 999 1000 rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_SHA1); 1001 t_print("Decrypt SHA1: %s\n", aStr.getStr()); 1002 CPPUNIT_ASSERT_MESSAGE("checksum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_SHA1) ); 1003 1004 delete [] pBuffer; 1005 } 1006 // Change the following lines only, if you add, remove or rename 1007 // member functions of the current class, 1008 // because these macros are need by auto register mechanism. 1009 1010 CPPUNIT_TEST_SUITE(digest_SHA1); 1011 CPPUNIT_TEST( SHA1_001 ); 1012 CPPUNIT_TEST_SUITE_END(); 1013 }; // class create 1014 // ----------------------------------------------------------------------------- 1015 class digest_HMAC_MD5 : public CppUnit::TestFixture 1016 { 1017 public: 1018 // initialise your test code values here. 1019 void setUp() 1020 { 1021 } 1022 1023 void tearDown() 1024 { 1025 } 1026 1027 // ------------------------------------ 1028 void HMAC_MD5_001() 1029 { 1030 rtl::OString aMsg1 = sSampleString; 1031 1032 sal_uInt8 *pKeyBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_MD5 ]; 1033 CPPUNIT_ASSERT( pKeyBuffer ); 1034 memset(pKeyBuffer, 0, RTL_DIGEST_LENGTH_HMAC_MD5); 1035 1036 sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_MD5 ]; 1037 CPPUNIT_ASSERT( pBuffer ); 1038 memset(pBuffer, 0, RTL_DIGEST_LENGTH_HMAC_MD5); 1039 1040 sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); 1041 sal_Int32 nLen = aMsg1.getLength(); 1042 1043 rtlDigestError aError = rtl_digest_HMAC_MD5(pKeyBuffer, RTL_DIGEST_LENGTH_HMAC_MD5, pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_HMAC_MD5); 1044 1045 CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); 1046 1047 rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_HMAC_MD5); 1048 t_print("Decrypt HMAC_MD5: %s\n", aStr.getStr()); 1049 CPPUNIT_ASSERT_MESSAGE("md5sum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_HMAC_MD5) ); 1050 1051 delete [] pBuffer; 1052 } 1053 // Change the following lines only, if you add, remove or rename 1054 // member functions of the current class, 1055 // because these macros are need by auto register mechanism. 1056 1057 CPPUNIT_TEST_SUITE(digest_HMAC_MD5); 1058 CPPUNIT_TEST( HMAC_MD5_001 ); 1059 CPPUNIT_TEST_SUITE_END(); 1060 }; // class create 1061 // ----------------------------------------------------------------------------- 1062 class digest_HMAC_SHA1 : public CppUnit::TestFixture 1063 { 1064 public: 1065 // initialise your test code values here. 1066 void setUp() 1067 { 1068 } 1069 1070 void tearDown() 1071 { 1072 } 1073 1074 // ------------------------------------ 1075 void HMAC_SHA1_001() 1076 { 1077 rtl::OString aMsg1 = sSampleString; 1078 1079 sal_uInt8 *pKeyBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_SHA1 ]; 1080 CPPUNIT_ASSERT( pKeyBuffer ); 1081 memset(pKeyBuffer, 0, RTL_DIGEST_LENGTH_HMAC_SHA1); 1082 1083 sal_uInt8 *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_SHA1 ]; 1084 CPPUNIT_ASSERT( pBuffer ); 1085 memset(pBuffer, 0, RTL_DIGEST_LENGTH_HMAC_SHA1); 1086 1087 sal_uInt8 *pMsg1 = (sal_uInt8*)aMsg1.getStr(); 1088 sal_Int32 nLen = aMsg1.getLength(); 1089 1090 rtlDigestError aError = rtl_digest_HMAC_SHA1(pKeyBuffer, RTL_DIGEST_LENGTH_HMAC_SHA1, pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_HMAC_SHA1); 1091 1092 CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); 1093 1094 rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_HMAC_SHA1); 1095 t_print("Decrypt HMAC_SHA1: %s\n", aStr.getStr()); 1096 CPPUNIT_ASSERT_MESSAGE("md5sum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_HMAC_SHA1) ); 1097 1098 delete [] pBuffer; 1099 } 1100 1101 // Change the following lines only, if you add, remove or rename 1102 // member functions of the current class, 1103 // because these macros are need by auto register mechanism. 1104 1105 CPPUNIT_TEST_SUITE(digest_HMAC_SHA1); 1106 CPPUNIT_TEST( HMAC_SHA1_001 ); 1107 CPPUNIT_TEST_SUITE_END(); 1108 }; // class create 1109 // ----------------------------------------------------------------------------- 1110 class digest_PBKDF2 : public CppUnit::TestFixture 1111 { 1112 public: 1113 // initialise your test code values here. 1114 void setUp() 1115 { 1116 } 1117 1118 void tearDown() 1119 { 1120 } 1121 1122 // ------------------------------------ 1123 rtl::OString /* key */ run_check_PBKDF2(rtl::OString const& _sPassword, bool _bClearSalt, sal_uInt32 _nCount) 1124 { 1125 sal_uInt32 nKeyLen = RTL_DIGEST_LENGTH_HMAC_SHA1; 1126 sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; 1127 CPPUNIT_ASSERT( pKeyBuffer ); 1128 memset(pKeyBuffer, 0, nKeyLen); 1129 1130 sal_uInt8 *pPassword = (sal_uInt8*)_sPassword.getStr(); 1131 sal_Int32 nPasswordLen = _sPassword.getLength(); 1132 1133 sal_uInt32 nSaltDataLen = RTL_DIGEST_LENGTH_HMAC_SHA1; 1134 sal_uInt8 *pSaltData = new sal_uInt8[ nSaltDataLen ]; 1135 CPPUNIT_ASSERT( pSaltData ); 1136 memset(pSaltData, 0, nSaltDataLen); 1137 1138 if (! _bClearSalt) 1139 { 1140 // wilful contamination 1141 pSaltData[0] = 1; 1142 } 1143 1144 rtlDigestError aError = rtl_digest_PBKDF2(pKeyBuffer, nKeyLen, pPassword, nPasswordLen, pSaltData, nSaltDataLen, _nCount); 1145 1146 CPPUNIT_ASSERT(aError == rtl_Digest_E_None ); 1147 1148 rtl::OString aKey = createHex(pKeyBuffer, nKeyLen); 1149 t_print("Key: %s\n", aKey.getStr()); 1150 1151 // rtl::OString sSalt = createHex(pSaltData, nSaltDataLen); 1152 // t_print("Salt: %s\n", sSalt.getStr()); 1153 1154 // CPPUNIT_ASSERT_MESSAGE("md5sum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_PBKDF2) ); 1155 1156 delete [] pSaltData; 1157 delete [] pKeyBuffer; 1158 return aKey; 1159 } 1160 1161 void PBKDF2_001() 1162 { 1163 rtl::OString aPassword = "Password"; 1164 1165 // all permutations 1166 run_check_PBKDF2(aPassword, false, 1); 1167 run_check_PBKDF2(aPassword, false, 2); 1168 run_check_PBKDF2(aPassword, true, 1); 1169 run_check_PBKDF2(aPassword, true, 2); 1170 run_check_PBKDF2(aPassword, false, 3); 1171 run_check_PBKDF2(aPassword, false, 4); 1172 run_check_PBKDF2(aPassword, true, 3); 1173 run_check_PBKDF2(aPassword, true, 4); 1174 } 1175 // Change the following lines only, if you add, remove or rename 1176 // member functions of the current class, 1177 // because these macros are need by auto register mechanism. 1178 1179 CPPUNIT_TEST_SUITE(digest_PBKDF2); 1180 CPPUNIT_TEST( PBKDF2_001 ); 1181 CPPUNIT_TEST_SUITE_END(); 1182 }; // class create 1183 // ----------------------------------------------------------------------------- 1184 1185 class update : public CppUnit::TestFixture 1186 { 1187 public: 1188 // initialise your test code values here. 1189 void setUp() 1190 { 1191 } 1192 1193 void tearDown() 1194 { 1195 } 1196 1197 void update_000() 1198 { 1199 rtlDigest aHandle = NULL; 1200 rtlDigestError aError = rtl_digest_update(aHandle, NULL, 0); 1201 CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); 1202 } 1203 1204 void updateMD2_000() 1205 { 1206 rtlDigest aHandle = NULL; 1207 rtlDigestError aError = rtl_digest_updateMD2(aHandle, NULL, 0); 1208 CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); 1209 } 1210 1211 void updateMD2_001() 1212 { 1213 rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); 1214 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != 0); 1215 1216 rtl::OString aMsg = sSampleString; 1217 const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr(); 1218 1219 rtlDigestError aError = rtl_digest_updateMD2(aHandle, NULL, 0); 1220 CPPUNIT_ASSERT_MESSAGE("handle parameter 'pData' wrong", aError == rtl_Digest_E_Argument ); 1221 1222 /* rtlDigestError */ aError = rtl_digest_updateMD2(aHandle, pData, 0); 1223 CPPUNIT_ASSERT_MESSAGE("handle parameter 'nSize' wrong", aError == rtl_Digest_E_None ); 1224 1225 rtl_digest_destroyMD2(aHandle); 1226 } 1227 void updateMD5_000() 1228 { 1229 rtlDigest aHandle = NULL; 1230 rtlDigestError aError = rtl_digest_updateMD5(aHandle, NULL, 0); 1231 CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); 1232 } 1233 1234 void updateMD5_001() 1235 { 1236 // use wrong Algorithm!!! This is volitional! 1237 rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); 1238 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != 0); 1239 1240 rtl::OString aMsg = sSampleString; 1241 const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr(); 1242 sal_uInt32 nSize = ( aMsg.getLength() ); 1243 1244 rtlDigestError aError = rtl_digest_updateMD5(aHandle, pData, nSize); 1245 CPPUNIT_ASSERT_MESSAGE("handle parameter 'handle' wrong", aError == rtl_Digest_E_Algorithm ); 1246 1247 rtl_digest_destroyMD5(aHandle); 1248 } 1249 1250 void updateMD5_002() 1251 { 1252 rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 1253 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", aHandle != 0); 1254 1255 rtl::OString aMsg = sSampleString; 1256 const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr(); 1257 1258 rtlDigestError aError = rtl_digest_updateMD5(aHandle, NULL, 0); 1259 CPPUNIT_ASSERT_MESSAGE("handle parameter 'pData' wrong", aError == rtl_Digest_E_Argument ); 1260 1261 /* rtlDigestError */ aError = rtl_digest_updateMD5(aHandle, pData, 0); 1262 CPPUNIT_ASSERT_MESSAGE("handle parameter 'nSize' wrong", aError == rtl_Digest_E_None ); 1263 1264 rtl_digest_destroyMD5(aHandle); 1265 } 1266 1267 void updateSHA_000() 1268 { 1269 rtlDigest aHandle = NULL; 1270 rtlDigestError aError = rtl_digest_updateSHA(aHandle, NULL, 0); 1271 CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); 1272 } 1273 1274 void updateSHA1_000() 1275 { 1276 rtlDigest aHandle = NULL; 1277 rtlDigestError aError = rtl_digest_updateSHA1(aHandle, NULL, 0); 1278 CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); 1279 } 1280 1281 void updateHMAC_MD5_000() 1282 { 1283 rtlDigest aHandle = NULL; 1284 rtlDigestError aError = rtl_digest_updateHMAC_MD5(aHandle, NULL, 0); 1285 CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); 1286 } 1287 1288 void updateHMAC_SHA1_000() 1289 { 1290 rtlDigest aHandle = NULL; 1291 rtlDigestError aError = rtl_digest_updateHMAC_SHA1(aHandle, NULL, 0); 1292 CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); 1293 } 1294 1295 // Change the following lines only, if you add, remove or rename 1296 // member functions of the current class, 1297 // because these macros are need by auto register mechanism. 1298 1299 CPPUNIT_TEST_SUITE(update); 1300 CPPUNIT_TEST(update_000); 1301 CPPUNIT_TEST(updateMD5_000); 1302 CPPUNIT_TEST(updateMD5_001); 1303 CPPUNIT_TEST(updateMD5_002); 1304 CPPUNIT_TEST(updateMD5_000); 1305 CPPUNIT_TEST(updateSHA_000); 1306 CPPUNIT_TEST(updateSHA1_000); 1307 CPPUNIT_TEST(updateHMAC_MD5_000); 1308 CPPUNIT_TEST(updateHMAC_SHA1_000); 1309 CPPUNIT_TEST_SUITE_END(); 1310 }; // class create 1311 // ----------------------------------------------------------------------------- 1312 1313 class get : public CppUnit::TestFixture 1314 { 1315 public: 1316 // initialise your test code values here. 1317 void setUp() 1318 { 1319 } 1320 1321 void tearDown() 1322 { 1323 } 1324 1325 void get_000() 1326 { 1327 rtlDigest aHandle = NULL; 1328 rtlDigestError aError = rtl_digest_get(aHandle, NULL, 0); 1329 CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); 1330 } 1331 void getMD5_000() 1332 { 1333 rtlDigest aHandle = NULL; 1334 rtlDigestError aError = rtl_digest_getMD5(aHandle, NULL, 0); 1335 CPPUNIT_ASSERT_MESSAGE("does not handle wrong parameter", aError == rtl_Digest_E_Argument ); 1336 } 1337 void getMD5_001() 1338 { 1339 // test with wrong algorithm 1340 rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 ); 1341 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != 0); 1342 1343 sal_uInt32 nKeyLen = rtl_digest_queryLength( aHandle ); 1344 sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; 1345 1346 rtlDigestError aError = rtl_digest_getMD5(aHandle, NULL, 0); 1347 CPPUNIT_ASSERT_MESSAGE("handle 2. parameter wrong", aError == rtl_Digest_E_Argument ); 1348 1349 /* rtlDigestError */ aError = rtl_digest_getMD5(aHandle, pKeyBuffer, 0); 1350 CPPUNIT_ASSERT_MESSAGE("handle parameter 'handle' wrong", aError == rtl_Digest_E_Algorithm ); 1351 1352 rtl_digest_destroyMD2(aHandle); 1353 } 1354 1355 void getMD5_002() 1356 { 1357 rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 1358 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", aHandle != 0); 1359 1360 sal_uInt32 nKeyLen = rtl_digest_queryLength( aHandle ); 1361 sal_uInt8 *pKeyBuffer = new sal_uInt8[ nKeyLen ]; 1362 1363 rtlDigestError aError = rtl_digest_getMD5(aHandle, NULL /* pKeyBuffer */ , nKeyLen); 1364 CPPUNIT_ASSERT_MESSAGE("handle parameter 'pData' wrong", aError == rtl_Digest_E_Argument ); 1365 1366 /* rtlDigestError */ aError = rtl_digest_getMD5(aHandle, pKeyBuffer, 0); 1367 CPPUNIT_ASSERT_MESSAGE("handle parameter 'nSize' wrong", aError == rtl_Digest_E_BufferSize ); 1368 1369 rtl_digest_destroyMD5(aHandle); 1370 delete [] pKeyBuffer; 1371 } 1372 1373 // Change the following lines only, if you add, remove or rename 1374 // member functions of the current class, 1375 // because these macros are need by auto register mechanism. 1376 1377 CPPUNIT_TEST_SUITE(get); 1378 CPPUNIT_TEST(get_000); 1379 CPPUNIT_TEST(getMD5_000); 1380 CPPUNIT_TEST(getMD5_001); 1381 CPPUNIT_TEST(getMD5_002); 1382 CPPUNIT_TEST_SUITE_END(); 1383 }; // class create 1384 1385 // ----------------------------------------------------------------------------- 1386 class destroy : public CppUnit::TestFixture 1387 { 1388 public: 1389 // initialise your test code values here. 1390 void setUp() 1391 { 1392 } 1393 1394 void tearDown() 1395 { 1396 } 1397 1398 void destroy_001() 1399 { 1400 rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 ); 1401 CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", handle != 0); 1402 1403 // not really testable 1404 // LLA: good will test. 1405 rtl_digest_destroy( handle ); 1406 } 1407 1408 // Change the following lines only, if you add, remove or rename 1409 // member functions of the current class, 1410 // because these macros are need by auto register mechanism. 1411 1412 CPPUNIT_TEST_SUITE(destroy); 1413 CPPUNIT_TEST(destroy_001); 1414 CPPUNIT_TEST_SUITE_END(); 1415 }; // class create 1416 // ----------------------------------------------------------------------------- 1417 1418 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::create, "rtl_digest"); 1419 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::createMD2, "rtl_digest"); 1420 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::createMD5, "rtl_digest"); 1421 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::createSHA, "rtl_digest"); 1422 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::createSHA1, "rtl_digest"); 1423 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::createHMAC_MD5, "rtl_digest"); 1424 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::createHMAC_SHA1, "rtl_digest"); 1425 1426 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::destroy, "rtl_digest"); 1427 1428 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::queryAlgorithm, "rtl_digest"); 1429 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::queryLength, "rtl_digest"); 1430 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::init, "rtl_digest"); 1431 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::equalTests, "rtl_digest"); 1432 1433 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::digest_MD2, "rtl_digest"); 1434 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::digest_MD5, "rtl_digest"); 1435 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::digest_SHA, "rtl_digest"); 1436 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::digest_SHA1, "rtl_digest"); 1437 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::digest_HMAC_MD5, "rtl_digest"); 1438 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::digest_HMAC_SHA1, "rtl_digest"); 1439 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::digest_PBKDF2, "rtl_digest"); 1440 1441 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::update, "rtl_digest"); 1442 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::get, "rtl_digest"); 1443 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_digest::destroy, "rtl_digest"); 1444 } // namespace rtl_digest 1445 1446 1447 // ----------------------------------------------------------------------------- 1448 1449 // this macro creates an empty function, which will called by the RegisterAllFunctions() 1450 // to let the user the possibility to also register some functions by hand. 1451 NOADDITIONAL; 1452