2016-02-27 18:48:19 +01:00
|
|
|
/* Copyright (c) 2007-2016, The Tor Project, Inc. */
|
2014-02-05 01:54:09 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file test_nodelist.c
|
|
|
|
* \brief Unit tests for nodelist related functions.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
#include "nodelist.h"
|
|
|
|
#include "test.h"
|
|
|
|
|
2014-09-23 18:22:36 +02:00
|
|
|
/** Test the case when node_get_by_id() returns NULL,
|
2014-02-05 01:54:09 +01:00
|
|
|
* node_get_verbose_nickname_by_id should return the base 16 encoding
|
|
|
|
* of the id.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_nodelist_node_get_verbose_nickname_by_id_null_node(void *arg)
|
|
|
|
{
|
|
|
|
char vname[MAX_VERBOSE_NICKNAME_LEN+1];
|
|
|
|
const char ID[] = "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
|
|
|
|
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
/* make sure node_get_by_id returns NULL */
|
Use coccinelle scripts to clean up our unit tests
This should get rid of most of the users of the old test_*
functions. Some are in macros and will need manual cleanup, though.
This patch is for 13119, and was automatically generated with these
scripts. The perl scripts are there because coccinelle hates
operators as macro arguments.
------------------------------
s/==,/_X_EQ_,/g;
s/!=,/_X_NE_,/g;
s/<,/_X_LT_,/g;
s/>,/_X_GT_,/g;
s/>=,/_X_GEQ_,/g;
s/<=,/_X_LEQ_,/g;
------------------------------
@@
expression a;
identifier func;
@@
func (...) {
<...
-test_fail_msg
+TT_DIE
(
+(
a
+)
)
...>
}
@@
identifier func;
@@
func (...) {
<...
-test_fail()
+TT_DIE(("Assertion failed."))
...>
}
@@
expression a;
identifier func;
@@
func (...) {
<...
-test_assert
+tt_assert
(a)
...>
}
@@
expression a, b;
identifier func;
@@
func (...) {
<...
-test_eq
+tt_int_op
(a,
+_X_EQ_,
b)
...>
}
@@
expression a, b;
identifier func;
@@
func (...) {
<...
-test_neq
+tt_int_op
(a,
+_X_NEQ_,
b)
...>
}
@@
expression a, b;
identifier func;
@@
func (...) {
<...
-test_streq
+tt_str_op
(a,
+_X_EQ_,
b)
...>
}
@@
expression a, b;
identifier func;
@@
func (...) {
<...
-test_strneq
+tt_str_op
(a,
+_X_NEQ_,
b)
...>
}
@@
expression a, b;
identifier func;
@@
func (...) {
<...
-test_eq_ptr
+tt_ptr_op
(a,
+_X_EQ_,
b)
...>
}
@@
expression a, b;
identifier func;
@@
func() {
<...
-test_neq_ptr
+tt_ptr_op
(a,
+_X_NEQ_,
b)
...>
}
@@
expression a, b, len;
identifier func;
@@
func (...) {
<...
-test_memeq
+tt_mem_op
(a,
+_X_EQ_,
b, len)
...>
}
@@
expression a, b, len;
identifier func;
@@
func (...) {
<...
-test_memneq
+tt_mem_op
(a,
+_X_NEQ_,
b, len)
...>
}
------------------------------
@@
char a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a == b
+a, _X_EQ_, b
)
...>
}
@@
int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a == b
+a, _X_EQ_, b
)
...>
}
@@
long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a == b
+a, _X_EQ_, b
)
...>
}
@@
unsigned int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a == b
+a, _X_EQ_, b
)
...>
}
@@
unsigned long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a == b
+a, _X_EQ_, b
)
...>
}
@@
char a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a != b
+a, _X_NEQ_, b
)
...>
}
@@
int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a != b
+a, _X_NEQ_, b
)
...>
}
@@
long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a != b
+a, _X_NEQ_, b
)
...>
}
@@
unsigned int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a != b
+a, _X_NEQ_, b
)
...>
}
@@
unsigned long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a != b
+a, _X_NEQ_, b
)
...>
}
@@
char a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a >= b
+a, _X_GEQ_, b
)
...>
}
@@
int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a >= b
+a, _X_GEQ_, b
)
...>
}
@@
long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a >= b
+a, _X_GEQ_, b
)
...>
}
@@
unsigned int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a >= b
+a, _X_GEQ_, b
)
...>
}
@@
unsigned long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a >= b
+a, _X_GEQ_, b
)
...>
}
@@
char a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a <= b
+a, _X_LEQ_, b
)
...>
}
@@
int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a <= b
+a, _X_LEQ_, b
)
...>
}
@@
long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a <= b
+a, _X_LEQ_, b
)
...>
}
@@
unsigned int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a <= b
+a, _X_LEQ_, b
)
...>
}
@@
unsigned long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a <= b
+a, _X_LEQ_, b
)
...>
}
@@
char a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a > b
+a, _X_GT_, b
)
...>
}
@@
int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a > b
+a, _X_GT_, b
)
...>
}
@@
long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a > b
+a, _X_GT_, b
)
...>
}
@@
unsigned int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a > b
+a, _X_GT_, b
)
...>
}
@@
unsigned long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a > b
+a, _X_GT_, b
)
...>
}
@@
char a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a < b
+a, _X_LT_, b
)
...>
}
@@
int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a < b
+a, _X_LT_, b
)
...>
}
@@
long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_int_op
(
-a < b
+a, _X_LT_, b
)
...>
}
@@
unsigned int a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a < b
+a, _X_LT_, b
)
...>
}
@@
unsigned long a, b;
identifier func;
@@
func (...) {
<...
-tt_assert
+tt_uint_op
(
-a < b
+a, _X_LT_, b
)
...>
}
------------------------------
s/_X_NEQ_/!=/g;
s/_X_NE_/!=/g;
s/_X_EQ_/==/g;
s/_X_GT_/>/g;
s/_X_LT_/</g;
s/_X_GEQ_/>=/g;
s/_X_LEQ_/<=/g;
s/test_mem_op\(/tt_mem_op\(/g;
2014-09-16 03:18:21 +02:00
|
|
|
tt_assert(!node_get_by_id(ID));
|
2014-02-05 01:54:09 +01:00
|
|
|
node_get_verbose_nickname_by_id(ID, vname);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(vname,OP_EQ, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
|
2014-02-05 01:54:09 +01:00
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:52:48 +01:00
|
|
|
/** For routers without named flag, get_verbose_nickname should return
|
|
|
|
* "Fingerprint~Nickname"
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_nodelist_node_get_verbose_nickname_not_named(void *arg)
|
|
|
|
{
|
|
|
|
node_t mock_node;
|
|
|
|
routerstatus_t mock_rs;
|
|
|
|
|
|
|
|
char vname[MAX_VERBOSE_NICKNAME_LEN+1];
|
|
|
|
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
memset(&mock_node, 0, sizeof(node_t));
|
|
|
|
memset(&mock_rs, 0, sizeof(routerstatus_t));
|
|
|
|
|
|
|
|
/* verbose nickname should use ~ instead of = for unnamed routers */
|
2014-02-07 18:25:49 +01:00
|
|
|
strlcpy(mock_rs.nickname, "TestOR", sizeof(mock_rs.nickname));
|
2014-02-05 02:52:48 +01:00
|
|
|
mock_node.rs = &mock_rs;
|
2014-02-07 18:25:49 +01:00
|
|
|
memcpy(mock_node.identity,
|
2014-02-05 02:52:48 +01:00
|
|
|
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
|
|
|
|
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
|
|
|
|
DIGEST_LEN);
|
|
|
|
node_get_verbose_nickname(&mock_node, vname);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_str_op(vname,OP_EQ, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~TestOR");
|
2014-02-05 02:52:48 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:29:48 +01:00
|
|
|
/** A node should be considered a directory server if it has an open dirport
|
|
|
|
* of it accepts tunnelled directory requests.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_nodelist_node_is_dir(void *arg)
|
|
|
|
{
|
2015-01-26 20:49:48 +01:00
|
|
|
(void)arg;
|
|
|
|
|
2014-10-29 01:29:48 +01:00
|
|
|
routerstatus_t rs;
|
|
|
|
routerinfo_t ri;
|
|
|
|
node_t node;
|
|
|
|
memset(&node, 0, sizeof(node_t));
|
|
|
|
memset(&rs, 0, sizeof(routerstatus_t));
|
|
|
|
memset(&ri, 0, sizeof(routerinfo_t));
|
|
|
|
|
|
|
|
tt_assert(!node_is_dir(&node));
|
|
|
|
|
|
|
|
node.rs = &rs;
|
|
|
|
tt_assert(!node_is_dir(&node));
|
|
|
|
|
|
|
|
rs.is_v2_dir = 1;
|
|
|
|
tt_assert(node_is_dir(&node));
|
|
|
|
|
|
|
|
rs.is_v2_dir = 0;
|
|
|
|
rs.dir_port = 1;
|
2015-12-18 19:24:23 +01:00
|
|
|
tt_assert(! node_is_dir(&node));
|
2014-10-29 01:29:48 +01:00
|
|
|
|
|
|
|
node.rs = NULL;
|
|
|
|
tt_assert(!node_is_dir(&node));
|
|
|
|
node.ri = &ri;
|
|
|
|
ri.supports_tunnelled_dir_requests = 1;
|
|
|
|
tt_assert(node_is_dir(&node));
|
|
|
|
ri.supports_tunnelled_dir_requests = 0;
|
|
|
|
ri.dir_port = 1;
|
2015-12-18 19:24:23 +01:00
|
|
|
tt_assert(! node_is_dir(&node));
|
2014-10-29 01:29:48 +01:00
|
|
|
|
2015-12-18 19:24:23 +01:00
|
|
|
done:
|
|
|
|
return;
|
2014-10-29 01:29:48 +01:00
|
|
|
}
|
|
|
|
|
2014-02-05 01:54:09 +01:00
|
|
|
#define NODE(name, flags) \
|
|
|
|
{ #name, test_nodelist_##name, (flags), NULL, NULL }
|
|
|
|
|
|
|
|
struct testcase_t nodelist_tests[] = {
|
|
|
|
NODE(node_get_verbose_nickname_by_id_null_node, TT_FORK),
|
2014-02-05 02:52:48 +01:00
|
|
|
NODE(node_get_verbose_nickname_not_named, TT_FORK),
|
2014-10-29 01:29:48 +01:00
|
|
|
NODE(node_is_dir, TT_FORK),
|
2014-02-05 01:54:09 +01:00
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|
|
|
|
|