1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
#include <test.h>
#include <unistd.h>
#include <string.h>
#include <string_lib.h>
#include <hash.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bn.h>
/*
* To test the hash implementation we need three things:
* - A string
* - A file
* - A RSA key
* We run one test for each, using two algorithms, MD5 and SHA256.
*/
static int initialized = 0;
static char message[] = "This is a message";
static int message_length = 0;
static char file[] = "/tmp/hashXXXXXX";
static int fd = -1;
static RSA *rsa = NULL;
void tests_setup()
{
int result = 0;
fd = mkstemp(file);
if (fd < 0)
{
initialized = 0;
return;
}
message_length = strlen(message);
result = write(fd, message, message_length);
if (result < 0)
{
close (fd);
unlink (file);
initialized = 0;
return;
}
OPENSSL_init_crypto(0, NULL);
initialized = 1;
rsa = RSA_new();
if (rsa)
{
BIGNUM *bn = NULL;
bn = BN_new();
if (!bn)
{
close (fd);
unlink (file);
RSA_free(rsa);
initialized = 0;
return;
}
BN_set_word(bn, RSA_F4);
RSA_generate_key_ex(rsa, 1024, bn, NULL);
BN_free(bn);
}
}
void tests_teardown()
{
if (fd >= 0)
{
close (fd);
unlink (file);
}
if (rsa)
{
RSA_free(rsa);
}
initialized = 0;
}
#define ASSERT_IF_NOT_INITIALIZED \
assert_int_equal(1, initialized)
/*
* Tests
* Each test does the same but from different sources, this is:
* 1. Create a new hash structure using MD5
* 2. Check the length of the generated hash.
* 3. Check the printable version (check that is not NULL).
* 4. Destroy the hash structure.
* 5. Create a new hash structure using SHA256.
* 6. Check the length of the generated hash.
* 7. Check the printable version (check that is not NULL).
* 8. Destroy the hash structure.
*/
static void test_HashString(void)
{
ASSERT_IF_NOT_INITIALIZED;
Hash *hash = NULL;
unsigned int length = 0;
assert_true(hash == NULL);
hash = HashNew(message, message_length, HASH_METHOD_MD5);
assert_true(hash != NULL);
assert_int_equal(HASH_METHOD_MD5, HashType(hash));
assert_int_equal(CF_MD5_LEN, HashLength(hash));
assert_true(HashData(hash, &length) != NULL);
assert_int_equal(length, CF_MD5_LEN);
assert_true(HashPrintable(hash) != NULL);
const char *md5_hash = HashPrintable(hash);
assert_true((md5_hash[0] == 'M') && (md5_hash[1] == 'D') && (md5_hash[2] == '5') && (md5_hash[3] == '='));
HashDestroy(&hash);
assert_true(hash == NULL);
hash = HashNew(message, message_length, HASH_METHOD_SHA256);
assert_true(hash != NULL);
assert_int_equal(HASH_METHOD_SHA256, HashType(hash));
assert_int_equal(CF_SHA256_LEN, HashLength(hash));
assert_true(HashData(hash, &length) != NULL);
assert_int_equal(length, CF_SHA256_LEN);
assert_true(HashPrintable(hash) != NULL);
const char *sha256_hash = HashPrintable(hash);
assert_true((sha256_hash[0] == 'S') && (sha256_hash[1] == 'H') && (sha256_hash[2] == 'A') && (sha256_hash[3] == '='));
HashDestroy(&hash);
/* Negative cases */
assert_true(HashNew(NULL, message_length, HASH_METHOD_MD5) == NULL);
assert_true(HashNew(message, 0, HASH_METHOD_MD5) == NULL);
assert_true(HashNew(message, message_length, HASH_METHOD_NONE) == NULL);
assert_true(HashNew(message, 0, HASH_METHOD_NONE) == NULL);
assert_true(HashNew(NULL, message_length, HASH_METHOD_NONE) == NULL);
}
static void test_HashDescriptor(void)
{
ASSERT_IF_NOT_INITIALIZED;
Hash *hash = NULL;
unsigned int length = 0;
assert_true(hash == NULL);
hash = HashNewFromDescriptor(fd, HASH_METHOD_MD5);
assert_true(hash != NULL);
assert_int_equal(HASH_METHOD_MD5, HashType(hash));
assert_int_equal(CF_MD5_LEN, HashLength(hash));
assert_true(HashData(hash, &length) != NULL);
assert_int_equal(length, CF_MD5_LEN);
assert_true(HashPrintable(hash) != NULL);
HashDestroy(&hash);
assert_true(hash == NULL);
hash = HashNewFromDescriptor(fd, HASH_METHOD_SHA256);
assert_true(hash != NULL);
assert_int_equal(HASH_METHOD_SHA256, HashType(hash));
assert_int_equal(CF_SHA256_LEN, HashLength(hash));
assert_true(HashData(hash, &length) != NULL);
assert_int_equal(length, CF_SHA256_LEN);
assert_true(HashPrintable(hash) != NULL);
HashDestroy(&hash);
/* Negative cases */
assert_true(HashNewFromDescriptor(-1, HASH_METHOD_MD5) == NULL);
assert_true(HashNewFromDescriptor(fd, HASH_METHOD_NONE) == NULL);
assert_true(HashNewFromDescriptor(-1, HASH_METHOD_NONE) == NULL);
}
static void test_HashKey(void)
{
ASSERT_IF_NOT_INITIALIZED;
Hash *hash = NULL;
unsigned int length = 0;
assert_true(hash == NULL);
hash = HashNewFromKey(rsa, HASH_METHOD_MD5);
assert_true(hash != NULL);
assert_int_equal(HASH_METHOD_MD5, HashType(hash));
assert_int_equal(CF_MD5_LEN, HashLength(hash));
assert_true(HashData(hash, &length) != NULL);
assert_int_equal(length, CF_MD5_LEN);
assert_true(HashPrintable(hash) != NULL);
HashDestroy(&hash);
assert_true(hash == NULL);
hash = HashNewFromKey(rsa, HASH_METHOD_SHA256);
assert_true(hash != NULL);
assert_int_equal(HASH_METHOD_SHA256, HashType(hash));
assert_int_equal(CF_SHA256_LEN, HashLength(hash));
assert_true(HashData(hash, &length) != NULL);
assert_int_equal(length, CF_SHA256_LEN);
assert_true(HashPrintable(hash) != NULL);
HashDestroy(&hash);
/* Negative cases */
assert_true(HashNewFromKey(NULL, HASH_METHOD_MD5) == NULL);
assert_true(HashNewFromKey(rsa, HASH_METHOD_NONE) == NULL);
assert_true(HashNewFromKey(NULL, HASH_METHOD_NONE) == NULL);
}
static void test_HashCopy(void)
{
ASSERT_IF_NOT_INITIALIZED;
Hash *hash = NULL;
Hash *copy = NULL;
unsigned int length = 0;
assert_true(hash == NULL);
hash = HashNew(message, message_length, HASH_METHOD_MD5);
assert_true(hash != NULL);
assert_int_equal(HASH_METHOD_MD5, HashType(hash));
assert_int_equal(CF_MD5_LEN, HashLength(hash));
assert_true(HashData(hash, &length) != NULL);
assert_int_equal(length, CF_MD5_LEN);
assert_true(HashPrintable(hash) != NULL);
assert_int_equal(0, HashCopy(hash, ©));
assert_int_equal(HASH_METHOD_MD5, HashType(copy));
assert_int_equal(CF_MD5_LEN, HashLength(copy));
assert_true(HashData(copy, &length) != NULL);
assert_int_equal(length, CF_MD5_LEN);
assert_true(HashPrintable(copy) != NULL);
assert_string_equal(HashPrintable(hash), HashPrintable(copy));
HashDestroy(©);
assert_true(copy == NULL);
/* Negative cases */
assert_int_equal(-1, HashCopy(NULL, ©));
assert_int_equal(-1, HashCopy(hash, NULL));
assert_int_equal(-1, HashCopy(NULL, NULL));
/* Finish */
HashDestroy(&hash);
assert_true(hash == NULL);
}
static void test_HashesMatch(void)
{
unsigned char digest_a[EVP_MAX_MD_SIZE + 1] = { 0 };
unsigned char digest_b[EVP_MAX_MD_SIZE + 1] = { 0 };
unsigned char digest_c[EVP_MAX_MD_SIZE + 1] = { 0 };
unsigned char digest_d[EVP_MAX_MD_SIZE + 1] = { 0 };
HashString("abc", 4, digest_a, HASH_METHOD_MD5);
HashString("abc", 4, digest_b, HASH_METHOD_MD5);
HashString("abc", 3, digest_c, HASH_METHOD_MD5);
HashString("abd", 4, digest_d, HASH_METHOD_MD5);
assert_true(HashesMatch(digest_a, digest_b, HASH_METHOD_MD5));
assert_false(HashesMatch(digest_b, digest_c, HASH_METHOD_MD5));
assert_false(HashesMatch(digest_c, digest_d, HASH_METHOD_MD5));
assert_false(HashesMatch(digest_a, digest_d, HASH_METHOD_MD5));
}
static void test_StringCopyTruncateAndHashIfNecessary(void)
{
char buf[40];
int ret;
const char *thirty_nine = "123456789_123456789_123456789_123456789";
ret = StringCopyTruncateAndHashIfNecessary(thirty_nine, buf, 40);
assert_string_equal(thirty_nine, buf);
assert_int_equal(ret, 39);
StringCopyTruncateAndHashIfNecessary("abc", buf, 40);
assert_string_equal("abc", buf);
const char *too_long = "The quick brown fox jumps over the lazy dog";
ret = StringCopyTruncateAndHashIfNecessary(too_long, buf, 40);
assert_int_equal(ret, 40);
assert_false(StringEqual(too_long, buf));
assert_true(strlen(too_long) != strlen(buf));
assert_int_equal(strlen(buf), 39);
assert_true(StringEqual(buf, "Th#MD5=9e107d9d372bb6826bd81d3542a419d6"));
ret = StringCopyTruncateAndHashIfNecessary(too_long, buf, 39);
assert_int_equal(ret, 39);
assert_true(StringEqual(buf, "T#MD5=9e107d9d372bb6826bd81d3542a419d6"));
ret = StringCopyTruncateAndHashIfNecessary(too_long, buf, 38);
assert_int_equal(ret, 38);
assert_true(StringEqual(buf, "#MD5=9e107d9d372bb6826bd81d3542a419d6"));
}
/*
* Main routine
* Notice the calls to both setup and teardown.
*/
int main()
{
PRINT_TEST_BANNER();
tests_setup();
const UnitTest tests[] =
{
unit_test(test_HashString),
unit_test(test_HashDescriptor),
unit_test(test_HashKey),
unit_test(test_HashCopy),
unit_test(test_HashesMatch),
unit_test(test_StringCopyTruncateAndHashIfNecessary),
};
int result = run_tests(tests);
tests_teardown();
return result;
}
|