Commit aca8ab9b61e1480270019817a539812432405363
1 parent
4085de54
add tests form crypt class @TODO: add assertion on length
Showing
1 changed file
with
81 additions
and
0 deletions
tests/cryptTest.c
0 → 100644
| 1 | +#include <mcrypt.h> | ||
| 2 | +#include <stdlib.h> | ||
| 3 | +#include <sys/types.h> | ||
| 4 | + | ||
| 5 | +#include "runtest.h" | ||
| 6 | +#include "token/cclass.h" | ||
| 7 | +#include "token/crypt.h" | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +#define DATA "testdata to be encrypted" | ||
| 11 | +#define PASSWORD "1234" | ||
| 12 | + | ||
| 13 | + | ||
| 14 | +const char testname[] = "cryptTest"; | ||
| 15 | +struct CRYPT * crypt = NULL; | ||
| 16 | + | ||
| 17 | + | ||
| 18 | +static | ||
| 19 | +void | ||
| 20 | +__setUp() | ||
| 21 | +{ | ||
| 22 | + crypt = new(CRYPT, MCRYPT_RIJNDAEL_256, MCRYPT_CFB); | ||
| 23 | +} | ||
| 24 | +void (* const setUp)() = __setUp; | ||
| 25 | + | ||
| 26 | +static | ||
| 27 | +void | ||
| 28 | +__tearDown() | ||
| 29 | +{ | ||
| 30 | + delete(&crypt); | ||
| 31 | +} | ||
| 32 | +void (* const tearDown)() = __tearDown; | ||
| 33 | + | ||
| 34 | +static | ||
| 35 | +int | ||
| 36 | +testEncryptDecryptCycle() | ||
| 37 | +{ | ||
| 38 | + size_t length = strlen(DATA);; | ||
| 39 | + void * encrypted = crypt_encrypt(crypt, DATA, PASSWORD, &length); | ||
| 40 | + void * decrypted = crypt_decrypt(crypt, encrypted, PASSWORD, &length); | ||
| 41 | + | ||
| 42 | + ASSERT_NOT_NULL(encrypted); | ||
| 43 | + ASSERT_NOT_NULL(decrypted); | ||
| 44 | + ASSERT_MEM_EQUAL(DATA, decrypted, length); | ||
| 45 | + | ||
| 46 | + free(encrypted); | ||
| 47 | + free(decrypted); | ||
| 48 | + | ||
| 49 | + return TEST_OK; | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +static | ||
| 53 | +int | ||
| 54 | +testCryptDifference() | ||
| 55 | +{ | ||
| 56 | + size_t length1, length2; | ||
| 57 | + void * encrypted1, * encrypted2; | ||
| 58 | + | ||
| 59 | + length1 = length2 = strlen(DATA); | ||
| 60 | + | ||
| 61 | + encrypted1 = crypt_encrypt(crypt, DATA, PASSWORD, &length1); | ||
| 62 | + encrypted2 = crypt_encrypt(crypt, DATA, PASSWORD, &length2); | ||
| 63 | + | ||
| 64 | + ASSERT_EQUAL(length1, length2); | ||
| 65 | + ASSERT_NOT_NULL(encrypted1); | ||
| 66 | + ASSERT_NOT_NULL(encrypted2); | ||
| 67 | + ASSERT_MEM_NOT_EQUAL(encrypted1, encrypted2, length1); | ||
| 68 | + | ||
| 69 | + free(encrypted1); | ||
| 70 | + free(encrypted2); | ||
| 71 | + | ||
| 72 | + return TEST_OK; | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +const testfunc tests[] = { | ||
| 76 | + testEncryptDecryptCycle, | ||
| 77 | + testCryptDifference | ||
| 78 | +}; | ||
| 79 | +const size_t count = FUNCS_COUNT(tests); | ||
| 80 | + | ||
| 81 | +// vim: set et ts=4 sw=4: |
Please
register
or
login
to post a comment