xref: /aoo42x/main/sal/qa/rtl/digest/rtl_digest.cxx (revision e6348c9c)
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 "gtest/gtest.h"
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 != NULL )
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 ::testing::Test
81 {
82 public:
83     // initialise your test code values here.
SetUp()84     void SetUp()
85     {
86     }
87 
TearDown()88     void TearDown()
89     {
90     }
91 }; // class create
92 
TEST_F(create,create_001)93 TEST_F(create, create_001)
94 {
95     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
96     ASSERT_TRUE(handle != 0) << "create with rtl_Digest_AlgorithmMD5";
97     rtl_digest_destroy( handle );
98 }
TEST_F(create,create_002)99 TEST_F(create, create_002)
100 {
101     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 );
102     ASSERT_TRUE(handle != 0) << "create with rtl_Digest_AlgorithmMD2";
103     rtl_digest_destroy( handle );
104 }
TEST_F(create,create_003)105 TEST_F(create, create_003)
106 {
107     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA );
108     ASSERT_TRUE(handle != 0) << "create with rtl_Digest_AlgorithmSHA";
109     rtl_digest_destroy( handle );
110 }
TEST_F(create,create_004)111 TEST_F(create, create_004)
112 {
113     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
114     ASSERT_TRUE(handle != 0) << "create with rtl_Digest_AlgorithmSHA1";
115     rtl_digest_destroy( handle );
116 }
TEST_F(create,create_005)117 TEST_F(create, create_005)
118 {
119     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 );
120     ASSERT_TRUE(handle != 0) << "create with rtl_Digest_AlgorithmHMAC_MD5";
121     rtl_digest_destroy( handle );
122 }
TEST_F(create,create_006)123 TEST_F(create, create_006)
124 {
125     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 );
126     ASSERT_TRUE(handle != 0) << "create with rtl_Digest_AlgorithmHMAC_SHA1";
127     rtl_digest_destroy( handle );
128 }
TEST_F(create,create_007)129 TEST_F(create, create_007)
130 {
131     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid );
132     printf("Handle is %p\n", handle);
133     ASSERT_TRUE(handle == 0) << "create with NULL";
134     rtl_digest_destroy( handle );
135 }
136 
137 
138 
139 // -----------------------------------------------------------------------------
140 
141 class createMD5 : public ::testing::Test
142 {
143 public:
144     // initialise your test code values here.
SetUp()145     void SetUp()
146     {
147     }
148 
TearDown()149     void TearDown()
150     {
151     }
152 }; // class create
153 
TEST_F(createMD5,createMD5_001)154 TEST_F(createMD5, createMD5_001)
155 {
156     rtlDigest handle = rtl_digest_createMD5();
157 
158     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
159     ASSERT_TRUE(rtl_Digest_AlgorithmMD5 == aAlgo) << "query handle";
160 
161     rtl_digest_destroy( handle );
162 }
163 
164 
165 // -----------------------------------------------------------------------------
166 
167 class createMD2 : public ::testing::Test
168 {
169 public:
170     // initialise your test code values here.
SetUp()171     void SetUp()
172     {
173     }
174 
TearDown()175     void TearDown()
176     {
177     }
178 }; // class create
179 
TEST_F(createMD2,createMD2_001)180 TEST_F(createMD2, createMD2_001)
181 {
182     rtlDigest handle = rtl_digest_createMD2( );
183 
184     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
185     ASSERT_TRUE(rtl_Digest_AlgorithmMD2 == aAlgo) << "query handle";
186 
187     rtl_digest_destroy( handle );
188 }
189 
190 // -----------------------------------------------------------------------------
191 
192 class createSHA : public ::testing::Test
193 {
194 public:
195     // initialise your test code values here.
SetUp()196     void SetUp()
197     {
198     }
199 
TearDown()200     void TearDown()
201     {
202     }
203 }; // class create
204 
TEST_F(createSHA,createSHA_001)205 TEST_F(createSHA, createSHA_001)
206 {
207     rtlDigest handle = rtl_digest_createSHA( );
208 
209     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
210     ASSERT_TRUE(rtl_Digest_AlgorithmSHA == aAlgo) << "query handle";
211 
212     rtl_digest_destroy( handle );
213 }
214 
215 // -----------------------------------------------------------------------------
216 
217 class createSHA1 : public ::testing::Test
218 {
219 public:
220     // initialise your test code values here.
SetUp()221     void SetUp()
222     {
223     }
224 
TearDown()225     void TearDown()
226     {
227     }
228 }; // class create
229 
TEST_F(createSHA1,createSHA1_001)230 TEST_F(createSHA1, createSHA1_001)
231 {
232     rtlDigest handle = rtl_digest_createSHA1();
233 
234     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
235     ASSERT_TRUE(rtl_Digest_AlgorithmSHA1 == aAlgo) << "query handle";
236 
237     rtl_digest_destroy( handle );
238 }
239 
240 // -----------------------------------------------------------------------------
241 
242 class createHMAC_MD5 : public ::testing::Test
243 {
244 public:
245     // initialise your test code values here.
SetUp()246     void SetUp()
247     {
248     }
249 
TearDown()250     void TearDown()
251     {
252     }
253 }; // class create
254 
TEST_F(createHMAC_MD5,createHMAC_MD5_001)255 TEST_F(createHMAC_MD5, createHMAC_MD5_001)
256 {
257     rtlDigest handle = rtl_digest_createHMAC_MD5();
258 
259     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
260     ASSERT_TRUE(rtl_Digest_AlgorithmHMAC_MD5 == aAlgo) << "query handle";
261 
262     rtl_digest_destroy( handle );
263 }
264 
265 // -----------------------------------------------------------------------------
266 
267 class createHMAC_SHA1 : public ::testing::Test
268 {
269 public:
270     // initialise your test code values here.
SetUp()271     void SetUp()
272     {
273     }
274 
TearDown()275     void TearDown()
276     {
277     }
278 }; // class create
279 
TEST_F(createHMAC_SHA1,createHMAC_SHA1_001)280 TEST_F(createHMAC_SHA1, createHMAC_SHA1_001)
281 {
282     rtlDigest handle = rtl_digest_createHMAC_SHA1();
283 
284     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
285     ASSERT_TRUE(rtl_Digest_AlgorithmHMAC_SHA1 == aAlgo) << "query handle";
286 
287     rtl_digest_destroy( handle );
288 }
289 
290 // -----------------------------------------------------------------------------
291 
292 class queryAlgorithm : public ::testing::Test
293 {
294 public:
295     // initialise your test code values here.
SetUp()296     void SetUp()
297     {
298     }
299 
TearDown()300     void TearDown()
301     {
302     }
303 }; // class create
304 
TEST_F(queryAlgorithm,query_001)305 TEST_F(queryAlgorithm, query_001)
306 {
307     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
308 
309     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
310     ASSERT_TRUE(rtl_Digest_AlgorithmMD5 == aAlgo) << "query handle";
311 
312     rtl_digest_destroy( handle );
313 }
TEST_F(queryAlgorithm,query_002)314 TEST_F(queryAlgorithm, query_002)
315 {
316     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 );
317 
318     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
319     ASSERT_TRUE(rtl_Digest_AlgorithmMD2 == aAlgo) << "query handle";
320 
321     rtl_digest_destroy( handle );
322 }
TEST_F(queryAlgorithm,query_003)323 TEST_F(queryAlgorithm, query_003)
324 {
325     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA );
326 
327     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
328     ASSERT_TRUE(rtl_Digest_AlgorithmSHA == aAlgo) << "query handle";
329 
330     rtl_digest_destroy( handle );
331 }
TEST_F(queryAlgorithm,query_004)332 TEST_F(queryAlgorithm, query_004)
333 {
334     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
335 
336     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
337     ASSERT_TRUE(rtl_Digest_AlgorithmSHA1 == aAlgo) << "query handle";
338 
339     rtl_digest_destroy( handle );
340 }
TEST_F(queryAlgorithm,query_005)341 TEST_F(queryAlgorithm, query_005)
342 {
343     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 );
344 
345     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
346     ASSERT_TRUE(rtl_Digest_AlgorithmHMAC_MD5 == aAlgo) << "query handle";
347 
348     rtl_digest_destroy( handle );
349 }
TEST_F(queryAlgorithm,query_006)350 TEST_F(queryAlgorithm, query_006)
351 {
352     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 );
353 
354     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
355     ASSERT_TRUE(rtl_Digest_AlgorithmHMAC_SHA1 == aAlgo) << "query handle";
356 
357     rtl_digest_destroy( handle );
358 }
TEST_F(queryAlgorithm,query_007)359 TEST_F(queryAlgorithm, query_007)
360 {
361     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid );
362 
363     rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
364     ASSERT_TRUE(rtl_Digest_AlgorithmInvalid == aAlgo) << "query handle";
365 
366     rtl_digest_destroy( handle );
367 }
368 
369 // -----------------------------------------------------------------------------
370 class queryLength : public ::testing::Test
371 {
372 public:
373     // initialise your test code values here.
SetUp()374     void SetUp()
375     {
376     }
377 
TearDown()378     void TearDown()
379     {
380     }
381 
382 }; // class create
383 
TEST_F(queryLength,queryLength_MD5)384 TEST_F(queryLength, queryLength_MD5)
385 {
386     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
387 
388     sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle);
389     // printf("nAlgoLength:=%d\n", nAlgoLength);
390     ASSERT_TRUE(RTL_DIGEST_LENGTH_MD5 == nAlgoLength) << "query Length";
391 
392     rtl_digest_destroy( handle );
393 }
TEST_F(queryLength,queryLength_MD2)394 TEST_F(queryLength, queryLength_MD2)
395 {
396     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 );
397 
398     sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle);
399     // printf("nAlgoLength:=%d\n", nAlgoLength);
400     ASSERT_TRUE(RTL_DIGEST_LENGTH_MD2 == nAlgoLength) << "query length";
401 
402     rtl_digest_destroy( handle );
403 }
TEST_F(queryLength,queryLength_SHA)404 TEST_F(queryLength, queryLength_SHA)
405 {
406     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA );
407 
408     sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle);
409     // printf("nAlgoLength:=%d\n", nAlgoLength);
410     ASSERT_TRUE(RTL_DIGEST_LENGTH_SHA == nAlgoLength) << "query length";
411 
412     rtl_digest_destroy( handle );
413 }
TEST_F(queryLength,queryLength_SHA1)414 TEST_F(queryLength, queryLength_SHA1)
415 {
416     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
417 
418     sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle);
419     // printf("nAlgoLength:=%d\n", nAlgoLength);
420     ASSERT_TRUE(RTL_DIGEST_LENGTH_SHA1 == nAlgoLength) << "query length";
421 
422     rtl_digest_destroy( handle );
423 }
TEST_F(queryLength,queryLength_HMAC_MD5)424 TEST_F(queryLength, queryLength_HMAC_MD5)
425 {
426     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 );
427 
428     sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle);
429     // printf("nAlgoLength:=%d\n", nAlgoLength);
430     ASSERT_TRUE(RTL_DIGEST_LENGTH_HMAC_MD5 == nAlgoLength) << "query length";
431 
432     rtl_digest_destroy( handle );
433 }
TEST_F(queryLength,queryLength_HMAC_SHA1)434 TEST_F(queryLength, queryLength_HMAC_SHA1)
435 {
436     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 );
437 
438     sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle);
439     // printf("nAlgoLength:=%d\n", nAlgoLength);
440     ASSERT_TRUE(RTL_DIGEST_LENGTH_HMAC_SHA1 == nAlgoLength) << "query length";
441 
442     rtl_digest_destroy( handle );
443 }
444 
TEST_F(queryLength,queryLength_Illegal)445 TEST_F(queryLength, queryLength_Illegal)
446 {
447     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid );
448 
449     sal_uInt32 nAlgoLength = rtl_digest_queryLength(handle);
450     // printf("nAlgoLength:=%d\n", nAlgoLength);
451     ASSERT_TRUE(0 == nAlgoLength) << "query length";
452 
453     rtl_digest_destroy( handle );
454 }
455 
456 // -----------------------------------------------------------------------------
457 
createHex(sal_uInt8 * _pMD5KeyBuffer,sal_uInt32 _nMD5KeyLen)458 rtl::OString createHex(sal_uInt8 *_pMD5KeyBuffer, sal_uInt32 _nMD5KeyLen)
459 {
460     // Create hex-value string from the MD5 value to keep the string size minimal
461     rtl::OStringBuffer aBuffer( _nMD5KeyLen * 2 + 1 );
462     for ( sal_uInt32 i = 0; i < _nMD5KeyLen; i++ )
463     {
464         sal_Int32 nValue = (sal_Int32)_pMD5KeyBuffer[i];
465         if (nValue < 16)                         // maximul hex value for 1 byte
466         {
467             aBuffer.append( sal_Int32(0), sal_Int16(16) /* radix */ );
468         }
469         aBuffer.append( nValue, 16 /* radix */ );
470     }
471 
472     return aBuffer.makeStringAndClear();
473 }
474 
475 
476 // -----------------------------------------------------------------------------
477 class init : public ::testing::Test
478 {
479 public:
480     // initialise your test code values here.
SetUp()481     void SetUp()
482     {
483     }
484 
TearDown()485     void TearDown()
486     {
487     }
488 }; // class init
489 
TEST_F(init,init_000)490 TEST_F(init, init_000)
491 {
492     rtlDigest handle = NULL;
493 
494     rtlDigestError aError = rtl_digest_init(handle, NULL, 0);
495 
496     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "init(NULL, 0, 0)";
497 }
498 
TEST_F(init,init_001)499 TEST_F(init, init_001)
500 {
501     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
502 
503     rtlDigestError aError = rtl_digest_init(handle, NULL, 0);
504 
505     ASSERT_TRUE(aError == rtl_Digest_E_None) << "init(handle, 0, 0)";
506 
507     rtl_digest_destroy( handle );
508 }
509 
510 // ------------------------------------
TEST_F(init,init_MD2)511 TEST_F(init, init_MD2)
512 {
513     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD2 );
514 
515     rtl::OString aMsg = sSampleString;
516     const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr();
517     sal_uInt32       nSize = ( aMsg.getLength() );
518 
519     rtlDigestError aError = rtl_digest_init(handle, pData, nSize);
520 
521     ASSERT_TRUE(aError == rtl_Digest_E_None) << "init(handle, pData, nSize)";
522 
523     rtl_digest_update( handle, pData, nSize );
524 
525     sal_uInt32     nKeyLen = rtl_digest_queryLength( handle );
526     sal_uInt8     *pKeyBuffer = new sal_uInt8[ nKeyLen ];
527 
528     rtl_digest_get( handle, pKeyBuffer, nKeyLen );
529     rtl::OString aSum = createHex(pKeyBuffer, nKeyLen);
530     delete [] pKeyBuffer;
531 
532     printf("MD2 Sum: %s\n", aSum.getStr());
533     // LLA: how to check right values
534     // samples?
535 
536     rtl_digest_destroy( handle );
537 }
538 
TEST_F(init,init_MD5)539 TEST_F(init, init_MD5)
540 {
541     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
542 
543     rtl::OString aMsg = sSampleString;
544     const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr();
545     sal_uInt32       nSize = ( aMsg.getLength() );
546 
547     rtlDigestError aError = rtl_digest_init(handle, pData, nSize);
548 
549     ASSERT_TRUE(aError == rtl_Digest_E_None) << "init(handle, pData, nSize)";
550 
551     rtl_digest_update( handle, pData, nSize );
552 
553     sal_uInt32     nKeyLen = rtl_digest_queryLength( handle );
554     sal_uInt8     *pKeyBuffer = new sal_uInt8[ nKeyLen ];
555 
556     rtl_digest_get( handle, pKeyBuffer, nKeyLen );
557     rtl::OString aSum = createHex(pKeyBuffer, nKeyLen);
558     delete [] pKeyBuffer;
559 
560     printf("MD5 Sum: %s\n", aSum.getStr());
561     // LLA: how to check right values
562     // samples?
563 
564     rtl_digest_destroy( handle );
565 }
566 
TEST_F(init,init_SHA)567 TEST_F(init, init_SHA)
568 {
569     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA );
570 
571     rtl::OString aMsg = sSampleString;
572     const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr();
573     sal_uInt32       nSize = ( aMsg.getLength() );
574 
575     rtlDigestError aError = rtl_digest_init(handle, pData, nSize);
576 
577     ASSERT_TRUE(aError == rtl_Digest_E_None) << "init(handle, pData, nSize)";
578 
579     rtl_digest_update( handle, pData, nSize );
580 
581     sal_uInt32     nKeyLen = rtl_digest_queryLength( handle );
582     sal_uInt8     *pKeyBuffer = new sal_uInt8[ nKeyLen ];
583 
584     rtl_digest_get( handle, pKeyBuffer, nKeyLen );
585     rtl::OString aSum = createHex(pKeyBuffer, nKeyLen);
586     delete [] pKeyBuffer;
587 
588     printf("SHA Sum: %s\n", aSum.getStr());
589     // LLA: how to check right values
590     // samples?
591 
592     rtl_digest_destroy( handle );
593 }
TEST_F(init,init_SHA1)594 TEST_F(init, init_SHA1)
595 {
596     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
597 
598     rtl::OString aMsg = sSampleString;
599     const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr();
600     sal_uInt32       nSize = ( aMsg.getLength() );
601 
602     rtlDigestError aError = rtl_digest_init(handle, pData, nSize);
603 
604     ASSERT_TRUE(aError == rtl_Digest_E_None) << "init(handle, pData, nSize)";
605 
606     rtl_digest_update( handle, pData, nSize );
607 
608     sal_uInt32     nKeyLen = rtl_digest_queryLength( handle );
609     sal_uInt8     *pKeyBuffer = new sal_uInt8[ nKeyLen ];
610 
611     rtl_digest_get( handle, pKeyBuffer, nKeyLen );
612     rtl::OString aSum = createHex(pKeyBuffer, nKeyLen);
613     delete [] pKeyBuffer;
614 
615     printf("SHA1 Sum: %s\n", aSum.getStr());
616     // LLA: how to check right values
617     // samples?
618 
619     rtl_digest_destroy( handle );
620 }
TEST_F(init,init_HMAC_MD5)621 TEST_F(init, init_HMAC_MD5)
622 {
623     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_MD5 );
624 
625     rtl::OString aMsg = sSampleString;
626     const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr();
627     sal_uInt32       nSize = ( aMsg.getLength() );
628 
629     sal_uInt32     nKeyLen = rtl_digest_queryLength( handle );
630     ASSERT_TRUE(nKeyLen) << "Keylen must be greater 0";
631 
632     sal_uInt8    *pKeyBuffer = new sal_uInt8[ nKeyLen ];
633     ASSERT_TRUE( pKeyBuffer );
634     memset(pKeyBuffer, 0, nKeyLen);
635 
636     rtlDigestError aError = rtl_digest_init(handle, pKeyBuffer, nKeyLen );
637 
638     ASSERT_TRUE(aError == rtl_Digest_E_None) << "init(handle, pData, nSize)";
639 
640     rtl_digest_update( handle, pData, nSize );
641 
642     rtl_digest_get( handle, pKeyBuffer, nKeyLen );
643     rtl::OString aSum = createHex(pKeyBuffer, nKeyLen);
644     delete [] pKeyBuffer;
645 
646     printf("HMAC_MD5 Sum: %s\n", aSum.getStr());
647     // LLA: how to check right values
648     // samples?
649 
650     rtl_digest_destroy( handle );
651 }
TEST_F(init,init_HMAC_SHA1)652 TEST_F(init, init_HMAC_SHA1)
653 {
654     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmHMAC_SHA1 );
655 
656     rtl::OString aMsg = sSampleString;
657     const sal_uInt8 *pData = (const sal_uInt8*)aMsg.getStr();
658     sal_uInt32       nSize = ( aMsg.getLength() );
659 
660     sal_uInt32     nKeyLen = rtl_digest_queryLength( handle );
661     ASSERT_TRUE(nKeyLen) << "Keylen must be greater 0";
662 
663     sal_uInt8    *pKeyBuffer = new sal_uInt8[ nKeyLen ];
664     ASSERT_TRUE( pKeyBuffer );
665     memset(pKeyBuffer, 0, nKeyLen);
666 
667     rtlDigestError aError = rtl_digest_init(handle, pKeyBuffer, nKeyLen );
668 
669     ASSERT_TRUE(aError == rtl_Digest_E_None) << "init(handle, pData, nSize)";
670 
671     rtl_digest_update( handle, pData, nSize );
672 
673     rtl_digest_get( handle, pKeyBuffer, nKeyLen );
674     rtl::OString aSum = createHex(pKeyBuffer, nKeyLen);
675     delete [] pKeyBuffer;
676 
677     printf("HMAC_SHA1 Sum: %s\n", aSum.getStr());
678     // LLA: how to check right values
679     // samples?
680 
681     rtl_digest_destroy( handle );
682 }
683 // ------------------------------------
684 
getMD5Sum(rtl::OString const & _aMsg)685 rtl::OString getMD5Sum(rtl::OString const& _aMsg )
686 {
687     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
688 
689     const sal_uInt8 *pData = (const sal_uInt8*)_aMsg.getStr();
690     sal_uInt32	     nSize = ( _aMsg.getLength() );
691 
692     rtl_digest_init(handle, pData, nSize);
693     rtl_digest_update( handle, pData, nSize );
694 
695     sal_uInt32     nMD5KeyLen = rtl_digest_queryLength( handle );
696     sal_uInt8     *pMD5KeyBuffer = new sal_uInt8[ nMD5KeyLen ];
697 
698     rtl_digest_get( handle, pMD5KeyBuffer, nMD5KeyLen );
699     rtl::OString aMD5Sum = createHex(pMD5KeyBuffer, nMD5KeyLen);
700     delete [] pMD5KeyBuffer;
701 
702     rtl_digest_destroy( handle );
703     return aMD5Sum;
704 }
705 
706 // -----------------------------------------------------------------------------
707 
708 class equalTests : public ::testing::Test
709 {
710 public:
711     // initialise your test code values here.
SetUp()712     void SetUp()
713     {
714     }
715 
TearDown()716     void TearDown()
717     {
718     }
719 }; // class create
720 
TEST_F(equalTests,equal_001)721 TEST_F(equalTests, equal_001)
722 {
723     rtl::OString aMsg1 = sSampleString;
724     rtl::OString aMsg2 = sSampleString;
725 
726     rtl::OString aMsgMD5Sum1 = getMD5Sum(aMsg1);
727     rtl::OString aMsgMD5Sum2 = getMD5Sum(aMsg2);
728 
729     ASSERT_TRUE(aMsgMD5Sum1.getLength() == 32 && aMsgMD5Sum2.getLength() == 32) << "md5sum must have a length";
730     ASSERT_TRUE(aMsgMD5Sum1.equals(aMsgMD5Sum2) == sal_True) << "source is the same, dest must be also the same";
731 }
732 // ------------------------------------
TEST_F(equalTests,equal_002)733 TEST_F(equalTests, equal_002)
734 {
735     rtl::OString aMsg1 = sSampleString;
736     rtl::OString aMsg2 = sSampleString_only_one_diff;
737 
738     rtl::OString aMsgMD5Sum1 = getMD5Sum(aMsg1);
739     rtl::OString aMsgMD5Sum2 = getMD5Sum(aMsg2);
740 
741     ASSERT_TRUE(aMsgMD5Sum1.getLength() == 32 && aMsgMD5Sum2.getLength() == 32) << "md5sum must have a length";
742     ASSERT_TRUE(aMsgMD5Sum1.equals(aMsgMD5Sum2) == sal_False) << "differ only in one char";
743 }
744 
745 
746 // -----------------------------------------------------------------------------
747 class digest_MD2 : public ::testing::Test
748 {
749 public:
750     // initialise your test code values here.
SetUp()751     void SetUp()
752     {
753     }
754 
TearDown()755     void TearDown()
756     {
757     }
758 }; // class digest_MD2
759 
TEST_F(digest_MD2,MD2_001)760 TEST_F(digest_MD2, MD2_001)
761 {
762     rtl::OString  aMsg1 = sSampleString;
763 
764     sal_uInt8    *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_MD2 ];
765     ASSERT_TRUE( pBuffer );
766     memset(pBuffer, 0, RTL_DIGEST_LENGTH_MD2 );
767 
768     sal_uInt8    *pMsg1 = (sal_uInt8*)aMsg1.getStr();
769     sal_Int32     nLen  = aMsg1.getLength();
770 
771     rtlDigestError aError = rtl_digest_MD2(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_MD2);
772 
773     ASSERT_TRUE(aError == rtl_Digest_E_None );
774 
775     rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_MD2);
776     printf("Decrypt MD2: %s\n", aStr.getStr());
777     ASSERT_TRUE(aStr.equals(sSampleString_MD2)) <<
778             "checksum of sample string is wrong. Code changes or sample problems, please check.";
779 
780     delete [] pBuffer;
781 }
782 // -----------------------------------------------------------------------------
783 class digest_MD5 : public ::testing::Test
784 {
785 public:
786     // initialise your test code values here.
SetUp()787     void SetUp()
788     {
789     }
790 
TearDown()791     void TearDown()
792     {
793     }
794 }; // class digest_MD5
795 
TEST_F(digest_MD5,MD5_001)796 TEST_F(digest_MD5, MD5_001)
797 {
798     rtl::OString  aMsg1 = sSampleString;
799 
800     sal_uInt8    *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_MD5 ];
801     ASSERT_TRUE( pBuffer );
802     memset(pBuffer, 0, RTL_DIGEST_LENGTH_MD5 );
803 
804     sal_uInt8    *pMsg1 = (sal_uInt8*)aMsg1.getStr();
805     sal_Int32     nLen  = aMsg1.getLength();
806 
807     rtlDigestError aError = rtl_digest_MD5(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_MD5);
808 
809     ASSERT_TRUE(aError == rtl_Digest_E_None );
810 
811     rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_MD5);
812     printf("Decrypt MD5: %s\n", aStr.getStr());
813     ASSERT_TRUE(aStr.equals(sSampleString_MD5) ) <<
814             "checksum of sample string is wrong. Code changes or sample problems, please check.";
815 
816     delete [] pBuffer;
817 }
818 
819 // -----------------------------------------------------------------------------
820 class digest_SHA : public ::testing::Test
821 {
822 public:
823     // initialise your test code values here.
SetUp()824     void SetUp()
825     {
826     }
827 
TearDown()828     void TearDown()
829     {
830     }
831 }; // class create
832 
TEST_F(digest_SHA,SHA_001)833 TEST_F(digest_SHA, SHA_001)
834 {
835     rtl::OString  aMsg1 = sSampleString;
836 
837     sal_uInt8    *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_SHA ];
838     ASSERT_TRUE( pBuffer );
839     memset(pBuffer, 0, RTL_DIGEST_LENGTH_SHA);
840 
841     sal_uInt8    *pMsg1 = (sal_uInt8*)aMsg1.getStr();
842     sal_Int32     nLen  = aMsg1.getLength();
843 
844     rtlDigestError aError = rtl_digest_SHA(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_SHA);
845 
846     ASSERT_TRUE(aError == rtl_Digest_E_None );
847 
848     rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_SHA);
849     printf("Decrypt SHA: %s\n", aStr.getStr());
850     ASSERT_TRUE(aStr.equals(sSampleString_SHA)) <<
851             "checksum of sample string is wrong. Code changes or sample problems, please check.";
852 
853     delete [] pBuffer;
854 }
855 // -----------------------------------------------------------------------------
856 class digest_SHA1 : public ::testing::Test
857 {
858 public:
859     // initialise your test code values here.
SetUp()860     void SetUp()
861     {
862     }
863 
TearDown()864     void TearDown()
865     {
866     }
867 }; // class create
868 
TEST_F(digest_SHA1,SHA1_001)869 TEST_F(digest_SHA1, SHA1_001)
870 {
871     rtl::OString  aMsg1 = sSampleString;
872 
873     sal_uInt8    *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_SHA1 ];
874     ASSERT_TRUE( pBuffer );
875     memset(pBuffer, 0, RTL_DIGEST_LENGTH_SHA1);
876 
877     sal_uInt8    *pMsg1 = (sal_uInt8*)aMsg1.getStr();
878     sal_Int32     nLen  = aMsg1.getLength();
879 
880     rtlDigestError aError = rtl_digest_SHA1(pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_SHA1);
881 
882     ASSERT_TRUE(aError == rtl_Digest_E_None );
883 
884     rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_SHA1);
885     printf("Decrypt SHA1: %s\n", aStr.getStr());
886     ASSERT_TRUE(aStr.equals(sSampleString_SHA1)) <<
887             "checksum of sample string is wrong. Code changes or sample problems, please check.";
888 
889     delete [] pBuffer;
890 }
891 // -----------------------------------------------------------------------------
892 class digest_HMAC_MD5 : public ::testing::Test
893 {
894 public:
895     // initialise your test code values here.
SetUp()896     void SetUp()
897     {
898     }
899 
TearDown()900     void TearDown()
901     {
902     }
903 }; // class create
904 
TEST_F(digest_HMAC_MD5,HMAC_MD5_001)905 TEST_F(digest_HMAC_MD5, HMAC_MD5_001)
906 {
907     rtl::OString  aMsg1 = sSampleString;
908 
909     sal_uInt8    *pKeyBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_MD5 ];
910     ASSERT_TRUE( pKeyBuffer );
911     memset(pKeyBuffer, 0, RTL_DIGEST_LENGTH_HMAC_MD5);
912 
913     sal_uInt8    *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_MD5 ];
914     ASSERT_TRUE( pBuffer );
915     memset(pBuffer, 0, RTL_DIGEST_LENGTH_HMAC_MD5);
916 
917     sal_uInt8    *pMsg1 = (sal_uInt8*)aMsg1.getStr();
918     sal_Int32     nLen  = aMsg1.getLength();
919 
920     rtlDigestError aError = rtl_digest_HMAC_MD5(pKeyBuffer, RTL_DIGEST_LENGTH_HMAC_MD5, pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_HMAC_MD5);
921 
922     ASSERT_TRUE(aError == rtl_Digest_E_None );
923 
924     rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_HMAC_MD5);
925     printf("Decrypt HMAC_MD5: %s\n", aStr.getStr());
926     ASSERT_TRUE(aStr.equals(sSampleString_HMAC_MD5)) <<
927             "md5sum of sample string is wrong. Code changes or sample problems, please check.";
928 
929     delete [] pBuffer;
930 }
931 
932 // -----------------------------------------------------------------------------
933 class digest_HMAC_SHA1 : public ::testing::Test
934 {
935 public:
936     // initialise your test code values here.
SetUp()937     void SetUp()
938     {
939     }
940 
TearDown()941     void TearDown()
942     {
943     }
944 }; // class create
945 
TEST_F(digest_HMAC_SHA1,HMAC_SHA1_001)946 TEST_F(digest_HMAC_SHA1, HMAC_SHA1_001)
947 {
948     rtl::OString  aMsg1 = sSampleString;
949 
950     sal_uInt8    *pKeyBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_SHA1 ];
951     ASSERT_TRUE( pKeyBuffer );
952     memset(pKeyBuffer, 0, RTL_DIGEST_LENGTH_HMAC_SHA1);
953 
954     sal_uInt8    *pBuffer = new sal_uInt8[ RTL_DIGEST_LENGTH_HMAC_SHA1 ];
955     ASSERT_TRUE( pBuffer );
956     memset(pBuffer, 0, RTL_DIGEST_LENGTH_HMAC_SHA1);
957 
958     sal_uInt8    *pMsg1 = (sal_uInt8*)aMsg1.getStr();
959     sal_Int32     nLen  = aMsg1.getLength();
960 
961     rtlDigestError aError = rtl_digest_HMAC_SHA1(pKeyBuffer, RTL_DIGEST_LENGTH_HMAC_SHA1, pMsg1, nLen, pBuffer, RTL_DIGEST_LENGTH_HMAC_SHA1);
962 
963     ASSERT_TRUE(aError == rtl_Digest_E_None );
964 
965     rtl::OString aStr = createHex(pBuffer, RTL_DIGEST_LENGTH_HMAC_SHA1);
966     printf("Decrypt HMAC_SHA1: %s\n", aStr.getStr());
967     ASSERT_TRUE(aStr.equals(sSampleString_HMAC_SHA1)) <<
968             "md5sum of sample string is wrong. Code changes or sample problems, please check.";
969 
970     delete [] pBuffer;
971 }
972 // -----------------------------------------------------------------------------
973 class digest_PBKDF2 : public ::testing::Test
974 {
975 public:
976     // initialise your test code values here.
SetUp()977     void SetUp()
978     {
979     }
980 
TearDown()981     void TearDown()
982     {
983     }
984 
run_check_PBKDF2(rtl::OString const & _sPassword,bool _bClearSalt,sal_uInt32 _nCount)985     rtl::OString /* key */ run_check_PBKDF2(rtl::OString const& _sPassword, bool _bClearSalt, sal_uInt32 _nCount)
986     {
987         sal_uInt32   nKeyLen = RTL_DIGEST_LENGTH_HMAC_SHA1;
988         sal_uInt8    *pKeyBuffer = new sal_uInt8[ nKeyLen ];
989         EXPECT_TRUE( pKeyBuffer );
990         memset(pKeyBuffer, 0, nKeyLen);
991 
992         sal_uInt8    *pPassword    = (sal_uInt8*)_sPassword.getStr();
993         sal_Int32     nPasswordLen = _sPassword.getLength();
994 
995         sal_uInt32   nSaltDataLen = RTL_DIGEST_LENGTH_HMAC_SHA1;
996         sal_uInt8    *pSaltData = new sal_uInt8[ nSaltDataLen ];
997         EXPECT_TRUE( pSaltData );
998         memset(pSaltData, 0, nSaltDataLen);
999 
1000         if (! _bClearSalt)
1001         {
1002             // wilful contamination
1003             pSaltData[0] = 1;
1004         }
1005 
1006         rtlDigestError aError = rtl_digest_PBKDF2(pKeyBuffer, nKeyLen, pPassword, nPasswordLen, pSaltData, nSaltDataLen, _nCount);
1007 
1008         EXPECT_TRUE(aError == rtl_Digest_E_None );
1009 
1010         rtl::OString aKey = createHex(pKeyBuffer, nKeyLen);
1011         printf("Key: %s\n", aKey.getStr());
1012 
1013         // rtl::OString sSalt = createHex(pSaltData, nSaltDataLen);
1014         // printf("Salt: %s\n", sSalt.getStr());
1015 
1016         // EXPECT_TRUE(aStr.equals(sSampleString_PBKDF2)) <<
1017         //      "md5sum of sample string is wrong. Code changes or sample problems, please check.";
1018 
1019         delete [] pSaltData;
1020         delete [] pKeyBuffer;
1021         return aKey;
1022     }
1023 }; // class create
1024 
TEST_F(digest_PBKDF2,PBKDF2_001)1025 TEST_F(digest_PBKDF2, PBKDF2_001)
1026 {
1027     rtl::OString  aPassword = "Password";
1028 
1029     // all permutations
1030     run_check_PBKDF2(aPassword, false, 1);
1031     run_check_PBKDF2(aPassword, false, 2);
1032     run_check_PBKDF2(aPassword, true,  1);
1033     run_check_PBKDF2(aPassword, true,  2);
1034     run_check_PBKDF2(aPassword, false, 3);
1035     run_check_PBKDF2(aPassword, false, 4);
1036     run_check_PBKDF2(aPassword, true,  3);
1037     run_check_PBKDF2(aPassword, true,  4);
1038 }
1039 
1040 // -----------------------------------------------------------------------------
1041 
1042 class update : public ::testing::Test
1043 {
1044 public:
1045     // initialise your test code values here.
SetUp()1046     void SetUp()
1047     {
1048     }
1049 
TearDown()1050     void TearDown()
1051     {
1052     }
1053 }; // class create
1054 
TEST_F(update,update_000)1055 TEST_F(update, update_000)
1056 {
1057     rtlDigest aHandle = NULL;
1058     rtlDigestError aError = rtl_digest_update(aHandle, NULL, 0);
1059     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "does not handle wrong parameter";
1060 }
1061 
TEST_F(update,updateMD2_000)1062 TEST_F(update, updateMD2_000)
1063 {
1064     rtlDigest aHandle = NULL;
1065     rtlDigestError aError = rtl_digest_updateMD2(aHandle, NULL, 0);
1066     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "does not handle wrong parameter";
1067 }
1068 
TEST_F(update,updateMD2_001)1069 TEST_F(update, updateMD2_001)
1070 {
1071     rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 );
1072     ASSERT_TRUE(aHandle != 0) << "create with rtl_Digest_AlgorithmMD2";
1073 
1074     rtl::OString aMsg = sSampleString;
1075     const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr();
1076 
1077     rtlDigestError aError = rtl_digest_updateMD2(aHandle, NULL, 0);
1078     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "handle parameter 'pData' wrong";
1079 
1080     /* rtlDigestError */ aError = rtl_digest_updateMD2(aHandle, pData, 0);
1081     ASSERT_TRUE(aError == rtl_Digest_E_None) << "handle parameter 'nSize' wrong";
1082 
1083     rtl_digest_destroyMD2(aHandle);
1084 }
TEST_F(update,updateMD5_000)1085 TEST_F(update, updateMD5_000)
1086 {
1087     rtlDigest aHandle = NULL;
1088     rtlDigestError aError = rtl_digest_updateMD5(aHandle, NULL, 0);
1089     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "does not handle wrong parameter";
1090 }
1091 
TEST_F(update,updateMD5_001)1092 TEST_F(update, updateMD5_001)
1093 {
1094     // use wrong Algorithm!!! This is volitional!
1095     rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 );
1096     ASSERT_TRUE(aHandle != 0) << "create with rtl_Digest_AlgorithmMD2";
1097 
1098     rtl::OString aMsg = sSampleString;
1099     const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr();
1100     sal_uInt32       nSize = ( aMsg.getLength() );
1101 
1102     rtlDigestError aError = rtl_digest_updateMD5(aHandle, pData, nSize);
1103     ASSERT_TRUE(aError == rtl_Digest_E_Algorithm) << "handle parameter 'handle' wrong";
1104 
1105     rtl_digest_destroyMD5(aHandle);
1106 }
1107 
TEST_F(update,updateMD5_002)1108 TEST_F(update, updateMD5_002)
1109 {
1110     rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
1111     ASSERT_TRUE(aHandle != 0) << "create with rtl_Digest_AlgorithmMD5";
1112 
1113     rtl::OString aMsg = sSampleString;
1114     const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr();
1115 
1116     rtlDigestError aError = rtl_digest_updateMD5(aHandle, NULL, 0);
1117     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "handle parameter 'pData' wrong";
1118 
1119     /* rtlDigestError */ aError = rtl_digest_updateMD5(aHandle, pData, 0);
1120     ASSERT_TRUE(aError == rtl_Digest_E_None) << "handle parameter 'nSize' wrong";
1121 
1122     rtl_digest_destroyMD5(aHandle);
1123 }
1124 
TEST_F(update,updateSHA_000)1125 TEST_F(update, updateSHA_000)
1126 {
1127     rtlDigest aHandle = NULL;
1128     rtlDigestError aError = rtl_digest_updateSHA(aHandle, NULL, 0);
1129     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "does not handle wrong parameter";
1130 }
1131 
TEST_F(update,updateSHA1_000)1132 TEST_F(update, updateSHA1_000)
1133 {
1134     rtlDigest aHandle = NULL;
1135     rtlDigestError aError = rtl_digest_updateSHA1(aHandle, NULL, 0);
1136     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "does not handle wrong parameter";
1137 }
1138 
TEST_F(update,updateHMAC_MD5_000)1139 TEST_F(update, updateHMAC_MD5_000)
1140 {
1141     rtlDigest aHandle = NULL;
1142     rtlDigestError aError = rtl_digest_updateHMAC_MD5(aHandle, NULL, 0);
1143     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "does not handle wrong parameter";
1144 }
1145 
TEST_F(update,updateHMAC_SHA1_000)1146 TEST_F(update, updateHMAC_SHA1_000)
1147 {
1148     rtlDigest aHandle = NULL;
1149     rtlDigestError aError = rtl_digest_updateHMAC_SHA1(aHandle, NULL, 0);
1150     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "does not handle wrong parameter";
1151 }
1152 
1153 // -----------------------------------------------------------------------------
1154 
1155 class get : public ::testing::Test
1156 {
1157 public:
1158     // initialise your test code values here.
SetUp()1159     void SetUp()
1160     {
1161     }
1162 
TearDown()1163     void TearDown()
1164     {
1165     }
1166 }; // class create
1167 
TEST_F(get,get_000)1168 TEST_F(get, get_000)
1169 {
1170     rtlDigest aHandle = NULL;
1171     rtlDigestError aError = rtl_digest_get(aHandle, NULL, 0);
1172     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "does not handle wrong parameter";
1173 }
TEST_F(get,getMD5_000)1174 TEST_F(get, getMD5_000)
1175 {
1176     rtlDigest aHandle = NULL;
1177     rtlDigestError aError = rtl_digest_getMD5(aHandle, NULL, 0);
1178     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "does not handle wrong parameter";
1179 }
TEST_F(get,getMD5_001)1180 TEST_F(get, getMD5_001)
1181 {
1182     // test with wrong algorithm
1183     rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 );
1184     ASSERT_TRUE(aHandle != 0) << "create with rtl_Digest_AlgorithmMD2";
1185 
1186     sal_uInt32     nKeyLen = rtl_digest_queryLength( aHandle );
1187     sal_uInt8     *pKeyBuffer = new sal_uInt8[ nKeyLen ];
1188 
1189     rtlDigestError aError = rtl_digest_getMD5(aHandle, NULL, 0);
1190     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "handle 2. parameter wrong";
1191 
1192     /* rtlDigestError */ aError = rtl_digest_getMD5(aHandle, pKeyBuffer, 0);
1193     ASSERT_TRUE(aError == rtl_Digest_E_Algorithm) << "handle parameter 'handle' wrong";
1194 
1195     rtl_digest_destroyMD2(aHandle);
1196 }
1197 
TEST_F(get,getMD5_002)1198 TEST_F(get, getMD5_002)
1199 {
1200     rtlDigest aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
1201     ASSERT_TRUE(aHandle != 0) << "create with rtl_Digest_AlgorithmMD5";
1202 
1203     sal_uInt32     nKeyLen = rtl_digest_queryLength( aHandle );
1204     sal_uInt8     *pKeyBuffer = new sal_uInt8[ nKeyLen ];
1205 
1206     rtlDigestError aError = rtl_digest_getMD5(aHandle, NULL /* pKeyBuffer */ , nKeyLen);
1207     ASSERT_TRUE(aError == rtl_Digest_E_Argument) << "handle parameter 'pData' wrong";
1208 
1209     /* rtlDigestError */ aError = rtl_digest_getMD5(aHandle, pKeyBuffer, 0);
1210     ASSERT_TRUE(aError == rtl_Digest_E_BufferSize) << "handle parameter 'nSize' wrong";
1211 
1212     rtl_digest_destroyMD5(aHandle);
1213     delete [] pKeyBuffer;
1214 }
1215 // -----------------------------------------------------------------------------
1216 class destroy : public ::testing::Test
1217 {
1218 public:
1219     // initialise your test code values here.
SetUp()1220     void SetUp()
1221     {
1222     }
1223 
TearDown()1224     void TearDown()
1225     {
1226     }
1227 }; // class create
1228 
TEST_F(destroy,destroy_001)1229 TEST_F(destroy, destroy_001)
1230 {
1231     rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
1232     ASSERT_TRUE(handle != 0) << "create with rtl_Digest_AlgorithmMD5";
1233 
1234     // not really testable
1235     // LLA: good will test.
1236     rtl_digest_destroy( handle );
1237 }
1238 // -----------------------------------------------------------------------------
1239 
1240 } // namespace rtl_digest
1241 
main(int argc,char ** argv)1242 int main(int argc, char **argv)
1243 {
1244     ::testing::InitGoogleTest(&argc, argv);
1245     return RUN_ALL_TESTS();
1246 }
1247