diff options
Diffstat (limited to 'tdewallet/backend/tests/testsha.cpp')
-rw-r--r-- | tdewallet/backend/tests/testsha.cpp | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/tdewallet/backend/tests/testsha.cpp b/tdewallet/backend/tests/testsha.cpp index 70879f015..cc6263edb 100644 --- a/tdewallet/backend/tests/testsha.cpp +++ b/tdewallet/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; +} |