[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [obfsproxy/master] Introduce tt_mem_op and use it throughout existing tests.



commit 434162e6900a7ebb1a745cbd1fe3324e1f4c244b
Author: Zack Weinberg <zackw@xxxxxxxxx>
Date:   Mon Jul 18 16:35:28 2011 -0700

    Introduce tt_mem_op and use it throughout existing tests.
---
 src/test/tinytest.c        |   17 +++++++++++++++++
 src/test/tinytest.h        |    4 ++++
 src/test/tinytest_macros.h |   12 ++++++++++++
 src/test/unittest_crypt.c  |   34 +++++++++++++++++-----------------
 src/test/unittest_obfs2.c  |   20 ++++++++------------
 src/test/unittest_socks.c  |   28 ++++++++++++++--------------
 6 files changed, 72 insertions(+), 43 deletions(-)

diff --git a/src/test/tinytest.c b/src/test/tinytest.c
index e3107b2..5151bcb 100644
--- a/src/test/tinytest.c
+++ b/src/test/tinytest.c
@@ -367,3 +367,20 @@ _tinytest_set_test_skipped(void)
 		cur_test_outcome = SKIP;
 }
 
+char *
+tt_base16_encode(const char *value, size_t vlen)
+{
+  static const char hex[] = "0123456789abcdef";
+  char *print = malloc(vlen * 2 + 1);
+  char *p = print;
+  const char *v = value, *vl = value + vlen;
+
+  assert(print);
+  while (v < vl) {
+    unsigned char c = *v++;
+    *p++ = hex[(c >> 4) & 0x0f];
+    *p++ = hex[(c >> 0) & 0x0f];
+  }
+  *p = '\0';
+  return print;
+}
diff --git a/src/test/tinytest.h b/src/test/tinytest.h
index cbe28b7..a3bdcfe 100644
--- a/src/test/tinytest.h
+++ b/src/test/tinytest.h
@@ -26,6 +26,8 @@
 #ifndef _TINYTEST_H
 #define _TINYTEST_H
 
+#include <stddef.h> /* size_t */
+
 /** Flag for a test that needs to run in a subprocess. */
 #define TT_FORK  (1<<0)
 /** Runtime flag for a test we've decided to skip. */
@@ -73,6 +75,8 @@ int _tinytest_get_verbosity(void);
 /** Implementation: Set a flag on tests matching a name; returns number
  * of tests that matched. */
 int _tinytest_set_flag(struct testgroup_t *, const char *, unsigned long);
+/** Implementation: Helper function for tt_mem_op. */
+char * tt_base16_encode(const char *value, size_t vlen);
 
 /** Set all tests in 'groups' matching the name 'named' to be skipped. */
 #define tinytest_skip(groups, named) \
diff --git a/src/test/tinytest_macros.h b/src/test/tinytest_macros.h
index 25ee7e2..f3e6d32 100644
--- a/src/test/tinytest_macros.h
+++ b/src/test/tinytest_macros.h
@@ -154,6 +154,9 @@
 #define tt_assert_op_type(a,op,b,type,fmt)				\
 	tt_assert_test_type(a,b,#a" "#op" "#b,type,(_val1 op _val2),fmt)
 
+#define tt_bool_op(a,op,b) \
+        tt_assert_test_type(a,b,#a" "#op" "#b,int,(!!_val1 op !!_val2),"%d")
+
 #define tt_int_op(a,op,b)			\
 	tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2),"%ld")
 
@@ -169,4 +172,13 @@
 	tt_assert_test_type(a,b,#a" "#op" "#b,const char *,		\
 			    (strcmp(_val1,_val2) op 0),"<%s>")
 
+#define tt_mem_op(expr1, op, expr2, len)                                \
+  tt_assert_test_fmt_type(expr1,expr2,#expr1" "#op" "#expr2,            \
+                          const char *,                                 \
+                          (memcmp(_val1, _val2, len) op 0),             \
+                          char *, "%s",                                 \
+                          { _print = tt_base16_encode(_value, len); },  \
+                          { free(_print); }                             \
+                          );
+
 #endif
diff --git a/src/test/unittest_crypt.c b/src/test/unittest_crypt.c
index a3574f6..aef4ead 100644
--- a/src/test/unittest_crypt.c
+++ b/src/test/unittest_crypt.c
@@ -20,10 +20,10 @@ test_crypt_hashvec(void *data)
   d = digest_new();
   digest_update(d, (unsigned char*)"", 0);
   digest_getdigest(d, output, 32);
