2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2001-2004 Roger Dingledine.
|
2006-02-09 06:46:49 +01:00
|
|
|
* Copyright 2004-2006 Roger Dingledine, Nick Mathewson. */
|
2003-04-07 04:12:02 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char test_c_id[] =
|
|
|
|
"$Id$";
|
2003-04-07 04:12:02 +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"
|
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
|
|
|
|
|
2003-08-14 19:13:52 +02:00
|
|
|
#ifdef MS_WINDOWS
|
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
|
|
|
|
|
2003-04-07 15:25:44 +02:00
|
|
|
#include "or.h"
|
|
|
|
#include "../common/test.h"
|
2004-09-02 20:22:51 +02:00
|
|
|
#include "../common/torgzip.h"
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2004-03-01 07:45:32 +01:00
|
|
|
int have_failed = 0;
|
|
|
|
|
2004-03-29 21:50:59 +02:00
|
|
|
/* These functions are file-local, but are exposed so we can test. */
|
2005-06-28 00:25:09 +02:00
|
|
|
void add_fingerprint_to_dir(const char *nickname, const char *fp,
|
|
|
|
smartlist_t *list);
|
2004-10-14 04:47:09 +02:00
|
|
|
void get_platform_str(char *platform, size_t len);
|
2005-07-15 21:31:11 +02:00
|
|
|
size_t read_escaped_data(const char *data, size_t len, int translate_newlines,
|
|
|
|
char **out);
|
2005-08-09 20:49:43 +02:00
|
|
|
or_options_t *options_new(void);
|
2003-07-30 21:10:20 +02:00
|
|
|
|
2004-08-11 21:20:24 +02:00
|
|
|
static char temp_dir[256];
|
|
|
|
|
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;
|
|
|
|
|
2004-10-21 01:30:38 +02:00
|
|
|
#ifdef MS_WINDOWS
|
2004-12-07 06:31:38 +01:00
|
|
|
// XXXX
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_snprintf(temp_dir, sizeof(temp_dir),
|
|
|
|
"c:\\windows\\temp\\tor_test_%d", (int)getpid());
|
2004-08-11 21:20:24 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-11-02 04:02:17 +01:00
|
|
|
static const char *
|
2004-08-11 21:20:24 +02:00
|
|
|
get_fname(const char *name)
|
|
|
|
{
|
|
|
|
static char buf[1024];
|
|
|
|
setup_directory();
|
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;
|
|
|
|
}
|
|
|
|
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2004-10-27 20:16:37 +02:00
|
|
|
remove_directory(void)
|
2004-08-11 21:20:24 +02:00
|
|
|
{
|
2005-09-10 03:42:42 +02:00
|
|
|
smartlist_t *elements = tor_listdir(temp_dir);
|
|
|
|
if (elements) {
|
|
|
|
SMARTLIST_FOREACH(elements, const char *, cp,
|
|
|
|
{
|
|
|
|
size_t len = strlen(cp)+strlen(temp_dir)+16;
|
|
|
|
char *tmp = tor_malloc(len);
|
|
|
|
tor_snprintf(tmp, len, "%s"PATH_SEPARATOR"%s", temp_dir, cp);
|
|
|
|
unlink(tmp);
|
|
|
|
tor_free(tmp);
|
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(elements);
|
2004-12-07 06:31:38 +01:00
|
|
|
}
|
2004-08-11 21:20:24 +02:00
|
|
|
rmdir(temp_dir);
|
2003-04-15 21:10:18 +02:00
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
2003-09-25 07:17:11 +02:00
|
|
|
buf_t *buf;
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2005-04-26 23:10:31 +02:00
|
|
|
int j;
|
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
|
|
|
|
2005-04-26 22:53:22 +02: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);
|
|
|
|
|
|
|
|
/* Okay, now make sure growing can work. */
|
|
|
|
buf = buf_new_with_capacity(16);
|
|
|
|
test_eq(buf_capacity(buf), 16);
|
|
|
|
write_to_buf(str+1, 255, buf);
|
|
|
|
test_eq(buf_capacity(buf), 256);
|
|
|
|
fetch_from_buf(str2, 254, buf);
|
|
|
|
test_memeq(str+1, str2, 254);
|
|
|
|
test_eq(buf_capacity(buf), 256);
|
|
|
|
assert_buf_ok(buf);
|
|
|
|
write_to_buf(str, 32, buf);
|
|
|
|
test_eq(buf_capacity(buf), 256);
|
|
|
|
assert_buf_ok(buf);
|
|
|
|
write_to_buf(str, 256, buf);
|
|
|
|
assert_buf_ok(buf);
|
|
|
|
test_eq(buf_capacity(buf), 512);
|
|
|
|
test_eq(buf_datalen(buf), 33+256);
|
|
|
|
fetch_from_buf(str2, 33, buf);
|
|
|
|
test_eq(*str2, str[255]);
|
|
|
|
|
|
|
|
test_memeq(str2+1, str, 32);
|
|
|
|
test_eq(buf_capacity(buf), 512);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
test_eq(buf_capacity(buf), 33668);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
test_eq(buf_capacity(buf),33668);
|
|
|
|
for (j=0; j < 120; ++j) {
|
|
|
|
fetch_from_buf(str2, 255,buf);
|
|
|
|
test_memeq(str2, str, 255);
|
|
|
|
}
|
|
|
|
|
2005-04-26 23:10:31 +02:00
|
|
|
#if 0
|
2005-04-26 22:53:22 +02:00
|
|
|
/****
|
|
|
|
* read_to_buf
|
|
|
|
****/
|
|
|
|
s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
|
2003-04-15 21:10:18 +02:00
|
|
|
write(s, str, 256);
|
|
|
|
close(s);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2004-08-11 21:20:24 +02:00
|
|
|
s = open(get_fname("data"), O_RDONLY, 0);
|
2003-04-15 21:10:18 +02:00
|
|
|
eof = 0;
|
2005-04-26 22:53:22 +02:00
|
|
|
errno = 0; /* XXXX */
|
2003-09-25 07:17:11 +02:00
|
|
|
i = read_to_buf(s, 10, buf, &eof);
|
2005-04-26 22:53:22 +02:00
|
|
|
printf("%s\n", strerror(errno));
|
2003-04-15 21:10:18 +02:00
|
|
|
test_eq(i, 10);
|
2005-04-26 22:53:22 +02:00
|
|
|
test_eq(eof, 0);
|
|
|
|
test_eq(buf_capacity(buf), 4096);
|
|
|
|
test_eq(buf_datalen(buf), 10);
|
|
|
|
|
2003-09-25 07:17:11 +02:00
|
|
|
test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
|
2003-04-15 21:10:18 +02:00
|
|
|
|
|
|
|
/* Test reading 0 bytes. */
|
2003-09-25 07:17:11 +02:00
|
|
|
i = read_to_buf(s, 0, buf, &eof);
|
2003-10-21 10:37:07 +02:00
|
|
|
test_eq(buf_capacity(buf), 512*1024);
|
2003-09-25 07:17:11 +02:00
|
|
|
test_eq(buf_datalen(buf), 10);
|
2003-04-15 21:10:18 +02:00
|
|
|
test_eq(eof, 0);
|
|
|
|
test_eq(i, 0);
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2003-04-15 21:10:18 +02:00
|
|
|
/* Now test when buffer is filled exactly. */
|
2003-09-25 07:17:11 +02:00
|
|
|
buf2 = buf_new_with_capacity(6);
|
|
|
|
i = read_to_buf(s, 6, buf2, &eof);
|
|
|
|
test_eq(buf_capacity(buf2), 6);
|
|
|
|
test_eq(buf_datalen(buf2), 6);
|
2003-04-15 21:10:18 +02:00
|
|
|
test_eq(eof, 0);
|
|
|
|
test_eq(i, 6);
|
2003-09-25 07:17:11 +02:00
|
|
|
test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
|
|
|
|
buf_free(buf2);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-04-15 21:10:18 +02:00
|
|
|
/* Now test when buffer is filled with more data to read. */
|
2003-09-25 07:17:11 +02:00
|
|
|
buf2 = buf_new_with_capacity(32);
|
|
|
|
i = read_to_buf(s, 128, buf2, &eof);
|
2003-10-21 10:37:07 +02:00
|
|
|
test_eq(buf_capacity(buf2), 128);
|
2003-09-25 07:17:11 +02:00
|
|
|
test_eq(buf_datalen(buf2), 32);
|
2003-04-15 21:10:18 +02:00
|
|
|
test_eq(eof, 0);
|
2003-09-25 07:17:11 +02:00
|
|
|
test_eq(i, 32);
|
|
|
|
buf_free(buf2);
|
2003-04-15 21:10:18 +02:00
|
|
|
|
|
|
|
/* Now read to eof. */
|
2003-09-25 07:17:11 +02:00
|
|
|
test_assert(buf_capacity(buf) > 256);
|
|
|
|
i = read_to_buf(s, 1024, buf, &eof);
|
|
|
|
test_eq(i, (256-32-10-6));
|
|
|
|
test_eq(buf_capacity(buf), MAX_BUF_SIZE);
|
|
|
|
test_eq(buf_datalen(buf), 256-6-32);
|
|
|
|
test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
|
2003-04-15 21:10:18 +02:00
|
|
|
test_eq(eof, 0);
|
|
|
|
|
2003-09-25 07:17:11 +02:00
|
|
|
i = read_to_buf(s, 1024, buf, &eof);
|
2003-04-15 21:10:18 +02:00
|
|
|
test_eq(i, 0);
|
2003-09-25 07:17:11 +02:00
|
|
|
test_eq(buf_capacity(buf), MAX_BUF_SIZE);
|
|
|
|
test_eq(buf_datalen(buf), 256-6-32);
|
2003-04-15 21:10:18 +02:00
|
|
|
test_eq(eof, 1);
|
2005-04-26 23:10:31 +02:00
|
|
|
#endif
|
2003-04-15 21:10:18 +02:00
|
|
|
|
2003-04-07 15:25:44 +02:00
|
|
|
buf_free(buf);
|
|
|
|
}
|
|
|
|
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2004-10-27 20:16:37 +02:00
|
|
|
test_crypto_dh(void)
|
2003-05-01 02:53:46 +02:00
|
|
|
{
|
|
|
|
crypto_dh_env_t *dh1, *dh2;
|
2004-05-01 22:46:28 +02:00
|
|
|
char p1[DH_BYTES];
|
|
|
|
char p2[DH_BYTES];
|
|
|
|
char s1[DH_BYTES];
|
|
|
|
char s2[DH_BYTES];
|
2003-06-14 03:30:53 +02:00
|
|
|
int s1len, s2len;
|
2003-05-01 02:53:46 +02:00
|
|
|
|
|
|
|
dh1 = crypto_dh_new();
|
|
|
|
dh2 = crypto_dh_new();
|
2004-05-01 22:46:28 +02:00
|
|
|
test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
|
|
|
|
test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
|
|
|
|
|
|
|
|
memset(p1, 0, DH_BYTES);
|
|
|
|
memset(p2, 0, DH_BYTES);
|
|
|
|
test_memeq(p1, p2, DH_BYTES);
|
|
|
|
test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
|
|
|
|
test_memneq(p1, p2, DH_BYTES);
|
|
|
|
test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
|
|
|
|
test_memneq(p1, p2, DH_BYTES);
|
|
|
|
|
|
|
|
memset(s1, 0, DH_BYTES);
|
|
|
|
memset(s2, 0xFF, DH_BYTES);
|
|
|
|
s1len = crypto_dh_compute_secret(dh1, p2, DH_BYTES, s1, 50);
|
|
|
|
s2len = crypto_dh_compute_secret(dh2, p1, DH_BYTES, s2, 50);
|
2003-06-14 03:30:53 +02:00
|
|
|
test_assert(s1len > 0);
|
|
|
|
test_eq(s1len, s2len);
|
|
|
|
test_memeq(s1, s2, s1len);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-05-01 02:53:46 +02:00
|
|
|
crypto_dh_free(dh1);
|
|
|
|
crypto_dh_free(dh2);
|
|
|
|
}
|
|
|
|
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2004-10-27 20:16:37 +02:00
|
|
|
test_crypto(void)
|
2003-05-01 02:53:46 +02:00
|
|
|
{
|
2003-04-16 17:24:09 +02:00
|
|
|
crypto_cipher_env_t *env1, *env2;
|
|
|
|
crypto_pk_env_t *pk1, *pk2;
|
|
|
|
char *data1, *data2, *data3, *cp;
|
2004-04-01 05:08:35 +02:00
|
|
|
int i, j, p, len;
|
2004-10-14 05:13:08 +02:00
|
|
|
size_t size;
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2003-05-20 08:41:23 +02:00
|
|
|
data1 = tor_malloc(1024);
|
|
|
|
data2 = tor_malloc(1024);
|
|
|
|
data3 = tor_malloc(1024);
|
2003-04-16 17:24:09 +02:00
|
|
|
test_assert(data1 && data2 && data3);
|
2003-04-15 21:10:18 +02:00
|
|
|
|
2003-06-13 23:23:14 +02:00
|
|
|
/* Try out RNG. */
|
2003-06-17 22:19:54 +02:00
|
|
|
test_assert(! crypto_seed_rng());
|
2004-11-02 03:28:51 +01:00
|
|
|
crypto_rand(data1, 100);
|
|
|
|
crypto_rand(data2, 100);
|
2003-06-13 23:23:14 +02:00
|
|
|
test_memneq(data1,data2,100);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
#if 0
|
2003-04-16 17:24:09 +02:00
|
|
|
/* Try out identity ciphers. */
|
|
|
|
env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
|
|
|
|
test_neq(env1, 0);
|
|
|
|
test_eq(crypto_cipher_generate_key(env1), 0);
|
|
|
|
test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i = 0; i < 1024; ++i) {
|
2003-04-16 17:24:09 +02:00
|
|
|
data1[i] = (char) i*73;
|
|
|
|
}
|
2004-11-02 03:28:51 +01:00
|
|
|
crypto_cipher_encrypt(env1, data2, data1, 1024);
|
2003-04-16 17:24:09 +02:00
|
|
|
test_memeq(data1, data2, 1024);
|
|
|
|
crypto_free_cipher_env(env1);
|
2004-04-03 04:40:30 +02:00
|
|
|
#endif
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
/* Now, test encryption and decryption with stream cipher. */
|
2003-04-16 17:24:09 +02:00
|
|
|
data1[0]='\0';
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i = 1023; i>0; i -= 35)
|
2003-04-16 17:24:09 +02:00
|
|
|
strncat(data1, "Now is the time for all good onions", i);
|
2004-04-03 04:40:30 +02:00
|
|
|
|
|
|
|
memset(data2, 0, 1024);
|
|
|
|
memset(data3, 0, 1024);
|
|
|
|
env1 = crypto_new_cipher_env();
|
|
|
|
test_neq(env1, 0);
|
|
|
|
env2 = crypto_new_cipher_env();
|
|
|
|
test_neq(env2, 0);
|
|
|
|
j = crypto_cipher_generate_key(env1);
|
|
|
|
crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
|
|
|
|
crypto_cipher_encrypt_init_cipher(env1);
|
|
|
|
crypto_cipher_decrypt_init_cipher(env2);
|
|
|
|
|
|
|
|
/* Try encrypting 512 chars. */
|
2004-11-02 03:28:51 +01:00
|
|
|
crypto_cipher_encrypt(env1, data2, data1, 512);
|
|
|
|
crypto_cipher_decrypt(env2, data3, data2, 512);
|
2004-04-03 04:40:30 +02:00
|
|
|
test_memeq(data1, data3, 512);
|
|
|
|
test_memneq(data1, data2, 512);
|
|
|
|
|
|
|
|
/* Now encrypt 1 at a time, and get 1 at a time. */
|
|
|
|
for (j = 512; j < 560; ++j) {
|
2004-11-02 03:28:51 +01:00
|
|
|
crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
|
2004-04-03 04:40:30 +02:00
|
|
|
}
|
|
|
|
for (j = 512; j < 560; ++j) {
|
2004-11-02 03:28:51 +01:00
|
|
|
crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
|
2004-04-03 04:40:30 +02:00
|
|
|
}
|
|
|
|
test_memeq(data1, data3, 560);
|
|
|
|
/* Now encrypt 3 at a time, and get 5 at a time. */
|
|
|
|
for (j = 560; j < 1024-5; j += 3) {
|
2004-11-02 03:28:51 +01:00
|
|
|
crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
|
2004-04-03 04:40:30 +02:00
|
|
|
}
|
|
|
|
for (j = 560; j < 1024-5; j += 5) {
|
2004-11-02 03:28:51 +01:00
|
|
|
crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
|
2004-04-03 04:40:30 +02:00
|
|
|
}
|
|
|
|
test_memeq(data1, data3, 1024-5);
|
|
|
|
/* Now make sure that when we encrypt with different chunk sizes, we get
|
|
|
|
the same results. */
|
|
|
|
crypto_free_cipher_env(env2);
|
|
|
|
|
|
|
|
memset(data3, 0, 1024);
|
|
|
|
env2 = crypto_new_cipher_env();
|
|
|
|
test_neq(env2, 0);
|
|
|
|
crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
|
|
|
|
crypto_cipher_encrypt_init_cipher(env2);
|
|
|
|
for (j = 0; j < 1024-16; j += 17) {
|
2004-11-02 03:28:51 +01:00
|
|
|
crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
|
2004-04-03 04:40:30 +02:00
|
|
|
}
|
|
|
|
for (j= 0; j < 1024-16; ++j) {
|
|
|
|
if (data2[j] != data3[j]) {
|
|
|
|
printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
|
2003-04-16 17:24:09 +02:00
|
|
|
}
|
|
|
|
}
|
2004-04-03 04:40:30 +02:00
|
|
|
test_memeq(data2, data3, 1024-16);
|
|
|
|
crypto_free_cipher_env(env1);
|
|
|
|
crypto_free_cipher_env(env2);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-04-16 17:24:09 +02:00
|
|
|
/* Test vectors for stream ciphers. */
|
|
|
|
/* XXXX Look up some test vectors for the ciphers and make sure we match. */
|
|
|
|
|
|
|
|
/* Test SHA-1 with a test vector from the specification. */
|
2004-11-02 03:28:51 +01:00
|
|
|
i = crypto_digest(data1, "abc", 3);
|
2003-04-16 17:24:09 +02:00
|
|
|
test_memeq(data1,
|
|
|
|
"\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
|
|
|
|
"\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
|
|
|
|
|
|
|
|
/* Public-key ciphers */
|
2004-04-03 04:40:30 +02:00
|
|
|
pk1 = crypto_new_pk_env();
|
|
|
|
pk2 = crypto_new_pk_env();
|
2003-04-16 17:24:09 +02:00
|
|
|
test_assert(pk1 && pk2);
|
|
|
|
test_assert(! crypto_pk_generate_key(pk1));
|
2004-10-14 04:47:09 +02:00
|
|
|
test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &size));
|
|
|
|
test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, size));
|
2003-04-16 17:24:09 +02:00
|
|
|
test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
|
2004-10-07 05:11:42 +02:00
|
|
|
tor_free(cp);
|
|
|
|
|
2003-04-16 17:24:09 +02:00
|
|
|
test_eq(128, crypto_pk_keysize(pk1));
|
|
|
|
test_eq(128, crypto_pk_keysize(pk2));
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2004-11-02 03:28:51 +01:00
|
|
|
test_eq(128, crypto_pk_public_encrypt(pk2, data1, "Hello whirled.", 15,
|
2004-04-03 04:40:30 +02:00
|
|
|
PK_PKCS1_OAEP_PADDING));
|
2004-11-02 03:28:51 +01:00
|
|
|
test_eq(128, crypto_pk_public_encrypt(pk1, data2, "Hello whirled.", 15,
|
2004-04-03 04:40:30 +02:00
|
|
|
PK_PKCS1_OAEP_PADDING));
|
2003-04-16 17:24:09 +02:00
|
|
|
/* oaep padding should make encryption not match */
|
|
|
|
test_memneq(data1, data2, 128);
|
2004-11-02 03:28:51 +01:00
|
|
|
test_eq(15, crypto_pk_private_decrypt(pk1, data3, data1, 128,
|
2004-05-12 21:30:28 +02:00
|
|
|
PK_PKCS1_OAEP_PADDING,1));
|
2003-04-16 17:24:09 +02:00
|
|
|
test_streq(data3, "Hello whirled.");
|
|
|
|
memset(data3, 0, 1024);
|
2004-11-02 03:28:51 +01:00
|
|
|
test_eq(15, crypto_pk_private_decrypt(pk1, data3, data2, 128,
|
2004-05-12 21:30:28 +02:00
|
|
|
PK_PKCS1_OAEP_PADDING,1));
|
2003-04-16 17:24:09 +02:00
|
|
|
test_streq(data3, "Hello whirled.");
|
|
|
|
/* Can't decrypt with public key. */
|
2004-11-02 03:28:51 +01:00
|
|
|
test_eq(-1, crypto_pk_private_decrypt(pk2, data3, data2, 128,
|
2004-05-12 21:30:28 +02:00
|
|
|
PK_PKCS1_OAEP_PADDING,1));
|
2003-04-16 17:24:09 +02:00
|
|
|
/* Try again with bad padding */
|
|
|
|
memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
|
2004-11-02 03:28:51 +01:00
|
|
|
test_eq(-1, crypto_pk_private_decrypt(pk1, data3, data2, 128,
|
2004-05-12 21:30:28 +02:00
|
|
|
PK_PKCS1_OAEP_PADDING,1));
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-04-16 17:24:09 +02:00
|
|
|
/* File operations: save and load private key */
|
2004-04-26 20:09:50 +02:00
|
|
|
test_assert(! crypto_pk_write_private_key_to_filename(pk1,
|
2004-08-11 21:20:24 +02:00
|
|
|
get_fname("pkey1")));
|
2004-04-26 20:09:50 +02:00
|
|
|
|
2003-12-17 22:09:31 +01:00
|
|
|
test_assert(! crypto_pk_read_private_key_from_filename(pk2,
|
2004-08-11 21:20:24 +02:00
|
|
|
get_fname("pkey1")));
|
2004-11-02 03:28:51 +01:00
|
|
|
test_eq(15, crypto_pk_private_decrypt(pk2, data3, data1, 128,
|
2004-05-12 21:30:28 +02:00
|
|
|
PK_PKCS1_OAEP_PADDING,1));
|
2003-04-16 17:24:09 +02:00
|
|
|
|
2003-05-07 20:30:46 +02:00
|
|
|
/* Now try signing. */
|
|
|
|
strcpy(data1, "Ossifrage");
|
2004-11-02 03:28:51 +01:00
|
|
|
test_eq(128, crypto_pk_private_sign(pk1, data2, data1, 10));
|
|
|
|
test_eq(10, crypto_pk_public_checksig(pk1, data3, data2, 128));
|
2003-05-07 20:30:46 +02:00
|
|
|
test_streq(data3, "Ossifrage");
|
2004-04-02 00:10:33 +02:00
|
|
|
/* Try signing digests. */
|
2004-11-02 03:28:51 +01:00
|
|
|
test_eq(128, crypto_pk_private_sign_digest(pk1, data2, data1, 10));
|
|
|
|
test_eq(20, crypto_pk_public_checksig(pk1, data3, data2, 128));
|
2004-04-02 00:10:33 +02:00
|
|
|
test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
|
|
|
|
test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
|
2003-05-07 20:30:46 +02:00
|
|
|
/*XXXX test failed signing*/
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2004-03-30 21:47:32 +02:00
|
|
|
/* Try encoding */
|
|
|
|
crypto_free_pk_env(pk2);
|
|
|
|
pk2 = NULL;
|
|
|
|
i = crypto_pk_asn1_encode(pk1, data1, 1024);
|
|
|
|
test_assert(i>0);
|
|
|
|
pk2 = crypto_pk_asn1_decode(data1, i);
|
|
|
|
test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
|
|
|
|
|
2004-04-01 05:08:35 +02:00
|
|
|
/* Try with hybrid encryption wrappers. */
|
2004-11-02 03:28:51 +01:00
|
|
|
crypto_rand(data1, 1024);
|
2004-04-01 05:08:35 +02:00
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
for (j = 85; j < 140; ++j) {
|
|
|
|
memset(data2,0,1024);
|
|
|
|
memset(data3,0,1024);
|
|
|
|
if (i == 0 && j < 129)
|
|
|
|
continue;
|
2004-04-03 04:40:30 +02:00
|
|
|
p = (i==0)?PK_NO_PADDING:
|
|
|
|
(i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
|
2004-11-02 03:28:51 +01:00
|
|
|
len = crypto_pk_public_hybrid_encrypt(pk1,data2,data1,j,p,0);
|
2004-04-01 05:08:35 +02:00
|
|
|
test_assert(len>=0);
|
2004-11-02 03:28:51 +01:00
|
|
|
len = crypto_pk_private_hybrid_decrypt(pk1,data3,data2,len,p,1);
|
2004-04-01 05:08:35 +02:00
|
|
|
test_eq(len,j);
|
|
|
|
test_memeq(data1,data3,j);
|
|
|
|
}
|
|
|
|
}
|
2003-12-17 22:09:31 +01:00
|
|
|
crypto_free_pk_env(pk1);
|
|
|
|
crypto_free_pk_env(pk2);
|
2003-04-16 17:24:09 +02:00
|
|
|
|
2003-05-07 20:30:46 +02:00
|
|
|
/* Base64 tests */
|
|
|
|
strcpy(data1, "Test string that contains 35 chars.");
|
|
|
|
strcat(data1, " 2nd string that contains 35 chars.");
|
|
|
|
|
|
|
|
i = base64_encode(data2, 1024, data1, 71);
|
|
|
|
j = base64_decode(data3, 1024, data2, i);
|
|
|
|
test_streq(data3, data1);
|
|
|
|
test_eq(j, 71);
|
|
|
|
test_assert(data2[i] == '\0');
|
|
|
|
|
2005-09-18 04:22:21 +02:00
|
|
|
crypto_rand(data1, DIGEST_LEN);
|
|
|
|
memset(data2, 100, 1024);
|
|
|
|
digest_to_base64(data2, data1);
|
|
|
|
test_eq(BASE64_DIGEST_LEN, strlen(data2));
|
|
|
|
test_eq(100, data2[BASE64_DIGEST_LEN+2]);
|
|
|
|
memset(data3, 99, 1024);
|
|
|
|
digest_from_base64(data3, data2);
|
|
|
|
test_memeq(data1, data3, DIGEST_LEN);
|
|
|
|
test_eq(99, data3[DIGEST_LEN+1]);
|
|
|
|
|
2004-03-30 21:47:32 +02:00
|
|
|
/* Base32 tests */
|
|
|
|
strcpy(data1, "5chrs");
|
|
|
|
/* bit pattern is: [35 63 68 72 73] ->
|
|
|
|
* [00110101 01100011 01101000 01110010 01110011]
|
|
|
|
* By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
|
|
|
|
*/
|
2004-07-22 10:30:06 +02:00
|
|
|
base32_encode(data2, 9, data1, 5);
|
2004-04-08 22:56:33 +02:00
|
|
|
test_streq(data2, "gvrwq4tt");
|
|
|
|
|
|
|
|
strcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4");
|
2004-07-22 10:30:06 +02:00
|
|
|
base32_encode(data2, 30, data1, 10);
|
2004-04-08 22:56:33 +02:00
|
|
|
test_streq(data2, "772w2rfobvomsywe");
|
2003-05-07 20:30:46 +02:00
|
|
|
|
2004-07-03 01:40:03 +02:00
|
|
|
/* Base16 tests */
|
|
|
|
strcpy(data1, "6chrs\xff");
|
2004-07-22 10:30:06 +02:00
|
|
|
base16_encode(data2, 13, data1, 6);
|
2004-07-03 01:40:03 +02:00
|
|
|
test_streq(data2, "3663687273FF");
|
|
|
|
|
|
|
|
strcpy(data1, "f0d678affc000100");
|
|
|
|
i = base16_decode(data2, 8, data1, 16);
|
|
|
|
test_eq(i,0);
|
|
|
|
test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
|
|
|
|
|
2005-09-30 22:47:58 +02:00
|
|
|
tor_free(data1);
|
|
|
|
tor_free(data2);
|
|
|
|
tor_free(data3);
|
2003-04-16 17:24:09 +02:00
|
|
|
}
|
|
|
|
|
2005-06-04 15:43:03 +02:00
|
|
|
static void
|
|
|
|
test_crypto_s2k(void)
|
|
|
|
{
|
|
|
|
char buf[29];
|
|
|
|
char buf2[29];
|
|
|
|
char *buf3;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(buf2, 0, sizeof(buf2));
|
|
|
|
buf3 = tor_malloc(65536);
|
|
|
|
memset(buf3, 0, 65536);
|
|
|
|
|
|
|
|
secret_to_key(buf+9, 20, "", 0, buf);
|
|
|
|
crypto_digest(buf2+9, buf3, 1024);
|
|
|
|
test_memeq(buf, buf2, 29);
|
|
|
|
|
|
|
|
memcpy(buf,"vrbacrda",8);
|
|
|
|
memcpy(buf2,"vrbacrda",8);
|
|
|
|
buf[8] = 96;
|
|
|
|
buf2[8] = 96;
|
|
|
|
secret_to_key(buf+9, 20, "12345678", 8, buf);
|
|
|
|
for (i = 0; i < 65536; i += 16) {
|
|
|
|
memcpy(buf3+i, "vrbacrda12345678", 16);
|
|
|
|
}
|
|
|
|
crypto_digest(buf2+9, buf3, 65536);
|
|
|
|
test_memeq(buf, buf2, 29);
|
|
|
|
}
|
|
|
|
|
2005-09-10 01:12:54 +02:00
|
|
|
static int
|
|
|
|
_compare_strs(const void **a, const void **b)
|
|
|
|
{
|
|
|
|
const char *s1 = *a, *s2 = *b;
|
|
|
|
return strcmp(s1, s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_compare_without_first_ch(const void *a, const void **b)
|
|
|
|
{
|
|
|
|
const char *s1 = a, *s2 = *b;
|
|
|
|
return strcasecmp(s1+1, s2);
|
|
|
|
}
|
|
|
|
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
test_util(void)
|
|
|
|
{
|
2003-04-17 01:21:44 +02:00
|
|
|
struct timeval start, end;
|
2003-11-10 07:28:53 +01:00
|
|
|
struct tm a_time;
|
2004-04-07 21:46:27 +02:00
|
|
|
smartlist_t *sl;
|
2004-08-04 03:11:15 +02:00
|
|
|
char timestr[RFC1123_TIME_LEN+1];
|
2004-10-06 15:31:48 +02:00
|
|
|
char buf[1024];
|
2004-08-04 03:11:15 +02:00
|
|
|
time_t t_res;
|
|
|
|
int i;
|
2004-10-12 17:48:30 +02:00
|
|
|
uint32_t u32;
|
|
|
|
uint16_t u16;
|
2004-11-04 23:29:45 +01:00
|
|
|
char *cp, *k, *v;
|
2003-04-17 01:21:44 +02:00
|
|
|
|
|
|
|
start.tv_sec = 5;
|
|
|
|
start.tv_usec = 5000;
|
|
|
|
|
|
|
|
end.tv_sec = 5;
|
|
|
|
end.tv_usec = 5000;
|
|
|
|
|
|
|
|
test_eq(0L, tv_udiff(&start, &end));
|
|
|
|
|
|
|
|
end.tv_usec = 7000;
|
|
|
|
|
|
|
|
test_eq(2000L, tv_udiff(&start, &end));
|
|
|
|
|
|
|
|
end.tv_sec = 6;
|
|
|
|
|
|
|
|
test_eq(1002000L, tv_udiff(&start, &end));
|
|
|
|
|
|
|
|
end.tv_usec = 0;
|
|
|
|
|
|
|
|
test_eq(995000L, tv_udiff(&start, &end));
|
|
|
|
|
|
|
|
end.tv_sec = 4;
|
|
|
|
|
2004-11-23 09:36:26 +01:00
|
|
|
test_eq(-1005000L, tv_udiff(&start, &end));
|
2003-04-17 01:21:44 +02:00
|
|
|
|
2003-11-10 07:28:53 +01:00
|
|
|
/* The test values here are confirmed to be correct on a platform
|
2003-11-10 09:06:55 +01:00
|
|
|
* with a working timegm. */
|
2003-11-10 07:28:53 +01:00
|
|
|
a_time.tm_year = 2003-1900;
|
|
|
|
a_time.tm_mon = 7;
|
|
|
|
a_time.tm_mday = 30;
|
|
|
|
a_time.tm_hour = 6;
|
|
|
|
a_time.tm_min = 14;
|
|
|
|
a_time.tm_sec = 55;
|
|
|
|
test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
|
|
|
|
a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
|
|
|
|
test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
|
|
|
|
a_time.tm_mon = 1; /* Try a leap year, in feb. */
|
|
|
|
a_time.tm_mday = 10;
|
|
|
|
test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
|
2004-04-07 21:46:27 +02:00
|
|
|
|
2004-08-07 04:46:16 +02:00
|
|
|
format_rfc1123_time(timestr, 0);
|
2004-08-04 03:11:15 +02:00
|
|
|
test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
|
2004-08-07 04:46:16 +02:00
|
|
|
format_rfc1123_time(timestr, (time_t)1091580502UL);
|
2004-08-04 03:11:15 +02:00
|
|
|
test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
|
|
|
|
|
|
|
|
t_res = 0;
|
2004-08-07 04:46:16 +02:00
|
|
|
i = parse_rfc1123_time(timestr, &t_res);
|
2004-08-04 03:11:15 +02:00
|
|
|
test_eq(i,0);
|
|
|
|
test_eq(t_res, (time_t)1091580502UL);
|
2004-04-07 21:46:27 +02:00
|
|
|
|
|
|
|
/* Test smartlist */
|
|
|
|
sl = smartlist_create();
|
|
|
|
smartlist_add(sl, (void*)1);
|
|
|
|
smartlist_add(sl, (void*)2);
|
|
|
|
smartlist_add(sl, (void*)3);
|
|
|
|
smartlist_add(sl, (void*)4);
|
2004-11-12 22:14:06 +01:00
|
|
|
smartlist_del_keeporder(sl, 1);
|
2004-04-07 21:46:27 +02:00
|
|
|
smartlist_insert(sl, 1, (void*)22);
|
|
|
|
smartlist_insert(sl, 0, (void*)0);
|
|
|
|
smartlist_insert(sl, 5, (void*)555);
|
2005-08-12 19:24:53 +02:00
|
|
|
test_eq_ptr((void*)0, smartlist_get(sl,0));
|
|
|
|
test_eq_ptr((void*)1, smartlist_get(sl,1));
|
|
|
|
test_eq_ptr((void*)22, smartlist_get(sl,2));
|
|
|
|
test_eq_ptr((void*)3, smartlist_get(sl,3));
|
|
|
|
test_eq_ptr((void*)4, smartlist_get(sl,4));
|
|
|
|
test_eq_ptr((void*)555, smartlist_get(sl,5));
|
2004-08-17 23:06:36 +02:00
|
|
|
|
|
|
|
smartlist_clear(sl);
|
2004-09-02 20:22:51 +02:00
|
|
|
smartlist_split_string(sl, "abc", ":", 0, 0);
|
2004-08-17 23:06:36 +02:00
|
|
|
test_eq(1, smartlist_len(sl));
|
|
|
|
test_streq("abc", smartlist_get(sl, 0));
|
2004-09-02 20:22:51 +02:00
|
|
|
smartlist_split_string(sl, "a::bc::", "::", 0, 0);
|
2004-08-17 23:06:36 +02:00
|
|
|
test_eq(4, smartlist_len(sl));
|
|
|
|
test_streq("a", smartlist_get(sl, 1));
|
|
|
|
test_streq("bc", smartlist_get(sl, 2));
|
|
|
|
test_streq("", smartlist_get(sl, 3));
|
2004-11-03 19:33:07 +01:00
|
|
|
cp = smartlist_join_strings(sl, "", 0, NULL);
|
2004-10-14 21:51:47 +02:00
|
|
|
test_streq(cp, "abcabc");
|
|
|
|
tor_free(cp);
|
2004-11-03 19:33:07 +01:00
|
|
|
cp = smartlist_join_strings(sl, "!", 0, NULL);
|
2004-10-14 21:51:47 +02:00
|
|
|
test_streq(cp, "abc!a!bc!");
|
|
|
|
tor_free(cp);
|
2004-11-03 19:33:07 +01:00
|
|
|
cp = smartlist_join_strings(sl, "XY", 0, NULL);
|
2004-10-14 21:51:47 +02:00
|
|
|
test_streq(cp, "abcXYaXYbcXY");
|
|
|
|
tor_free(cp);
|
2004-11-03 19:33:07 +01:00
|
|
|
cp = smartlist_join_strings(sl, "XY", 1, NULL);
|
2004-10-14 21:51:47 +02:00
|
|
|
test_streq(cp, "abcXYaXYbcXYXY");
|
|
|
|
tor_free(cp);
|
2004-11-03 19:33:07 +01:00
|
|
|
cp = smartlist_join_strings(sl, "", 1, NULL);
|
2004-10-14 21:51:47 +02:00
|
|
|
test_streq(cp, "abcabc");
|
|
|
|
tor_free(cp);
|
|
|
|
|
2004-09-02 20:22:51 +02:00
|
|
|
smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
|
2004-08-17 23:06:36 +02:00
|
|
|
test_eq(8, smartlist_len(sl));
|
|
|
|
test_streq("", smartlist_get(sl, 4));
|
|
|
|
test_streq("def", smartlist_get(sl, 5));
|
|
|
|
test_streq(" ", smartlist_get(sl, 6));
|
|
|
|
test_streq("ghijk", smartlist_get(sl, 7));
|
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
|
|
smartlist_clear(sl);
|
|
|
|
|
2004-09-02 20:22:51 +02:00
|
|
|
smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
|
2004-08-17 23:06:36 +02:00
|
|
|
test_eq(3, smartlist_len(sl));
|
|
|
|
test_streq("a", smartlist_get(sl,0));
|
|
|
|
test_streq("bbd", smartlist_get(sl,1));
|
|
|
|
test_streq("cdef", smartlist_get(sl,2));
|
2005-12-14 21:40:40 +01:00
|
|
|
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
|
|
|
|
SPLIT_SKIP_SPACE, 0);
|
2004-08-17 23:06:36 +02:00
|
|
|
test_eq(8, smartlist_len(sl));
|
|
|
|
test_streq("z", smartlist_get(sl,3));
|
|
|
|
test_streq("zhasd", smartlist_get(sl,4));
|
|
|
|
test_streq("", smartlist_get(sl,5));
|
|
|
|
test_streq("bnud", smartlist_get(sl,6));
|
|
|
|
test_streq("", smartlist_get(sl,7));
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
2004-09-02 20:22:51 +02:00
|
|
|
smartlist_clear(sl);
|
|
|
|
|
2004-12-16 22:10:51 +01:00
|
|
|
smartlist_split_string(sl, " ab\tc \td ef ", NULL,
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
test_eq(4, smartlist_len(sl));
|
|
|
|
test_streq("ab", smartlist_get(sl,0));
|
|
|
|
test_streq("c", smartlist_get(sl,1));
|
|
|
|
test_streq("d", smartlist_get(sl,2));
|
|
|
|
test_streq("ef", smartlist_get(sl,3));
|
|
|
|
smartlist_split_string(sl, "ghi\tj", NULL,
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
test_eq(6, smartlist_len(sl));
|
|
|
|
test_streq("ghi", smartlist_get(sl,4));
|
|
|
|
test_streq("j", smartlist_get(sl,5));
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
|
|
smartlist_clear(sl);
|
|
|
|
|
2005-07-19 01:36:12 +02:00
|
|
|
cp = smartlist_join_strings(sl, "XY", 0, NULL);
|
|
|
|
test_streq(cp, "");
|
|
|
|
tor_free(cp);
|
|
|
|
cp = smartlist_join_strings(sl, "XY", 1, NULL);
|
|
|
|
test_streq(cp, "XY");
|
|
|
|
tor_free(cp);
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2004-09-02 20:22:51 +02:00
|
|
|
test_eq(3, smartlist_len(sl));
|
|
|
|
test_streq("z", smartlist_get(sl, 0));
|
|
|
|
test_streq("zhasd", smartlist_get(sl, 1));
|
|
|
|
test_streq("bnud", smartlist_get(sl, 2));
|
2005-12-14 21:40:40 +01:00
|
|
|
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
|
2004-09-02 20:22:51 +02:00
|
|
|
test_eq(5, smartlist_len(sl));
|
|
|
|
test_streq("z", smartlist_get(sl, 3));
|
|
|
|
test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
|
2004-10-14 21:51:47 +02:00
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
|
|
smartlist_clear(sl);
|
2004-11-07 23:58:16 +01:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
smartlist_split_string(sl, "abcd\n", "\n",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2004-11-07 23:58:16 +01:00
|
|
|
test_eq(1, smartlist_len(sl));
|
|
|
|
test_streq("abcd", smartlist_get(sl, 0));
|
2005-12-14 21:40:40 +01:00
|
|
|
smartlist_split_string(sl, "efgh", "\n",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2004-11-07 23:58:16 +01:00
|
|
|
test_eq(2, smartlist_len(sl));
|
|
|
|
test_streq("efgh", smartlist_get(sl, 1));
|
|
|
|
|
2005-09-10 01:12:54 +02:00
|
|
|
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
|
|
|
smartlist_clear(sl);
|
|
|
|
|
|
|
|
/* Test smartlist sorting. */
|
|
|
|
smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
|
|
|
|
test_eq(7, smartlist_len(sl));
|
|
|
|
smartlist_sort(sl, _compare_strs);
|
|
|
|
cp = smartlist_join_strings(sl, ",", 0, NULL);
|
|
|
|
test_streq(cp,"and,arma,by,nickm,onion,router,the");
|
|
|
|
tor_free(cp);
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
|
|
|
|
_compare_without_first_ch));
|
2005-09-10 01:12:54 +02:00
|
|
|
test_streq("and", smartlist_bsearch(sl, " AND", _compare_without_first_ch));
|
|
|
|
test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", _compare_without_first_ch));
|
|
|
|
|
2006-06-18 09:35:10 +02:00
|
|
|
/* Test reverse() and pop_last() */
|
|
|
|
smartlist_reverse(sl);
|
|
|
|
cp = smartlist_join_strings(sl, ",", 0, NULL);
|
|
|
|
test_streq(cp,"the,router,onion,nickm,by,arma,and");
|
|
|
|
tor_free(cp);
|
|
|
|
cp = smartlist_pop_last(sl);
|
|
|
|
test_streq(cp, "and");
|
|
|
|
tor_free(cp);
|
|
|
|
test_eq(smartlist_len(sl), 6);
|
|
|
|
|
2004-10-06 15:31:48 +02:00
|
|
|
/* Test tor_strstrip() */
|
|
|
|
strcpy(buf, "Testing 1 2 3");
|
|
|
|
test_eq(0, tor_strstrip(buf, ",!"));
|
|
|
|
test_streq(buf, "Testing 1 2 3");
|
|
|
|
strcpy(buf, "!Testing 1 2 3?");
|
|
|
|
test_eq(5, tor_strstrip(buf, "!? "));
|
|
|
|
test_streq(buf, "Testing123");
|
2004-08-17 23:06:36 +02:00
|
|
|
|
2004-10-07 23:10:40 +02:00
|
|
|
/* Test tor_strpartition() */
|
2004-10-07 23:37:06 +02:00
|
|
|
test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
|
|
|
|
TERMINATE_IF_EVEN));
|
2004-10-07 23:10:40 +02:00
|
|
|
test_streq(buf, "abc##def##g");
|
2004-10-07 23:37:06 +02:00
|
|
|
test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
|
|
|
|
ALWAYS_TERMINATE));
|
|
|
|
test_streq(buf, "abc##def##g##");
|
|
|
|
test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
|
|
|
|
TERMINATE_IF_EVEN));
|
2004-10-07 23:10:40 +02:00
|
|
|
test_streq(buf, "abc##def##ghi##");
|
2004-10-07 23:37:06 +02:00
|
|
|
test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
|
|
|
|
NEVER_TERMINATE));
|
|
|
|
test_streq(buf, "abc##def##ghi");
|
2004-10-07 23:10:40 +02:00
|
|
|
|
2004-10-12 17:48:30 +02:00
|
|
|
/* Test parse_addr_port */
|
|
|
|
cp = NULL; u32 = 3; u16 = 3;
|
2006-07-06 04:44:07 +02:00
|
|
|
test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
|
2004-10-12 17:48:30 +02:00
|
|
|
test_streq(cp, "1.2.3.4");
|
2004-10-16 23:57:24 +02:00
|
|
|
test_eq(u32, 0x01020304u);
|
2004-10-12 17:48:30 +02:00
|
|
|
test_eq(u16, 0);
|
|
|
|
tor_free(cp);
|
2006-07-06 04:44:07 +02:00
|
|
|
test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
|
2004-10-12 17:48:30 +02:00
|
|
|
test_streq(cp, "4.3.2.1");
|
2004-10-16 23:57:24 +02:00
|
|
|
test_eq(u32, 0x04030201u);
|
2004-10-12 17:48:30 +02:00
|
|
|
test_eq(u16, 99);
|
|
|
|
tor_free(cp);
|
2006-07-06 04:44:07 +02:00
|
|
|
test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
|
|
|
|
&cp, NULL, &u16));
|
2004-10-12 17:48:30 +02:00
|
|
|
test_streq(cp, "nonexistent.address");
|
|
|
|
test_eq(u16, 4040);
|
|
|
|
tor_free(cp);
|
2006-07-06 04:44:07 +02:00
|
|
|
test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
|
2004-10-12 17:48:30 +02:00
|
|
|
test_streq(cp, "localhost");
|
2004-10-16 23:57:24 +02:00
|
|
|
test_eq(u32, 0x7f000001u);
|
2004-10-12 17:48:30 +02:00
|
|
|
test_eq(u16, 9999);
|
|
|
|
tor_free(cp);
|
|
|
|
u32 = 3;
|
2006-07-06 04:44:07 +02:00
|
|
|
test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
|
2004-10-12 17:48:30 +02:00
|
|
|
test_eq(cp, NULL);
|
2004-10-16 23:57:24 +02:00
|
|
|
test_eq(u32, 0x7f000001u);
|
2004-10-12 17:48:30 +02:00
|
|
|
test_eq(u16, 0);
|
|
|
|
tor_free(cp);
|
2006-02-13 01:26:43 +01:00
|
|
|
test_eq(0, addr_mask_get_bits(0x0u));
|
|
|
|
test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
|
|
|
|
test_eq(16, addr_mask_get_bits(0xFFFF0000u));
|
|
|
|
test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
|
|
|
|
test_eq(1, addr_mask_get_bits(0x80000000u));
|
2004-10-12 17:48:30 +02:00
|
|
|
|
2004-10-12 21:33:03 +02:00
|
|
|
/* Test tor_parse_long. */
|
|
|
|
test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
|
|
|
|
test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
|
|
|
|
|
2004-11-04 23:29:45 +01:00
|
|
|
/* Test parse_line_from_str */
|
2004-11-28 10:14:07 +01:00
|
|
|
strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
|
2004-11-04 23:29:45 +01:00
|
|
|
"k2\n"
|
2004-11-28 10:14:07 +01:00
|
|
|
"k3 \n" "\n" " \n" "#comment\n"
|
2004-11-04 23:29:45 +01:00
|
|
|
"k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
|
|
|
|
cp = buf;
|
|
|
|
|
|
|
|
cp = parse_line_from_str(cp, &k, &v);
|
|
|
|
test_streq(k, "k");
|
|
|
|
test_streq(v, "v");
|
|
|
|
test_assert(!strcmpstart(cp, " key value with"));
|
|
|
|
|
|
|
|
cp = parse_line_from_str(cp, &k, &v);
|
|
|
|
test_streq(k, "key");
|
|
|
|
test_streq(v, "value with spaces");
|
|
|
|
test_assert(!strcmpstart(cp, "keykey"));
|
|
|
|
|
|
|
|
cp = parse_line_from_str(cp, &k, &v);
|
|
|
|
test_streq(k, "keykey");
|
|
|
|
test_streq(v, "val");
|
|
|
|
test_assert(!strcmpstart(cp, "k2\n"));
|
|
|
|
|
|
|
|
cp = parse_line_from_str(cp, &k, &v);
|
|
|
|
test_streq(k, "k2");
|
|
|
|
test_streq(v, "");
|
|
|
|
test_assert(!strcmpstart(cp, "k3 \n"));
|
|
|
|
|
|
|
|
cp = parse_line_from_str(cp, &k, &v);
|
|
|
|
test_streq(k, "k3");
|
|
|
|
test_streq(v, "");
|
|
|
|
test_assert(!strcmpstart(cp, "\n \n"));
|
|
|
|
|
|
|
|
cp = parse_line_from_str(cp, &k, &v);
|
|
|
|
test_streq(k, "k4");
|
|
|
|
test_streq(v, "");
|
|
|
|
test_assert(!strcmpstart(cp, "k5#abc"));
|
|
|
|
|
|
|
|
cp = parse_line_from_str(cp, &k, &v);
|
|
|
|
test_streq(k, "k5");
|
|
|
|
test_streq(v, "");
|
|
|
|
test_assert(!strcmpstart(cp, "k6"));
|
|
|
|
|
|
|
|
cp = parse_line_from_str(cp, &k, &v);
|
|
|
|
test_streq(k, "k6");
|
|
|
|
test_streq(v, "val");
|
|
|
|
test_streq(cp, "");
|
|
|
|
|
2004-11-09 19:22:17 +01:00
|
|
|
/* Test for strcmpstart and strcmpend. */
|
|
|
|
test_assert(strcmpstart("abcdef", "abcdef")==0);
|
|
|
|
test_assert(strcmpstart("abcdef", "abc")==0);
|
|
|
|
test_assert(strcmpstart("abcdef", "abd")<0);
|
|
|
|
test_assert(strcmpstart("abcdef", "abb")>0);
|
|
|
|
test_assert(strcmpstart("ab", "abb")<0);
|
|
|
|
|
|
|
|
test_assert(strcmpend("abcdef", "abcdef")==0);
|
|
|
|
test_assert(strcmpend("abcdef", "def")==0);
|
|
|
|
test_assert(strcmpend("abcdef", "deg")<0);
|
|
|
|
test_assert(strcmpend("abcdef", "dee")>0);
|
|
|
|
test_assert(strcmpend("ab", "abb")<0);
|
|
|
|
|
2005-02-22 09:18:36 +01:00
|
|
|
{
|
|
|
|
char tmpbuf[INET_NTOA_BUF_LEN];
|
|
|
|
struct in_addr in;
|
|
|
|
tor_inet_aton("18.244.0.188",&in);
|
|
|
|
tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
|
|
|
|
test_streq(tmpbuf, "18.244.0.188");
|
|
|
|
}
|
|
|
|
|
2006-03-05 10:50:26 +01:00
|
|
|
/* Test 'escaped' */
|
|
|
|
test_streq("\"\"", escaped(""));
|
|
|
|
test_streq("\"abcd\"", escaped("abcd"));
|
|
|
|
test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
|
|
|
|
test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
|
|
|
|
|
2004-04-07 21:46:27 +02:00
|
|
|
/* XXXX test older functions. */
|
|
|
|
smartlist_free(sl);
|
2003-04-17 01:21:44 +02:00
|
|
|
}
|
|
|
|
|
2006-07-31 19:59:37 +02:00
|
|
|
static int
|
|
|
|
_compare_strings_for_pqueue(const void *s1, const void *s2)
|
|
|
|
{
|
|
|
|
return strcmp((const char*)s1, (const char*)s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_pqueue(void)
|
|
|
|
{
|
|
|
|
smartlist_t *sl;
|
|
|
|
int (*cmp)(const void *, const void*);
|
|
|
|
#define OK() smartlist_pqueue_assert_ok(sl, cmp)
|
|
|
|
|
|
|
|
cmp = _compare_strings_for_pqueue;
|
|
|
|
|
|
|
|
sl = smartlist_create();
|
2006-08-11 09:09:17 +02:00
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"cows");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"zebras");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"fish");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"frogs");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"apples");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"squid");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"daschunds");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"eggplants");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"weissbier");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"lobsters");
|
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"roquefort");
|
2006-07-31 19:59:37 +02:00
|
|
|
|
|
|
|
OK();
|
|
|
|
|
|
|
|
test_eq(smartlist_len(sl), 11);
|
|
|
|
test_streq(smartlist_get(sl, 0), "apples");
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "apples");
|
|
|
|
test_eq(smartlist_len(sl), 10);
|
|
|
|
OK();
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "cows");
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "daschunds");
|
2006-08-11 09:09:17 +02:00
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"chinchillas");
|
2006-07-31 19:59:37 +02:00
|
|
|
OK();
|
2006-08-11 09:09:17 +02:00
|
|
|
smartlist_pqueue_add(sl, cmp, (char*)"fireflies");
|
2006-07-31 19:59:37 +02:00
|
|
|
OK();
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "chinchillas");
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "eggplants");
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "fireflies");
|
|
|
|
OK();
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "fish");
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "frogs");
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "lobsters");
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "roquefort");
|
|
|
|
OK();
|
|
|
|
test_eq(smartlist_len(sl), 3);
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "squid");
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "weissbier");
|
|
|
|
test_streq(smartlist_pqueue_pop(sl, cmp), "zebras");
|
|
|
|
test_eq(smartlist_len(sl), 0);
|
|
|
|
OK();
|
|
|
|
#undef OK
|
|
|
|
smartlist_free(sl);
|
|
|
|
}
|
|
|
|
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2004-10-27 20:16:37 +02:00
|
|
|
test_gzip(void)
|
|
|
|
{
|
2006-06-18 09:35:10 +02:00
|
|
|
char *buf1, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
|
|
|
|
const char *ccp2;
|
2004-09-02 20:22:51 +02:00
|
|
|
size_t len1, len2;
|
2006-06-18 09:35:10 +02:00
|
|
|
tor_zlib_state_t *state;
|
2004-09-02 20:22:51 +02:00
|
|
|
|
2005-08-29 20:01:38 +02:00
|
|
|
buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
|
2006-10-09 05:39:06 +02:00
|
|
|
test_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
|
2004-09-02 20:22:51 +02:00
|
|
|
if (is_gzip_supported()) {
|
|
|
|
test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
|
|
|
|
GZIP_METHOD));
|
|
|
|
test_assert(buf2);
|
2006-10-09 05:39:06 +02:00
|
|
|
test_assert(!memcmp(buf2, "\037\213", 2)); /* Gztip magic. */
|
|
|
|
test_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
|
2004-09-02 20:22:51 +02:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
|
2006-02-13 00:44:02 +01:00
|
|
|
GZIP_METHOD, 1, LOG_INFO));
|
2004-09-02 20:22:51 +02:00
|
|
|
test_assert(buf3);
|
|
|
|
test_streq(buf1,buf3);
|
|
|
|
|
|
|
|
tor_free(buf2);
|
|
|
|
tor_free(buf3);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
|
|
|
|
ZLIB_METHOD));
|
|
|
|
test_assert(buf2);
|
|
|
|
test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
|
2006-10-09 05:39:06 +02:00
|
|
|
test_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
|
2004-09-02 20:22:51 +02:00
|
|
|
|
2006-02-13 00:44:02 +01:00
|
|
|
test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
|
|
|
|
ZLIB_METHOD, 1, LOG_INFO));
|
2004-09-02 20:22:51 +02:00
|
|
|
test_assert(buf3);
|
|
|
|
test_streq(buf1,buf3);
|
|
|
|
|
2005-10-14 00:48:09 +02:00
|
|
|
/* Check whether we can uncompress concatenated, compresed strings. */
|
2005-08-29 20:01:38 +02:00
|
|
|
tor_free(buf3);
|
|
|
|
buf2 = tor_realloc(buf2, len1*2);
|
|
|
|
memcpy(buf2+len1, buf2, len1);
|
2005-12-14 21:40:40 +01:00
|
|
|
test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
|
2006-02-13 00:44:02 +01:00
|
|
|
ZLIB_METHOD, 1, LOG_INFO));
|
2005-08-29 20:01:38 +02:00
|
|
|
test_eq(len2, (strlen(buf1)+1)*2);
|
|
|
|
test_memeq(buf3,
|
|
|
|
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
|
|
|
|
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
|
|
|
|
(strlen(buf1)+1)*2);
|
|
|
|
|
2005-10-14 00:48:09 +02:00
|
|
|
tor_free(buf1);
|
|
|
|
tor_free(buf2);
|
|
|
|
tor_free(buf3);
|
|
|
|
|
|
|
|
/* Check whether we can uncompress partial strings. */
|
2005-12-14 21:40:40 +01:00
|
|
|
buf1 =
|
|
|
|
tor_strdup("String with low redundancy that won't be compressed much.");
|
|
|
|
test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
|
|
|
|
ZLIB_METHOD));
|
2005-10-14 00:48:09 +02:00
|
|
|
tor_assert(len1>16);
|
|
|
|
/* when we allow an uncomplete string, we should succeed.*/
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
|
2006-02-13 00:44:02 +01:00
|
|
|
ZLIB_METHOD, 0, LOG_INFO));
|
2005-10-14 00:48:09 +02:00
|
|
|
buf3[len2]='\0';
|
|
|
|
tor_assert(len2 > 5);
|
|
|
|
tor_assert(!strcmpstart(buf1, buf3));
|
|
|
|
|
|
|
|
/* when we demand a complete string, this must fail. */
|
|
|
|
tor_free(buf3);
|
2006-02-13 00:44:02 +01:00
|
|
|
tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
|
|
|
|
ZLIB_METHOD, 1, LOG_INFO));
|
2005-10-14 00:48:09 +02:00
|
|
|
tor_assert(!buf3);
|
|
|
|
|
2006-06-18 09:35:10 +02:00
|
|
|
/* Now, try streaming compression. */
|
|
|
|
tor_free(buf1);
|
|
|
|
tor_free(buf2);
|
|
|
|
tor_free(buf3);
|
|
|
|
state = tor_zlib_new(1, ZLIB_METHOD);
|
|
|
|
tor_assert(state);
|
|
|
|
cp1 = buf1 = tor_malloc(1024);
|
|
|
|
len1 = 1024;
|
|
|
|
ccp2 = "ABCDEFGHIJABCDEFGHIJ";
|
|
|
|
len2 = 21;
|
2006-06-22 09:34:04 +02:00
|
|
|
test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
|
|
|
|
== TOR_ZLIB_OK);
|
2006-06-18 09:35:10 +02:00
|
|
|
test_eq(len2, 0); /* Make sure we compressed it all. */
|
|
|
|
test_assert(cp1 > buf1);
|
|
|
|
|
|
|
|
len2 = 0;
|
|
|
|
cp2 = cp1;
|
2006-06-22 09:34:04 +02:00
|
|
|
test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
|
|
|
|
== TOR_ZLIB_DONE);
|
2006-06-18 09:35:10 +02:00
|
|
|
test_eq(len2, 0);
|
|
|
|
test_assert(cp1 > cp2); /* Make sure we really added something. */
|
|
|
|
|
|
|
|
tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
|
|
|
|
ZLIB_METHOD, 1, LOG_WARN));
|
|
|
|
test_streq(buf3, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
|
|
|
|
tor_free(buf3);
|
|
|
|
|
|
|
|
tor_zlib_free(state);
|
|
|
|
|
2004-09-02 20:22:51 +02:00
|
|
|
tor_free(buf2);
|
|
|
|
tor_free(buf3);
|
|
|
|
tor_free(buf1);
|
|
|
|
}
|
|
|
|
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
|
|
|
test_strmap(void)
|
2004-10-27 20:16:37 +02:00
|
|
|
{
|
2004-03-19 23:07:24 +01:00
|
|
|
strmap_t *map;
|
2005-11-24 02:47:05 +01:00
|
|
|
// strmap_iter_t *iter;
|
|
|
|
// const char *k;
|
2004-03-19 23:07:24 +01:00
|
|
|
void *v;
|
|
|
|
|
|
|
|
map = strmap_new();
|
|
|
|
v = strmap_set(map, "K1", (void*)99);
|
|
|
|
test_eq(v, NULL);
|
|
|
|
v = strmap_set(map, "K2", (void*)101);
|
|
|
|
test_eq(v, NULL);
|
|
|
|
v = strmap_set(map, "K1", (void*)100);
|
|
|
|
test_eq(v, (void*)99);
|
2005-08-12 19:24:53 +02:00
|
|
|
test_eq_ptr(strmap_get(map,"K1"), (void*)100);
|
|
|
|
test_eq_ptr(strmap_get(map,"K2"), (void*)101);
|
|
|
|
test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
|
2006-07-31 20:01:27 +02:00
|
|
|
strmap_assert_ok(map);
|
2004-03-19 23:07:24 +01:00
|
|
|
|
|
|
|
v = strmap_remove(map,"K2");
|
2006-07-31 20:01:27 +02:00
|
|
|
strmap_assert_ok(map);
|
2005-08-12 19:24:53 +02:00
|
|
|
test_eq_ptr(v, (void*)101);
|
|
|
|
test_eq_ptr(strmap_get(map,"K2"), NULL);
|
|
|
|
test_eq_ptr(strmap_remove(map,"K2"), NULL);
|
2004-03-19 23:07:24 +01:00
|
|
|
|
|
|
|
strmap_set(map, "K2", (void*)101);
|
|
|
|
strmap_set(map, "K3", (void*)102);
|
|
|
|
strmap_set(map, "K4", (void*)103);
|
2006-07-31 20:01:27 +02:00
|
|
|
strmap_assert_ok(map);
|
2004-03-19 23:07:24 +01:00
|
|
|
strmap_set(map, "K5", (void*)104);
|
|
|
|
strmap_set(map, "K6", (void*)105);
|
2006-07-31 20:01:27 +02:00
|
|
|
strmap_assert_ok(map);
|
2004-03-19 23:07:24 +01:00
|
|
|
|
2005-11-23 05:18:45 +01:00
|
|
|
#if 0
|
2004-03-19 23:07:24 +01:00
|
|
|
iter = strmap_iter_init(map);
|
|
|
|
strmap_iter_get(iter,&k,&v);
|
|
|
|
test_streq(k, "K1");
|
|
|
|
test_eq(v, (void*)10000);
|
|
|
|
iter = strmap_iter_next(map,iter);
|
|
|
|
strmap_iter_get(iter,&k,&v);
|
|
|
|
test_streq(k, "K2");
|
|
|
|
test_eq(v, (void*)10201);
|
|
|
|
iter = strmap_iter_next_rmv(map,iter);
|
|
|
|
strmap_iter_get(iter,&k,&v);
|
|
|
|
test_streq(k, "K3");
|
|
|
|
test_eq(v, (void*)10404);
|
|
|
|
iter = strmap_iter_next(map,iter); /* K5 */
|
|
|
|
test_assert(!strmap_iter_done(iter));
|
|
|
|
iter = strmap_iter_next(map,iter); /* K6 */
|
|
|
|
test_assert(!strmap_iter_done(iter));
|
|
|
|
iter = strmap_iter_next(map,iter); /* done */
|
|
|
|
test_assert(strmap_iter_done(iter));
|
|
|
|
|
|
|
|
/* Make sure we removed K2, but not the others. */
|
2005-08-12 19:24:53 +02:00
|
|
|
test_eq_ptr(strmap_get(map, "K2"), NULL);
|
|
|
|
test_eq_ptr(strmap_get(map, "K5"), (void*)10816);
|
2005-11-23 05:18:45 +01:00
|
|
|
#endif
|
|
|
|
|
2006-07-31 20:01:27 +02:00
|
|
|
strmap_assert_ok(map);
|
2004-03-19 23:07:24 +01:00
|
|
|
/* Clean up after ourselves. */
|
|
|
|
strmap_free(map, NULL);
|
2004-03-20 02:21:19 +01:00
|
|
|
|
|
|
|
/* Now try some lc functions. */
|
|
|
|
map = strmap_new();
|
|
|
|
strmap_set_lc(map,"Ab.C", (void*)1);
|
2005-08-12 19:24:53 +02:00
|
|
|
test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
|
2006-07-31 20:01:27 +02:00
|
|
|
strmap_assert_ok(map);
|
2005-08-12 19:24:53 +02:00
|
|
|
test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
|
|
|
|
test_eq_ptr(strmap_get(map,"AB.C"), NULL);
|
|
|
|
test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
|
2006-07-31 20:01:27 +02:00
|
|
|
strmap_assert_ok(map);
|
2005-08-12 19:24:53 +02:00
|
|
|
test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
|
2004-03-20 02:21:19 +01:00
|
|
|
strmap_free(map,NULL);
|
2004-03-19 23:07:24 +01:00
|
|
|
}
|
|
|
|
|
2005-07-15 21:31:11 +02:00
|
|
|
static void
|
|
|
|
test_control_formats(void)
|
|
|
|
{
|
|
|
|
char *out;
|
2005-07-22 23:12:10 +02:00
|
|
|
const char *inp =
|
2005-07-15 21:31:11 +02:00
|
|
|
"..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
sz = read_escaped_data(inp, strlen(inp), 1, &out);
|
|
|
|
test_streq(out,
|
|
|
|
".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
|
2006-10-09 04:35:51 +02:00
|
|
|
test_eq(sz, strlen(out));
|
2005-07-15 21:31:11 +02:00
|
|
|
|
|
|
|
tor_free(out);
|
|
|
|
}
|
|
|
|
|
2004-11-09 21:04:00 +01:00
|
|
|
static void
|
2004-11-02 04:02:17 +01:00
|
|
|
test_onion(void)
|
2004-10-27 20:16:37 +02:00
|
|
|
{
|
2003-12-13 08:01:46 +01:00
|
|
|
#if 0
|
2003-11-12 20:34:34 +01:00
|
|
|
char **names;
|
|
|
|
int i,num;
|
|
|
|
|
2004-04-07 21:57:40 +02:00
|
|
|
names = parse_nickname_list(" foo bar\t baz quux ", &num);
|
2003-12-17 22:09:31 +01:00
|
|
|
test_eq(num,4);
|
2003-11-12 20:34:34 +01:00
|
|
|
test_streq(names[0],"foo");
|
|
|
|
test_streq(names[1],"bar");
|
|
|
|
test_streq(names[2],"baz");
|
|
|
|
test_streq(names[3],"quux");
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i=0;i<num;i++)
|
2003-11-12 20:34:34 +01:00
|
|
|
tor_free(names[i]);
|
|
|
|
tor_free(names);
|
2003-12-13 08:01:46 +01:00
|
|
|
#endif
|
2003-11-12 20:34:34 +01:00
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
crypto_dh_env_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 */
|
|
|
|
crypto_pk_env_t *pk = NULL;
|
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
pk = crypto_new_pk_env();
|
2003-05-05 06:27:00 +02:00
|
|
|
test_assert(! crypto_pk_generate_key(pk));
|
|
|
|
|
|
|
|
/* 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-05-05 06:27:00 +02:00
|
|
|
crypto_dh_free(c_dh);
|
|
|
|
|
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);
|
2003-06-13 23:23:14 +02:00
|
|
|
crypto_free_pk_env(pk);
|
2003-05-05 06:27:00 +02:00
|
|
|
}
|
|
|
|
|
2006-08-11 09:09:17 +02:00
|
|
|
extern smartlist_t *fingerprint_list;
|
|
|
|
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
2004-10-27 20:16:37 +02:00
|
|
|
test_dir_format(void)
|
2003-05-06 19:38:16 +02:00
|
|
|
{
|
2003-09-25 07:17:11 +02:00
|
|
|
char buf[8192], buf2[8192];
|
2004-04-08 22:22:01 +02:00
|
|
|
char platform[256];
|
2004-07-22 22:34:55 +02:00
|
|
|
char fingerprint[FINGERPRINT_LEN+1];
|
2003-09-25 07:17:11 +02:00
|
|
|
char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
|
2004-10-14 04:47:09 +02:00
|
|
|
size_t pk1_str_len, pk2_str_len, pk3_str_len;
|
2003-05-06 19:38:16 +02:00
|
|
|
routerinfo_t r1, r2;
|
2003-09-25 07:17:11 +02:00
|
|
|
crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
|
|
|
|
routerinfo_t *rp1 = NULL, *rp2 = NULL;
|
2004-12-04 02:14:36 +01:00
|
|
|
addr_policy_t ex1, ex2;
|
2003-12-05 10:51:49 +01:00
|
|
|
routerlist_t *dir1 = NULL, *dir2 = NULL;
|
2004-07-15 03:59:18 +02:00
|
|
|
tor_version_t ver1;
|
2004-08-09 06:26:25 +02:00
|
|
|
char *bw_lines = NULL;
|
2003-05-06 19:38:16 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
test_assert( (pk1 = crypto_new_pk_env()) );
|
|
|
|
test_assert( (pk2 = crypto_new_pk_env()) );
|
|
|
|
test_assert( (pk3 = crypto_new_pk_env()) );
|
2003-05-06 19:38:16 +02:00
|
|
|
test_assert(! crypto_pk_generate_key(pk1));
|
|
|
|
test_assert(! crypto_pk_generate_key(pk2));
|
2003-09-25 07:17:11 +02:00
|
|
|
test_assert(! crypto_pk_generate_key(pk3));
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2004-08-18 06:44:24 +02:00
|
|
|
test_assert( is_legal_nickname("a"));
|
|
|
|
test_assert(!is_legal_nickname(""));
|
|
|
|
test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
|
|
|
|
test_assert(!is_legal_nickname("hyphen-")); /* bad char */
|
|
|
|
test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
|
|
|
|
test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
|
|
|
|
/* valid */
|
|
|
|
test_assert( is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
|
2006-10-03 20:58:40 +02:00
|
|
|
test_assert( is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
|
|
|
|
test_assert( is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
|
2004-08-18 06:44:24 +02:00
|
|
|
/* too short */
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
|
|
|
|
/* illegal char */
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
|
2006-10-03 20:58:40 +02:00
|
|
|
/* hex part too long */
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
|
|
|
|
/* Bad nickname */
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
|
|
|
|
"abcdefghijklmnoppqrst"));
|
|
|
|
/* Bad extra char. */
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest(
|
|
|
|
"$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
|
2004-08-18 06:44:24 +02:00
|
|
|
test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
|
|
|
|
test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
|
|
|
|
test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
|
|
|
|
|
2004-04-08 22:22:01 +02:00
|
|
|
get_platform_str(platform, sizeof(platform));
|
2004-10-17 17:45:54 +02:00
|
|
|
memset(&r1,0,sizeof(r1));
|
|
|
|
memset(&r2,0,sizeof(r2));
|
2005-01-07 02:12:30 +01:00
|
|
|
r1.address = tor_strdup("18.244.0.1");
|
2003-05-06 19:38:16 +02:00
|
|
|
r1.addr = 0xc0a80001u; /* 192.168.0.1 */
|
2005-11-05 21:15:27 +01:00
|
|
|
r1.cache_info.published_on = 0;
|
2003-05-06 19:38:16 +02:00
|
|
|
r1.or_port = 9000;
|
|
|
|
r1.dir_port = 9003;
|
2003-09-25 07:17:11 +02:00
|
|
|
r1.onion_pkey = pk1;
|
|
|
|
r1.identity_pkey = pk2;
|
2004-04-25 00:17:50 +02:00
|
|
|
r1.bandwidthrate = 1000;
|
|
|
|
r1.bandwidthburst = 5000;
|
2004-08-15 10:15:12 +02:00
|
|
|
r1.bandwidthcapacity = 10000;
|
2003-05-06 19:38:16 +02:00
|
|
|
r1.exit_policy = NULL;
|
2004-11-02 04:02:17 +01:00
|
|
|
r1.nickname = tor_strdup("Magri");
|
2004-04-08 22:22:01 +02:00
|
|
|
r1.platform = tor_strdup(platform);
|
2003-05-06 19:38:16 +02:00
|
|
|
|
2004-11-12 20:39:13 +01:00
|
|
|
ex1.policy_type = ADDR_POLICY_ACCEPT;
|
2003-05-06 19:38:16 +02:00
|
|
|
ex1.string = NULL;
|
2003-11-14 21:45:47 +01:00
|
|
|
ex1.addr = 0;
|
|
|
|
ex1.msk = 0;
|
2003-12-13 03:44:02 +01:00
|
|
|
ex1.prt_min = ex1.prt_max = 80;
|
2003-05-06 19:38:16 +02:00
|
|
|
ex1.next = &ex2;
|
2004-11-12 20:39:13 +01:00
|
|
|
ex2.policy_type = ADDR_POLICY_REJECT;
|
2003-11-14 21:45:47 +01:00
|
|
|
ex2.addr = 18 << 24;
|
|
|
|
ex2.msk = 0xFF000000u;
|
2004-10-12 21:33:03 +02:00
|
|
|
ex2.prt_min = ex2.prt_max = 24;
|
2003-05-06 19:38:16 +02:00
|
|
|
ex2.next = NULL;
|
2005-01-07 02:12:30 +01:00
|
|
|
r2.address = tor_strdup("1.1.1.1");
|
2003-05-06 19:38:16 +02:00
|
|
|
r2.addr = 0x0a030201u; /* 10.3.2.1 */
|
2004-04-14 21:51:57 +02:00
|
|
|
r2.platform = tor_strdup(platform);
|
2005-11-05 21:15:27 +01:00
|
|
|
r2.cache_info.published_on = 5;
|
2003-05-06 19:38:16 +02:00
|
|
|
r2.or_port = 9005;
|
|
|
|
r2.dir_port = 0;
|
2003-09-25 07:17:11 +02:00
|
|
|
r2.onion_pkey = pk2;
|
|
|
|
r2.identity_pkey = pk1;
|
2004-08-15 10:15:12 +02:00
|
|
|
r2.bandwidthrate = r2.bandwidthburst = r2.bandwidthcapacity = 3000;
|
2003-05-06 19:38:16 +02:00
|
|
|
r2.exit_policy = &ex1;
|
2004-11-02 04:02:17 +01:00
|
|
|
r2.nickname = tor_strdup("Fred");
|
2003-05-06 19:38:16 +02:00
|
|
|
|
2004-08-09 06:26:25 +02:00
|
|
|
bw_lines = rep_hist_get_bandwidth_lines();
|
|
|
|
test_assert(bw_lines);
|
2004-09-24 00:18:50 +02:00
|
|
|
test_assert(!strcmpstart(bw_lines, "opt write-history "));
|
2004-08-09 06:26:25 +02:00
|
|
|
|
2003-12-17 22:09:31 +01:00
|
|
|
test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
|
2003-05-06 19:38:16 +02:00
|
|
|
&pk1_str_len));
|
2003-12-17 22:09:31 +01:00
|
|
|
test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
|
2003-05-06 19:38:16 +02:00
|
|
|
&pk2_str_len));
|
2003-12-17 22:09:31 +01:00
|
|
|
test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
|
2003-09-25 07:17:11 +02:00
|
|
|
&pk3_str_len));
|
2003-10-02 00:31:13 +02:00
|
|
|
|
|
|
|
memset(buf, 0, 2048);
|
|
|
|
test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2005-09-29 08:45:03 +02:00
|
|
|
strcpy(buf2, "router Magri 18.244.0.1 9000 0 0\n"
|
2004-07-20 23:13:11 +02:00
|
|
|
"platform Tor "VERSION" on ");
|
2003-10-02 00:31:13 +02:00
|
|
|
strcat(buf2, get_uname());
|
|
|
|
strcat(buf2, "\n"
|
2003-09-26 20:27:35 +02:00
|
|
|
"published 1970-01-01 00:00:00\n"
|
2004-07-22 22:34:55 +02:00
|
|
|
"opt fingerprint ");
|
2004-10-07 23:37:06 +02:00
|
|
|
test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
|
2004-07-22 22:34:55 +02:00
|
|
|
strcat(buf2, fingerprint);
|
2005-05-02 23:22:31 +02:00
|
|
|
strcat(buf2, "\nuptime 0\n"
|
2004-07-20 23:13:11 +02:00
|
|
|
/* XXX the "0" above is hardcoded, but even if we made it reflect
|
|
|
|
* uptime, that still wouldn't make it right, because the two
|
|
|
|
* descriptors might be made on different seconds... hm. */
|
2004-07-12 20:02:54 +02:00
|
|
|
"bandwidth 1000 5000 10000\n"
|
2003-09-26 20:27:35 +02:00
|
|
|
"onion-key\n");
|
2003-05-06 19:38:16 +02:00
|
|
|
strcat(buf2, pk1_str);
|
2003-09-25 07:17:11 +02:00
|
|
|
strcat(buf2, "signing-key\n");
|
|
|
|
strcat(buf2, pk2_str);
|
2006-09-21 23:49:03 +02:00
|
|
|
#ifdef USE_EVENTDNS
|
|
|
|
strcat(buf2, "opt eventdns 1\n");
|
|
|
|
#else
|
|
|
|
strcat(buf2, "opt eventdns 0\n");
|
|
|
|
#endif
|
2004-08-09 06:26:25 +02:00
|
|
|
strcat(buf2, bw_lines);
|
2003-09-25 07:17:11 +02:00
|
|
|
strcat(buf2, "router-signature\n");
|
2005-12-14 21:40:40 +01:00
|
|
|
buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
|
|
|
|
* twice */
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-05-06 19:38:16 +02:00
|
|
|
test_streq(buf, buf2);
|
2004-08-09 06:26:25 +02:00
|
|
|
tor_free(bw_lines);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-10-02 00:31:13 +02:00
|
|
|
test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
|
2003-05-06 19:38:16 +02:00
|
|
|
cp = buf;
|
2006-06-22 09:01:54 +02:00
|
|
|
rp1 = router_parse_entry_from_string((const char*)cp,NULL,1);
|
2003-05-06 19:38:16 +02:00
|
|
|
test_assert(rp1);
|
|
|
|
test_streq(rp1->address, r1.address);
|
|
|
|
test_eq(rp1->or_port, r1.or_port);
|
2005-09-29 08:45:03 +02:00
|
|
|
//test_eq(rp1->dir_port, r1.dir_port);
|
2004-01-11 08:41:01 +01:00
|
|
|
test_eq(rp1->bandwidthrate, r1.bandwidthrate);
|
2004-04-25 00:17:50 +02:00
|
|
|
test_eq(rp1->bandwidthburst, r1.bandwidthburst);
|
2004-08-15 10:15:12 +02:00
|
|
|
test_eq(rp1->bandwidthcapacity, r1.bandwidthcapacity);
|
2003-09-25 07:17:11 +02:00
|
|
|
test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
|
|
|
|
test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
|
2003-05-06 19:38:16 +02:00
|
|
|
test_assert(rp1->exit_policy == NULL);
|
|
|
|
|
2003-12-17 22:09:31 +01:00
|
|
|
#if 0
|
2003-10-02 00:31:13 +02:00
|
|
|
/* XXX Once we have exit policies, test this again. XXX */
|
2003-08-28 06:21:57 +02:00
|
|
|
strcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n");
|
2003-05-06 19:38:16 +02:00
|
|
|
strcat(buf2, pk2_str);
|
2003-05-07 04:13:23 +02:00
|
|
|
strcat(buf2, "signing-key\n");
|
|
|
|
strcat(buf2, pk1_str);
|
2003-05-06 19:38:16 +02:00
|
|
|
strcat(buf2, "accept *:80\nreject 18.*:24\n\n");
|
2003-10-02 00:31:13 +02:00
|
|
|
test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
|
2003-05-06 19:38:16 +02:00
|
|
|
test_streq(buf, buf2);
|
|
|
|
|
|
|
|
cp = buf;
|
2006-06-22 09:01:54 +02:00
|
|
|
rp2 = router_parse_entry_from_string(&cp,1);
|
2003-05-06 19:38:16 +02:00
|
|
|
test_assert(rp2);
|
|
|
|
test_streq(rp2->address, r2.address);
|
|
|
|
test_eq(rp2->or_port, r2.or_port);
|
|
|
|
test_eq(rp2->dir_port, r2.dir_port);
|
|
|
|
test_eq(rp2->bandwidth, r2.bandwidth);
|
2003-09-25 07:17:11 +02:00
|
|
|
test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
|
|
|
|
test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
|
2003-05-06 19:38:16 +02:00
|
|
|
test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
|
|
|
|
test_streq(rp2->exit_policy->string, "accept *:80");
|
|
|
|
test_streq(rp2->exit_policy->address, "*");
|
|
|
|
test_streq(rp2->exit_policy->port, "80");
|
|
|
|
test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
|
|
|
|
test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
|
|
|
|
test_streq(rp2->exit_policy->next->address, "18.*");
|
|
|
|
test_streq(rp2->exit_policy->next->port, "24");
|
|
|
|
test_assert(rp2->exit_policy->next->next == NULL);
|
2003-09-25 07:17:11 +02:00
|
|
|
#endif
|
2003-05-07 04:13:23 +02:00
|
|
|
|
|
|
|
/* Okay, now for the directories. */
|
2005-06-28 00:25:09 +02:00
|
|
|
{
|
|
|
|
fingerprint_list = smartlist_create();
|
|
|
|
crypto_pk_get_fingerprint(pk2, buf, 1);
|
|
|
|
add_fingerprint_to_dir("Magri", buf, fingerprint_list);
|
|
|
|
crypto_pk_get_fingerprint(pk1, buf, 1);
|
|
|
|
add_fingerprint_to_dir("Fred", buf, fingerprint_list);
|
|
|
|
}
|
2006-01-10 21:47:24 +01:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
char d[DIGEST_LEN];
|
|
|
|
const char *m;
|
2006-04-18 05:07:24 +02:00
|
|
|
/* XXXX NM re-enable. */
|
2004-03-29 21:50:59 +02:00
|
|
|
/* Make sure routers aren't too far in the past any more. */
|
2005-11-05 21:15:27 +01:00
|
|
|
r1.cache_info.published_on = time(NULL);
|
|
|
|
r2.cache_info.published_on = time(NULL)-3*60*60;
|
2004-03-29 21:50:59 +02:00
|
|
|
test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
|
2005-08-31 08:14:37 +02:00
|
|
|
test_eq(dirserv_add_descriptor(buf,&m), 2);
|
2004-03-29 21:50:59 +02:00
|
|
|
test_assert(router_dump_router_to_string(buf, 2048, &r2, pk1)>0);
|
2005-08-31 08:14:37 +02:00
|
|
|
test_eq(dirserv_add_descriptor(buf,&m), 2);
|
2004-11-06 06:18:11 +01:00
|
|
|
get_options()->Nickname = tor_strdup("DirServer");
|
2006-06-13 13:11:19 +02:00
|
|
|
test_assert(!dirserv_dump_directory_to_string(&cp,pk3, 0));
|
2005-09-15 07:28:18 +02:00
|
|
|
crypto_pk_get_digest(pk3, d);
|
|
|
|
test_assert(!router_parse_directory(cp));
|
2004-04-07 21:46:27 +02:00
|
|
|
test_eq(2, smartlist_len(dir1->routers));
|
2006-01-10 21:47:24 +01:00
|
|
|
tor_free(cp);
|
|
|
|
}
|
2005-09-15 07:28:18 +02:00
|
|
|
#endif
|
2004-03-29 21:50:59 +02:00
|
|
|
dirserv_free_fingerprint_list();
|
2005-09-15 07:28:18 +02:00
|
|
|
|
2003-10-21 11:48:17 +02:00
|
|
|
tor_free(pk1_str);
|
|
|
|
tor_free(pk2_str);
|
2003-05-07 04:13:23 +02:00
|
|
|
if (pk1) crypto_free_pk_env(pk1);
|
|
|
|
if (pk2) crypto_free_pk_env(pk2);
|
2003-05-09 04:00:33 +02:00
|
|
|
if (rp1) routerinfo_free(rp1);
|
|
|
|
if (rp2) routerinfo_free(rp2);
|
2004-03-29 21:50:59 +02:00
|
|
|
tor_free(dir1); /* XXXX And more !*/
|
2003-10-21 11:48:17 +02:00
|
|
|
tor_free(dir2); /* And more !*/
|
2003-08-23 12:09:25 +02:00
|
|
|
|
2004-07-13 21:16:49 +02:00
|
|
|
/* Try out version parsing functionality */
|
|
|
|
test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
|
|
|
|
test_eq(0, ver1.major);
|
|
|
|
test_eq(3, ver1.minor);
|
|
|
|
test_eq(4, ver1.micro);
|
|
|
|
test_eq(VER_PRE, ver1.status);
|
|
|
|
test_eq(2, ver1.patchlevel);
|
|
|
|
test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
|
|
|
|
test_eq(0, ver1.major);
|
|
|
|
test_eq(3, ver1.minor);
|
|
|
|
test_eq(4, ver1.micro);
|
|
|
|
test_eq(VER_RC, ver1.status);
|
|
|
|
test_eq(1, ver1.patchlevel);
|
|
|
|
test_eq(0, tor_version_parse("1.3.4", &ver1));
|
|
|
|
test_eq(1, ver1.major);
|
|
|
|
test_eq(3, ver1.minor);
|
|
|
|
test_eq(4, ver1.micro);
|
|
|
|
test_eq(VER_RELEASE, ver1.status);
|
|
|
|
test_eq(0, ver1.patchlevel);
|
|
|
|
test_eq(0, tor_version_parse("1.3.4.999", &ver1));
|
|
|
|
test_eq(1, ver1.major);
|
|
|
|
test_eq(3, ver1.minor);
|
|
|
|
test_eq(4, ver1.micro);
|
|
|
|
test_eq(VER_RELEASE, ver1.status);
|
|
|
|
test_eq(999, ver1.patchlevel);
|
2004-11-30 00:04:26 +01:00
|
|
|
test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
|
|
|
|
test_eq(0, ver1.major);
|
|
|
|
test_eq(1, ver1.minor);
|
|
|
|
test_eq(2, ver1.micro);
|
|
|
|
test_eq(4, ver1.patchlevel);
|
|
|
|
test_eq(VER_RELEASE, ver1.status);
|
|
|
|
test_streq("alpha", ver1.status_tag);
|
|
|
|
test_eq(0, tor_version_parse("0.1.2.4", &ver1));
|
|
|
|
test_eq(0, ver1.major);
|
|
|
|
test_eq(1, ver1.minor);
|
|
|
|
test_eq(2, ver1.micro);
|
|
|
|
test_eq(4, ver1.patchlevel);
|
|
|
|
test_eq(VER_RELEASE, ver1.status);
|
|
|
|
test_streq("", ver1.status_tag);
|
2004-07-17 01:33:42 +02:00
|
|
|
|
2005-09-23 19:11:20 +02:00
|
|
|
#define test_eq_vs(vs1, vs2) test_eq_type(version_status_t, "%d", (vs1), (vs2))
|
2005-12-14 21:40:40 +01:00
|
|
|
#define test_v_i_o(val, ver, lst) \
|
|
|
|
test_eq_vs(val, tor_version_is_obsolete(ver, lst))
|
2005-09-23 19:11:20 +02:00
|
|
|
|
2005-09-21 02:41:06 +02:00
|
|
|
/* make sure tor_version_is_obsolete() works */
|
2005-12-14 21:40:40 +01:00
|
|
|
test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
|
|
|
|
test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
|
|
|
|
test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
|
|
|
|
test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
|
|
|
|
test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
|
|
|
|
test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
|
|
|
|
test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
|
|
|
|
test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
|
|
|
|
test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
|
|
|
|
test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
|
|
|
|
test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5");
|
2005-03-01 02:44:57 +01:00
|
|
|
/* Not on list, but newer than any in same series. */
|
2005-12-14 21:40:40 +01:00
|
|
|
test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
|
|
|
|
"Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
|
2005-03-01 02:44:57 +01:00
|
|
|
/* Series newer than any on list. */
|
2005-12-14 21:40:40 +01:00
|
|
|
test_v_i_o(VS_NEW, "0.1.2.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
|
2005-03-01 02:44:57 +01:00
|
|
|
/* Series older than any on list. */
|
2005-12-14 21:40:40 +01:00
|
|
|
test_v_i_o(VS_OLD, "0.0.1.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
|
2005-03-01 02:44:57 +01:00
|
|
|
/* Not on list, not newer than any on same series. */
|
2005-12-14 21:40:40 +01:00
|
|
|
test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
|
|
|
|
"Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
|
2005-03-01 02:44:57 +01:00
|
|
|
/* On list, not newer than any on same series. */
|
2005-12-14 21:40:40 +01:00
|
|
|
test_v_i_o(VS_UNRECOMMENDED,
|
|
|
|
"0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
|
2004-09-27 08:00:43 +02:00
|
|
|
test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
|
|
|
|
test_eq(1, tor_version_as_new_as(
|
2005-12-14 21:40:40 +01:00
|
|
|
"Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
|
|
|
|
"sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
|
|
|
|
"0.0.8rc2"));
|
2004-09-27 08:00:43 +02:00
|
|
|
test_eq(0, tor_version_as_new_as(
|
2005-12-14 21:40:40 +01:00
|
|
|
"Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
|
|
|
|
"sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
|
2004-09-27 08:00:43 +02:00
|
|
|
|
2003-05-06 19:38:16 +02:00
|
|
|
}
|
|
|
|
|
2005-05-14 02:13:17 +02:00
|
|
|
static void
|
|
|
|
test_exit_policies(void)
|
|
|
|
{
|
|
|
|
addr_policy_t *policy;
|
|
|
|
|
2005-08-08 23:58:48 +02:00
|
|
|
policy = router_parse_addr_policy_from_string("reject 192.168.0.0/16:*",-1);
|
2005-05-14 02:13:17 +02:00
|
|
|
test_eq(NULL, policy->next);
|
|
|
|
test_eq(ADDR_POLICY_REJECT, policy->policy_type);
|
|
|
|
test_eq(0xc0a80000u, policy->addr);
|
|
|
|
test_eq(0xffff0000u, policy->msk);
|
|
|
|
test_eq(1, policy->prt_min);
|
|
|
|
test_eq(65535, policy->prt_max);
|
|
|
|
test_streq("reject 192.168.0.0/16:*", policy->string);
|
|
|
|
|
2006-02-01 04:53:52 +01:00
|
|
|
// test_assert(exit_policy_implicitly_allows_local_networks(policy, 0));
|
2005-08-12 19:24:53 +02:00
|
|
|
test_assert(ADDR_POLICY_ACCEPTED ==
|
2006-03-27 04:25:34 +02:00
|
|
|
compare_addr_to_addr_policy(0x01020304u, 2, policy));
|
2005-08-12 19:24:53 +02:00
|
|
|
test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
|
2006-03-27 04:25:34 +02:00
|
|
|
compare_addr_to_addr_policy(0, 2, policy));
|
2005-08-12 19:24:53 +02:00
|
|
|
test_assert(ADDR_POLICY_REJECTED ==
|
2006-03-27 04:25:34 +02:00
|
|
|
compare_addr_to_addr_policy(0xc0a80102, 2, policy));
|
2005-05-14 02:13:17 +02:00
|
|
|
|
|
|
|
addr_policy_free(policy);
|
|
|
|
|
2006-02-01 04:53:52 +01:00
|
|
|
#if 0
|
2005-05-14 02:13:17 +02:00
|
|
|
/* Copied from router.c */
|
|
|
|
policy = NULL;
|
2005-07-23 03:58:05 +02:00
|
|
|
options_append_default_exit_policy(&policy);
|
2005-05-14 02:13:17 +02:00
|
|
|
test_assert(policy);
|
|
|
|
test_assert(!exit_policy_implicitly_allows_local_networks(policy, 1));
|
|
|
|
|
|
|
|
addr_policy_free(policy);
|
2006-02-01 04:53:52 +01:00
|
|
|
#endif
|
2005-05-14 02:13:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-11-02 04:02:17 +01:00
|
|
|
static void
|
|
|
|
test_rend_fns(void)
|
2004-03-31 04:07:38 +02:00
|
|
|
{
|
2004-04-03 03:59:53 +02:00
|
|
|
char address1[] = "fooaddress.onion";
|
|
|
|
char address2[] = "aaaaaaaaaaaaaaaa.onion";
|
2004-11-29 09:34:54 +01:00
|
|
|
char address3[] = "fooaddress.exit";
|
2005-06-07 20:01:46 +02:00
|
|
|
char address4[] = "tor.eff.org";
|
2004-03-31 04:07:38 +02:00
|
|
|
rend_service_descriptor_t *d1, *d2;
|
|
|
|
char *encoded;
|
2004-10-14 05:13:08 +02:00
|
|
|
size_t len;
|
2005-06-29 23:46:55 +02:00
|
|
|
crypto_pk_env_t *pk1, *pk2;
|
2004-03-31 04:07:38 +02:00
|
|
|
time_t now;
|
2004-04-03 04:40:30 +02:00
|
|
|
pk1 = crypto_new_pk_env();
|
2005-06-29 23:46:55 +02:00
|
|
|
pk2 = crypto_new_pk_env();
|
2004-03-31 04:07:38 +02:00
|
|
|
test_assert(!crypto_pk_generate_key(pk1));
|
2005-06-29 23:46:55 +02:00
|
|
|
test_assert(!crypto_pk_generate_key(pk2));
|
|
|
|
|
|
|
|
/* Test unversioned descriptor */
|
2004-03-31 04:07:38 +02:00
|
|
|
d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
|
2005-06-29 23:46:55 +02:00
|
|
|
d1->pk = crypto_pk_dup_key(pk1);
|
2004-03-31 04:07:38 +02:00
|
|
|
now = time(NULL);
|
|
|
|
d1->timestamp = now;
|
|
|
|
d1->n_intro_points = 3;
|
2005-06-29 23:46:55 +02:00
|
|
|
d1->version = 0;
|
2004-03-31 04:07:38 +02:00
|
|
|
d1->intro_points = tor_malloc(sizeof(char*)*3);
|
|
|
|
d1->intro_points[0] = tor_strdup("tom");
|
|
|
|
d1->intro_points[1] = tor_strdup("crow");
|
|
|
|
d1->intro_points[2] = tor_strdup("joel");
|
2005-06-29 23:46:55 +02:00
|
|
|
test_assert(! rend_encode_service_descriptor(d1, 0, pk1, &encoded, &len));
|
2004-03-31 04:07:38 +02:00
|
|
|
d2 = rend_parse_service_descriptor(encoded, len);
|
|
|
|
test_assert(d2);
|
|
|
|
|
|
|
|
test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
|
|
|
|
test_eq(d2->timestamp, now);
|
2005-06-29 23:46:55 +02:00
|
|
|
test_eq(d2->version, 0);
|
|
|
|
test_eq(d2->protocols, 1);
|
2004-03-31 04:07:38 +02:00
|
|
|
test_eq(d2->n_intro_points, 3);
|
|
|
|
test_streq(d2->intro_points[0], "tom");
|
|
|
|
test_streq(d2->intro_points[1], "crow");
|
|
|
|
test_streq(d2->intro_points[2], "joel");
|
2005-06-29 23:46:55 +02:00
|
|
|
test_eq(NULL, d2->intro_point_extend_info);
|
|
|
|
|
|
|
|
rend_service_descriptor_free(d1);
|
|
|
|
rend_service_descriptor_free(d2);
|
|
|
|
tor_free(encoded);
|
|
|
|
|
|
|
|
/* Test versioned descriptor. */
|
|
|
|
d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
|
|
|
|
d1->pk = crypto_pk_dup_key(pk1);
|
|
|
|
now = time(NULL);
|
|
|
|
d1->timestamp = now;
|
|
|
|
d1->n_intro_points = 2;
|
|
|
|
d1->version = 1;
|
|
|
|
d1->protocols = 60;
|
|
|
|
d1->intro_points = tor_malloc(sizeof(char*)*2);
|
|
|
|
d1->intro_point_extend_info = tor_malloc(sizeof(extend_info_t*)*2);
|
|
|
|
d1->intro_points[0] = tor_strdup("tom");
|
|
|
|
d1->intro_points[1] = tor_strdup("crow");
|
|
|
|
d1->intro_point_extend_info[0] = tor_malloc_zero(sizeof(extend_info_t));
|
|
|
|
strcpy(d1->intro_point_extend_info[0]->nickname, "tom");
|
|
|
|
d1->intro_point_extend_info[0]->addr = 1234;
|
|
|
|
d1->intro_point_extend_info[0]->port = 4567;
|
|
|
|
d1->intro_point_extend_info[0]->onion_key = crypto_pk_dup_key(pk1);
|
|
|
|
memset(d1->intro_point_extend_info[0]->identity_digest, 'a', DIGEST_LEN);
|
|
|
|
|
|
|
|
d1->intro_point_extend_info[1] = tor_malloc_zero(sizeof(extend_info_t));
|
|
|
|
strcpy(d1->intro_point_extend_info[1]->nickname, "crow");
|
|
|
|
d1->intro_point_extend_info[1]->addr = 6060842;
|
|
|
|
d1->intro_point_extend_info[1]->port = 8000;
|
|
|
|
d1->intro_point_extend_info[1]->onion_key = crypto_pk_dup_key(pk2);
|
|
|
|
memset(d1->intro_point_extend_info[1]->identity_digest, 'b', DIGEST_LEN);
|
|
|
|
|
|
|
|
test_assert(! rend_encode_service_descriptor(d1, 1, pk1, &encoded, &len));
|
|
|
|
d2 = rend_parse_service_descriptor(encoded, len);
|
|
|
|
test_assert(d2);
|
|
|
|
|
|
|
|
test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
|
|
|
|
test_eq(d2->timestamp, now);
|
|
|
|
test_eq(d2->version, 1);
|
|
|
|
test_eq(d2->protocols, 60);
|
|
|
|
test_eq(d2->n_intro_points, 2);
|
|
|
|
test_streq(d2->intro_points[0], d2->intro_point_extend_info[0]->nickname);
|
|
|
|
test_streq(d2->intro_points[1], d2->intro_point_extend_info[1]->nickname);
|
|
|
|
test_eq(d2->intro_point_extend_info[0]->addr, 1234);
|
|
|
|
test_eq(d2->intro_point_extend_info[0]->port, 4567);
|
2005-12-14 21:40:40 +01:00
|
|
|
test_assert(!crypto_pk_cmp_keys(pk1,
|
|
|
|
d2->intro_point_extend_info[0]->onion_key));
|
2005-06-29 23:46:55 +02:00
|
|
|
test_memeq(d2->intro_point_extend_info[0]->identity_digest,
|
|
|
|
d1->intro_point_extend_info[0]->identity_digest, DIGEST_LEN);
|
|
|
|
test_eq(d2->intro_point_extend_info[1]->addr, 6060842);
|
|
|
|
test_eq(d2->intro_point_extend_info[1]->port, 8000);
|
|
|
|
|
|
|
|
test_memeq(d2->intro_point_extend_info[1]->identity_digest,
|
|
|
|
d1->intro_point_extend_info[1]->identity_digest, DIGEST_LEN);
|
2004-03-31 04:07:38 +02:00
|
|
|
|
2005-08-12 19:24:53 +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));
|
2004-04-03 03:59:53 +02:00
|
|
|
|
2004-03-31 04:07:38 +02:00
|
|
|
rend_service_descriptor_free(d1);
|
|
|
|
rend_service_descriptor_free(d2);
|
2005-06-29 23:46:55 +02:00
|
|
|
crypto_free_pk_env(pk1);
|
|
|
|
crypto_free_pk_env(pk2);
|
2004-03-31 04:07:38 +02:00
|
|
|
}
|
|
|
|
|
2005-12-01 05:35:58 +01:00
|
|
|
static void
|
|
|
|
bench_aes(void)
|
|
|
|
{
|
|
|
|
int len, i;
|
|
|
|
char *b1, *b2;
|
|
|
|
crypto_cipher_env_t *c;
|
|
|
|
struct timeval start, end;
|
|
|
|
const int iters = 100000;
|
|
|
|
uint64_t nsec;
|
|
|
|
c = crypto_new_cipher_env();
|
|
|
|
crypto_cipher_generate_key(c);
|
|
|
|
crypto_cipher_encrypt_init_cipher(c);
|
|
|
|
for (len = 1; len <= 8192; len *= 2) {
|
|
|
|
b1 = tor_malloc_zero(len);
|
|
|
|
b2 = tor_malloc_zero(len);
|
|
|
|
tor_gettimeofday(&start);
|
|
|
|
for (i = 0; i < iters; ++i) {
|
|
|
|
crypto_cipher_encrypt(c, b1, b2, len);
|
|
|
|
}
|
|
|
|
tor_gettimeofday(&end);
|
|
|
|
tor_free(b1);
|
|
|
|
tor_free(b2);
|
|
|
|
nsec = (uint64_t) tv_udiff(&start,&end);
|
|
|
|
nsec *= 1000;
|
|
|
|
nsec /= (iters*len);
|
|
|
|
printf("%d bytes: "U64_FORMAT" nsec per byte\n", len,
|
|
|
|
U64_PRINTF_ARG(nsec));
|
|
|
|
}
|
|
|
|
crypto_free_cipher_env(c);
|
|
|
|
}
|
|
|
|
|
2003-12-17 22:09:31 +01:00
|
|
|
int
|
2005-06-11 20:52:12 +02:00
|
|
|
main(int c, char**v)
|
|
|
|
{
|
2005-08-09 20:49:43 +02:00
|
|
|
or_options_t *options = options_new();
|
2006-03-26 08:51:26 +02:00
|
|
|
char *errmsg = NULL;
|
2006-06-22 09:34:04 +02:00
|
|
|
(void) c;
|
|
|
|
(void) v;
|
2005-08-27 01:22:27 +02:00
|
|
|
options->command = CMD_RUN_UNITTESTS;
|
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);
|
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
|
|
|
|
2003-06-14 03:39:23 +02:00
|
|
|
crypto_seed_rng();
|
2005-08-22 02:34:42 +02:00
|
|
|
|
2005-12-01 05:35:58 +01:00
|
|
|
if (0) {
|
|
|
|
bench_aes();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-07 04:46:16 +02:00
|
|
|
rep_hist_init();
|
2004-08-11 21:20:24 +02:00
|
|
|
atexit(remove_directory);
|
2004-08-07 04:46:16 +02:00
|
|
|
|
2005-02-22 04:01:31 +01:00
|
|
|
printf("Running Tor unit tests on %s\n", get_uname());
|
|
|
|
|
2005-04-26 22:53:22 +02:00
|
|
|
puts("========================== Buffers =========================");
|
|
|
|
test_buffers();
|
2003-06-14 03:30:53 +02:00
|
|
|
puts("\n========================== Crypto ==========================");
|
2004-10-07 05:11:42 +02:00
|
|
|
// add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
|
2003-05-02 00:55:51 +02:00
|
|
|
test_crypto();
|
2003-06-13 23:23:14 +02:00
|
|
|
test_crypto_dh();
|
2005-06-04 15:43:03 +02:00
|
|
|
test_crypto_s2k();
|
2003-05-05 06:27:00 +02:00
|
|
|
puts("\n========================= Util ============================");
|
2004-09-02 20:22:51 +02:00
|
|
|
test_gzip();
|
2003-04-17 01:21:44 +02:00
|
|
|
test_util();
|
2004-03-19 23:07:24 +01:00
|
|
|
test_strmap();
|
2005-07-15 21:31:11 +02:00
|
|
|
test_control_formats();
|
2006-07-31 19:59:37 +02:00
|
|
|
test_pqueue();
|
2003-05-06 19:38:16 +02:00
|
|
|
puts("\n========================= Onion Skins =====================");
|
2003-11-12 20:34:34 +01:00
|
|
|
test_onion();
|
2003-06-14 03:39:23 +02:00
|
|
|
test_onion_handshake();
|
2003-05-06 19:38:16 +02:00
|
|
|
puts("\n========================= Directory Formats ===============");
|
|
|
|
test_dir_format();
|
2005-05-14 02:13:17 +02:00
|
|
|
puts("\n========================= Exit policies ===================");
|
|
|
|
test_exit_policies();
|
2004-03-31 04:07:38 +02:00
|
|
|
puts("\n========================= Rendezvous functionality ========");
|
|
|
|
test_rend_fns();
|
2003-04-16 17:24:09 +02:00
|
|
|
puts("");
|
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
|
|
|
|