diff options
author | Alexander Golubev <fatzer2@gmail.com> | 2016-12-21 01:13:31 +0300 |
---|---|---|
committer | Slávek Banko <slavek.banko@axis.cz> | 2017-07-22 15:56:51 +0200 |
commit | 73423048c5291d285a0a5142d5b89b2b2e14819b (patch) | |
tree | f01f1be8607cc54d91d7e366247cbb49b61a2e77 /kwallet/backend | |
parent | 933dc8a3f8b14eff9c51c1a7ece8c29754ad378a (diff) | |
download | tdelibs-73423048c5291d285a0a5142d5b89b2b2e14819b.tar.gz tdelibs-73423048c5291d285a0a5142d5b89b2b2e14819b.zip |
tdeui & tdewallet: add tests
Signed-off-by: Alexander Golubev <fatzer2@gmail.com>
(cherry picked from commit 16a176dab1e978bef8f8d49801fa98a028d1d17b)
Diffstat (limited to 'kwallet/backend')
-rw-r--r-- | kwallet/backend/CMakeLists.txt | 2 | ||||
-rw-r--r-- | kwallet/backend/tests/CMakeLists.txt | 33 | ||||
-rw-r--r-- | kwallet/backend/tests/backendtest.cpp | 46 | ||||
-rw-r--r-- | kwallet/backend/tests/testsha.cpp | 54 |
4 files changed, 102 insertions, 33 deletions
diff --git a/kwallet/backend/CMakeLists.txt b/kwallet/backend/CMakeLists.txt index d447cc255..6ff630d18 100644 --- a/kwallet/backend/CMakeLists.txt +++ b/kwallet/backend/CMakeLists.txt @@ -9,6 +9,8 @@ # ################################################# +add_subdirectory ( tests ) + include_directories( ${TQT_INCLUDE_DIRS} ${CMAKE_BINARY_DIR} diff --git a/kwallet/backend/tests/CMakeLists.txt b/kwallet/backend/tests/CMakeLists.txt new file mode 100644 index 000000000..5f79bd5d9 --- /dev/null +++ b/kwallet/backend/tests/CMakeLists.txt @@ -0,0 +1,33 @@ +################################################# +# +# (C) 2016 Alexander Golubev +# fatzer2 (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +include_directories( + ${TQT_INCLUDE_DIRS} + ${CMAKE_BINARY_DIR} + ${CMAKE_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/dcop + ${CMAKE_SOURCE_DIR}/kwallet/client + ${CMAKE_SOURCE_DIR}/kwallet/backend + ${CMAKE_BINARY_DIR}/kdecore + ${CMAKE_SOURCE_DIR}/kdecore +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + +tde_add_check_executable( backendtest AUTOMOC + LINK kwalletbackend-shared kwalletclient-shared TEST ) +tde_add_check_executable( testbf AUTOMOC + LINK kwalletbackend-shared kwalletclient-shared TEST ) +tde_add_check_executable( testsha AUTOMOC + LINK kwalletbackend-shared kwalletclient-shared TEST ) diff --git a/kwallet/backend/tests/backendtest.cpp b/kwallet/backend/tests/backendtest.cpp index e7599b3a2..c96fa42ce 100644 --- a/kwallet/backend/tests/backendtest.cpp +++ b/kwallet/backend/tests/backendtest.cpp @@ -2,12 +2,28 @@ #include <stdio.h> #include <kapplication.h> +#include <kaboutdata.h> +#include <kcmdlineargs.h> #include <tqstring.h> #include "kwalletbackend.h" +#define CHECK_RETURN(func, test, test_str) { \ + int rc = (func); \ + test_cnt++;\ + if (test) {\ + printf("%-20s returned %d as expected (should be %s)\n", #func, rc, test_str);\ + } else {\ + printf("%-20s returned %d UNEXPECTEDLY (should be %s)\n", #func, rc, test_str);\ + test_failed++;\ + }\ +} + int main(int argc, char **argv) { - KApplication a(argc, argv, "kwalletbackendtest"); + KAboutData aboutData( "kwalletbackendtest", "kwallet backend testing routine", "0.1" ); + + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication a(false, false); KWallet::Backend be("ktestwallet"); printf("KWalletBackend constructed\n"); @@ -18,28 +34,22 @@ int main(int argc, char **argv) { bpass.duplicate("bpassword", 9); cpass.duplicate("cpassword", 9); - printf("Passwords initialised.\n"); - int rc = be.close(apass); - - printf("be.close(apass) returned %d (should be -255)\n", rc); - rc = be.open(bpass); + int test_cnt = 0; + int test_failed = 0; - printf("be.open(bpass) returned %d (should be 0 or 1)\n", rc); - - rc = be.close(bpass); - - printf("be.close(bpass) returned %d (should be 0)\n", rc); - - rc = be.open(apass); - - printf("be.open(apass) returned %d (should be negative)\n", rc); + printf("Passwords initialised.\n"); - rc = be.open(bpass); + CHECK_RETURN(be.close(apass), rc==-255, "-255"); + CHECK_RETURN(be.open(bpass), rc==0 || rc==1, "0 or 1"); + CHECK_RETURN(be.close(bpass), rc==0, "0 or 1"); + CHECK_RETURN(be.open(apass), rc<0, "negative"); + CHECK_RETURN(be.open(bpass), rc==0, "0"); - printf("be.open(bpass) returned %d (should be 0)\n", rc); + printf ("===========================================\n"); + printf ("%d test failed out of %d\n", test_failed, test_cnt); - return 0; + return test_failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/kwallet/backend/tests/testsha.cpp b/kwallet/backend/tests/testsha.cpp index 70879f015..cc6263edb 100644 --- a/kwallet/backend/tests/testsha.cpp +++ b/kwallet/backend/tests/testsha.cpp @@ -3,12 +3,27 @@ #include <string.h> #include "sha1.h" +void printHex (const unsigned char *data) { + for (int i = 0; i < 20; i++) { + printf("%.2X", *data++); + if (i>0 && (i-1)%2 == 0) printf(" "); + } + printf("\n"); +} int main() { -SHA1 *sha1; -unsigned char data[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; -unsigned long et[] = {0x11223344}; -int rc; + SHA1 *sha1; + const unsigned char data[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; + const unsigned char expected[20] = { + 0x84, 0x98, 0x3e, 0x44, + 0x1c, 0x3b, 0xd2, 0x6e, + 0xba, 0xae, 0x4a, 0xa1, + 0xf9, 0x51, 0x29, 0xe5, + 0xe5, 0x46, 0x70, 0xf1 + }; + + unsigned long et[] = {0x11223344}; + int rc; printf("%d: 0x11 == %d and 0x44 == %d\n", ((unsigned char *)et)[0], 0x11, 0x44); @@ -22,22 +37,31 @@ int rc; printf("About to process [%s]\n", data); rc = sha1->process(data, strlen((char *)data)); - if (rc != strlen((char *)data)) { + if (rc != (int)strlen((char *)data)) { printf("Error processing the data. rc=%d\n", rc); + return -1; } else printf("Done.\n"); -const unsigned char *res = sha1->getHash(); + const unsigned char *res = sha1->hash(); if (res) { - for (int i = 0; i < 20; i++) { - printf("%.2X", *res++); - if (i>0 && (i-1)%2 == 0) printf(" "); - } - printf("\n"); - } else printf("Error - getHash() returned NULL!\n"); + if (memcmp (res, expected, 20) ==0 ) { + printf("The result is expected: "); + printHex (res); + } else { + printf("The result is different from expected:\n"); + printf("Result: "); + printHex (res); + printf("Expected: "); + printHex (expected); + return -1; + } + } else { + printf("Error - hash() returned NULL!\n"); + return -1; + } delete sha1; -} - - + return 0; +} |