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