TOR: update to v0.2.5.12
[tomato.git] / release / src / router / tor / src / test / test_cell_queue.c
blob92629823ec389dc06ef8559751ffea526cfee817
1 /* Copyright (c) 2013, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define CIRCUITLIST_PRIVATE
5 #define RELAY_PRIVATE
6 #include "or.h"
7 #include "circuitlist.h"
8 #include "relay.h"
9 #include "test.h"
11 static void
12 test_cq_manip(void *arg)
14 packed_cell_t *pc1=NULL, *pc2=NULL, *pc3=NULL, *pc4=NULL, *pc_tmp=NULL;
15 cell_queue_t cq;
16 cell_t cell;
17 (void) arg;
19 #ifdef ENABLE_MEMPOOLS
20 init_cell_pool();
21 #endif /* ENABLE_MEMPOOLS */
23 cell_queue_init(&cq);
24 tt_int_op(cq.n, ==, 0);
26 pc1 = packed_cell_new();
27 pc2 = packed_cell_new();
28 pc3 = packed_cell_new();
29 pc4 = packed_cell_new();
30 tt_assert(pc1 && pc2 && pc3 && pc4);
32 tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
34 /* Add and remove a singleton. */
35 cell_queue_append(&cq, pc1);
36 tt_int_op(cq.n, ==, 1);
37 tt_ptr_op(pc1, ==, cell_queue_pop(&cq));
38 tt_int_op(cq.n, ==, 0);
40 /* Add and remove four items */
41 cell_queue_append(&cq, pc4);
42 cell_queue_append(&cq, pc3);
43 cell_queue_append(&cq, pc2);
44 cell_queue_append(&cq, pc1);
45 tt_int_op(cq.n, ==, 4);
46 tt_ptr_op(pc4, ==, cell_queue_pop(&cq));
47 tt_ptr_op(pc3, ==, cell_queue_pop(&cq));
48 tt_ptr_op(pc2, ==, cell_queue_pop(&cq));
49 tt_ptr_op(pc1, ==, cell_queue_pop(&cq));
50 tt_int_op(cq.n, ==, 0);
51 tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
53 /* Try a packed copy (wide, then narrow, which is a bit of a cheat, since a
54 * real cell queue has only one type.) */
55 memset(&cell, 0, sizeof(cell));
56 cell.circ_id = 0x12345678;
57 cell.command = 10;
58 strlcpy((char*)cell.payload, "Lorax ipsum gruvvulus thneed amet, snergelly "
59 "once-ler lerkim, sed do barbaloot tempor gluppitus ut labore et "
60 "truffula magna aliqua.",
61 sizeof(cell.payload));
62 cell_queue_append_packed_copy(NULL /*circ*/, &cq, 0 /*exitward*/, &cell,
63 1 /*wide*/, 0 /*stats*/);
64 cell.circ_id = 0x2013;
65 cell_queue_append_packed_copy(NULL /*circ*/, &cq, 0 /*exitward*/, &cell,
66 0 /*wide*/, 0 /*stats*/);
67 tt_int_op(cq.n, ==, 2);
69 pc_tmp = cell_queue_pop(&cq);
70 tt_int_op(cq.n, ==, 1);
71 tt_ptr_op(pc_tmp, !=, NULL);
72 test_mem_op(pc_tmp->body, ==, "\x12\x34\x56\x78\x0a", 5);
73 test_mem_op(pc_tmp->body+5, ==, cell.payload, sizeof(cell.payload));
74 packed_cell_free(pc_tmp);
76 pc_tmp = cell_queue_pop(&cq);
77 tt_int_op(cq.n, ==, 0);
78 tt_ptr_op(pc_tmp, !=, NULL);
79 test_mem_op(pc_tmp->body, ==, "\x20\x13\x0a", 3);
80 test_mem_op(pc_tmp->body+3, ==, cell.payload, sizeof(cell.payload));
81 packed_cell_free(pc_tmp);
82 pc_tmp = NULL;
84 tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
86 /* Now make sure cell_queue_clear works. */
87 cell_queue_append(&cq, pc2);
88 cell_queue_append(&cq, pc1);
89 tt_int_op(cq.n, ==, 2);
90 cell_queue_clear(&cq);
91 pc2 = pc1 = NULL; /* prevent double-free */
92 tt_int_op(cq.n, ==, 0);
94 done:
95 packed_cell_free(pc1);
96 packed_cell_free(pc2);
97 packed_cell_free(pc3);
98 packed_cell_free(pc4);
99 packed_cell_free(pc_tmp);
101 cell_queue_clear(&cq);
103 #ifdef ENABLE_MEMPOOLS
104 free_cell_pool();
105 #endif /* ENABLE_MEMPOOLS */
108 static void
109 test_circuit_n_cells(void *arg)
111 packed_cell_t *pc1=NULL, *pc2=NULL, *pc3=NULL, *pc4=NULL, *pc5=NULL;
112 origin_circuit_t *origin_c=NULL;
113 or_circuit_t *or_c=NULL;
115 (void)arg;
117 #ifdef ENABLE_MEMPOOLS
118 init_cell_pool();
119 #endif /* ENABLE_MEMPOOLS */
121 pc1 = packed_cell_new();
122 pc2 = packed_cell_new();
123 pc3 = packed_cell_new();
124 pc4 = packed_cell_new();
125 pc5 = packed_cell_new();
126 tt_assert(pc1 && pc2 && pc3 && pc4 && pc5);
128 or_c = or_circuit_new(0, NULL);
129 origin_c = origin_circuit_new();
130 origin_c->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
132 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 0);
133 cell_queue_append(&or_c->p_chan_cells, pc1);
134 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 1);
135 cell_queue_append(&or_c->base_.n_chan_cells, pc2);
136 cell_queue_append(&or_c->base_.n_chan_cells, pc3);
137 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 3);
139 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c)), ==, 0);
140 cell_queue_append(&origin_c->base_.n_chan_cells, pc4);
141 cell_queue_append(&origin_c->base_.n_chan_cells, pc5);
142 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c)), ==, 2);
144 done:
145 circuit_free(TO_CIRCUIT(or_c));
146 circuit_free(TO_CIRCUIT(origin_c));
148 #ifdef ENABLE_MEMPOOLS
149 free_cell_pool();
150 #endif /* ENABLE_MEMPOOLS */
153 struct testcase_t cell_queue_tests[] = {
154 { "basic", test_cq_manip, TT_FORK, NULL, NULL, },
155 { "circ_n_cells", test_circuit_n_cells, TT_FORK, NULL, NULL },
156 END_OF_TESTCASES