[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r14180: Add some unit tests, particularly for AES counter mode. (in tor/trunk: . src/common src/or)
Author: nickm
Date: 2008-03-25 12:16:05 -0400 (Tue, 25 Mar 2008)
New Revision: 14180
Modified:
tor/trunk/
tor/trunk/ChangeLog
tor/trunk/src/common/test.h
tor/trunk/src/or/test.c
Log:
r19039@catbus: nickm | 2008-03-25 12:15:58 -0400
Add some unit tests, particularly for AES counter mode.
Property changes on: tor/trunk
___________________________________________________________________
svk:merge ticket from /tor/trunk [r19039] on 8246c3cf-6607-4228-993b-4d95d33730f1
Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog 2008-03-25 10:44:28 UTC (rev 14179)
+++ tor/trunk/ChangeLog 2008-03-25 16:16:05 UTC (rev 14180)
@@ -31,6 +31,7 @@
fixed in late 2006. Our new behavior is to call RAND_poll() at
startup, and to call RAND_poll() when we reseed later only if we
have a non-buggy OpenSSL version.
+ - Lots of new unit tests.
o Code simplifications and refactoring:
- Refactor code using connection_ap_handshake_attach_circuit() to
Modified: tor/trunk/src/common/test.h
===================================================================
--- tor/trunk/src/common/test.h 2008-03-25 10:44:28 UTC (rev 14179)
+++ tor/trunk/src/common/test.h 2008-03-25 16:16:05 UTC (rev 14180)
@@ -142,6 +142,29 @@
return; \
} STMT_END
+#define test_memeq_hex(expr1, hex) \
+ STMT_BEGIN \
+ const void *_test_v1 = (expr1); \
+ const char *_test_v2 = (hex); \
+ size_t _len_v2 = strlen(_test_v2); \
+ char *mem2 = tor_malloc(_len_v2/2); \
+ tor_assert((_len_v2 & 1) == 0); \
+ base16_decode(mem2, _len_v2/2, _test_v2, _len_v2); \
+ if (!memcmp(mem2, _test_v1, _len_v2/2)) { \
+ printf("."); fflush(stdout); } else { \
+ char *mem1 = tor_malloc(_len_v2)+1; \
+ base16_encode(mem1, _len_v2+1, _test_v1, _len_v2/2); \
+ printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
+ " %s != %s\n", \
+ _SHORT_FILE_, \
+ __LINE__, \
+ PRETTY_FUNCTION, \
+ #expr1, _test_v2, mem1, _test_v2); \
+ return; \
+ } \
+ tor_free(mem2); \
+ STMT_END
+
#define test_memneq(expr1, expr2, len) \
STMT_BEGIN \
void *_test_v1=(expr1), *_test_v2=(expr2); \
Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c 2008-03-25 10:44:28 UTC (rev 14179)
+++ tor/trunk/src/or/test.c 2008-03-25 16:16:05 UTC (rev 14180)
@@ -456,14 +456,56 @@
crypto_free_cipher_env(env1);
crypto_free_cipher_env(env2);
- /* Test vectors for stream ciphers. */
- /* XXXX Look up some test vectors for the ciphers and make sure we match. */
+ /* NIST test vector for aes. */
+ env1 = crypto_new_cipher_env(); /* IV starts at 0 */
+ crypto_cipher_set_key(env1, "\x80\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00");
+ crypto_cipher_encrypt_init_cipher(env1);
+ crypto_cipher_encrypt(env1, data1,
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00", 16);
+ test_memeq_hex(data1, "0EDD33D3C621E546455BD8BA1418BEC8");
+ /* Now test rollover. All these values are originally from a python
+ * script. */
+ crypto_cipher_set_iv(env1, "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
+ memset(data2, 0, 1024);
+ crypto_cipher_encrypt(env1, data1, data2, 32);
+ test_memeq_hex(data1, "335fe6da56f843199066c14a00a40231"
+ "cdd0b917dbc7186908a6bfb5ffd574d3");
+
+ crypto_cipher_set_iv(env1, "\x00\x00\x00\x00\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
+ memset(data2, 0, 1024);
+ crypto_cipher_encrypt(env1, data1, data2, 32);
+ test_memeq_hex(data1, "e627c6423fa2d77832a02b2794094b73"
+ "3e63c721df790d2c6469cc1953a3ffac");
+
+ crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
+ memset(data2, 0, 1024);
+ crypto_cipher_encrypt(env1, data1, data2, 32);
+ test_memeq_hex(data1, "2aed2bff0de54f9328efd070bf48f70a"
+ "0EDD33D3C621E546455BD8BA1418BEC8");
+
+ /* Now check rollover on inplace cipher. */
+ crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
+ crypto_cipher_crypt_inplace(env1, data2, 64);
+ test_memeq_hex(data2, "2aed2bff0de54f9328efd070bf48f70a"
+ "0EDD33D3C621E546455BD8BA1418BEC8"
+ "93e2c5243d6839eac58503919192f7ae"
+ "1908e67cafa08d508816659c2e693191");
+ crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff");
+ crypto_cipher_crypt_inplace(env1, data2, 64);
+ test_assert(tor_mem_is_zero(data2, 64));
+ crypto_free_cipher_env(env1);
+
/* Test SHA-1 with a test vector from the specification. */
i = crypto_digest(data1, "abc", 3);
- test_memeq(data1,
- "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
- "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
+ test_memeq_hex(data1, "A9993E364706816ABA3E25717850C26C9CD0D89D");
/* Test HMAC-SHA-1 with test cases from RFC2202. */
{
@@ -768,6 +810,11 @@
test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
tor_gettimeofday(&start);
+ /* Tests for corner cases of strl operations */
+ test_eq(5, strlcpy(buf, "Hello", 0));
+ strlcpy(buf, "Hello", sizeof(buf));
+ test_eq(10, strlcat(buf, "Hello", 5));
+
/* Test tor_strstrip() */
strlcpy(buf, "Testing 1 2 3", sizeof(buf));
tor_strstrip(buf, ",!");
@@ -996,7 +1043,7 @@
test_eq_ptr(eat_whitespace(s), s+5);
}
- /* Test memmem */
+ /* Test memmem and memstr */
{
const char *haystack = "abcde";
tor_assert(!tor_memmem(haystack, 5, "ef", 2));
@@ -1004,6 +1051,9 @@
test_eq_ptr(tor_memmem(haystack, 5, "cde", 3), haystack + 2);
haystack = "ababcad";
test_eq_ptr(tor_memmem(haystack, 7, "abc", 3), haystack + 2);
+ test_eq_ptr(tor_memstr(haystack, 7, "abc"), haystack + 2);
+ test_assert(!tor_memstr(haystack, 7, "fe"));
+ test_assert(!tor_memstr(haystack, 7, "longerthantheoriginal"));
}
/* Test wrap_string */