1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <sal/types.h>
23 #include <cppunit/TestFixture.h>
24 #include <cppunit/extensions/HelperMacros.h>
25 #include <cppunit/plugin/TestPlugIn.h>
27 #include <rtl/strbuf.hxx>
28 #include <rtl/cipher.h>
30 // -----------------------------------------------------------------------------
34 class create
: public CppUnit::TestFixture
37 // initialise your test code values here.
48 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeECB
);
49 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
50 rtl_cipher_destroy(aCipher
);
54 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmInvalid
, rtl_Cipher_ModeECB
);
55 CPPUNIT_ASSERT_MESSAGE("create provide wrong object.", aCipher
== NULL
);
59 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeCBC
);
60 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
61 rtl_cipher_destroy(aCipher
);
65 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmInvalid
, rtl_Cipher_ModeCBC
);
66 CPPUNIT_ASSERT_MESSAGE("create provide wrong object.", aCipher
== NULL
);
70 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeStream
);
71 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
72 rtl_cipher_destroy(aCipher
);
76 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmInvalid
, rtl_Cipher_ModeStream
);
77 CPPUNIT_ASSERT_MESSAGE("create provide wrong object.", aCipher
== NULL
);
81 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeInvalid
);
82 CPPUNIT_ASSERT_MESSAGE("create provide wrong object.", aCipher
== NULL
);
86 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmInvalid
, rtl_Cipher_ModeInvalid
);
87 CPPUNIT_ASSERT_MESSAGE("create provide wrong object.", aCipher
== NULL
);
90 // Change the following lines only, if you add, remove or rename
91 // member functions of the current class,
92 // because these macros are need by auto register mechanism.
94 CPPUNIT_TEST_SUITE(create
);
95 CPPUNIT_TEST(create_001
);
96 CPPUNIT_TEST(create_002
);
97 CPPUNIT_TEST(create_003
);
98 CPPUNIT_TEST(create_004
);
99 CPPUNIT_TEST(create_005
);
100 CPPUNIT_TEST(create_006
);
101 CPPUNIT_TEST(create_007
);
102 CPPUNIT_TEST(create_008
);
103 CPPUNIT_TEST_SUITE_END();
106 // -----------------------------------------------------------------------------
107 class createBF
: public CppUnit::TestFixture
110 // initialise your test code values here.
121 rtlCipher aCipher
= rtl_cipher_createBF(rtl_Cipher_ModeECB
);
122 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
123 rtl_cipher_destroy(aCipher
);
127 rtlCipher aCipher
= rtl_cipher_createBF(rtl_Cipher_ModeCBC
);
128 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
129 rtl_cipher_destroy(aCipher
);
133 rtlCipher aCipher
= rtl_cipher_createBF(rtl_Cipher_ModeStream
);
134 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
135 rtl_cipher_destroy(aCipher
);
139 rtlCipher aCipher
= rtl_cipher_createBF(rtl_Cipher_ModeInvalid
);
140 CPPUNIT_ASSERT_MESSAGE("create provide wrong object.", aCipher
== NULL
);
141 // rtl_cipher_destroy(aCipher);
143 // Change the following lines only, if you add, remove or rename
144 // member functions of the current class,
145 // because these macros are need by auto register mechanism.
147 CPPUNIT_TEST_SUITE(createBF
);
148 CPPUNIT_TEST(createBF_001
);
149 CPPUNIT_TEST(createBF_002
);
150 CPPUNIT_TEST(createBF_003
);
151 CPPUNIT_TEST(createBF_004
);
152 CPPUNIT_TEST_SUITE_END();
154 // -----------------------------------------------------------------------------
155 class decode
: public CppUnit::TestFixture
158 // initialise your test code values here.
167 void test_encode(sal_uInt8 _nKeyValue
, sal_uInt8 _nArgValue
, rtl::OString
const& _sPlainTextStr
)
169 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeECB
);
170 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
172 sal_uInt32 nKeyLen
= 16;
173 sal_uInt8
*pKeyBuffer
= new sal_uInt8
[ nKeyLen
];
174 memset(pKeyBuffer
, 0, nKeyLen
);
175 pKeyBuffer
[0] = _nKeyValue
;
177 sal_uInt32 nArgLen
= 16;
178 sal_uInt8
*pArgBuffer
= new sal_uInt8
[ nArgLen
];
179 memset(pArgBuffer
, 0, nArgLen
);
180 pArgBuffer
[0] = _nArgValue
;
182 rtlCipherError aError
= rtl_cipher_init(aCipher
, rtl_Cipher_DirectionEncode
, pKeyBuffer
, nKeyLen
, pArgBuffer
, nArgLen
);
183 CPPUNIT_ASSERT_MESSAGE("wrong init", aError
== rtl_Cipher_E_None
);
185 sal_uInt32 nPlainTextLen
= 16;
186 sal_uInt8
*pPlainTextBuffer
= new sal_uInt8
[ nPlainTextLen
];
187 memset(pPlainTextBuffer
, 0, nPlainTextLen
);
188 strncpy((char*)pPlainTextBuffer
, _sPlainTextStr
.getStr(), 16);
190 sal_uInt32 nCipherLen
= 16;
191 sal_uInt8
*pCipherBuffer
= new sal_uInt8
[ nCipherLen
];
192 memset(pCipherBuffer
, 0, nCipherLen
);
194 /* rtlCipherError */ aError
= rtl_cipher_encode(aCipher
, pPlainTextBuffer
, nPlainTextLen
, pCipherBuffer
, nCipherLen
);
195 CPPUNIT_ASSERT_MESSAGE("wrong encode", aError
== rtl_Cipher_E_None
);
197 sal_uInt32 nPlainText2Len
= 16;
198 sal_uInt8
*pPlainText2Buffer
= new sal_uInt8
[ nPlainText2Len
];
199 memset(pPlainText2Buffer
, 0, nPlainText2Len
);
201 /* rtlCipherError */ aError
= rtl_cipher_decode(aCipher
, pCipherBuffer
, nCipherLen
, pPlainText2Buffer
, nPlainText2Len
);
202 CPPUNIT_ASSERT_MESSAGE("decode should not work", aError
!= rtl_Cipher_E_None
);
204 delete [] pPlainText2Buffer
;
206 delete [] pCipherBuffer
;
207 delete [] pPlainTextBuffer
;
209 delete [] pArgBuffer
;
210 delete [] pKeyBuffer
;
212 rtl_cipher_destroy(aCipher
);
215 void test_encode_and_decode(sal_uInt8 _nKeyValue
, sal_uInt8 _nArgValue
, rtl::OString
const& _sPlainTextStr
)
217 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeECB
);
218 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
220 sal_uInt32 nKeyLen
= 16;
221 sal_uInt8
*pKeyBuffer
= new sal_uInt8
[ nKeyLen
];
222 memset(pKeyBuffer
, 0, nKeyLen
);
223 pKeyBuffer
[0] = _nKeyValue
;
225 sal_uInt32 nArgLen
= 16;
226 sal_uInt8
*pArgBuffer
= new sal_uInt8
[ nArgLen
];
227 memset(pArgBuffer
, 0, nArgLen
);
228 pArgBuffer
[0] = _nArgValue
;
230 rtlCipherError aError
= rtl_cipher_init(aCipher
, rtl_Cipher_DirectionBoth
, pKeyBuffer
, nKeyLen
, pArgBuffer
, nArgLen
);
231 CPPUNIT_ASSERT_MESSAGE("wrong init", aError
== rtl_Cipher_E_None
);
233 sal_uInt32 nPlainTextLen
= 16;
234 sal_uInt8
*pPlainTextBuffer
= new sal_uInt8
[ nPlainTextLen
];
235 memset(pPlainTextBuffer
, 0, nPlainTextLen
);
236 strncpy((char*)pPlainTextBuffer
, _sPlainTextStr
.getStr(), 16);
238 sal_uInt32 nCipherLen
= 16;
239 sal_uInt8
*pCipherBuffer
= new sal_uInt8
[ nCipherLen
];
240 memset(pCipherBuffer
, 0, nCipherLen
);
242 /* rtlCipherError */ aError
= rtl_cipher_encode(aCipher
, pPlainTextBuffer
, nPlainTextLen
, pCipherBuffer
, nCipherLen
);
243 CPPUNIT_ASSERT_MESSAGE("wrong encode", aError
== rtl_Cipher_E_None
);
245 sal_uInt32 nPlainText2Len
= 16;
246 sal_uInt8
*pPlainText2Buffer
= new sal_uInt8
[ nPlainText2Len
];
247 memset(pPlainText2Buffer
, 0, nPlainText2Len
);
249 /* rtlCipherError */ aError
= rtl_cipher_decode(aCipher
, pCipherBuffer
, nCipherLen
, pPlainText2Buffer
, nPlainText2Len
);
250 CPPUNIT_ASSERT_MESSAGE("wrong decode", aError
== rtl_Cipher_E_None
);
252 sal_Int32 nCompare
= memcmp(pPlainTextBuffer
, pPlainText2Buffer
, 16);
254 CPPUNIT_ASSERT_MESSAGE("compare between plain and decoded plain failed", nCompare
== 0);
256 delete [] pPlainText2Buffer
;
258 delete [] pCipherBuffer
;
259 delete [] pPlainTextBuffer
;
261 delete [] pArgBuffer
;
262 delete [] pKeyBuffer
;
264 rtl_cipher_destroy(aCipher
);
269 test_encode_and_decode(0,0,"");
270 test_encode_and_decode(0,0,"hallo");
271 test_encode_and_decode(1,0,"B2Aahg5B");
272 test_encode_and_decode(1,2,"Longer text string");
278 test_encode(0,0,"hallo");
279 test_encode(1,0,"B2Aahg5B");
280 test_encode(1,2,"Longer text string");
282 // Change the following lines only, if you add, remove or rename
283 // member functions of the current class,
284 // because these macros are need by auto register mechanism.
286 CPPUNIT_TEST_SUITE(decode
);
287 CPPUNIT_TEST(decode_001
);
288 CPPUNIT_TEST(decode_002
);
289 CPPUNIT_TEST_SUITE_END();
291 // -----------------------------------------------------------------------------
292 class decodeBF
: public CppUnit::TestFixture
295 // initialise your test code values here.
307 // Change the following lines only, if you add, remove or rename
308 // member functions of the current class,
309 // because these macros are need by auto register mechanism.
311 CPPUNIT_TEST_SUITE(decodeBF
);
312 CPPUNIT_TEST(decodeBF_001
);
313 CPPUNIT_TEST_SUITE_END();
315 // -----------------------------------------------------------------------------
316 class destroy
: public CppUnit::TestFixture
319 // initialise your test code values here.
330 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeCBC
);
331 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
332 rtl_cipher_destroy(aCipher
);
334 // Change the following lines only, if you add, remove or rename
335 // member functions of the current class,
336 // because these macros are need by auto register mechanism.
338 CPPUNIT_TEST_SUITE(destroy
);
339 CPPUNIT_TEST(destroy_001
);
340 CPPUNIT_TEST_SUITE_END();
342 // -----------------------------------------------------------------------------
343 class destroyBF
: public CppUnit::TestFixture
346 // initialise your test code values here.
357 rtlCipher aCipher
= rtl_cipher_createBF(rtl_Cipher_ModeECB
);
358 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
359 rtl_cipher_destroyBF(aCipher
);
363 // Change the following lines only, if you add, remove or rename
364 // member functions of the current class,
365 // because these macros are need by auto register mechanism.
367 CPPUNIT_TEST_SUITE(destroyBF
);
368 CPPUNIT_TEST(destroyBF_001
);
369 CPPUNIT_TEST_SUITE_END();
370 }; // class destroyBF
371 // -----------------------------------------------------------------------------
372 class encode
: public CppUnit::TestFixture
375 // initialise your test code values here.
384 void test_encode(sal_uInt8 _nKeyValue
, sal_uInt8 _nArgValue
, sal_uInt8 _nDataValue
)
386 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeECB
);
387 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
389 sal_uInt32 nKeyLen
= 16;
390 sal_uInt8
*pKeyBuffer
= new sal_uInt8
[ nKeyLen
];
391 memset(pKeyBuffer
, 0, nKeyLen
);
392 pKeyBuffer
[0] = _nKeyValue
;
394 sal_uInt32 nArgLen
= 16;
395 sal_uInt8
*pArgBuffer
= new sal_uInt8
[ nArgLen
];
396 memset(pArgBuffer
, 0, nArgLen
);
397 pArgBuffer
[0] = _nArgValue
;
399 rtlCipherError aError
= rtl_cipher_init(aCipher
, rtl_Cipher_DirectionEncode
, pKeyBuffer
, nKeyLen
, pArgBuffer
, nArgLen
);
400 CPPUNIT_ASSERT_MESSAGE("wrong init", aError
== rtl_Cipher_E_None
);
402 sal_uInt32 nDataLen
= 16;
403 sal_uInt8
*pDataBuffer
= new sal_uInt8
[ nDataLen
];
404 memset(pDataBuffer
, 0, nDataLen
);
405 pDataBuffer
[0] = _nDataValue
;
407 sal_uInt32 nLen
= 16;
408 sal_uInt8
*pBuffer
= new sal_uInt8
[ nLen
];
409 memset(pBuffer
, 0, nLen
);
411 /* rtlCipherError */ aError
= rtl_cipher_encode(aCipher
, pDataBuffer
, nDataLen
, pBuffer
, nLen
);
412 CPPUNIT_ASSERT_MESSAGE("wrong encode", aError
== rtl_Cipher_E_None
);
415 delete [] pDataBuffer
;
417 delete [] pArgBuffer
;
418 delete [] pKeyBuffer
;
420 rtl_cipher_destroy(aCipher
);
436 // Change the following lines only, if you add, remove or rename
437 // member functions of the current class,
438 // because these macros are need by auto register mechanism.
440 CPPUNIT_TEST_SUITE(encode
);
441 CPPUNIT_TEST(encode_001
);
442 CPPUNIT_TEST_SUITE_END();
444 // -----------------------------------------------------------------------------
445 class encodeBF
: public CppUnit::TestFixture
448 // initialise your test code values here.
460 // Change the following lines only, if you add, remove or rename
461 // member functions of the current class,
462 // because these macros are need by auto register mechanism.
464 CPPUNIT_TEST_SUITE(encodeBF
);
465 CPPUNIT_TEST(encodeBF_001
);
466 CPPUNIT_TEST_SUITE_END();
468 // -----------------------------------------------------------------------------
469 class init
: public CppUnit::TestFixture
472 // initialise your test code values here.
483 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeECB
);
484 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
486 sal_uInt32 nKeyLen
= 16;
487 sal_uInt8
*pKeyBuffer
= new sal_uInt8
[ nKeyLen
];
488 memset(pKeyBuffer
, 0, nKeyLen
);
490 sal_uInt32 nArgLen
= 16;
491 sal_uInt8
*pArgBuffer
= new sal_uInt8
[ nArgLen
];
492 memset(pArgBuffer
, 0, nArgLen
);
494 rtlCipherError aError
= rtl_cipher_init(aCipher
, rtl_Cipher_DirectionEncode
, pKeyBuffer
, nKeyLen
, pArgBuffer
, nArgLen
);
495 CPPUNIT_ASSERT_MESSAGE("wrong init", aError
== rtl_Cipher_E_None
);
497 delete [] pArgBuffer
;
498 delete [] pKeyBuffer
;
500 rtl_cipher_destroy(aCipher
);
505 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeECB
);
506 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
508 sal_uInt32 nKeyLen
= 16;
509 sal_uInt8
*pKeyBuffer
= new sal_uInt8
[ nKeyLen
];
510 memset(pKeyBuffer
, 0, nKeyLen
);
513 sal_uInt32 nArgLen
= 16;
514 sal_uInt8
*pArgBuffer
= new sal_uInt8
[ nArgLen
];
515 memset(pArgBuffer
, 0, nArgLen
);
517 rtlCipherError aError
= rtl_cipher_init(aCipher
, rtl_Cipher_DirectionEncode
, pKeyBuffer
, nKeyLen
, pArgBuffer
, nArgLen
);
518 CPPUNIT_ASSERT_MESSAGE("wrong init", aError
== rtl_Cipher_E_None
);
520 delete [] pArgBuffer
;
521 delete [] pKeyBuffer
;
523 rtl_cipher_destroy(aCipher
);
527 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeECB
);
528 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
530 sal_uInt32 nKeyLen
= 16;
531 sal_uInt8
*pKeyBuffer
= new sal_uInt8
[ nKeyLen
];
532 memset(pKeyBuffer
, 0, nKeyLen
);
534 sal_uInt32 nArgLen
= 16;
535 sal_uInt8
*pArgBuffer
= new sal_uInt8
[ nArgLen
];
536 memset(pArgBuffer
, 0, nArgLen
);
539 rtlCipherError aError
= rtl_cipher_init(aCipher
, rtl_Cipher_DirectionEncode
, pKeyBuffer
, nKeyLen
, pArgBuffer
, nArgLen
);
540 CPPUNIT_ASSERT_MESSAGE("wrong init", aError
== rtl_Cipher_E_None
);
542 delete [] pArgBuffer
;
543 delete [] pKeyBuffer
;
545 rtl_cipher_destroy(aCipher
);
549 rtlCipher aCipher
= rtl_cipher_create(rtl_Cipher_AlgorithmBF
, rtl_Cipher_ModeECB
);
550 CPPUNIT_ASSERT_MESSAGE("create failed.", aCipher
!= NULL
);
552 sal_uInt32 nKeyLen
= 16;
553 sal_uInt8
*pKeyBuffer
= new sal_uInt8
[ nKeyLen
];
554 memset(pKeyBuffer
, 0, nKeyLen
);
557 sal_uInt32 nArgLen
= 16;
558 sal_uInt8
*pArgBuffer
= new sal_uInt8
[ nArgLen
];
559 memset(pArgBuffer
, 0, nArgLen
);
562 rtlCipherError aError
= rtl_cipher_init(aCipher
, rtl_Cipher_DirectionEncode
, pKeyBuffer
, nKeyLen
, pArgBuffer
, nArgLen
);
563 CPPUNIT_ASSERT_MESSAGE("wrong init", aError
== rtl_Cipher_E_None
);
565 delete [] pArgBuffer
;
566 delete [] pKeyBuffer
;
568 rtl_cipher_destroy(aCipher
);
570 // Change the following lines only, if you add, remove or rename
571 // member functions of the current class,
572 // because these macros are need by auto register mechanism.
574 CPPUNIT_TEST_SUITE(init
);
575 CPPUNIT_TEST(init_001
);
576 CPPUNIT_TEST(init_002
);
577 CPPUNIT_TEST(init_003
);
578 CPPUNIT_TEST(init_004
);
579 CPPUNIT_TEST_SUITE_END();
581 // -----------------------------------------------------------------------------
582 class initBF
: public CppUnit::TestFixture
585 // initialise your test code values here.
596 // seems to be the same as init, so empty
599 // Change the following lines only, if you add, remove or rename
600 // member functions of the current class,
601 // because these macros are need by auto register mechanism.
603 CPPUNIT_TEST_SUITE(initBF
);
604 CPPUNIT_TEST(initBF_001
);
605 CPPUNIT_TEST_SUITE_END();
608 // -----------------------------------------------------------------------------
610 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::create
);
611 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::createBF
);
612 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::decode
);
613 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::decodeBF
);
614 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::destroy
);
615 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::destroyBF
);
616 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::encode
);
617 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::encodeBF
);
618 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::init
);
619 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_cipher::initBF
);
621 } // namespace rtl_cipher
624 // -----------------------------------------------------------------------------
626 // this macro creates an empty function, which will called by the RegisterAllFunctions()
627 // to let the user the possibility to also register some functions by hand.
628 CPPUNIT_PLUGIN_IMPLEMENT();
630 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */