1 /* Copyright (c) 2013-2016, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define CIRCUITLIST_PRIVATE
7 #include "circuitlist.h"
12 test_cq_manip(void *arg
)
14 packed_cell_t
*pc1
=NULL
, *pc2
=NULL
, *pc3
=NULL
, *pc4
=NULL
, *pc_tmp
=NULL
;
20 tt_int_op(cq
.n
, OP_EQ
, 0);
22 pc1
= packed_cell_new();
23 pc2
= packed_cell_new();
24 pc3
= packed_cell_new();
25 pc4
= packed_cell_new();
26 tt_assert(pc1
&& pc2
&& pc3
&& pc4
);
28 tt_ptr_op(NULL
, OP_EQ
, cell_queue_pop(&cq
));
30 /* Add and remove a singleton. */
31 cell_queue_append(&cq
, pc1
);
32 tt_int_op(cq
.n
, OP_EQ
, 1);
33 tt_ptr_op(pc1
, OP_EQ
, cell_queue_pop(&cq
));
34 tt_int_op(cq
.n
, OP_EQ
, 0);
36 /* Add and remove four items */
37 cell_queue_append(&cq
, pc4
);
38 cell_queue_append(&cq
, pc3
);
39 cell_queue_append(&cq
, pc2
);
40 cell_queue_append(&cq
, pc1
);
41 tt_int_op(cq
.n
, OP_EQ
, 4);
42 tt_ptr_op(pc4
, OP_EQ
, cell_queue_pop(&cq
));
43 tt_ptr_op(pc3
, OP_EQ
, cell_queue_pop(&cq
));
44 tt_ptr_op(pc2
, OP_EQ
, cell_queue_pop(&cq
));
45 tt_ptr_op(pc1
, OP_EQ
, cell_queue_pop(&cq
));
46 tt_int_op(cq
.n
, OP_EQ
, 0);
47 tt_ptr_op(NULL
, OP_EQ
, cell_queue_pop(&cq
));
49 /* Try a packed copy (wide, then narrow, which is a bit of a cheat, since a
50 * real cell queue has only one type.) */
51 memset(&cell
, 0, sizeof(cell
));
52 cell
.circ_id
= 0x12345678;
54 strlcpy((char*)cell
.payload
, "Lorax ipsum gruvvulus thneed amet, snergelly "
55 "once-ler lerkim, sed do barbaloot tempor gluppitus ut labore et "
56 "truffula magna aliqua.",
57 sizeof(cell
.payload
));
58 cell_queue_append_packed_copy(NULL
/*circ*/, &cq
, 0 /*exitward*/, &cell
,
59 1 /*wide*/, 0 /*stats*/);
60 cell
.circ_id
= 0x2013;
61 cell_queue_append_packed_copy(NULL
/*circ*/, &cq
, 0 /*exitward*/, &cell
,
62 0 /*wide*/, 0 /*stats*/);
63 tt_int_op(cq
.n
, OP_EQ
, 2);
65 pc_tmp
= cell_queue_pop(&cq
);
66 tt_int_op(cq
.n
, OP_EQ
, 1);
67 tt_ptr_op(pc_tmp
, OP_NE
, NULL
);
68 tt_mem_op(pc_tmp
->body
, OP_EQ
, "\x12\x34\x56\x78\x0a", 5);
69 tt_mem_op(pc_tmp
->body
+5, OP_EQ
, cell
.payload
, sizeof(cell
.payload
));
70 packed_cell_free(pc_tmp
);
72 pc_tmp
= cell_queue_pop(&cq
);
73 tt_int_op(cq
.n
, OP_EQ
, 0);
74 tt_ptr_op(pc_tmp
, OP_NE
, NULL
);
75 tt_mem_op(pc_tmp
->body
, OP_EQ
, "\x20\x13\x0a", 3);
76 tt_mem_op(pc_tmp
->body
+3, OP_EQ
, cell
.payload
, sizeof(cell
.payload
));
77 packed_cell_free(pc_tmp
);
80 tt_ptr_op(NULL
, OP_EQ
, cell_queue_pop(&cq
));
82 /* Now make sure cell_queue_clear works. */
83 cell_queue_append(&cq
, pc2
);
84 cell_queue_append(&cq
, pc1
);
85 tt_int_op(cq
.n
, OP_EQ
, 2);
86 cell_queue_clear(&cq
);
87 pc2
= pc1
= NULL
; /* prevent double-free */
88 tt_int_op(cq
.n
, OP_EQ
, 0);
91 packed_cell_free(pc1
);
92 packed_cell_free(pc2
);
93 packed_cell_free(pc3
);
94 packed_cell_free(pc4
);
95 packed_cell_free(pc_tmp
);
97 cell_queue_clear(&cq
);
101 test_circuit_n_cells(void *arg
)
103 packed_cell_t
*pc1
=NULL
, *pc2
=NULL
, *pc3
=NULL
, *pc4
=NULL
, *pc5
=NULL
;
104 origin_circuit_t
*origin_c
=NULL
;
105 or_circuit_t
*or_c
=NULL
;
109 pc1
= packed_cell_new();
110 pc2
= packed_cell_new();
111 pc3
= packed_cell_new();
112 pc4
= packed_cell_new();
113 pc5
= packed_cell_new();
114 tt_assert(pc1
&& pc2
&& pc3
&& pc4
&& pc5
);
116 or_c
= or_circuit_new(0, NULL
);
117 origin_c
= origin_circuit_new();
118 origin_c
->base_
.purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
120 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c
)), OP_EQ
, 0);
121 cell_queue_append(&or_c
->p_chan_cells
, pc1
);
122 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c
)), OP_EQ
, 1);
123 cell_queue_append(&or_c
->base_
.n_chan_cells
, pc2
);
124 cell_queue_append(&or_c
->base_
.n_chan_cells
, pc3
);
125 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c
)), OP_EQ
, 3);
127 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c
)), OP_EQ
, 0);
128 cell_queue_append(&origin_c
->base_
.n_chan_cells
, pc4
);
129 cell_queue_append(&origin_c
->base_
.n_chan_cells
, pc5
);
130 tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c
)), OP_EQ
, 2);
133 circuit_free(TO_CIRCUIT(or_c
));
134 circuit_free(TO_CIRCUIT(origin_c
));
137 struct testcase_t cell_queue_tests
[] = {
138 { "basic", test_cq_manip
, TT_FORK
, NULL
, NULL
, },
139 { "circ_n_cells", test_circuit_n_cells
, TT_FORK
, NULL
, NULL
},