-  tt_int_op(0, ==, memcmp(output,
-                          "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8"
-                          "\x99\x6f\xb9\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c"
-                          "\xa4\x95\x99\x1b\x78\x52\xb8\x55", 32));
+  tt_mem_op(output, ==,
+            "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8"
+            "\x99\x6f\xb9\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c"
+            "\xa4\x95\x99\x1b\x78\x52\xb8\x55", 32);
 
   /* Second SHA256 test vector:
      Test for the 256-bit entry of:
@@ -34,10 +34,10 @@ test_crypt_hashvec(void *data)
                 "\x10\xb4\x70\xb1\x44\x78\x44\x11\xc9\x3a\x4d\x50\x45\x56\x83"
                 "\x4d\xae\x3e\xa4\xa5\xbb", 32);
   digest_getdigest(d, output, 32);
-  tt_int_op(0, ==, memcmp(output,
-                          "\x56\x05\x9e\x8c\xb3\xc2\x97\x8b\x19\x82\x08\xbf"
-                          "\x5c\xa1\xe1\xea\x56\x59\xb7\x37\xa5\x06\x32\x4b"
-                          "\x7c\xec\x75\xb5\xeb\xaf\x05\x7d", 32));
+  tt_mem_op(output, ==,
+            "\x56\x05\x9e\x8c\xb3\xc2\x97\x8b\x19\x82\x08\xbf"
+            "\x5c\xa1\xe1\xea\x56\x59\xb7\x37\xa5\x06\x32\x4b"
+            "\x7c\xec\x75\xb5\xeb\xaf\x05\x7d", 32);
 
   /* Third SHA test vector:
      Test for the 1304-bit entry of:
@@ -57,10 +57,10 @@ test_crypt_hashvec(void *data)
                 "\x8a\x19\xc8\x18\xc2\xea\x2e\x9d\x4e\x2d\xe9\x19\x0c\x9d\xdd"
                 "\xb8\x06", 163);
   digest_getdigest(d, output, 32);
-  tt_int_op(0, ==, memcmp(output,
-                          "\xc9\x07\x18\x04\x43\xde\xe3\xcb\xcc\xb4\xc3\x13"
-                          "\x28\xe6\x25\x15\x85\x27\xa5\x93\xb8\x78\xde\x1b"
-                          "\x8e\x4b\xa3\x7f\x1d\x69\xfb\x66", 32));
+  tt_mem_op(output, ==,
+            "\xc9\x07\x18\x04\x43\xde\xe3\xcb\xcc\xb4\xc3\x13"
+            "\x28\xe6\x25\x15\x85\x27\xa5\x93\xb8\x78\xde\x1b"
+            "\x8e\x4b\xa3\x7f\x1d\x69\xfb\x66", 32);
 
   /* XXX Try doing init, update, update, output. */
   /* XXX add a base16-decode function so we can implement a tt_mem_op or
@@ -114,13 +114,13 @@ test_crypt_aes1(void *data)
 
   for (i = 0; i < 4; i++) {
     tt_int_op(0, ==, crypt->pos);
-    tt_int_op(0, ==, memcmp(crypt->ivec, testvec[i].counter, 16));
+    tt_mem_op(crypt->ivec, ==, testvec[i].counter, 16);
 
     memcpy(vec, testvec[i].plaintext, 16);
     stream_crypt(crypt, vec, 16);
 
-    tt_int_op(0, ==, memcmp(crypt->ecount_buf, testvec[i].keystream, 16));
-    tt_int_op(0, ==, memcmp(vec, testvec[i].ciphertext, 16));
+    tt_mem_op(crypt->ecount_buf, ==, testvec[i].keystream, 16);
+    tt_mem_op(vec, ==, testvec[i].ciphertext, 16);
   }
 
  end:
@@ -147,7 +147,7 @@ test_crypt_aes2(void *data)
   stream_crypt(crypt1, res1, 16);
   stream_crypt(crypt2, res2, 16);
 
-  tt_int_op(0, !=, memcmp(res1, res2, 16));
+  tt_mem_op(res1, !=, res2, 16);
 
  end:
   if (crypt1)
@@ -169,7 +169,7 @@ test_crypt_rng(void *data)
   tt_int_op(0, ==, random_bytes(data1, 100));
   tt_int_op(0, ==, random_bytes(data2, 100));
 
-  tt_int_op(0, !=, memcmp(data1, data2, 100));
+  tt_mem_op(data1, !=, data2, 100);
 
  end:
   ;
diff --git a/src/test/unittest_obfs2.c b/src/test/unittest_obfs2.c
index 099acd2..00fc12e 100644
--- a/src/test/unittest_obfs2.c
+++ b/src/test/unittest_obfs2.c
@@ -177,13 +177,11 @@ test_obfs2_handshake(void *state)
   /* The handshake is now complete. We should have:
      client's send_crypto == server's recv_crypto
      server's send_crypto == client's recv_crypto . */
-  tt_int_op(0, ==, memcmp(client_state->send_crypto,
-                          server_state->recv_crypto,
-                          sizeof(crypt_t)));
+  tt_mem_op(client_state->send_crypto, ==, server_state->recv_crypto,
+            sizeof(crypt_t));
 
-  tt_int_op(0, ==, memcmp(client_state->recv_crypto,
-                          server_state->send_crypto,
-                          sizeof(crypt_t)));
+  tt_mem_op(client_state->recv_crypto, ==, server_state->send_crypto,
+            sizeof(crypt_t));
 
  end:;
 }
@@ -353,13 +351,11 @@ test_obfs2_split_handshake(void *state)
   /* The handshake is finally complete. We should have: */
   /*    client's send_crypto == server's recv_crypto */
   /*    server's send_crypto == client's recv_crypto . */
