aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_crypto.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2013-01-03 12:38:44 -0500
committerNick Mathewson <nickm@torproject.org>2013-01-03 12:38:44 -0500
commit27ac306deb19cd45496cff839c6caccb9e81d37c (patch)
treedeb490e9b687da96e372c6f059210b068d9e7b89 /src/test/test_crypto.c
parentb1bdecd703879ca09bf63bf1453a70c4b80ac96d (diff)
downloadtor-27ac306deb19cd45496cff839c6caccb9e81d37c.tar
tor-27ac306deb19cd45496cff839c6caccb9e81d37c.tar.gz
Add a unit test for the curve25519 keypair persistence functions
Diffstat (limited to 'src/test/test_crypto.c')
-rw-r--r--src/test/test_crypto.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c
index 1cd09e280..9c69f31cc 100644
--- a/src/test/test_crypto.c
+++ b/src/test/test_crypto.c
@@ -9,6 +9,7 @@
#include "or.h"
#include "test.h"
#include "aes.h"
+#include "util.h"
#ifdef CURVE25519_ENABLED
#include "crypto_curve25519.h"
#endif
@@ -1005,6 +1006,59 @@ test_crypto_curve25519_wrappers(void *arg)
done:
;
}
+
+static void
+test_crypto_curve25519_persist(void *arg)
+{
+ curve25519_keypair_t keypair, keypair2;
+ char *fname = tor_strdup(get_fname("curve25519_keypair"));
+ char *tag = NULL;
+ char *content = NULL;
+ const char *cp;
+ struct stat st;
+
+ (void)arg;
+
+ tt_int_op(0,==,curve25519_keypair_generate(&keypair, 0));
+
+ tt_int_op(0,==,curve25519_keypair_write_to_file(&keypair, fname, "testing"));
+ tt_int_op(0,==,curve25519_keypair_read_from_file(&keypair2, &tag, fname));
+ tt_str_op(tag,==,"testing");
+
+ test_memeq(keypair.pubkey.public_key,
+ keypair2.pubkey.public_key,
+ CURVE25519_PUBKEY_LEN);
+ test_memeq(keypair.seckey.secret_key,
+ keypair2.seckey.secret_key,
+ CURVE25519_SECKEY_LEN);
+
+ content = read_file_to_str(fname, RFTS_BIN, &st);
+ tt_assert(content);
+ tt_assert(!strcmpstart(content, "== c25519v1: testing =="));
+ cp = content + strlen("== c25519v1: testing ==");
+ tt_int_op(st.st_size, ==, 64 + strlen("== c25519v1: testing =="));
+ test_memeq(keypair.seckey.secret_key,
+ cp,
+ CURVE25519_SECKEY_LEN);
+ cp += CURVE25519_SECKEY_LEN;
+ test_memeq(keypair.pubkey.public_key,
+ cp,
+ CURVE25519_SECKEY_LEN);
+
+ tor_free(fname);
+ fname = tor_strdup(get_fname("bogus_keypair"));
+
+ tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname));
+
+ content[64] ^= 0xff;
+ tt_int_op(0, ==, write_bytes_to_file(fname, content, st.st_size, 1));
+ tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname));
+
+ done:
+ tor_free(fname);
+ tor_free(content);
+}
+
#endif
static void *
@@ -1043,6 +1097,7 @@ struct testcase_t crypto_tests[] = {
#ifdef CURVE25519_ENABLED
{ "curve25519_impl", test_crypto_curve25519_impl, 0, NULL, NULL },
{ "curve25519_wrappers", test_crypto_curve25519_wrappers, 0, NULL, NULL },
+ { "curve25519_persist", test_crypto_curve25519_persist, 0, NULL, NULL },
#endif
END_OF_TESTCASES
};