aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_crypto.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-02-26 11:15:43 -0500
committerNick Mathewson <nickm@torproject.org>2014-02-28 08:53:14 -0500
commit0be9e6099b4ca5597459ce1a9561812a8e7343e0 (patch)
treed57034d28effb74aef9655428c3d8326980ea301 /src/test/test_crypto.c
parent25c0435aa577400a024c1514d545037b73f5a0ea (diff)
downloadtor-0be9e6099b4ca5597459ce1a9561812a8e7343e0.tar
tor-0be9e6099b4ca5597459ce1a9561812a8e7343e0.tar.gz
Unit tests for pk fingerprint functions
Diffstat (limited to 'src/test/test_crypto.c')
-rw-r--r--src/test/test_crypto.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c
index 00b8693b1..1fda33476 100644
--- a/src/test/test_crypto.c
+++ b/src/test/test_crypto.c
@@ -509,6 +509,56 @@ test_crypto_pk(void)
tor_free(encoded);
}
+static void
+test_crypto_pk_fingerprints(void *arg)
+{
+ crypto_pk_t *pk = NULL;
+ char encoded[512];
+ char d[DIGEST_LEN], d2[DIGEST_LEN];
+ char fingerprint[FINGERPRINT_LEN+1];
+ int n;
+ unsigned i;
+ char *mem_op_hex_tmp=NULL;
+
+ (void)arg;
+
+ pk = pk_generate(1);
+ tt_assert(pk);
+ n = crypto_pk_asn1_encode(pk, encoded, sizeof(encoded));
+ tt_int_op(n, >, 0);
+ tt_int_op(n, >, 128);
+ tt_int_op(n, <, 256);
+
+ /* Is digest as expected? */
+ crypto_digest(d, encoded, n);
+ tt_int_op(0, ==, crypto_pk_get_digest(pk, d2));
+ test_memeq(d, d2, DIGEST_LEN);
+
+ /* Is fingerprint right? */
+ tt_int_op(0, ==, crypto_pk_get_fingerprint(pk, fingerprint, 0));
+ tt_int_op(strlen(fingerprint), ==, DIGEST_LEN * 2);
+ test_memeq_hex(d, fingerprint);
+
+ /* Are spaces right? */
+ tt_int_op(0, ==, crypto_pk_get_fingerprint(pk, fingerprint, 1));
+ for (i = 4; i < strlen(fingerprint); i += 5) {
+ tt_int_op(fingerprint[i], ==, ' ');
+ }
+ tor_strstrip(fingerprint, " ");
+ tt_int_op(strlen(fingerprint), ==, DIGEST_LEN * 2);
+ test_memeq_hex(d, fingerprint);
+
+ /* Now hash again and check crypto_pk_get_hashed_fingerprint. */
+ crypto_digest(d2, d, sizeof(d));
+ tt_int_op(0, ==, crypto_pk_get_hashed_fingerprint(pk, fingerprint));
+ tt_int_op(strlen(fingerprint), ==, DIGEST_LEN * 2);
+ test_memeq_hex(d2, fingerprint);
+
+ done:
+ crypto_pk_free(pk);
+ tor_free(mem_op_hex_tmp);
+}
+
/** Sanity check for crypto pk digests */
static void
test_crypto_digests(void)
@@ -1234,6 +1284,7 @@ struct testcase_t crypto_tests[] = {
{ "aes_EVP", test_crypto_aes, TT_FORK, &pass_data, (void*)"evp" },
CRYPTO_LEGACY(sha),
CRYPTO_LEGACY(pk),
+ { "pk_fingerprints", test_crypto_pk_fingerprints, TT_FORK, NULL, NULL },
CRYPTO_LEGACY(digests),
CRYPTO_LEGACY(dh),
CRYPTO_LEGACY(s2k),