summaryrefslogtreecommitdiff
path: root/test/routing.c
blob: 7905bc0866265b492a2ea697cabdabd3f7316492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "unity.h"
#include "routing.h"

#define IP(a, b, c, d) (a << 24 | b << 16 | c << 8 | d)

struct nodeid self;

void setUp() {
	self = (struct nodeid){.inner={ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }};
	routing_init(&self);
}

void test_deny_self_add() {
	routing_flush();
	struct entry* entry;
	if(!routing_offer(&self, &entry))
		TEST_PASS();

	TEST_FAIL();
}

void test_can_find_added() {
	routing_flush();

	// The address we are going to store
	struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};

	// Make a nodeid that is one bit different
	struct nodeid new = self;

	new.inner[4] += 1;
	struct entry* entry;
	TEST_ASSERT_TRUE_MESSAGE(routing_offer(&new, &entry), "Did not accept new entry");
	new.inner[4] += 1;
	TEST_ASSERT_TRUE_MESSAGE(routing_offer(&new, &entry), "Did not accept new entry");

	// Set the entries
	entry->addr = addr;
	entry->last = time(NULL);

	// Ask for 3 nodes
	struct entry *out[3] = {0};
	size_t n = routing_closest(&new, 3, out);
	// Since we only put 2 in, we should get 2 out
	TEST_ASSERT_EQUAL_INT(2, n);

	TEST_ASSERT_EQUAL_MEMORY(&addr, &out[0]->addr, sizeof(struct addr));
	TEST_ASSERT_EQUAL_MEMORY(&new, &out[0]->id, sizeof(struct nodeid));
}

void test_discard_offer_when_bucket_full() {
	routing_flush();

	// The address we are going to store
	struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};

	struct nodeid new = self;
	// Flip the top bit of the id to go into the low resolution bucket
	new.inner[0] ^= 0x80000000;

	// Fill up the bucket with entries
	for(uint8_t i = 0; i < 8; i++) {
		struct entry* entry;
		TEST_ASSERT_TRUE_MESSAGE(routing_offer(&new, &entry), "Did not accept new entry");

		// Set the entries
		entry->addr = addr;
		entry->last = time(NULL);

		new.inner[4] += 1;
	}

	struct entry* entry;
	TEST_ASSERT_FALSE_MESSAGE(routing_offer(&new, &entry), "Accepted entry when bucket was full");
}

void test_discard_offer_when_nodeid_added_twice() {
	routing_flush();

	// The address we are going to store
	struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};

	struct nodeid new = self;
	new.inner[4] ^= 0x00000001;

	struct entry* entry;
	TEST_ASSERT_TRUE(routing_offer(&new, &entry));
	entry->addr = addr;
	entry->last = time(NULL);

	bool accept = routing_offer(&new, &entry);
	TEST_ASSERT_FALSE_MESSAGE(accept, "Accepted entry when bucket was full");
}

void test_interested_when_space_in_bucket() {
	routing_flush();

	struct nodeid new = self;
	new.inner[4] ^= 0x00000001;

	bool interest = routing_interested(&new);

	TEST_ASSERT_TRUE_MESSAGE(interest, "Was not interested in node");
}

void test_not_interested_when_nodeid_in_table() {
	routing_flush();

	struct nodeid new = self;
	new.inner[4] ^= 0x00000001;

	struct entry* entry;
	TEST_ASSERT_TRUE(routing_offer(&new, &entry));
	entry->addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};
	entry->last = time(NULL);

	bool interest = routing_interested(&new);
	TEST_ASSERT_FALSE_MESSAGE(interest, "Was interested in node");
}

void test_not_interested_when_bucket_is_full() {
	routing_flush();

	// The address we are going to store
	struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};

	struct nodeid new = self;
	// Flip the top bit of the id to go into the low resolution bucket
	new.inner[0] ^= 0x80000000;

	// Fill up the bucket with entries
	for(uint8_t i = 0; i < 8; i++) {
		struct entry* entry;
		TEST_ASSERT_TRUE_MESSAGE(routing_offer(&new, &entry), "Did not accept new entry");

		// Set the entries
		entry->addr = addr;
		entry->last = time(NULL);

		new.inner[4] += 1;
	}

	TEST_ASSERT_FALSE_MESSAGE(routing_interested(&new), "Still interested when bucket was full");
}

void test_lowest_ts_is_oldest() {
	routing_flush();

	// The address we are going to store
	struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};

	struct nodeid new = self;
	// Flip the top bit of the id to go into the low resolution bucket
	new.inner_b[0] ^= 0x80;

	// Fill up the bucket with entries
	for(uint8_t i = 0; i < 2; i++) {
		struct entry* entry;
		TEST_ASSERT_TRUE_MESSAGE(routing_offer(&new, &entry), "Did not accept new entry");

		// Set the entries
		entry->id = new;
		entry->addr = addr;
		// Invert the timestamps to make the last one have lowest timestamp
		entry->last = 2-i;

		new.inner_b[19] += 1;
	}

	struct entry* dest = NULL;
	routing_oldest(&dest);

	TEST_ASSERT_NOT_NULL(dest);
	TEST_ASSERT_EQUAL(1, dest->id.inner_b[19]);
}