[or-cvs] Add unittests for compression detection. Make all rendezvou...
Nick Mathewson
nickm at seul.org
Wed Jan 19 22:47:51 UTC 2005
Update of /home/or/cvsroot/tor/src/or
In directory moria.mit.edu:/tmp/cvs-serv26735/src/or
Modified Files:
directory.c test.c
Log Message:
Add unittests for compression detection. Make all rendezvous descriptors "plausible".
Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/directory.c,v
retrieving revision 1.194
retrieving revision 1.195
diff -u -d -r1.194 -r1.195
--- directory.c 19 Jan 2005 22:40:33 -0000 1.194
+++ directory.c 19 Jan 2005 22:47:48 -0000 1.195
@@ -48,7 +48,7 @@
int purpose, const char *resource,
const char *payload, size_t payload_len);
static int directory_handle_command(connection_t *conn);
-static int body_is_plausible(const char *body, size_t body_len);
+static int body_is_plausible(const char *body, size_t body_len, int purpose);
/********* START VARIABLES **********/
@@ -552,21 +552,25 @@
* running-list or directory opening. This is a sign of possible compression.
**/
static int
-body_is_plausible(const char *body, size_t len)
+body_is_plausible(const char *body, size_t len, int purpose)
{
int i;
if (len < 32)
return 0;
- if (!strcmpstart(body,"router") ||
- !strcmpstart(body,"signed-directory") ||
- !strcmpstart(body,"network-status") ||
- !strcmpstart(body,"running-routers"))
+ if (purpose != DIR_PURPOSE_FETCH_RENDDESC) {
+ if (!strcmpstart(body,"router") ||
+ !strcmpstart(body,"signed-directory") ||
+ !strcmpstart(body,"network-status") ||
+ !strcmpstart(body,"running-routers"))
+ return 1;
+ for (i=0;i<32;++i) {
+ if (!isprint(body[i]) && !isspace(body[i]))
+ return 0;
+ }
+ return 1;
+ } else {
return 1;
- for (i=0;i<32;++i) {
- if (!isprint(body[i]) && !isspace(body[i]))
- return 0;
}
- return 1;
}
/** We are a client, and we've finished reading the server's
@@ -618,7 +622,7 @@
}
}
- plausible = body_is_plausible(body, body_len);
+ plausible = body_is_plausible(body, body_len, conn->purpose);
if (compression || !plausible) {
char *new_body = NULL;
size_t new_len = 0;
@@ -628,7 +632,7 @@
const char *description1, *description2;
if (compression == ZLIB_METHOD)
description1 = "as deflated";
- else if (compression = GZIP_METHOD)
+ else if (compression == GZIP_METHOD)
description1 = "as gzipped";
else if (compression == 0)
description1 = "as uncompressed";
Index: test.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/test.c,v
retrieving revision 1.164
retrieving revision 1.165
diff -u -d -r1.164 -r1.165
--- test.c 7 Jan 2005 01:12:30 -0000 1.164
+++ test.c 19 Jan 2005 22:47:48 -0000 1.165
@@ -802,11 +802,13 @@
size_t len1, len2;
buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
+ test_eq(detect_compression_method(buf1, strlen(buf1)), 0);
if (is_gzip_supported()) {
test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
GZIP_METHOD));
test_assert(buf2);
test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
+ test_eq(detect_compression_method(buf2, strlen(buf1)), GZIP_METHOD);
test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, GZIP_METHOD));
test_assert(buf3);
@@ -820,6 +822,7 @@
ZLIB_METHOD));
test_assert(buf2);
test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
+ test_eq(detect_compression_method(buf2, strlen(buf1)), ZLIB_METHOD);
test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, ZLIB_METHOD));
test_assert(buf3);
More information about the tor-commits
mailing list