2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2012-06-05 02:58:17 +02:00
|
|
|
* Copyright (c) 2007-2012, The Tor Project, Inc. */
|
2003-04-07 04:12:02 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2008-12-23 18:56:31 +01:00
|
|
|
/* Ordinarily defined in tor_main.c; this bit is just here to provide one
|
|
|
|
* since we're not linking to tor_main.c */
|
2009-06-12 19:38:37 +02:00
|
|
|
const char tor_git_revision[] = "";
|
2007-04-17 01:56:31 +02:00
|
|
|
|
2005-06-11 07:31:17 +02:00
|
|
|
/**
|
|
|
|
* \file test.c
|
|
|
|
* \brief Unit tests for many pieces of the lower level Tor modules.
|
|
|
|
**/
|
|
|
|
|
2004-12-07 06:31:38 +01:00
|
|
|
#include "orconfig.h"
|
2008-09-09 22:43:31 +02:00
|
|
|
|
2003-04-15 21:10:18 +02:00
|
|
|
#include <stdio.h>
|
2003-08-12 05:08:41 +02:00
|
|
|
#ifdef HAVE_FCNTL_H
|
2003-04-15 21:10:18 +02:00
|
|
|
#include <fcntl.h>
|
2003-08-12 05:08:41 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2003-08-12 05:08:41 +02:00
|
|
|
/* For mkdir() */
|
|
|
|
#include <direct.h>
|
2004-12-07 06:31:38 +01:00
|
|
|
#else
|
2004-08-11 21:20:24 +02:00
|
|
|
#include <dirent.h>
|
2004-12-07 06:31:38 +01:00
|
|
|
#endif
|
|
|
|
|
2007-04-11 15:18:25 +02:00
|
|
|
/* These macros pull in declarations for some functions and structures that
|
|
|
|
* are typically file-private. */
|
2008-02-21 00:38:55 +01:00
|
|
|
#define BUFFERS_PRIVATE
|
2007-04-11 15:18:25 +02:00
|
|
|
#define CONFIG_PRIVATE
|
2007-12-18 22:27:08 +01:00
|
|
|
#define GEOIP_PRIVATE
|
2007-04-11 15:18:25 +02:00
|
|
|
#define ROUTER_PRIVATE
|
2009-08-28 08:28:20 +02:00
|
|
|
#define CIRCUIT_PRIVATE
|
|
|
|
|
2009-09-18 11:01:39 +02:00
|
|
|
/*
|
|
|
|
* Linux doesn't provide lround in math.h by default, but mac os does...
|
|
|
|
* It's best just to leave math.h out of the picture entirely.
|
|
|
|
*/
|
|
|
|
//#include <math.h>
|
|
|
|
long int lround(double x);
|
|
|
|
double fabs(double x);
|
2007-04-11 02:30:25 +02:00
|
|
|
|
2003-04-07 15:25:44 +02:00
|
|
|
#include "or.h"
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "buffers.h"
|
|
|
|
#include "circuitbuild.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 10:43:02 +02:00
|
|
|
#include "connection_edge.h"
|
2010-07-21 14:38:52 +02:00
|
|
|
#include "geoip.h"
|
2010-07-21 17:52:54 +02:00
|
|
|
#include "rendcommon.h"
|
2007-08-08 07:50:31 +02:00
|
|
|
#include "test.h"
|
|
|
|
#include "torgzip.h"
|
|
|
|
#include "mempool.h"
|
2008-03-26 17:33:33 +01:00
|
|
|
#include "memarea.h"
|
2010-07-23 20:38:25 +02:00
|
|
|
#include "onion.h"
|
2010-07-23 20:51:25 +02:00
|
|
|
#include "policies.h"
|
2010-07-23 22:57:20 +02:00
|
|
|
#include "rephist.h"
|
2010-07-23 23:23:43 +02:00
|
|
|
#include "routerparse.h"
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2008-09-09 22:43:31 +02:00
|
|
|
#ifdef USE_DMALLOC
|
|
|
|
#include <dmalloc.h>
|
2008-09-25 19:37:00 +02:00
|
|
|
#include <openssl/crypto.h>
|
2010-08-16 06:00:13 +02:00
|
|
|
#include "main.h"
|
2008-09-09 22:43:31 +02:00
|
|
|
#endif
|
|
|
|
|
2008-12-22 20:14:08 +01:00
|
|
|
/** Set to true if any unit test has failed. Mostly, this is set by the macros
|
|
|
|
* in test.h */
|
2004-03-01 07:45:32 +01:00
|
|
|
int have_failed = 0;
|
|
|
|
|
2008-12-22 20:14:08 +01:00
|
|
|
/** Temporary directory (set up by setup_directory) under which we store all
|
|
|
|
* our files during testing. */
|
2004-08-11 21:20:24 +02:00
|
|
|
static char temp_dir[256];
|
2012-05-16 16:08:24 +02:00
|
|
|
#ifdef _WIN32
|
2012-06-05 17:06:26 +02:00
|
|
|
#define pid_t int
|
2012-05-16 16:08:24 +02:00
|
|
|
#endif
|
2010-08-16 03:20:19 +02:00
|
|
|
static pid_t temp_dir_setup_in_pid = 0;
|
2004-08-11 21:20:24 +02:00
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Select and create the temporary directory we'll use to run our unit tests.
|
|
|
|
* Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
|
|
|
|
* idempotent. */
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2004-10-27 20:16:37 +02:00
|
|
|
setup_directory(void)
|
2004-08-11 21:20:24 +02:00
|
|
|
{
|
|
|
|
static int is_setup = 0;
|
2003-08-12 05:08:41 +02:00
|
|
|
int r;
|
2004-08-11 21:20:24 +02:00
|
|
|
if (is_setup) return;
|
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2011-04-08 00:34:11 +02:00
|
|
|
{
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
const char *tmp = buf;
|
|
|
|
/* If this fails, we're probably screwed anyway */
|
2012-06-07 17:59:32 +02:00
|
|
|
if (!GetTempPathA(sizeof(buf),buf))
|
2011-04-08 00:34:11 +02:00
|
|
|
tmp = "c:\\windows\\temp";
|
|
|
|
tor_snprintf(temp_dir, sizeof(temp_dir),
|
|
|
|
"%s\\tor_test_%d", tmp, (int)getpid());
|
|
|
|
r = mkdir(temp_dir);
|
|
|
|
}
|
2003-08-12 05:08:41 +02:00
|
|
|
#else
|
2004-12-07 06:31:38 +01:00
|
|
|
tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
|
2004-08-11 21:20:24 +02:00
|
|
|
r = mkdir(temp_dir, 0700);
|
|
|
|
#endif
|
|
|
|
if (r) {
|
|
|
|
fprintf(stderr, "Can't create directory %s:", temp_dir);
|
|
|
|
perror("");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
is_setup = 1;
|
2010-08-16 03:20:19 +02:00
|
|
|
temp_dir_setup_in_pid = getpid();
|
2004-08-11 21:20:24 +02:00
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Return a filename relative to our testing temporary directory */
|
2009-09-22 19:06:47 +02:00
|
|
|
const char *
|
2004-08-11 21:20:24 +02:00
|
|
|
get_fname(const char *name)
|
|
|
|
{
|
|
|
|
static char buf[1024];
|
|
|
|
setup_directory();
|
2010-08-20 19:24:54 +02:00
|
|
|
if (!name)
|
|
|
|
return temp_dir;
|
2004-10-27 08:37:34 +02:00
|
|
|
tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
|
2004-08-11 21:20:24 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2010-10-07 21:28:54 +02:00
|
|
|
/* Remove a directory and all of its subdirectories */
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2010-10-07 21:28:54 +02:00
|
|
|
rm_rf(const char *dir)
|
2004-08-11 21:20:24 +02:00
|
|
|
{
|
2010-10-07 21:28:54 +02:00
|
|
|
struct stat st;
|
2010-08-16 03:20:19 +02:00
|
|
|
smartlist_t *elements;
|
2010-10-07 21:28:54 +02:00
|
|
|
|
|
|
|
elements = tor_listdir(dir);
|
2005-09-10 03:42:42 +02:00
|
|
|
if (elements) {
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(elements, const char *, cp) {
|
2010-10-07 21:28:54 +02:00
|
|
|
char *tmp = NULL;
|
|
|
|
tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
|
|
|
|
if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
|
|
|
|
rm_rf(tmp);
|
|
|
|
} else {
|
|
|
|
if (unlink(tmp)) {
|
|
|
|
fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
2005-09-10 03:42:42 +02:00
|
|
|
tor_free(tmp);
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(cp);
|
2005-09-10 03:42:42 +02:00
|
|
|
SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(elements);
|
2004-12-07 06:31:38 +01:00
|
|
|
}
|
2010-10-07 21:28:54 +02:00
|
|
|
if (rmdir(dir))
|
|
|
|
fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove all files stored under the temporary directory, and the directory
|
|
|
|
* itself. Called by atexit(). */
|
|
|
|
static void
|
|
|
|
remove_directory(void)
|
|
|
|
{
|
|
|
|
if (getpid() != temp_dir_setup_in_pid) {
|
|
|
|
/* Only clean out the tempdir when the main process is exiting. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rm_rf(temp_dir);
|
2003-04-15 21:10:18 +02:00
|
|
|
}
|
|
|
|
|
2008-12-18 07:12:19 +01:00
|
|
|
/** Define this if unit tests spend too much time generating public keys*/
|
|
|
|
#undef CACHE_GENERATED_KEYS
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
static crypto_pk_t *pregen_keys[5] = {NULL, NULL, NULL, NULL, NULL};
|
2008-12-22 18:53:04 +01:00
|
|
|
#define N_PREGEN_KEYS ((int)(sizeof(pregen_keys)/sizeof(pregen_keys[0])))
|
|
|
|
|
|
|
|
/** Generate and return a new keypair for use in unit tests. If we're using
|
|
|
|
* the key cache optimization, we might reuse keys: we only guarantee that
|
|
|
|
* keys made with distinct values for <b>idx</b> are different. The value of
|
|
|
|
* <b>idx</b> must be at least 0, and less than N_PREGEN_KEYS. */
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *
|
2006-10-23 05:48:58 +02:00
|
|
|
pk_generate(int idx)
|
|
|
|
{
|
2008-12-18 07:12:19 +01:00
|
|
|
#ifdef CACHE_GENERATED_KEYS
|
2008-12-22 18:53:04 +01:00
|
|
|
tor_assert(idx < N_PREGEN_KEYS);
|
2008-09-25 19:37:00 +02:00
|
|
|
if (! pregen_keys[idx]) {
|
2012-01-18 21:53:30 +01:00
|
|
|
pregen_keys[idx] = crypto_pk_new();
|
2008-09-25 19:37:00 +02:00
|
|
|
tor_assert(!crypto_pk_generate_key(pregen_keys[idx]));
|
|
|
|
}
|
|
|
|
return crypto_pk_dup_key(pregen_keys[idx]);
|
2008-12-18 07:12:19 +01:00
|
|
|
#else
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *result;
|
2008-12-18 07:12:19 +01:00
|
|
|
(void) idx;
|
2012-01-18 21:53:30 +01:00
|
|
|
result = crypto_pk_new();
|
2008-12-18 07:12:19 +01:00
|
|
|
tor_assert(!crypto_pk_generate_key(result));
|
|
|
|
return result;
|
|
|
|
#endif
|
2008-09-25 19:37:00 +02:00
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Free all storage used for the cached key optimization. */
|
2008-09-25 19:37:00 +02:00
|
|
|
static void
|
|
|
|
free_pregenerated_keys(void)
|
|
|
|
{
|
|
|
|
unsigned idx;
|
2008-12-22 18:53:04 +01:00
|
|
|
for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
|
2008-09-25 19:37:00 +02:00
|
|
|
if (pregen_keys[idx]) {
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(pregen_keys[idx]);
|
2008-09-25 19:37:00 +02:00
|
|
|
pregen_keys[idx] = NULL;
|
|
|
|
}
|
2006-10-23 05:48:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
typedef struct socks_test_data_t {
|
|
|
|
socks_request_t *req;
|
|
|
|
buf_t *buf;
|
|
|
|
} socks_test_data_t;
|
|
|
|
|
|
|
|
static void *
|
|
|
|
socks_test_setup(const struct testcase_t *testcase)
|
|
|
|
{
|
|
|
|
socks_test_data_t *data = tor_malloc(sizeof(socks_test_data_t));
|
|
|
|
(void)testcase;
|
|
|
|
data->buf = buf_new_with_capacity(256);
|
|
|
|
data->req = socks_request_new();
|
|
|
|
config_register_addressmaps(get_options());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
static int
|
|
|
|
socks_test_cleanup(const struct testcase_t *testcase, void *ptr)
|
|
|
|
{
|
|
|
|
socks_test_data_t *data = ptr;
|
|
|
|
(void)testcase;
|
|
|
|
buf_free(data->buf);
|
|
|
|
socks_request_free(data->req);
|
|
|
|
tor_free(data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct testcase_setup_t socks_setup = {
|
|
|
|
socks_test_setup, socks_test_cleanup
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SOCKS_TEST_INIT() \
|
|
|
|
socks_test_data_t *testdata = ptr; \
|
|
|
|
buf_t *buf = testdata->buf; \
|
|
|
|
socks_request_t *socks = testdata->req;
|
|
|
|
#define ADD_DATA(buf, s) \
|
|
|
|
write_to_buf(s, sizeof(s)-1, buf)
|
|
|
|
|
|
|
|
static void
|
|
|
|
socks_request_clear(socks_request_t *socks)
|
|
|
|
{
|
|
|
|
tor_free(socks->username);
|
|
|
|
tor_free(socks->password);
|
|
|
|
memset(socks, 0, sizeof(socks_request_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Perform unsupported SOCKS 4 commands */
|
2010-08-03 23:28:55 +02:00
|
|
|
static void
|
2011-06-29 23:30:55 +02:00
|
|
|
test_socks_4_unsupported_commands(void *ptr)
|
2010-08-03 23:28:55 +02:00
|
|
|
{
|
2011-06-29 23:30:55 +02:00
|
|
|
SOCKS_TEST_INIT();
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
/* SOCKS 4 Send BIND [02] to IP address 2.2.2.2:4369 */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x04\x02\x11\x11\x02\x02\x02\x02\x00");
|
2010-08-03 23:28:55 +02:00
|
|
|
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks) == -1);
|
|
|
|
test_eq(4, socks->socks_version);
|
|
|
|
test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
|
|
|
|
|
2011-06-29 17:47:35 +02:00
|
|
|
done:
|
2010-08-03 23:28:55 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
/** Perform supported SOCKS 4 commands */
|
2010-08-03 23:28:55 +02:00
|
|
|
static void
|
2011-06-29 23:30:55 +02:00
|
|
|
test_socks_4_supported_commands(void *ptr)
|
2010-08-03 23:28:55 +02:00
|
|
|
{
|
2011-06-29 23:30:55 +02:00
|
|
|
SOCKS_TEST_INIT();
|
|
|
|
|
|
|
|
test_eq(0, buf_datalen(buf));
|
|
|
|
|
|
|
|
/* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4370 */
|
|
|
|
ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x03\x00");
|
2010-08-03 23:28:55 +02:00
|
|
|
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks) == 1);
|
|
|
|
test_eq(4, socks->socks_version);
|
|
|
|
test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(SOCKS_COMMAND_CONNECT, socks->command);
|
|
|
|
test_streq("2.2.2.3", socks->address);
|
|
|
|
test_eq(4370, socks->port);
|
|
|
|
test_assert(socks->got_auth == 0);
|
|
|
|
test_assert(! socks->username);
|
|
|
|
|
|
|
|
test_eq(0, buf_datalen(buf));
|
|
|
|
socks_request_clear(socks);
|
2010-08-03 23:28:55 +02:00
|
|
|
|
|
|
|
/* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4369 with userid*/
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x04me\x00");
|
2010-08-03 23:28:55 +02:00
|
|
|
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks) == 1);
|
|
|
|
test_eq(4, socks->socks_version);
|
|
|
|
test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(SOCKS_COMMAND_CONNECT, socks->command);
|
|
|
|
test_streq("2.2.2.4", socks->address);
|
|
|
|
test_eq(4370, socks->port);
|
|
|
|
test_assert(socks->got_auth == 1);
|
|
|
|
test_assert(socks->username);
|
|
|
|
test_eq(2, socks->usernamelen);
|
|
|
|
test_memeq("me", socks->username, 2);
|
|
|
|
|
|
|
|
test_eq(0, buf_datalen(buf));
|
|
|
|
socks_request_clear(socks);
|
|
|
|
|
|
|
|
/* SOCKS 4a Send RESOLVE [F0] request for torproject.org */
|
|
|
|
ADD_DATA(buf, "\x04\xF0\x01\x01\x00\x00\x00\x02me\x00torproject.org\x00");
|
2010-08-03 23:28:55 +02:00
|
|
|
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
2011-06-29 23:30:55 +02:00
|
|
|
get_options()->SafeSocks) == 1);
|
2010-08-03 23:28:55 +02:00
|
|
|
test_eq(4, socks->socks_version);
|
|
|
|
test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
|
2011-06-29 23:30:55 +02:00
|
|
|
test_streq("torproject.org", socks->address);
|
|
|
|
|
|
|
|
test_eq(0, buf_datalen(buf));
|
2010-08-03 23:28:55 +02:00
|
|
|
|
2011-06-29 17:47:35 +02:00
|
|
|
done:
|
2010-08-03 23:28:55 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
/** Perform unsupported SOCKS 5 commands */
|
2010-08-03 23:28:55 +02:00
|
|
|
static void
|
2011-06-29 23:30:55 +02:00
|
|
|
test_socks_5_unsupported_commands(void *ptr)
|
2010-08-03 23:28:55 +02:00
|
|
|
{
|
2011-06-29 23:30:55 +02:00
|
|
|
SOCKS_TEST_INIT();
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
/* SOCKS 5 Send unsupported BIND [02] command */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x05\x02\x00\x01");
|
|
|
|
|
|
|
|
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks), 0);
|
|
|
|
test_eq(0, buf_datalen(buf));
|
2010-08-03 23:28:55 +02:00
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(0, socks->reply[1]);
|
|
|
|
ADD_DATA(buf, "\x05\x02\x00\x01\x02\x02\x02\x01\x01\x01");
|
|
|
|
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks), -1);
|
|
|
|
/* XXX: shouldn't tor reply 'command not supported' [07]? */
|
|
|
|
|
|
|
|
buf_clear(buf);
|
|
|
|
socks_request_clear(socks);
|
2010-08-03 23:28:55 +02:00
|
|
|
|
|
|
|
/* SOCKS 5 Send unsupported UDP_ASSOCIATE [03] command */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x05\x03\x00\x01\x02");
|
|
|
|
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks), 0);
|
2010-08-03 23:28:55 +02:00
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(0, socks->reply[1]);
|
|
|
|
ADD_DATA(buf, "\x05\x03\x00\x01\x02\x02\x02\x01\x01\x01");
|
|
|
|
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks), -1);
|
|
|
|
/* XXX: shouldn't tor reply 'command not supported' [07]? */
|
2010-08-03 23:28:55 +02:00
|
|
|
|
2011-06-29 17:47:35 +02:00
|
|
|
done:
|
2010-08-03 23:28:55 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
/** Perform supported SOCKS 5 commands */
|
2010-08-03 23:28:55 +02:00
|
|
|
static void
|
2011-06-29 23:30:55 +02:00
|
|
|
test_socks_5_supported_commands(void *ptr)
|
2010-08-03 23:28:55 +02:00
|
|
|
{
|
2011-06-29 23:30:55 +02:00
|
|
|
SOCKS_TEST_INIT();
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
/* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x05\x01\x00");
|
|
|
|
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks), 0);
|
2010-08-03 23:28:55 +02:00
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(0, socks->reply[1]);
|
2011-06-29 23:30:55 +02:00
|
|
|
|
|
|
|
ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
|
|
|
|
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks), 1);
|
2010-08-03 23:28:55 +02:00
|
|
|
test_streq("2.2.2.2", socks->address);
|
|
|
|
test_eq(4369, socks->port);
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(0, buf_datalen(buf));
|
|
|
|
socks_request_clear(socks);
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
/* SOCKS 5 Send CONNECT [01] to FQDN torproject.org:4369 */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x05\x01\x00");
|
|
|
|
ADD_DATA(buf, "\x05\x01\x00\x03\x0Etorproject.org\x11\x11");
|
|
|
|
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks), 1);
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(0, socks->reply[1]);
|
2011-06-29 23:30:55 +02:00
|
|
|
test_streq("torproject.org", socks->address);
|
2010-08-03 23:28:55 +02:00
|
|
|
test_eq(4369, socks->port);
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(0, buf_datalen(buf));
|
|
|
|
socks_request_clear(socks);
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
/* SOCKS 5 Send RESOLVE [F0] request for torproject.org:4369 */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x05\x01\x00");
|
|
|
|
ADD_DATA(buf, "\x05\xF0\x00\x03\x0Etorproject.org\x01\x02");
|
|
|
|
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks) == 1);
|
2010-08-03 23:28:55 +02:00
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(0, socks->reply[1]);
|
2011-06-29 23:30:55 +02:00
|
|
|
test_streq("torproject.org", socks->address);
|
2010-08-03 23:28:55 +02:00
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(0, buf_datalen(buf));
|
|
|
|
socks_request_clear(socks);
|
|
|
|
|
|
|
|
/* SOCKS 5 Send RESOLVE_PTR [F1] for IP address 2.2.2.5 */
|
|
|
|
ADD_DATA(buf, "\x05\x01\x00");
|
|
|
|
ADD_DATA(buf, "\x05\xF1\x00\x01\x02\x02\x02\x05\x01\x03");
|
2010-08-03 23:28:55 +02:00
|
|
|
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks) == 1);
|
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(0, socks->reply[1]);
|
2011-06-29 23:30:55 +02:00
|
|
|
test_streq("2.2.2.5", socks->address);
|
|
|
|
|
|
|
|
test_eq(0, buf_datalen(buf));
|
2010-08-03 23:28:55 +02:00
|
|
|
|
2011-06-29 17:47:35 +02:00
|
|
|
done:
|
2010-08-03 23:28:55 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
/** Perform SOCKS 5 authentication */
|
2010-08-03 23:28:55 +02:00
|
|
|
static void
|
2011-06-29 23:30:55 +02:00
|
|
|
test_socks_5_no_authenticate(void *ptr)
|
2010-08-03 23:28:55 +02:00
|
|
|
{
|
2011-06-29 23:30:55 +02:00
|
|
|
SOCKS_TEST_INIT();
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
/*SOCKS 5 No Authentication */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf,"\x05\x01\x00");
|
2010-08-03 23:28:55 +02:00
|
|
|
test_assert(!fetch_from_buf_socks(buf, socks,
|
|
|
|
get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks));
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(SOCKS_NO_AUTH, socks->reply[1]);
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(0, buf_datalen(buf));
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
/*SOCKS 5 Send username/password anyway - pretend to be broken */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf,"\x01\x02\x01\x01\x02\x01\x01");
|
2010-08-03 23:28:55 +02:00
|
|
|
test_assert(!fetch_from_buf_socks(buf, socks,
|
|
|
|
get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks));
|
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(0, socks->reply[1]);
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(2, socks->usernamelen);
|
|
|
|
test_eq(2, socks->passwordlen);
|
|
|
|
|
|
|
|
test_memeq("\x01\x01", socks->username, 2);
|
|
|
|
test_memeq("\x01\x01", socks->password, 2);
|
|
|
|
|
2011-06-29 17:47:35 +02:00
|
|
|
done:
|
2010-08-03 23:28:55 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
/** Perform SOCKS 5 authentication */
|
2010-08-03 23:28:55 +02:00
|
|
|
static void
|
2011-06-29 23:30:55 +02:00
|
|
|
test_socks_5_authenticate(void *ptr)
|
2010-08-03 23:28:55 +02:00
|
|
|
{
|
2011-06-29 23:30:55 +02:00
|
|
|
SOCKS_TEST_INIT();
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
/* SOCKS 5 Negotiate username/password authentication */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x05\x01\x02");
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
test_assert(!fetch_from_buf_socks(buf, socks,
|
|
|
|
get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks));
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(SOCKS_USER_PASS, socks->reply[1]);
|
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(0, buf_datalen(buf));
|
|
|
|
|
2010-08-03 23:28:55 +02:00
|
|
|
/* SOCKS 5 Send username/password */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x01\x02me\x08mypasswd");
|
2010-08-03 23:28:55 +02:00
|
|
|
test_assert(!fetch_from_buf_socks(buf, socks,
|
|
|
|
get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks));
|
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(0, socks->reply[1]);
|
2011-06-29 23:30:55 +02:00
|
|
|
|
|
|
|
test_eq(2, socks->usernamelen);
|
|
|
|
test_eq(8, socks->passwordlen);
|
|
|
|
|
|
|
|
test_memeq("me", socks->username, 2);
|
|
|
|
test_memeq("mypasswd", socks->password, 8);
|
|
|
|
|
2011-06-29 17:47:35 +02:00
|
|
|
done:
|
2010-08-03 23:28:55 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
/** Perform SOCKS 5 authentication and send data all in one go */
|
2010-12-14 20:59:42 +01:00
|
|
|
static void
|
2011-06-29 23:30:55 +02:00
|
|
|
test_socks_5_authenticate_with_data(void *ptr)
|
2010-12-14 20:59:42 +01:00
|
|
|
{
|
2011-06-29 23:30:55 +02:00
|
|
|
SOCKS_TEST_INIT();
|
|
|
|
|
2010-12-14 20:59:42 +01:00
|
|
|
/* SOCKS 5 Negotiate username/password authentication */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x05\x01\x02");
|
|
|
|
|
2010-12-14 20:59:42 +01:00
|
|
|
test_assert(!fetch_from_buf_socks(buf, socks,
|
|
|
|
get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks));
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(SOCKS_USER_PASS, socks->reply[1]);
|
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
test_eq(0, buf_datalen(buf));
|
|
|
|
|
2010-12-14 20:59:42 +01:00
|
|
|
/* SOCKS 5 Send username/password */
|
|
|
|
/* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
|
2011-06-29 23:44:29 +02:00
|
|
|
ADD_DATA(buf, "\x01\x02me\x03you\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
|
|
|
|
test_assert(fetch_from_buf_socks(buf, socks,
|
2010-12-14 20:59:42 +01:00
|
|
|
get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks) == 1);
|
|
|
|
test_eq(5, socks->socks_version);
|
|
|
|
test_eq(2, socks->replylen);
|
|
|
|
test_eq(5, socks->reply[0]);
|
|
|
|
test_eq(0, socks->reply[1]);
|
2011-06-29 23:44:29 +02:00
|
|
|
|
2010-12-14 20:59:42 +01:00
|
|
|
test_streq("2.2.2.2", socks->address);
|
|
|
|
test_eq(4369, socks->port);
|
2011-06-29 23:30:55 +02:00
|
|
|
|
2011-06-29 23:44:29 +02:00
|
|
|
test_eq(2, socks->usernamelen);
|
|
|
|
test_eq(3, socks->passwordlen);
|
|
|
|
test_memeq("me", socks->username, 2);
|
|
|
|
test_memeq("you", socks->password, 3);
|
|
|
|
|
2011-06-29 17:47:35 +02:00
|
|
|
done:
|
2010-12-14 20:59:42 +01:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
/** Perform SOCKS 5 authentication before method negotiated */
|
2010-10-17 15:14:51 +02:00
|
|
|
static void
|
2011-06-29 23:30:55 +02:00
|
|
|
test_socks_5_auth_before_negotiation(void *ptr)
|
2010-10-17 15:14:51 +02:00
|
|
|
{
|
2011-06-29 23:30:55 +02:00
|
|
|
SOCKS_TEST_INIT();
|
|
|
|
|
2010-10-17 15:14:51 +02:00
|
|
|
/* SOCKS 5 Send username/password */
|
2011-06-29 23:30:55 +02:00
|
|
|
ADD_DATA(buf, "\x01\x02me\x02me");
|
2010-10-17 15:14:51 +02:00
|
|
|
test_assert(fetch_from_buf_socks(buf, socks,
|
|
|
|
get_options()->TestSocks,
|
|
|
|
get_options()->SafeSocks) == -1);
|
|
|
|
test_eq(0, socks->socks_version);
|
|
|
|
test_eq(0, socks->replylen);
|
|
|
|
test_eq(0, socks->reply[0]);
|
|
|
|
test_eq(0, socks->reply[1]);
|
|
|
|
|
2011-06-29 17:47:35 +02:00
|
|
|
done:
|
2010-10-17 15:14:51 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2011-07-20 15:34:19 +02:00
|
|
|
static void
|
|
|
|
test_buffer_copy(void *arg)
|
|
|
|
{
|
|
|
|
generic_buffer_t *buf=NULL, *buf2=NULL;
|
|
|
|
const char *s;
|
|
|
|
size_t len;
|
|
|
|
char b[256];
|
|
|
|
int i;
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
buf = generic_buffer_new();
|
|
|
|
tt_assert(buf);
|
|
|
|
|
|
|
|
/* Copy an empty buffer. */
|
|
|
|
tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
|
|
|
|
tt_assert(buf2);
|
|
|
|
tt_int_op(0, ==, generic_buffer_len(buf2));
|
|
|
|
|
|
|
|
/* Now try with a short buffer. */
|
|
|
|
s = "And now comes an act of enormous enormance!";
|
|
|
|
len = strlen(s);
|
|
|
|
generic_buffer_add(buf, s, len);
|
|
|
|
tt_int_op(len, ==, generic_buffer_len(buf));
|
|
|
|
/* Add junk to buf2 so we can test replacing.*/
|
|
|
|
generic_buffer_add(buf2, "BLARG", 5);
|
|
|
|
tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
|
|
|
|
tt_int_op(len, ==, generic_buffer_len(buf2));
|
|
|
|
generic_buffer_get(buf2, b, len);
|
|
|
|
test_mem_op(b, ==, s, len);
|
|
|
|
/* Now free buf2 and retry so we can test allocating */
|
|
|
|
generic_buffer_free(buf2);
|
|
|
|
buf2 = NULL;
|
|
|
|
tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
|
|
|
|
tt_int_op(len, ==, generic_buffer_len(buf2));
|
|
|
|
generic_buffer_get(buf2, b, len);
|
|
|
|
test_mem_op(b, ==, s, len);
|
|
|
|
/* Clear buf for next test */
|
|
|
|
generic_buffer_get(buf, b, len);
|
|
|
|
tt_int_op(generic_buffer_len(buf),==,0);
|
|
|
|
|
|
|
|
/* Okay, now let's try a bigger buffer. */
|
|
|
|
s = "Quis autem vel eum iure reprehenderit qui in ea voluptate velit "
|
|
|
|
"esse quam nihil molestiae consequatur, vel illum qui dolorem eum "
|
|
|
|
"fugiat quo voluptas nulla pariatur?";
|
|
|
|
len = strlen(s);
|
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
b[0]=i;
|
|
|
|
generic_buffer_add(buf, b, 1);
|
|
|
|
generic_buffer_add(buf, s, len);
|
|
|
|
}
|
|
|
|
tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
|
|
|
|
tt_int_op(generic_buffer_len(buf2), ==, generic_buffer_len(buf));
|
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
generic_buffer_get(buf2, b, len+1);
|
|
|
|
tt_int_op((unsigned char)b[0],==,i);
|
|
|
|
test_mem_op(b+1, ==, s, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (buf)
|
|
|
|
generic_buffer_free(buf);
|
|
|
|
if (buf2)
|
|
|
|
generic_buffer_free(buf2);
|
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Run unit tests for buffers.c */
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
test_buffers(void)
|
|
|
|
{
|
2003-04-15 21:10:18 +02:00
|
|
|
char str[256];
|
|
|
|
char str2[256];
|
|
|
|
|
2008-09-10 02:16:32 +02:00
|
|
|
buf_t *buf = NULL, *buf2 = NULL;
|
2008-02-21 00:38:55 +01:00
|
|
|
const char *cp;
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2005-04-26 23:10:31 +02:00
|
|
|
int j;
|
2007-04-21 19:26:12 +02:00
|
|
|
size_t r;
|
2003-04-15 21:10:18 +02:00
|
|
|
|
|
|
|
/****
|
|
|
|
* buf_new
|
|
|
|
****/
|
2003-09-25 07:17:11 +02:00
|
|
|
if (!(buf = buf_new()))
|
2003-04-07 15:25:44 +02:00
|
|
|
test_fail();
|
2003-04-15 21:10:18 +02:00
|
|
|
|
2007-12-26 01:12:08 +01:00
|
|
|
//test_eq(buf_capacity(buf), 4096);
|
2003-09-25 07:17:11 +02:00
|
|
|
test_eq(buf_datalen(buf), 0);
|
2003-04-15 21:10:18 +02:00
|
|
|
|
|
|
|
/****
|
2005-04-26 22:53:22 +02:00
|
|
|
* General pointer frobbing
|
|
|
|
*/
|
2003-04-15 21:10:18 +02:00
|
|
|
for (j=0;j<256;++j) {
|
|
|
|
str[j] = (char)j;
|
2003-04-07 15:25:44 +02:00
|
|
|
}
|
2005-04-26 22:53:22 +02:00
|
|
|
write_to_buf(str, 256, buf);
|
|
|
|
write_to_buf(str, 256, buf);
|
|
|
|
test_eq(buf_datalen(buf), 512);
|
|
|
|
fetch_from_buf(str2, 200, buf);
|
|
|
|
test_memeq(str, str2, 200);
|
|
|
|
test_eq(buf_datalen(buf), 312);
|
|
|
|
memset(str2, 0, sizeof(str2));
|
|
|
|
|
|
|
|
fetch_from_buf(str2, 256, buf);
|
|
|
|
test_memeq(str+200, str2, 56);
|
|
|
|
test_memeq(str, str2+56, 200);
|
|
|
|
test_eq(buf_datalen(buf), 56);
|
|
|
|
memset(str2, 0, sizeof(str2));
|
|
|
|
/* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
|
|
|
|
* another 3584 bytes, we hit the end. */
|
2005-04-26 22:55:32 +02:00
|
|
|
for (j=0;j<15;++j) {
|
2005-04-26 22:53:22 +02:00
|
|
|
write_to_buf(str, 256, buf);
|
|
|
|
}
|
|
|
|
assert_buf_ok(buf);
|
|
|
|
test_eq(buf_datalen(buf), 3896);
|
|
|
|
fetch_from_buf(str2, 56, buf);
|
|
|
|
test_eq(buf_datalen(buf), 3840);
|
|
|
|
test_memeq(str+200, str2, 56);
|
2005-04-26 22:55:32 +02:00
|
|
|
for (j=0;j<15;++j) {
|
2005-04-26 22:53:22 +02:00
|
|
|
memset(str2, 0, sizeof(str2));
|
|
|
|
fetch_from_buf(str2, 256, buf);
|
|
|
|
test_memeq(str, str2, 256);
|
|
|
|
}
|
|
|
|
test_eq(buf_datalen(buf), 0);
|
|
|
|
buf_free(buf);
|
2008-09-10 02:16:32 +02:00
|
|
|
buf = NULL;
|
2005-04-26 22:53:22 +02:00
|
|
|
|
|
|
|
/* Okay, now make sure growing can work. */
|
|
|
|
buf = buf_new_with_capacity(16);
|
2007-12-26 01:12:08 +01:00
|
|
|
//test_eq(buf_capacity(buf), 16);
|
2005-04-26 22:53:22 +02:00
|
|
|
write_to_buf(str+1, 255, buf);
|
2007-12-26 01:12:08 +01:00
|
|
|
//test_eq(buf_capacity(buf), 256);
|
2005-04-26 22:53:22 +02:00
|
|
|
fetch_from_buf(str2, 254, buf);
|
|
|
|
test_memeq(str+1, str2, 254);
|
2007-12-26 01:12:08 +01:00
|
|
|
//test_eq(buf_capacity(buf), 256);
|
2005-04-26 22:53:22 +02:00
|
|
|
assert_buf_ok(buf);
|
|
|
|
write_to_buf(str, 32, buf);
|
2007-12-26 01:12:08 +01:00
|
|
|
//test_eq(buf_capacity(buf), 256);
|
2005-04-26 22:53:22 +02:00
|
|
|
assert_buf_ok(buf);
|
|
|
|
write_to_buf(str, 256, buf);
|
|
|
|
assert_buf_ok(buf);
|
2007-12-26 01:12:08 +01:00
|
|
|
//test_eq(buf_capacity(buf), 512);
|
2005-04-26 22:53:22 +02:00
|
|
|
test_eq(buf_datalen(buf), 33+256);
|
|
|
|
fetch_from_buf(str2, 33, buf);
|
|
|
|
test_eq(*str2, str[255]);
|
|
|
|
|
|
|
|
test_memeq(str2+1, str, 32);
|
2007-12-26 01:12:08 +01:00
|
|
|
//test_eq(buf_capacity(buf), 512);
|
2005-04-26 22:53:22 +02:00
|
|
|
test_eq(buf_datalen(buf), 256);
|
|
|
|
fetch_from_buf(str2, 256, buf);
|
|
|
|
test_memeq(str, str2, 256);
|
|
|
|
|
|
|
|
/* now try shrinking: case 1. */
|
|
|
|
buf_free(buf);
|
|
|
|
buf = buf_new_with_capacity(33668);
|
|
|
|
for (j=0;j<67;++j) {
|
|
|
|
write_to_buf(str,255, buf);
|
|
|
|
}
|
2007-12-26 01:12:08 +01:00
|
|
|
//test_eq(buf_capacity(buf), 33668);
|
2005-04-26 22:53:22 +02:00
|
|
|
test_eq(buf_datalen(buf), 17085);
|
|
|
|
for (j=0; j < 40; ++j) {
|
|
|
|
fetch_from_buf(str2, 255,buf);
|
|
|
|
test_memeq(str2, str, 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now try shrinking: case 2. */
|
|
|
|
buf_free(buf);
|
|
|
|
buf = buf_new_with_capacity(33668);
|
|
|
|
for (j=0;j<67;++j) {
|
|
|
|
write_to_buf(str,255, buf);
|
|
|
|
}
|
|
|
|
for (j=0; j < 20; ++j) {
|
|
|
|
fetch_from_buf(str2, 255,buf);
|
|
|
|
test_memeq(str2, str, 255);
|
|
|
|
}
|
|
|
|
for (j=0;j<80;++j) {
|
|
|
|
write_to_buf(str,255, buf);
|
|
|
|
}
|
2007-12-26 01:12:08 +01:00
|
|
|
//test_eq(buf_capacity(buf),33668);
|
2005-04-26 22:53:22 +02:00
|
|
|
for (j=0; j < 120; ++j) {
|
|
|
|
fetch_from_buf(str2, 255,buf);
|
|
|
|
test_memeq(str2, str, 255);
|
|
|
|
}
|
|
|
|
|
2007-04-21 19:26:12 +02:00
|
|
|
/* Move from buf to buf. */
|
|
|
|
buf_free(buf);
|
|
|
|
buf = buf_new_with_capacity(4096);
|
|
|
|
buf2 = buf_new_with_capacity(4096);
|
|
|
|
for (j=0;j<100;++j)
|
|
|
|
write_to_buf(str, 255, buf);
|
|
|
|
test_eq(buf_datalen(buf), 25500);
|
|
|
|
for (j=0;j<100;++j) {
|
|
|
|
r = 10;
|
|
|
|
move_buf_to_buf(buf2, buf, &r);
|
|
|
|
test_eq(r, 0);
|
|
|
|
}
|
|
|
|
test_eq(buf_datalen(buf), 24500);
|
|
|
|
test_eq(buf_datalen(buf2), 1000);
|
|
|
|
for (j=0;j<3;++j) {
|
|
|
|
fetch_from_buf(str2, 255, buf2);
|
|
|
|
test_memeq(str2, str, 255);
|
|
|
|
}
|
|
|
|
r = 8192; /*big move*/
|
|
|
|
move_buf_to_buf(buf2, buf, &r);
|
|
|
|
test_eq(r, 0);
|
|
|
|
r = 30000; /* incomplete move */
|
|
|
|
move_buf_to_buf(buf2, buf, &r);
|
|
|
|
test_eq(r, 13692);
|
|
|
|
for (j=0;j<97;++j) {
|
|
|
|
fetch_from_buf(str2, 255, buf2);
|
|
|
|
test_memeq(str2, str, 255);
|
|
|
|
}
|
|
|
|
buf_free(buf);
|
|
|
|
buf_free(buf2);
|
2008-09-10 02:16:32 +02:00
|
|
|
buf = buf2 = NULL;
|
2007-04-21 19:26:12 +02:00
|
|
|
|
2008-02-21 00:38:55 +01:00
|
|
|
buf = buf_new_with_capacity(5);
|
|
|
|
cp = "Testing. This is a moderately long Testing string.";
|
|
|
|
for (j = 0; cp[j]; j++)
|
|
|
|
write_to_buf(cp+j, 1, buf);
|
|
|
|
test_eq(0, buf_find_string_offset(buf, "Testing", 7));
|
|
|
|
test_eq(1, buf_find_string_offset(buf, "esting", 6));
|
|
|
|
test_eq(1, buf_find_string_offset(buf, "est", 3));
|
|
|
|
test_eq(39, buf_find_string_offset(buf, "ing str", 7));
|
|
|
|
test_eq(35, buf_find_string_offset(buf, "Testing str", 11));
|
|
|
|
test_eq(32, buf_find_string_offset(buf, "ng ", 3));
|
2008-02-21 03:10:38 +01:00
|
|
|
test_eq(43, buf_find_string_offset(buf, "string.", 7));
|
2008-02-21 00:38:55 +01:00
|
|
|
test_eq(-1, buf_find_string_offset(buf, "shrdlu", 6));
|
|
|
|
test_eq(-1, buf_find_string_offset(buf, "Testing thing", 13));
|
|
|
|
test_eq(-1, buf_find_string_offset(buf, "ngx", 3));
|
|
|
|
buf_free(buf);
|
2008-09-10 02:16:32 +02:00
|
|
|
buf = NULL;
|
2008-02-20 18:48:39 +01:00
|
|
|
|
2008-09-09 22:43:31 +02:00
|
|
|
done:
|
2008-09-10 02:16:32 +02:00
|
|
|
if (buf)
|
|
|
|
buf_free(buf);
|
|
|
|
if (buf2)
|
|
|
|
buf_free(buf2);
|
2003-04-07 15:25:44 +02:00
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Run unit tests for the onion handshake code. */
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2004-10-27 20:16:37 +02:00
|
|
|
test_onion_handshake(void)
|
|
|
|
{
|
2003-05-05 06:27:00 +02:00
|
|
|
/* client-side */
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_dh_t *c_dh = NULL;
|
2003-12-16 09:21:58 +01:00
|
|
|
char c_buf[ONIONSKIN_CHALLENGE_LEN];
|
2003-05-05 06:27:00 +02:00
|
|
|
char c_keys[40];
|
|
|
|
|
|
|
|
/* server-side */
|
2003-12-16 09:21:58 +01:00
|
|
|
char s_buf[ONIONSKIN_REPLY_LEN];
|
2003-05-05 06:27:00 +02:00
|
|
|
char s_keys[40];
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-05-05 06:27:00 +02:00
|
|
|
/* shared */
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *pk = NULL;
|
2003-05-05 06:27:00 +02:00
|
|
|
|
2006-10-23 05:48:58 +02:00
|
|
|
pk = pk_generate(0);
|
2003-05-05 06:27:00 +02:00
|
|
|
|
|
|
|
/* client handshake 1. */
|
2003-12-16 09:21:58 +01:00
|
|
|
memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
|
2003-05-05 06:27:00 +02:00
|
|
|
test_assert(! onion_skin_create(pk, &c_dh, c_buf));
|
|
|
|
|
|
|
|
/* server handshake */
|
2003-12-16 09:21:58 +01:00
|
|
|
memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
|
2003-05-05 06:27:00 +02:00
|
|
|
memset(s_keys, 0, 40);
|
2005-12-14 21:40:40 +01:00
|
|
|
test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
|
|
|
|
s_buf, s_keys, 40));
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-05-05 06:27:00 +02:00
|
|
|
/* client handshake 2 */
|
|
|
|
memset(c_keys, 0, 40);
|
|
|
|
test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-06-13 23:23:14 +02:00
|
|
|
if (memcmp(c_keys, s_keys, 40)) {
|
|
|
|
puts("Aiiiie");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-05-05 06:27:00 +02:00
|
|
|
test_memeq(c_keys, s_keys, 40);
|
|
|
|
memset(s_buf, 0, 40);
|
|
|
|
test_memneq(c_keys, s_buf, 40);
|
2008-09-09 22:43:31 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (c_dh)
|
|
|
|
crypto_dh_free(c_dh);
|
|
|
|
if (pk)
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(pk);
|
2003-05-05 06:27:00 +02:00
|
|
|
}
|
|
|
|
|
2009-08-28 08:28:20 +02:00
|
|
|
static void
|
|
|
|
test_circuit_timeout(void)
|
|
|
|
{
|
|
|
|
/* Plan:
|
|
|
|
* 1. Generate 1000 samples
|
|
|
|
* 2. Estimate parameters
|
|
|
|
* 3. If difference, repeat
|
|
|
|
* 4. Save state
|
|
|
|
* 5. load state
|
|
|
|
* 6. Estimate parameters
|
|
|
|
* 7. compare differences
|
|
|
|
*/
|
|
|
|
circuit_build_times_t initial;
|
|
|
|
circuit_build_times_t estimate;
|
|
|
|
circuit_build_times_t final;
|
2009-09-18 11:01:39 +02:00
|
|
|
double timeout1, timeout2;
|
2009-08-28 08:28:20 +02:00
|
|
|
or_state_t state;
|
2009-09-18 11:01:39 +02:00
|
|
|
int i, runs;
|
2010-07-06 17:49:50 +02:00
|
|
|
double close_ms;
|
2009-09-02 05:27:43 +02:00
|
|
|
circuit_build_times_init(&initial);
|
|
|
|
circuit_build_times_init(&estimate);
|
|
|
|
circuit_build_times_init(&final);
|
|
|
|
|
2009-08-28 08:28:20 +02:00
|
|
|
memset(&state, 0, sizeof(or_state_t));
|
|
|
|
|
2009-09-02 05:27:43 +02:00
|
|
|
circuitbuild_running_unit_tests();
|
2009-09-17 00:20:25 +02:00
|
|
|
#define timeout0 (build_time_t)(30*1000.0)
|
2010-07-06 17:49:50 +02:00
|
|
|
initial.Xm = 3000;
|
2010-01-22 01:10:02 +01:00
|
|
|
circuit_build_times_initial_alpha(&initial,
|
|
|
|
CBT_DEFAULT_QUANTILE_CUTOFF/100.0,
|
2009-08-28 08:28:20 +02:00
|
|
|
timeout0);
|
2010-07-06 17:49:50 +02:00
|
|
|
close_ms = MAX(circuit_build_times_calculate_timeout(&initial,
|
|
|
|
CBT_DEFAULT_CLOSE_QUANTILE/100.0),
|
|
|
|
CBT_DEFAULT_TIMEOUT_INITIAL_VALUE);
|
2009-08-28 08:28:20 +02:00
|
|
|
do {
|
2010-01-22 01:10:02 +01:00
|
|
|
for (i=0; i < CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE; i++) {
|
2010-07-06 17:49:50 +02:00
|
|
|
build_time_t sample = circuit_build_times_generate_sample(&initial,0,1);
|
|
|
|
|
|
|
|
if (sample > close_ms) {
|
|
|
|
circuit_build_times_add_time(&estimate, CBT_BUILD_ABANDONED);
|
|
|
|
} else {
|
|
|
|
circuit_build_times_add_time(&estimate, sample);
|
2009-08-28 08:28:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
circuit_build_times_update_alpha(&estimate);
|
|
|
|
timeout1 = circuit_build_times_calculate_timeout(&estimate,
|
2010-01-22 01:10:02 +01:00
|
|
|
CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
|
2009-09-02 05:27:43 +02:00
|
|
|
circuit_build_times_set_timeout(&estimate);
|
2011-08-31 02:42:51 +02:00
|
|
|
log_notice(LD_CIRC, "Timeout1 is %f, Xm is %d", timeout1, estimate.Xm);
|
2010-07-06 17:49:50 +02:00
|
|
|
/* 2% error */
|
2009-08-28 08:28:20 +02:00
|
|
|
} while (fabs(circuit_build_times_cdf(&initial, timeout0) -
|
2010-07-06 17:49:50 +02:00
|
|
|
circuit_build_times_cdf(&initial, timeout1)) > 0.02);
|
2009-08-28 08:28:20 +02:00
|
|
|
|
2010-06-08 04:06:06 +02:00
|
|
|
test_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
|
2009-08-28 08:28:20 +02:00
|
|
|
|
2009-09-02 05:27:43 +02:00
|
|
|
circuit_build_times_update_state(&estimate, &state);
|
2010-08-15 14:22:32 +02:00
|
|
|
test_assert(circuit_build_times_parse_state(&final, &state) == 0);
|
2009-08-28 08:28:20 +02:00
|
|
|
|
|
|
|
circuit_build_times_update_alpha(&final);
|
|
|
|
timeout2 = circuit_build_times_calculate_timeout(&final,
|
2010-01-22 01:10:02 +01:00
|
|
|
CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
|
2009-09-02 05:27:43 +02:00
|
|
|
|
|
|
|
circuit_build_times_set_timeout(&final);
|
2011-08-31 02:42:51 +02:00
|
|
|
log_notice(LD_CIRC, "Timeout2 is %f, Xm is %d", timeout2, final.Xm);
|
2009-08-28 08:28:20 +02:00
|
|
|
|
2010-06-08 04:06:06 +02:00
|
|
|
/* 5% here because some accuracy is lost due to histogram conversion */
|
2009-08-28 08:28:20 +02:00
|
|
|
test_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
|
2009-09-07 05:14:13 +02:00
|
|
|
circuit_build_times_cdf(&initial, timeout2)) < 0.05);
|
2009-08-28 08:28:20 +02:00
|
|
|
|
2009-09-18 11:01:39 +02:00
|
|
|
for (runs = 0; runs < 50; runs++) {
|
|
|
|
int build_times_idx = 0;
|
|
|
|
int total_build_times = 0;
|
|
|
|
|
2010-06-15 10:13:49 +02:00
|
|
|
final.close_ms = final.timeout_ms = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
|
|
|
|
estimate.close_ms = estimate.timeout_ms
|
|
|
|
= CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2010-01-22 01:10:02 +01:00
|
|
|
for (i = 0; i < CBT_DEFAULT_RECENT_CIRCUITS*2; i++) {
|
2009-09-18 11:01:39 +02:00
|
|
|
circuit_build_times_network_circ_success(&estimate);
|
|
|
|
circuit_build_times_add_time(&estimate,
|
|
|
|
circuit_build_times_generate_sample(&estimate, 0,
|
2010-01-22 01:10:02 +01:00
|
|
|
CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
|
2010-06-15 10:13:49 +02:00
|
|
|
|
2009-09-18 11:01:39 +02:00
|
|
|
circuit_build_times_network_circ_success(&estimate);
|
|
|
|
circuit_build_times_add_time(&final,
|
|
|
|
circuit_build_times_generate_sample(&final, 0,
|
2010-01-22 01:10:02 +01:00
|
|
|
CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
|
2009-09-18 11:01:39 +02:00
|
|
|
}
|
2009-09-02 05:27:43 +02:00
|
|
|
|
2009-09-18 11:01:39 +02:00
|
|
|
test_assert(!circuit_build_times_network_check_changed(&estimate));
|
|
|
|
test_assert(!circuit_build_times_network_check_changed(&final));
|
|
|
|
|
|
|
|
/* Reset liveness to be non-live */
|
|
|
|
final.liveness.network_last_live = 0;
|
|
|
|
estimate.liveness.network_last_live = 0;
|
|
|
|
|
|
|
|
build_times_idx = estimate.build_times_idx;
|
|
|
|
total_build_times = estimate.total_build_times;
|
|
|
|
|
2010-09-29 17:55:11 +02:00
|
|
|
test_assert(circuit_build_times_network_check_live(&estimate));
|
|
|
|
test_assert(circuit_build_times_network_check_live(&final));
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2010-09-29 17:55:11 +02:00
|
|
|
circuit_build_times_count_close(&estimate, 0,
|
|
|
|
(time_t)(approx_time()-estimate.close_ms/1000.0-1));
|
|
|
|
circuit_build_times_count_close(&final, 0,
|
|
|
|
(time_t)(approx_time()-final.close_ms/1000.0-1));
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2009-09-18 11:01:39 +02:00
|
|
|
test_assert(!circuit_build_times_network_check_live(&estimate));
|
|
|
|
test_assert(!circuit_build_times_network_check_live(&final));
|
|
|
|
|
|
|
|
log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
|
|
|
|
build_times_idx, estimate.build_times_idx,
|
|
|
|
total_build_times, estimate.total_build_times);
|
|
|
|
|
|
|
|
/* Check rollback index. Should match top of loop. */
|
|
|
|
test_assert(build_times_idx == estimate.build_times_idx);
|
2010-06-08 04:06:06 +02:00
|
|
|
// This can fail if estimate.total_build_times == 1000, because
|
|
|
|
// in that case, rewind actually causes us to lose timeouts
|
|
|
|
if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
|
|
|
|
test_assert(total_build_times == estimate.total_build_times);
|
2009-09-18 11:01:39 +02:00
|
|
|
|
|
|
|
/* Now simulate that the network has become live and we need
|
|
|
|
* a change */
|
|
|
|
circuit_build_times_network_is_live(&estimate);
|
|
|
|
circuit_build_times_network_is_live(&final);
|
|
|
|
|
2010-01-22 01:10:02 +01:00
|
|
|
for (i = 0; i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT; i++) {
|
2010-06-15 10:13:49 +02:00
|
|
|
circuit_build_times_count_timeout(&estimate, 1);
|
2009-09-18 11:01:39 +02:00
|
|
|
|
2010-01-22 01:10:02 +01:00
|
|
|
if (i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1) {
|
2010-06-15 10:13:49 +02:00
|
|
|
circuit_build_times_count_timeout(&final, 1);
|
2009-09-18 11:01:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 01:50:44 +02:00
|
|
|
test_assert(estimate.liveness.after_firsthop_idx == 0);
|
|
|
|
test_assert(final.liveness.after_firsthop_idx ==
|
2010-01-22 01:10:02 +01:00
|
|
|
CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
|
2009-09-18 11:01:39 +02:00
|
|
|
|
|
|
|
test_assert(circuit_build_times_network_check_live(&estimate));
|
|
|
|
test_assert(circuit_build_times_network_check_live(&final));
|
|
|
|
|
2010-06-15 10:13:49 +02:00
|
|
|
circuit_build_times_count_timeout(&final, 1);
|
2009-09-18 11:01:39 +02:00
|
|
|
}
|
2009-09-01 03:10:27 +02:00
|
|
|
|
2010-08-16 00:29:27 +02:00
|
|
|
done:
|
2009-08-28 08:28:20 +02:00
|
|
|
return;
|
2009-09-15 04:15:57 +02:00
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
|
|
|
|
* that policies_summarize() produces the string <b>expected_summary</b> from
|
|
|
|
* it. */
|
2008-08-13 14:46:06 +02:00
|
|
|
static void
|
2008-08-13 14:46:13 +02:00
|
|
|
test_policy_summary_helper(const char *policy_str,
|
|
|
|
const char *expected_summary)
|
2008-08-13 14:46:06 +02:00
|
|
|
{
|
|
|
|
config_line_t line;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *policy = smartlist_new();
|
2008-09-09 22:43:31 +02:00
|
|
|
char *summary = NULL;
|
2008-12-18 05:45:51 +01:00
|
|
|
int r;
|
2010-10-02 00:12:30 +02:00
|
|
|
short_policy_t *short_policy = NULL;
|
2008-08-13 14:46:06 +02:00
|
|
|
|
|
|
|
line.key = (char*)"foo";
|
|
|
|
line.value = (char *)policy_str;
|
|
|
|
line.next = NULL;
|
|
|
|
|
2009-10-27 09:03:41 +01:00
|
|
|
r = policies_parse_exit_policy(&line, &policy, 0, NULL, 1);
|
2008-12-18 05:45:51 +01:00
|
|
|
test_eq(r, 0);
|
2008-08-13 14:46:06 +02:00
|
|
|
summary = policy_summarize(policy);
|
|
|
|
|
2008-08-14 14:37:50 +02:00
|
|
|
test_assert(summary != NULL);
|
|
|
|
test_streq(summary, expected_summary);
|
2008-09-09 22:43:31 +02:00
|
|
|
|
2010-10-02 00:12:30 +02:00
|
|
|
short_policy = parse_short_policy(summary);
|
|
|
|
tt_assert(short_policy);
|
|
|
|
|
2008-09-09 22:43:31 +02:00
|
|
|
done:
|
2008-08-14 14:37:50 +02:00
|
|
|
tor_free(summary);
|
2008-09-09 22:43:31 +02:00
|
|
|
if (policy)
|
|
|
|
addr_policy_list_free(policy);
|
2010-10-02 00:12:30 +02:00
|
|
|
short_policy_free(short_policy);
|
2008-08-13 14:46:06 +02:00
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Run unit tests for generating summary lines of exit policies */
|
2005-05-14 02:13:17 +02:00
|
|
|
static void
|
2006-10-20 01:05:25 +02:00
|
|
|
test_policies(void)
|
2005-05-14 02:13:17 +02:00
|
|
|
{
|
2008-08-13 21:25:18 +02:00
|
|
|
int i;
|
2010-02-03 05:43:09 +01:00
|
|
|
smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
|
2010-02-05 11:59:15 +01:00
|
|
|
*policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
|
|
|
|
*policy7 = NULL;
|
2008-01-02 05:43:44 +01:00
|
|
|
addr_policy_t *p;
|
2007-07-19 20:46:09 +02:00
|
|
|
tor_addr_t tar;
|
2006-10-20 01:05:25 +02:00
|
|
|
config_line_t line;
|
2008-09-10 07:34:03 +02:00
|
|
|
smartlist_t *sm = NULL;
|
2008-09-25 17:20:38 +02:00
|
|
|
char *policy_str = NULL;
|
2005-05-14 02:13:17 +02:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
policy = smartlist_new();
|
2008-01-02 05:43:44 +01:00
|
|
|
|
|
|
|
p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
|
2008-07-24 15:44:04 +02:00
|
|
|
test_assert(p != NULL);
|
2008-01-02 05:43:44 +01:00
|
|
|
test_eq(ADDR_POLICY_REJECT, p->policy_type);
|
2008-07-24 15:44:04 +02:00
|
|
|
tor_addr_from_ipv4h(&tar, 0xc0a80000u);
|
2008-07-25 17:11:21 +02:00
|
|
|
test_eq(0, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
|
2008-01-02 05:43:44 +01:00
|
|
|
test_eq(16, p->maskbits);
|
|
|
|
test_eq(1, p->prt_min);
|
|
|
|
test_eq(65535, p->prt_max);
|
|
|
|
|
|
|
|
smartlist_add(policy, p);
|
2005-05-14 02:13:17 +02:00
|
|
|
|
2011-07-15 18:31:09 +02:00
|
|
|
tor_addr_from_ipv4h(&tar, 0x01020304u);
|
2005-08-12 19:24:53 +02:00
|
|
|
test_assert(ADDR_POLICY_ACCEPTED ==
|
2011-07-15 18:31:09 +02:00
|
|
|
compare_tor_addr_to_addr_policy(&tar, 2, policy));
|
|
|
|
tor_addr_make_unspec(&tar);
|
2005-08-12 19:24:53 +02:00
|
|
|
test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
|
2011-07-15 18:31:09 +02:00
|
|
|
compare_tor_addr_to_addr_policy(&tar, 2, policy));
|
|
|
|
tor_addr_from_ipv4h(&tar, 0xc0a80102);
|
2005-08-12 19:24:53 +02:00
|
|
|
test_assert(ADDR_POLICY_REJECTED ==
|
2011-07-15 18:31:09 +02:00
|
|
|
compare_tor_addr_to_addr_policy(&tar, 2, policy));
|
2005-05-14 02:13:17 +02:00
|
|
|
|
2009-10-27 09:03:41 +01:00
|
|
|
test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, NULL, 1));
|
2006-10-20 01:05:25 +02:00
|
|
|
test_assert(policy2);
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
policy3 = smartlist_new();
|
2010-02-02 14:51:12 +01:00
|
|
|
p = router_parse_addr_policy_item_from_string("reject *:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy3, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("accept *:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy3, p);
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
policy4 = smartlist_new();
|
2010-02-03 05:43:09 +01:00
|
|
|
p = router_parse_addr_policy_item_from_string("accept *:443",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy4, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("accept *:443",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy4, p);
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
policy5 = smartlist_new();
|
2010-02-03 12:45:27 +01:00
|
|
|
p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("reject *:1-65534",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("reject *:65535",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
|
|
|
p = router_parse_addr_policy_item_from_string("accept *:1-65535",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy5, p);
|
2010-02-05 11:59:15 +01:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
policy6 = smartlist_new();
|
2010-02-04 00:06:00 +01:00
|
|
|
p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy6, p);
|
2010-02-03 12:45:27 +01:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
policy7 = smartlist_new();
|
2010-02-05 11:59:15 +01:00
|
|
|
p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*",-1);
|
|
|
|
test_assert(p != NULL);
|
|
|
|
smartlist_add(policy7, p);
|
|
|
|
|
2006-10-20 01:05:25 +02:00
|
|
|
test_assert(!exit_policy_is_general_exit(policy));
|
|
|
|
test_assert(exit_policy_is_general_exit(policy2));
|
2008-03-17 17:51:48 +01:00
|
|
|
test_assert(!exit_policy_is_general_exit(NULL));
|
2010-02-02 14:51:12 +01:00
|
|
|
test_assert(!exit_policy_is_general_exit(policy3));
|
2010-02-03 05:43:09 +01:00
|
|
|
test_assert(!exit_policy_is_general_exit(policy4));
|
2010-02-03 12:45:27 +01:00
|
|
|
test_assert(!exit_policy_is_general_exit(policy5));
|
2010-02-04 00:06:00 +01:00
|
|
|
test_assert(!exit_policy_is_general_exit(policy6));
|
2010-02-05 11:59:15 +01:00
|
|
|
test_assert(!exit_policy_is_general_exit(policy7));
|
2006-10-20 01:05:25 +02:00
|
|
|
|
|
|
|
test_assert(cmp_addr_policies(policy, policy2));
|
2008-03-17 17:51:48 +01:00
|
|
|
test_assert(cmp_addr_policies(policy, NULL));
|
2006-10-20 01:05:25 +02:00
|
|
|
test_assert(!cmp_addr_policies(policy2, policy2));
|
2008-03-17 17:51:48 +01:00
|
|
|
test_assert(!cmp_addr_policies(NULL, NULL));
|
2006-10-20 01:05:25 +02:00
|
|
|
|
|
|
|
test_assert(!policy_is_reject_star(policy2));
|
|
|
|
test_assert(policy_is_reject_star(policy));
|
2008-03-17 17:51:48 +01:00
|
|
|
test_assert(policy_is_reject_star(NULL));
|
2006-10-20 01:05:25 +02:00
|
|
|
|
2008-01-02 05:43:44 +01:00
|
|
|
addr_policy_list_free(policy);
|
2008-09-10 07:34:03 +02:00
|
|
|
policy = NULL;
|
2005-05-14 02:13:17 +02:00
|
|
|
|
2006-10-20 01:05:25 +02:00
|
|
|
/* make sure compacting logic works. */
|
2005-05-14 02:13:17 +02:00
|
|
|
policy = NULL;
|
2006-10-20 01:05:25 +02:00
|
|
|
line.key = (char*)"foo";
|
|
|
|
line.value = (char*)"accept *:80,reject private:*,reject *:*";
|
|
|
|
line.next = NULL;
|
2009-10-27 09:03:41 +01:00
|
|
|
test_assert(0 == policies_parse_exit_policy(&line, &policy, 0, NULL, 1));
|
2005-05-14 02:13:17 +02:00
|
|
|
test_assert(policy);
|
2007-11-30 21:09:09 +01:00
|
|
|
//test_streq(policy->string, "accept *:80");
|
|
|
|
//test_streq(policy->next->string, "reject *:*");
|
2008-01-02 05:43:44 +01:00
|
|
|
test_eq(smartlist_len(policy), 2);
|
2005-05-14 02:13:17 +02:00
|
|
|
|
2008-08-13 14:46:06 +02:00
|
|
|
/* test policy summaries */
|
|
|
|
/* check if we properly ignore private IP addresses */
|
2008-08-13 14:46:13 +02:00
|
|
|
test_policy_summary_helper("reject 192.168.0.0/16:*,"
|
|
|
|
"reject 0.0.0.0/8:*,"
|
|
|
|
"reject 10.0.0.0/8:*,"
|
|
|
|
"accept *:10-30,"
|
|
|
|
"accept *:90,"
|
|
|
|
"reject *:*",
|
|
|
|
"accept 10-30,90");
|
2008-08-13 14:46:06 +02:00
|
|
|
/* check all accept policies, and proper counting of rejects */
|
2008-08-13 14:46:13 +02:00
|
|
|
test_policy_summary_helper("reject 11.0.0.0/9:80,"
|
|
|
|
"reject 12.0.0.0/9:80,"
|
|
|
|
"reject 13.0.0.0/9:80,"
|
|
|
|
"reject 14.0.0.0/9:80,"
|
|
|
|
"accept *:*", "accept 1-65535");
|
|
|
|
test_policy_summary_helper("reject 11.0.0.0/9:80,"
|
|
|
|
"reject 12.0.0.0/9:80,"
|
|
|
|
"reject 13.0.0.0/9:80,"
|
|
|
|
"reject 14.0.0.0/9:80,"
|
|
|
|
"reject 15.0.0.0:81,"
|
|
|
|
"accept *:*", "accept 1-65535");
|
|
|
|
test_policy_summary_helper("reject 11.0.0.0/9:80,"
|
|
|
|
"reject 12.0.0.0/9:80,"
|
|
|
|
"reject 13.0.0.0/9:80,"
|
|
|
|
"reject 14.0.0.0/9:80,"
|
|
|
|
"reject 15.0.0.0:80,"
|
|
|
|
"accept *:*",
|
|
|
|
"reject 80");
|
2008-08-13 14:46:06 +02:00
|
|
|
/* no exits */
|
2008-08-13 14:46:13 +02:00
|
|
|
test_policy_summary_helper("accept 11.0.0.0/9:80,"
|
|
|
|
"reject *:*",
|
2008-08-14 14:37:50 +02:00
|
|
|
"reject 1-65535");
|
2008-08-13 14:46:06 +02:00
|
|
|
/* port merging */
|
2008-08-13 14:46:13 +02:00
|
|
|
test_policy_summary_helper("accept *:80,"
|
|
|
|
"accept *:81,"
|
|
|
|
"accept *:100-110,"
|
|
|
|
"accept *:111,"
|
|
|
|
"reject *:*",
|
|
|
|
"accept 80-81,100-111");
|
2008-08-13 14:46:06 +02:00
|
|
|
/* border ports */
|
2008-08-13 14:46:13 +02:00
|
|
|
test_policy_summary_helper("accept *:1,"
|
|
|
|
"accept *:3,"
|
|
|
|
"accept *:65535,"
|
|
|
|
"reject *:*",
|
|
|
|
"accept 1,3,65535");
|
2008-08-13 21:25:18 +02:00
|
|
|
/* holes */
|
|
|
|
test_policy_summary_helper("accept *:1,"
|
|
|
|
"accept *:3,"
|
|
|
|
"accept *:5,"
|
|
|
|
"accept *:7,"
|
|
|
|
"reject *:*",
|
|
|
|
"accept 1,3,5,7");
|
|
|
|
test_policy_summary_helper("reject *:1,"
|
|
|
|
"reject *:3,"
|
|
|
|
"reject *:5,"
|
|
|
|
"reject *:7,"
|
|
|
|
"accept *:*",
|
|
|
|
"reject 1,3,5,7");
|
2008-09-10 07:34:03 +02:00
|
|
|
|
2008-08-13 21:25:18 +02:00
|
|
|
/* truncation ports */
|
2012-01-18 21:53:30 +01:00
|
|
|
sm = smartlist_new();
|
2008-08-13 21:25:18 +02:00
|
|
|
for (i=1; i<2000; i+=2) {
|
|
|
|
char buf[POLICY_BUF_LEN];
|
|
|
|
tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
|
|
|
|
smartlist_add(sm, tor_strdup(buf));
|
|
|
|
}
|
|
|
|
smartlist_add(sm, tor_strdup("accept *:*"));
|
|
|
|
policy_str = smartlist_join_strings(sm, ",", 0, NULL);
|
|
|
|
test_policy_summary_helper( policy_str,
|
|
|
|
"accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
|
|
|
|
"46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
|
|
|
|
"92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
|
|
|
|
"130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
|
|
|
|
"166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
|
|
|
|
"202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
|
|
|
|
"238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
|
|
|
|
"274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
|
|
|
|
"310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
|
|
|
|
"346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
|
|
|
|
"382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
|
|
|
|
"418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
|
|
|
|
"454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
|
|
|
|
"490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
|
2008-09-25 17:20:38 +02:00
|
|
|
|
2008-09-09 22:43:31 +02:00
|
|
|
done:
|
2010-02-02 14:51:12 +01:00
|
|
|
addr_policy_list_free(policy);
|
|
|
|
addr_policy_list_free(policy2);
|
|
|
|
addr_policy_list_free(policy3);
|
2010-02-03 05:43:09 +01:00
|
|
|
addr_policy_list_free(policy4);
|
2010-02-03 12:45:27 +01:00
|
|
|
addr_policy_list_free(policy5);
|
2010-02-04 00:06:00 +01:00
|
|
|
addr_policy_list_free(policy6);
|
2010-02-05 11:59:15 +01:00
|
|
|
addr_policy_list_free(policy7);
|
2008-09-25 17:20:38 +02:00
|
|
|
tor_free(policy_str);
|
|
|
|
if (sm) {
|
|
|
|
SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
|
|
|
|
smartlist_free(sm);
|
|
|
|
}
|
2005-05-14 02:13:17 +02:00
|
|
|
}
|
|
|
|
|
2009-05-02 23:31:58 +02:00
|
|
|
/** Test encoding and parsing of rendezvous service descriptors. */
|
2007-10-28 20:48:14 +01:00
|
|
|
static void
|
2009-05-02 23:31:58 +02:00
|
|
|
test_rend_fns(void)
|
2007-10-28 20:48:14 +01:00
|
|
|
{
|
2008-09-25 17:20:38 +02:00
|
|
|
rend_service_descriptor_t *generated = NULL, *parsed = NULL;
|
2007-10-28 20:48:14 +01:00
|
|
|
char service_id[DIGEST_LEN];
|
2007-11-29 16:25:04 +01:00
|
|
|
char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
|
2007-10-28 20:48:14 +01:00
|
|
|
const char *next_desc;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *descs = smartlist_new();
|
2007-10-28 20:48:14 +01:00
|
|
|
char computed_desc_id[DIGEST_LEN];
|
|
|
|
char parsed_desc_id[DIGEST_LEN];
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *pk1 = NULL, *pk2 = NULL;
|
2007-10-28 20:48:14 +01:00
|
|
|
time_t now;
|
2008-09-25 17:20:38 +02:00
|
|
|
char *intro_points_encrypted = NULL;
|
2007-10-28 20:48:14 +01:00
|
|
|
size_t intro_points_size;
|
|
|
|
size_t encoded_size;
|
|
|
|
int i;
|
2009-05-02 23:31:58 +02:00
|
|
|
char address1[] = "fooaddress.onion";
|
|
|
|
char address2[] = "aaaaaaaaaaaaaaaa.onion";
|
|
|
|
char address3[] = "fooaddress.exit";
|
|
|
|
char address4[] = "www.torproject.org";
|
|
|
|
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
|
|
|
|
test_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
|
|
|
|
test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
|
|
|
|
test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
|
2009-05-02 23:31:58 +02:00
|
|
|
|
2007-10-28 20:48:14 +01:00
|
|
|
pk1 = pk_generate(0);
|
|
|
|
pk2 = pk_generate(1);
|
|
|
|
generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
|
|
|
|
generated->pk = crypto_pk_dup_key(pk1);
|
|
|
|
crypto_pk_get_digest(generated->pk, service_id);
|
2007-11-29 16:25:04 +01:00
|
|
|
base32_encode(service_id_base32, REND_SERVICE_ID_LEN_BASE32+1,
|
|
|
|
service_id, REND_SERVICE_ID_LEN);
|
2007-10-28 20:48:14 +01:00
|
|
|
now = time(NULL);
|
|
|
|
generated->timestamp = now;
|
|
|
|
generated->version = 2;
|
|
|
|
generated->protocols = 42;
|
2012-01-18 21:53:30 +01:00
|
|
|
generated->intro_nodes = smartlist_new();
|
2008-09-10 02:11:53 +02:00
|
|
|
|
2007-12-21 10:28:22 +01:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *okey = pk_generate(2 + i);
|
2007-12-21 10:28:22 +01:00
|
|
|
intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
|
2008-12-18 07:02:23 +01:00
|
|
|
intro->extend_info->onion_key = okey;
|
2007-12-21 10:28:22 +01:00
|
|
|
crypto_pk_get_digest(intro->extend_info->onion_key,
|
|
|
|
intro->extend_info->identity_digest);
|
2007-10-28 20:48:14 +01:00
|
|
|
//crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
|
2007-12-21 10:28:22 +01:00
|
|
|
intro->extend_info->nickname[0] = '$';
|
|
|
|
base16_encode(intro->extend_info->nickname + 1,
|
|
|
|
sizeof(intro->extend_info->nickname) - 1,
|
|
|
|
intro->extend_info->identity_digest, DIGEST_LEN);
|
2008-08-05 22:08:19 +02:00
|
|
|
/* Does not cover all IP addresses. */
|
|
|
|
tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
|
2010-08-07 20:31:58 +02:00
|
|
|
intro->extend_info->port = 1 + crypto_rand_int(65535);
|
2007-12-21 11:44:10 +01:00
|
|
|
intro->intro_key = crypto_pk_dup_key(pk2);
|
2007-12-21 10:28:22 +01:00
|
|
|
smartlist_add(generated->intro_nodes, intro);
|
2007-10-28 20:48:14 +01:00
|
|
|
}
|
2008-08-19 17:41:28 +02:00
|
|
|
test_assert(rend_encode_v2_descriptors(descs, generated, now, 0,
|
|
|
|
REND_NO_AUTH, NULL, NULL) > 0);
|
2007-10-28 20:48:14 +01:00
|
|
|
test_assert(rend_compute_v2_desc_id(computed_desc_id, service_id_base32,
|
|
|
|
NULL, now, 0) == 0);
|
2007-12-15 21:28:09 +01:00
|
|
|
test_memeq(((rend_encoded_v2_service_descriptor_t *)
|
|
|
|
smartlist_get(descs, 0))->desc_id, computed_desc_id, DIGEST_LEN);
|
2007-10-28 20:48:14 +01:00
|
|
|
test_assert(rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
|
|
|
|
&intro_points_encrypted,
|
|
|
|
&intro_points_size,
|
|
|
|
&encoded_size,
|
|
|
|
&next_desc,
|
2007-12-15 21:28:09 +01:00
|
|
|
((rend_encoded_v2_service_descriptor_t *)
|
|
|
|
smartlist_get(descs, 0))->desc_str) == 0);
|
2007-10-28 20:48:14 +01:00
|
|
|
test_assert(parsed);
|
2007-12-15 21:28:09 +01:00
|
|
|
test_memeq(((rend_encoded_v2_service_descriptor_t *)
|
|
|
|
smartlist_get(descs, 0))->desc_id, parsed_desc_id, DIGEST_LEN);
|
2008-11-03 17:36:15 +01:00
|
|
|
test_eq(rend_parse_introduction_points(parsed, intro_points_encrypted,
|
|
|
|
intro_points_size), 3);
|
2007-10-28 20:48:14 +01:00
|
|
|
test_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
|
|
|
|
test_eq(parsed->timestamp, now);
|
|
|
|
test_eq(parsed->version, 2);
|
|
|
|
test_eq(parsed->protocols, 42);
|
2007-12-21 10:28:22 +01:00
|
|
|
test_eq(smartlist_len(parsed->intro_nodes), 3);
|
|
|
|
for (i = 0; i < smartlist_len(parsed->intro_nodes); i++) {
|
|
|
|
rend_intro_point_t *par_intro = smartlist_get(parsed->intro_nodes, i),
|
|
|
|
*gen_intro = smartlist_get(generated->intro_nodes, i);
|
|
|
|
extend_info_t *par_info = par_intro->extend_info;
|
|
|
|
extend_info_t *gen_info = gen_intro->extend_info;
|
2007-10-28 20:48:14 +01:00
|
|
|
test_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
|
|
|
|
test_memeq(gen_info->identity_digest, par_info->identity_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
test_streq(gen_info->nickname, par_info->nickname);
|
2008-08-05 22:08:19 +02:00
|
|
|
test_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
|
2007-10-28 20:48:14 +01:00
|
|
|
test_eq(gen_info->port, par_info->port);
|
|
|
|
}
|
2008-09-25 17:20:38 +02:00
|
|
|
|
2009-01-13 15:43:51 +01:00
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
rend_service_descriptor_free(generated);
|
|
|
|
parsed = generated = NULL;
|
|
|
|
|
2008-09-09 22:43:31 +02:00
|
|
|
done:
|
2008-09-25 17:20:38 +02:00
|
|
|
if (descs) {
|
|
|
|
for (i = 0; i < smartlist_len(descs); i++)
|
|
|
|
rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
|
|
|
|
smartlist_free(descs);
|
|
|
|
}
|
|
|
|
if (parsed)
|
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
if (generated)
|
|
|
|
rend_service_descriptor_free(generated);
|
|
|
|
if (pk1)
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(pk1);
|
2008-12-18 07:02:23 +01:00
|
|
|
if (pk2)
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(pk2);
|
2008-09-25 17:20:38 +02:00
|
|
|
tor_free(intro_points_encrypted);
|
2007-10-28 20:48:14 +01:00
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Run unit tests for GeoIP code. */
|
2007-12-18 22:27:08 +01:00
|
|
|
static void
|
|
|
|
test_geoip(void)
|
|
|
|
{
|
|
|
|
int i, j;
|
2011-08-04 12:28:12 +02:00
|
|
|
time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
|
2008-09-10 02:11:53 +02:00
|
|
|
char *s = NULL;
|
2011-08-04 14:45:24 +02:00
|
|
|
const char *bridge_stats_1 =
|
|
|
|
"bridge-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"bridge-ips zz=24,xy=8\n",
|
|
|
|
*dirreq_stats_1 =
|
2011-08-04 12:28:12 +02:00
|
|
|
"dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"dirreq-v3-ips ab=8\n"
|
|
|
|
"dirreq-v2-ips \n"
|
|
|
|
"dirreq-v3-reqs ab=8\n"
|
|
|
|
"dirreq-v2-reqs \n"
|
|
|
|
"dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
|
|
|
|
"not-modified=0,busy=0\n"
|
|
|
|
"dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
|
|
|
|
"busy=0\n"
|
|
|
|
"dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
|
|
|
|
*dirreq_stats_2 =
|
|
|
|
"dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"dirreq-v3-ips \n"
|
|
|
|
"dirreq-v2-ips \n"
|
|
|
|
"dirreq-v3-reqs \n"
|
|
|
|
"dirreq-v2-reqs \n"
|
|
|
|
"dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
|
|
|
|
"not-modified=0,busy=0\n"
|
|
|
|
"dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
|
|
|
|
"busy=0\n"
|
|
|
|
"dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
|
|
|
|
*dirreq_stats_3 =
|
|
|
|
"dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"dirreq-v3-ips \n"
|
|
|
|
"dirreq-v2-ips \n"
|
|
|
|
"dirreq-v3-reqs \n"
|
|
|
|
"dirreq-v2-reqs \n"
|
|
|
|
"dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
|
|
|
|
"not-modified=0,busy=0\n"
|
|
|
|
"dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
|
|
|
|
"busy=0\n"
|
|
|
|
"dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
|
|
|
|
*dirreq_stats_4 =
|
|
|
|
"dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"dirreq-v3-ips \n"
|
|
|
|
"dirreq-v2-ips \n"
|
|
|
|
"dirreq-v3-reqs \n"
|
|
|
|
"dirreq-v2-reqs \n"
|
|
|
|
"dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
|
|
|
|
"not-modified=0,busy=0\n"
|
|
|
|
"dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
|
|
|
|
"busy=0\n"
|
|
|
|
"dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
|
|
|
|
"dirreq-v3-tunneled-dl complete=0,timeout=0,running=4\n"
|
2011-08-04 14:14:01 +02:00
|
|
|
"dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
|
|
|
|
*entry_stats_1 =
|
|
|
|
"entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"entry-ips ab=8\n",
|
|
|
|
*entry_stats_2 =
|
|
|
|
"entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"entry-ips \n";
|
2012-02-09 11:12:30 +01:00
|
|
|
tor_addr_t addr;
|
2007-12-18 22:27:08 +01:00
|
|
|
|
|
|
|
/* Populate the DB a bit. Add these in order, since we can't do the final
|
|
|
|
* 'sort' step. These aren't very good IP addresses, but they're perfectly
|
|
|
|
* fine uint32_t values. */
|
|
|
|
test_eq(0, geoip_parse_entry("10,50,AB"));
|
|
|
|
test_eq(0, geoip_parse_entry("52,90,XY"));
|
|
|
|
test_eq(0, geoip_parse_entry("95,100,AB"));
|
|
|
|
test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\""));
|
|
|
|
test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\""));
|
|
|
|
test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\""));
|
|
|
|
|
2010-07-31 21:20:28 +02:00
|
|
|
/* We should have 4 countries: ??, ab, xy, zz. */
|
|
|
|
test_eq(4, geoip_get_n_countries());
|
2007-12-18 22:27:08 +01:00
|
|
|
/* Make sure that country ID actually works. */
|
|
|
|
#define NAMEFOR(x) geoip_get_country_name(geoip_get_country_by_ip(x))
|
2010-07-31 21:20:28 +02:00
|
|
|
test_streq("??", NAMEFOR(3));
|
|
|
|
test_eq(0, geoip_get_country_by_ip(3));
|
2007-12-18 22:27:08 +01:00
|
|
|
test_streq("ab", NAMEFOR(32));
|
|
|
|
test_streq("??", NAMEFOR(5));
|
|
|
|
test_streq("??", NAMEFOR(51));
|
|
|
|
test_streq("xy", NAMEFOR(150));
|
|
|
|
test_streq("xy", NAMEFOR(190));
|
|
|
|
test_streq("??", NAMEFOR(2000));
|
|
|
|
#undef NAMEFOR
|
|
|
|
|
2011-06-14 19:01:38 +02:00
|
|
|
get_options_mutable()->BridgeRelay = 1;
|
|
|
|
get_options_mutable()->BridgeRecordUsageByCountry = 1;
|
2007-12-18 22:27:08 +01:00
|
|
|
/* Put 9 observations in AB... */
|
2012-02-09 11:12:30 +01:00
|
|
|
for (i=32; i < 40; ++i) {
|
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) i);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now-7200);
|
|
|
|
}
|
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) 225);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now-7200);
|
2007-12-18 22:27:08 +01:00
|
|
|
/* and 3 observations in XY, several times. */
|
|
|
|
for (j=0; j < 10; ++j)
|
2012-02-09 11:12:30 +01:00
|
|
|
for (i=52; i < 55; ++i) {
|
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) i);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now-3600);
|
|
|
|
}
|
2007-12-18 22:27:08 +01:00
|
|
|
/* and 17 observations in ZZ... */
|
2012-02-09 11:12:30 +01:00
|
|
|
for (i=110; i < 127; ++i) {
|
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) i);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
|
|
|
|
}
|
2010-06-21 18:52:46 +02:00
|
|
|
s = geoip_get_client_history(GEOIP_CLIENT_CONNECT);
|
2007-12-18 22:27:08 +01:00
|
|
|
test_assert(s);
|
2008-06-04 20:38:37 +02:00
|
|
|
test_streq("zz=24,ab=16,xy=8", s);
|
2007-12-18 22:27:08 +01:00
|
|
|
tor_free(s);
|
|
|
|
|
2009-05-27 23:47:59 +02:00
|
|
|
/* Now clear out all the AB observations. */
|
2007-12-18 22:27:08 +01:00
|
|
|
geoip_remove_old_clients(now-6000);
|
2010-06-21 18:52:46 +02:00
|
|
|
s = geoip_get_client_history(GEOIP_CLIENT_CONNECT);
|
2007-12-18 22:27:08 +01:00
|
|
|
test_assert(s);
|
2009-05-27 23:47:59 +02:00
|
|
|
test_streq("zz=24,xy=8", s);
|
2008-09-10 02:11:53 +02:00
|
|
|
|
2011-08-04 14:45:24 +02:00
|
|
|
/* Start testing bridge statistics by making sure that we don't output
|
|
|
|
* bridge stats without initializing them. */
|
|
|
|
s = geoip_format_bridge_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Initialize stats and generate the bridge-stats history string out of
|
|
|
|
* the connecting clients added above. */
|
|
|
|
geoip_bridge_stats_init(now);
|
|
|
|
s = geoip_format_bridge_stats(now + 86400);
|
|
|
|
test_streq(bridge_stats_1, s);
|
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Stop collecting bridge stats and make sure we don't write a history
|
|
|
|
* string anymore. */
|
|
|
|
geoip_bridge_stats_term();
|
|
|
|
s = geoip_format_bridge_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
2011-08-04 12:28:12 +02:00
|
|
|
/* Stop being a bridge and start being a directory mirror that gathers
|
|
|
|
* directory request statistics. */
|
2011-08-04 14:14:01 +02:00
|
|
|
geoip_bridge_stats_term();
|
2011-08-04 12:28:12 +02:00
|
|
|
get_options_mutable()->BridgeRelay = 0;
|
|
|
|
get_options_mutable()->BridgeRecordUsageByCountry = 0;
|
|
|
|
get_options_mutable()->DirReqStatistics = 1;
|
|
|
|
|
|
|
|
/* Start testing dirreq statistics by making sure that we don't collect
|
|
|
|
* dirreq stats without initializing them. */
|
2012-02-09 11:12:30 +01:00
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) 100);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
|
2011-08-04 12:28:12 +02:00
|
|
|
s = geoip_format_dirreq_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Initialize stats, note one connecting client, and generate the
|
|
|
|
* dirreq-stats history string. */
|
|
|
|
geoip_dirreq_stats_init(now);
|
2012-02-09 11:12:30 +01:00
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) 100);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
|
2011-08-04 12:28:12 +02:00
|
|
|
s = geoip_format_dirreq_stats(now + 86400);
|
|
|
|
test_streq(dirreq_stats_1, s);
|
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Stop collecting stats, add another connecting client, and ensure we
|
|
|
|
* don't generate a history string. */
|
|
|
|
geoip_dirreq_stats_term();
|
2012-02-09 11:12:30 +01:00
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) 101);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
|
2011-08-04 12:28:12 +02:00
|
|
|
s = geoip_format_dirreq_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Re-start stats, add a connecting client, reset stats, and make sure
|
|
|
|
* that we get an all empty history string. */
|
|
|
|
geoip_dirreq_stats_init(now);
|
2012-02-09 11:12:30 +01:00
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) 100);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
|
2011-08-04 12:28:12 +02:00
|
|
|
geoip_reset_dirreq_stats(now);
|
|
|
|
s = geoip_format_dirreq_stats(now + 86400);
|
|
|
|
test_streq(dirreq_stats_2, s);
|
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Note a successful network status response and make sure that it
|
|
|
|
* appears in the history string. */
|
|
|
|
geoip_note_ns_response(GEOIP_CLIENT_NETWORKSTATUS, GEOIP_SUCCESS);
|
|
|
|
s = geoip_format_dirreq_stats(now + 86400);
|
|
|
|
test_streq(dirreq_stats_3, s);
|
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Start a tunneled directory request. */
|
|
|
|
geoip_start_dirreq((uint64_t) 1, 1024, GEOIP_CLIENT_NETWORKSTATUS,
|
|
|
|
DIRREQ_TUNNELED);
|
|
|
|
s = geoip_format_dirreq_stats(now + 86400);
|
|
|
|
test_streq(dirreq_stats_4, s);
|
|
|
|
|
2011-08-04 14:14:01 +02:00
|
|
|
/* Stop collecting directory request statistics and start gathering
|
|
|
|
* entry stats. */
|
|
|
|
geoip_dirreq_stats_term();
|
|
|
|
get_options_mutable()->DirReqStatistics = 0;
|
|
|
|
get_options_mutable()->EntryStatistics = 1;
|
|
|
|
|
|
|
|
/* Start testing entry statistics by making sure that we don't collect
|
|
|
|
* anything without initializing entry stats. */
|
2012-02-09 11:12:30 +01:00
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) 100);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
|
2011-08-04 14:14:01 +02:00
|
|
|
s = geoip_format_entry_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Initialize stats, note one connecting client, and generate the
|
|
|
|
* entry-stats history string. */
|
|
|
|
geoip_entry_stats_init(now);
|
2012-02-09 11:12:30 +01:00
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) 100);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
|
2011-08-04 14:14:01 +02:00
|
|
|
s = geoip_format_entry_stats(now + 86400);
|
|
|
|
test_streq(entry_stats_1, s);
|
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Stop collecting stats, add another connecting client, and ensure we
|
|
|
|
* don't generate a history string. */
|
|
|
|
geoip_entry_stats_term();
|
2012-02-09 11:12:30 +01:00
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) 101);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
|
2011-08-04 14:14:01 +02:00
|
|
|
s = geoip_format_entry_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Re-start stats, add a connecting client, reset stats, and make sure
|
|
|
|
* that we get an all empty history string. */
|
|
|
|
geoip_entry_stats_init(now);
|
2012-02-09 11:12:30 +01:00
|
|
|
tor_addr_from_ipv4h(&addr, (uint32_t) 100);
|
|
|
|
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
|
2011-08-04 14:14:01 +02:00
|
|
|
geoip_reset_entry_stats(now);
|
|
|
|
s = geoip_format_entry_stats(now + 86400);
|
|
|
|
test_streq(entry_stats_2, s);
|
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Stop collecting entry statistics. */
|
|
|
|
geoip_entry_stats_term();
|
|
|
|
get_options_mutable()->EntryStatistics = 0;
|
|
|
|
|
2008-09-09 22:43:31 +02:00
|
|
|
done:
|
2008-09-10 02:11:53 +02:00
|
|
|
tor_free(s);
|
2007-12-18 22:27:08 +01:00
|
|
|
}
|
|
|
|
|
2010-08-11 14:13:08 +02:00
|
|
|
/** Run unit tests for stats code. */
|
|
|
|
static void
|
|
|
|
test_stats(void)
|
|
|
|
{
|
|
|
|
time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
|
|
|
|
char *s = NULL;
|
2010-11-23 21:09:12 +01:00
|
|
|
int i;
|
2010-08-11 14:13:08 +02:00
|
|
|
|
2010-08-18 15:44:02 +02:00
|
|
|
/* Start with testing exit port statistics; we shouldn't collect exit
|
|
|
|
* stats without initializing them. */
|
2010-08-11 14:13:08 +02:00
|
|
|
rep_hist_note_exit_stream_opened(80);
|
|
|
|
rep_hist_note_exit_bytes(80, 100, 10000);
|
2010-08-16 02:55:24 +02:00
|
|
|
s = rep_hist_format_exit_stats(now + 86400);
|
2010-08-11 14:13:08 +02:00
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Initialize stats, note some streams and bytes, and generate history
|
|
|
|
* string. */
|
|
|
|
rep_hist_exit_stats_init(now);
|
|
|
|
rep_hist_note_exit_stream_opened(80);
|
|
|
|
rep_hist_note_exit_bytes(80, 100, 10000);
|
|
|
|
rep_hist_note_exit_stream_opened(443);
|
|
|
|
rep_hist_note_exit_bytes(443, 100, 10000);
|
|
|
|
rep_hist_note_exit_bytes(443, 100, 10000);
|
2010-08-16 02:55:24 +02:00
|
|
|
s = rep_hist_format_exit_stats(now + 86400);
|
2010-08-11 14:13:08 +02:00
|
|
|
test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"exit-kibibytes-written 80=1,443=1,other=0\n"
|
|
|
|
"exit-kibibytes-read 80=10,443=20,other=0\n"
|
|
|
|
"exit-streams-opened 80=4,443=4,other=0\n", s);
|
|
|
|
tor_free(s);
|
|
|
|
|
2010-11-23 21:09:12 +01:00
|
|
|
/* Add a few bytes on 10 more ports and ensure that only the top 10
|
|
|
|
* ports are contained in the history string. */
|
|
|
|
for (i = 50; i < 60; i++) {
|
|
|
|
rep_hist_note_exit_bytes(i, i, i);
|
|
|
|
rep_hist_note_exit_stream_opened(i);
|
|
|
|
}
|
|
|
|
s = rep_hist_format_exit_stats(now + 86400);
|
|
|
|
test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"exit-kibibytes-written 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
|
|
|
|
"59=1,80=1,443=1,other=1\n"
|
|
|
|
"exit-kibibytes-read 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
|
|
|
|
"59=1,80=10,443=20,other=1\n"
|
|
|
|
"exit-streams-opened 52=4,53=4,54=4,55=4,56=4,57=4,58=4,"
|
|
|
|
"59=4,80=4,443=4,other=4\n", s);
|
|
|
|
tor_free(s);
|
|
|
|
|
2010-08-11 14:13:08 +02:00
|
|
|
/* Stop collecting stats, add some bytes, and ensure we don't generate
|
|
|
|
* a history string. */
|
|
|
|
rep_hist_exit_stats_term();
|
|
|
|
rep_hist_note_exit_bytes(80, 100, 10000);
|
2010-08-16 02:55:24 +02:00
|
|
|
s = rep_hist_format_exit_stats(now + 86400);
|
2010-08-11 14:13:08 +02:00
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Re-start stats, add some bytes, reset stats, and see what history we
|
2010-08-18 15:44:02 +02:00
|
|
|
* get when observing no streams or bytes at all. */
|
2010-08-11 14:13:08 +02:00
|
|
|
rep_hist_exit_stats_init(now);
|
|
|
|
rep_hist_note_exit_stream_opened(80);
|
|
|
|
rep_hist_note_exit_bytes(80, 100, 10000);
|
|
|
|
rep_hist_reset_exit_stats(now);
|
2010-08-16 02:55:24 +02:00
|
|
|
s = rep_hist_format_exit_stats(now + 86400);
|
2010-08-11 14:13:08 +02:00
|
|
|
test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"exit-kibibytes-written other=0\n"
|
|
|
|
"exit-kibibytes-read other=0\n"
|
|
|
|
"exit-streams-opened other=0\n", s);
|
2010-08-18 15:44:02 +02:00
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Continue with testing connection statistics; we shouldn't collect
|
|
|
|
* conn stats without initializing them. */
|
|
|
|
rep_hist_note_or_conn_bytes(1, 20, 400, now);
|
|
|
|
s = rep_hist_format_conn_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Initialize stats, note bytes, and generate history string. */
|
|
|
|
rep_hist_conn_stats_init(now);
|
|
|
|
rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
|
|
|
|
rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
|
|
|
|
rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
|
|
|
|
rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
|
|
|
|
s = rep_hist_format_conn_stats(now + 86400);
|
2010-08-24 08:11:17 +02:00
|
|
|
test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n", s);
|
2010-08-18 15:44:02 +02:00
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Stop collecting stats, add some bytes, and ensure we don't generate
|
|
|
|
* a history string. */
|
|
|
|
rep_hist_conn_stats_term();
|
|
|
|
rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
|
|
|
|
s = rep_hist_format_conn_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Re-start stats, add some bytes, reset stats, and see what history we
|
2010-08-24 08:11:17 +02:00
|
|
|
* get when observing no bytes at all. */
|
2010-08-18 15:44:02 +02:00
|
|
|
rep_hist_conn_stats_init(now);
|
|
|
|
rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
|
|
|
|
rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
|
|
|
|
rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
|
|
|
|
rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
|
|
|
|
rep_hist_reset_conn_stats(now);
|
|
|
|
s = rep_hist_format_conn_stats(now + 86400);
|
2010-08-24 08:11:17 +02:00
|
|
|
test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n", s);
|
2011-08-03 13:29:03 +02:00
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Continue with testing buffer statistics; we shouldn't collect buffer
|
|
|
|
* stats without initializing them. */
|
|
|
|
rep_hist_add_buffer_stats(2.0, 2.0, 20);
|
|
|
|
s = rep_hist_format_buffer_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Initialize stats, add statistics for a single circuit, and generate
|
|
|
|
* the history string. */
|
|
|
|
rep_hist_buffer_stats_init(now);
|
|
|
|
rep_hist_add_buffer_stats(2.0, 2.0, 20);
|
|
|
|
s = rep_hist_format_buffer_stats(now + 86400);
|
|
|
|
test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"cell-processed-cells 20,0,0,0,0,0,0,0,0,0\n"
|
|
|
|
"cell-queued-cells 2.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
|
|
|
|
"0.00,0.00\n"
|
|
|
|
"cell-time-in-queue 2,0,0,0,0,0,0,0,0,0\n"
|
|
|
|
"cell-circuits-per-decile 1\n", s);
|
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Add nineteen more circuit statistics to the one that's already in the
|
|
|
|
* history to see that the math works correctly. */
|
|
|
|
for (i = 21; i < 30; i++)
|
|
|
|
rep_hist_add_buffer_stats(2.0, 2.0, i);
|
|
|
|
for (i = 20; i < 30; i++)
|
|
|
|
rep_hist_add_buffer_stats(3.5, 3.5, i);
|
|
|
|
s = rep_hist_format_buffer_stats(now + 86400);
|
|
|
|
test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"cell-processed-cells 29,28,27,26,25,24,23,22,21,20\n"
|
|
|
|
"cell-queued-cells 2.75,2.75,2.75,2.75,2.75,2.75,2.75,2.75,"
|
|
|
|
"2.75,2.75\n"
|
|
|
|
"cell-time-in-queue 3,3,3,3,3,3,3,3,3,3\n"
|
|
|
|
"cell-circuits-per-decile 2\n", s);
|
|
|
|
tor_free(s);
|
|
|
|
|
|
|
|
/* Stop collecting stats, add statistics for one circuit, and ensure we
|
|
|
|
* don't generate a history string. */
|
|
|
|
rep_hist_buffer_stats_term();
|
|
|
|
rep_hist_add_buffer_stats(2.0, 2.0, 20);
|
|
|
|
s = rep_hist_format_buffer_stats(now + 86400);
|
|
|
|
test_assert(!s);
|
|
|
|
|
|
|
|
/* Re-start stats, add statistics for one circuit, reset stats, and make
|
|
|
|
* sure that the history has all zeros. */
|
|
|
|
rep_hist_buffer_stats_init(now);
|
|
|
|
rep_hist_add_buffer_stats(2.0, 2.0, 20);
|
|
|
|
rep_hist_reset_buffer_stats(now);
|
|
|
|
s = rep_hist_format_buffer_stats(now + 86400);
|
|
|
|
test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
|
|
|
|
"cell-processed-cells 0,0,0,0,0,0,0,0,0,0\n"
|
|
|
|
"cell-queued-cells 0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
|
|
|
|
"0.00,0.00\n"
|
|
|
|
"cell-time-in-queue 0,0,0,0,0,0,0,0,0,0\n"
|
|
|
|
"cell-circuits-per-decile 0\n", s);
|
2010-08-11 14:13:08 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(s);
|
|
|
|
}
|
|
|
|
|
2009-09-21 20:23:13 +02:00
|
|
|
static void *
|
|
|
|
legacy_test_setup(const struct testcase_t *testcase)
|
|
|
|
{
|
|
|
|
return testcase->setup_data;
|
|
|
|
}
|
|
|
|
|
2009-09-22 19:06:47 +02:00
|
|
|
void
|
2009-09-21 20:23:13 +02:00
|
|
|
legacy_test_helper(void *data)
|
|
|
|
{
|
|
|
|
void (*fn)(void) = data;
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
|
|
|
|
{
|
|
|
|
(void)ptr;
|
|
|
|
(void)testcase;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-09-22 19:06:47 +02:00
|
|
|
const struct testcase_setup_t legacy_setup = {
|
2009-09-21 20:23:13 +02:00
|
|
|
legacy_test_setup, legacy_test_cleanup
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ENT(name) \
|
|
|
|
{ #name, legacy_test_helper, 0, &legacy_setup, test_ ## name }
|
|
|
|
#define SUBENT(group, name) \
|
|
|
|
{ #group "_" #name, legacy_test_helper, 0, &legacy_setup, \
|
|
|
|
test_ ## group ## _ ## name }
|
|
|
|
#define DISABLED(name) \
|
2011-11-09 10:54:00 +01:00
|
|
|
{ #name, legacy_test_helper, TT_SKIP, &legacy_setup, test_ ## name }
|
2010-08-15 14:30:37 +02:00
|
|
|
#define FORK(name) \
|
|
|
|
{ #name, legacy_test_helper, TT_FORK, &legacy_setup, test_ ## name }
|
2009-09-21 20:23:13 +02:00
|
|
|
|
|
|
|
static struct testcase_t test_array[] = {
|
2007-08-18 21:39:14 +02:00
|
|
|
ENT(buffers),
|
2011-07-20 15:34:19 +02:00
|
|
|
{ "buffer_copy", test_buffer_copy, 0, NULL, NULL },
|
2007-08-18 21:39:14 +02:00
|
|
|
ENT(onion_handshake),
|
2009-08-28 08:28:20 +02:00
|
|
|
ENT(circuit_timeout),
|
2007-08-18 21:39:14 +02:00
|
|
|
ENT(policies),
|
|
|
|
ENT(rend_fns),
|
2007-12-18 22:27:08 +01:00
|
|
|
ENT(geoip),
|
2010-08-15 14:30:37 +02:00
|
|
|
FORK(stats),
|
2008-12-22 20:00:09 +01:00
|
|
|
|
2009-09-21 20:23:13 +02:00
|
|
|
END_OF_TESTCASES
|
2007-08-18 21:39:14 +02:00
|
|
|
};
|
|
|
|
|
2011-06-29 23:30:55 +02:00
|
|
|
#define SOCKSENT(name) \
|
|
|
|
{ #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
|
|
|
|
|
|
|
|
static struct testcase_t socks_tests[] = {
|
|
|
|
SOCKSENT(4_unsupported_commands),
|
|
|
|
SOCKSENT(4_supported_commands),
|
|
|
|
|
|
|
|
SOCKSENT(5_unsupported_commands),
|
|
|
|
SOCKSENT(5_supported_commands),
|
|
|
|
SOCKSENT(5_no_authenticate),
|
|
|
|
SOCKSENT(5_auth_before_negotiation),
|
|
|
|
SOCKSENT(5_authenticate),
|
|
|
|
SOCKSENT(5_authenticate_with_data),
|
|
|
|
|
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|
|
|
|
|
2009-09-22 19:29:55 +02:00
|
|
|
extern struct testcase_t addr_tests[];
|
2009-09-22 19:06:47 +02:00
|
|
|
extern struct testcase_t crypto_tests[];
|
2009-09-22 19:15:06 +02:00
|
|
|
extern struct testcase_t container_tests[];
|
2009-09-22 19:29:55 +02:00
|
|
|
extern struct testcase_t util_tests[];
|
2009-09-22 19:49:09 +02:00
|
|
|
extern struct testcase_t dir_tests[];
|
2010-10-07 21:28:54 +02:00
|
|
|
extern struct testcase_t microdesc_tests[];
|
2011-07-13 19:06:14 +02:00
|
|
|
extern struct testcase_t pt_tests[];
|
2010-08-02 21:09:37 +02:00
|
|
|
extern struct testcase_t config_tests[];
|
2012-07-22 10:50:33 +02:00
|
|
|
extern struct testcase_t introduce_tests[];
|
2012-07-05 23:01:42 +02:00
|
|
|
extern struct testcase_t replaycache_tests[];
|
2009-09-22 19:06:47 +02:00
|
|
|
|
2009-09-21 20:23:13 +02:00
|
|
|
static struct testgroup_t testgroups[] = {
|
|
|
|
{ "", test_array },
|
2011-06-29 23:30:55 +02:00
|
|
|
{ "socks/", socks_tests },
|
2009-09-22 19:29:55 +02:00
|
|
|
{ "addr/", addr_tests },
|
2009-09-22 19:06:47 +02:00
|
|
|
{ "crypto/", crypto_tests },
|
2009-09-22 19:15:06 +02:00
|
|
|
{ "container/", container_tests },
|
2009-09-22 19:29:55 +02:00
|
|
|
{ "util/", util_tests },
|
2009-09-22 19:49:09 +02:00
|
|
|
{ "dir/", dir_tests },
|
2010-10-07 21:28:54 +02:00
|
|
|
{ "dir/md/", microdesc_tests },
|
2011-07-13 19:06:14 +02:00
|
|
|
{ "pt/", pt_tests },
|
2010-08-02 21:09:37 +02:00
|
|
|
{ "config/", config_tests },
|
2012-07-05 23:01:42 +02:00
|
|
|
{ "replaycache/", replaycache_tests },
|
2012-07-22 10:50:33 +02:00
|
|
|
{ "introduce/", introduce_tests },
|
2009-09-21 20:23:13 +02:00
|
|
|
END_OF_GROUPS
|
|
|
|
};
|
2007-08-18 21:39:14 +02:00
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Main entry point for unit test code: parse the command line, and run
|
|
|
|
* some unit tests. */
|
2003-12-17 22:09:31 +01:00
|
|
|
int
|
2009-09-21 20:23:13 +02:00
|
|
|
main(int c, const char **v)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2008-09-25 19:37:00 +02:00
|
|
|
or_options_t *options;
|
2006-03-26 08:51:26 +02:00
|
|
|
char *errmsg = NULL;
|
2009-09-21 20:23:13 +02:00
|
|
|
int i, i_out;
|
2007-08-18 21:39:14 +02:00
|
|
|
int loglevel = LOG_ERR;
|
|
|
|
|
2008-09-25 19:37:00 +02:00
|
|
|
#ifdef USE_DMALLOC
|
|
|
|
{
|
|
|
|
int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
|
|
|
|
tor_assert(r);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-12-18 18:19:04 +01:00
|
|
|
update_approx_time(time(NULL));
|
2008-09-25 19:37:00 +02:00
|
|
|
options = options_new();
|
2008-04-23 22:32:31 +02:00
|
|
|
tor_threads_init();
|
2008-01-02 06:38:53 +01:00
|
|
|
init_logging();
|
|
|
|
|
2009-09-21 20:23:13 +02:00
|
|
|
for (i_out = i = 1; i < c; ++i) {
|
|
|
|
if (!strcmp(v[i], "--warn")) {
|
2007-08-18 21:39:14 +02:00
|
|
|
loglevel = LOG_WARN;
|
2009-09-21 20:23:13 +02:00
|
|
|
} else if (!strcmp(v[i], "--notice")) {
|
2007-08-18 21:39:14 +02:00
|
|
|
loglevel = LOG_NOTICE;
|
2009-09-21 20:23:13 +02:00
|
|
|
} else if (!strcmp(v[i], "--info")) {
|
2007-08-18 21:39:14 +02:00
|
|
|
loglevel = LOG_INFO;
|
2009-09-21 20:23:13 +02:00
|
|
|
} else if (!strcmp(v[i], "--debug")) {
|
2007-08-18 21:39:14 +02:00
|
|
|
loglevel = LOG_DEBUG;
|
2009-09-21 20:23:13 +02:00
|
|
|
} else {
|
|
|
|
v[i_out++] = v[i];
|
2007-08-18 21:39:14 +02:00
|
|
|
}
|
|
|
|
}
|
2009-09-21 20:23:13 +02:00
|
|
|
c = i_out;
|
2007-08-18 21:39:14 +02:00
|
|
|
|
2008-03-05 23:31:39 +01:00
|
|
|
{
|
2008-09-10 07:34:03 +02:00
|
|
|
log_severity_list_t s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
|
|
set_log_severity_config(loglevel, LOG_ERR, &s);
|
2008-12-03 00:36:58 +01:00
|
|
|
add_stream_log(&s, "", fileno(stdout));
|
2008-03-05 23:31:39 +01:00
|
|
|
}
|
2007-08-18 21:39:14 +02:00
|
|
|
|
2005-08-27 01:22:27 +02:00
|
|
|
options->command = CMD_RUN_UNITTESTS;
|
2011-07-01 17:56:09 +02:00
|
|
|
if (crypto_global_init(0, NULL, NULL)) {
|
|
|
|
printf("Can't initialize crypto subsystem; exiting.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2011-11-30 02:37:57 +01:00
|
|
|
crypto_set_tls_dh_prime(NULL);
|
2007-01-11 02:41:07 +01:00
|
|
|
rep_hist_init();
|
2004-12-07 06:31:38 +01:00
|
|
|
network_init();
|
2005-08-22 02:34:42 +02:00
|
|
|
setup_directory();
|
2004-11-06 06:18:11 +01:00
|
|
|
options_init(options);
|
2005-08-22 02:34:42 +02:00
|
|
|
options->DataDirectory = tor_strdup(temp_dir);
|
2009-07-24 01:13:33 +02:00
|
|
|
options->EntryStatistics = 1;
|
2006-03-26 08:51:26 +02:00
|
|
|
if (set_options(options, &errmsg) < 0) {
|
|
|
|
printf("Failed to set initial options: %s\n", errmsg);
|
|
|
|
tor_free(errmsg);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-04-17 01:21:44 +02:00
|
|
|
|
2008-03-21 22:59:57 +01:00
|
|
|
crypto_seed_rng(1);
|
2005-08-22 02:34:42 +02:00
|
|
|
|
2004-08-11 21:20:24 +02:00
|
|
|
atexit(remove_directory);
|
2004-08-07 04:46:16 +02:00
|
|
|
|
2009-10-11 00:50:57 +02:00
|
|
|
have_failed = (tinytest_main(c, v, testgroups) != 0);
|
2004-03-01 07:45:32 +01:00
|
|
|
|
2008-09-25 19:37:00 +02:00
|
|
|
free_pregenerated_keys();
|
2008-09-09 22:43:31 +02:00
|
|
|
#ifdef USE_DMALLOC
|
|
|
|
tor_free_all(0);
|
|
|
|
dmalloc_log_unfreed();
|
|
|
|
#endif
|
2008-03-25 21:20:45 +01:00
|
|
|
|
2004-03-01 07:45:32 +01:00
|
|
|
if (have_failed)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2003-04-07 04:12:02 +02:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|