-  tt_int_op(0, ==, memcmp(client_state->send_crypto,
-                          server_state->recv_crypto,
-                          sizeof(crypt_t)));
+  tt_mem_op(client_state->send_crypto, ==, server_state->recv_crypto,
+            sizeof(crypt_t));
 
-  tt_int_op(0, ==, memcmp(client_state->recv_crypto,
-                          server_state->send_crypto,
-                          sizeof(crypt_t)));
+  tt_mem_op(client_state->recv_crypto, ==, server_state->send_crypto,
+            sizeof(crypt_t));
 
  end:;
 }
diff --git a/src/test/unittest_socks.c b/src/test/unittest_socks.c
index 9ba9e7e..c6039af 100644
--- a/src/test/unittest_socks.c
+++ b/src/test/unittest_socks.c
@@ -331,9 +331,9 @@ test_socks_socks5_request_reply(void *data)
 
   tt_assert(rep1[3] == SOCKS5_ATYP_IPV4);
   /* check address */
-  tt_int_op(0, ==, memcmp(rep1+4, "\x7f\x00\x00\x01", 4));
+  tt_mem_op(rep1+4, ==, "\x7f\x00\x00\x01", 4);
   /* check port */
-  tt_int_op(0, ==, memcmp(rep1+4+4,"\x1c\xbd",2));
+  tt_mem_op(rep1+4+4, ==, "\x1c\xbd", 2);
 
   /* emptying s->dest buffer before next test  */
   size_t buffer_len = evbuffer_get_length(s->dest);
@@ -352,11 +352,11 @@ test_socks_socks5_request_reply(void *data)
   
   tt_assert(rep2[3] = SOCKS5_ATYP_IPV6);
   /* Test returned address against inet_pton(d:1:5:e:a:5:e:0) */
-  tt_int_op(0, ==, memcmp(rep2+4,
-                          "\x00\x0d\x00\x01\x00\x05\x00\x0e\x00"
-                          "\x0a\x00\x05\x00\x0e\x00\x00",  
-                          16));
-  tt_int_op(0, ==, memcmp(rep2+4+16, "\x1c\xbd", 2));
+  tt_mem_op(rep2+4, ==,
+            "\x00\x0d\x00\x01\x00\x05\x00\x0e\x00"
+            "\x0a\x00\x05\x00\x0e\x00\x00",
+            16);
+  tt_mem_op(rep2+4+16, ==, "\x1c\xbd", 2);
 
   /* emptying dest buffer before next test  */
   buffer_len = evbuffer_get_length(s->dest);
@@ -377,9 +377,9 @@ test_socks_socks5_request_reply(void *data)
   tt_assert(rep3[3] == SOCKS5_ATYP_FQDN);
   tt_assert(rep3[4] == strlen(fqdn));
   /* check fqdn */
-  tt_int_op(0, ==, memcmp(rep3+5,fqdn,strlen(fqdn)));
+  tt_mem_op(rep3+5, ==, fqdn,strlen(fqdn));
   /* check port */
-  tt_int_op(0, ==, memcmp(rep3+5+strlen(fqdn),"\x1c\xbd",2));
+  tt_mem_op(rep3+5+strlen(fqdn), ==, "\x1c\xbd",2);
 
   /* Fourth test: 
      We ask the server while having an empty parsereq and with a
@@ -391,8 +391,8 @@ test_socks_socks5_request_reply(void *data)
   evbuffer_remove(s->dest,rep4,255);
 
   tt_assert(rep4[3] == SOCKS5_ATYP_IPV4);
-  tt_int_op(0, ==, memcmp(rep4+4,"\x00\x00\x00\x00",4));
-  tt_int_op(0, ==, memcmp(rep4+4+4, "\x00\x00", 2));
+  tt_mem_op(rep4+4, ==, "\x00\x00\x00\x00",4);
+  tt_mem_op(rep4+4+4, ==, "\x00\x00", 2);
   
  end:;
 }
@@ -552,9 +552,9 @@ test_socks_socks4_request_reply(void *data)
   tt_assert(rep1[0] == '\x00');
   tt_assert(rep1[1] == SOCKS4_SUCCESS);
   /* check port */
-  tt_int_op(0, ==, memcmp(rep1+2,"\x1c\xbd",2));
+  tt_mem_op(rep1+2, ==, "\x1c\xbd",2);
   /* check address */
-  tt_int_op(0, ==, memcmp(rep1+2+2,"\x7f\x00\x00\x01", 4));
+  tt_mem_op(rep1+2+2, ==, "\x7f\x00\x00\x01", 4);
 
   /* emptying dest buffer before next test  */
   size_t buffer_len = evbuffer_get_length(s->dest);
@@ -574,7 +574,7 @@ test_socks_socks4_request_reply(void *data)
 
   tt_assert(rep2[1] == SOCKS4_FAILED);
   /* check port */
-  tt_int_op(0, ==, memcmp(rep2+2,"\x1c\xbd",2));
+  tt_mem_op(rep2+2, ==, "\x1c\xbd", 2);
   /* check address */
   /*  tt_str_op(rep1+2+2, ==, "www.test.example"); */
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits