| 1 |
/* |
| 2 |
* Test D-Bus Input/Output Queues |
| 3 |
*/ |
| 4 |
|
| 5 |
#undef NDEBUG |
| 6 |
#include <c-stdaux.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include "dbus/queue.h" |
| 9 |
#include "util/fdlist.h" |
| 10 |
#include "util/user.h" |
| 11 |
|
| 12 |
#define TEST_8 "01234567" |
| 13 |
#define TEST_64 TEST_8 TEST_8 TEST_8 TEST_8 TEST_8 TEST_8 TEST_8 TEST_8 |
| 14 |
#define TEST_512 TEST_64 TEST_64 TEST_64 TEST_64 TEST_64 TEST_64 TEST_64 TEST_64 |
| 15 |
#define TEST_2k TEST_512 TEST_512 TEST_512 TEST_512 |
| 16 |
#define TEST_8k TEST_2k TEST_2k TEST_2k TEST_2k |
| 17 |
#define TEST_32k TEST_8k TEST_8k TEST_8k TEST_8k |
| 18 |
|
| 19 |
static void test_in_setup(void) { |
| 20 |
_c_cleanup_(iqueue_deinit) IQueue iq = IQUEUE_NULL(iq); |
| 21 |
|
| 22 |
iqueue_init(&iq, NULL); |
| 23 |
iqueue_deinit(&iq); |
| 24 |
} |
| 25 |
|
| 26 |
static void test_in_special(void) { |
| 27 |
_c_cleanup_(iqueue_deinit) IQueue iq = IQUEUE_NULL(iq); |
| 28 |
int r; |
| 29 |
|
| 30 |
iqueue_init(&iq, NULL); |
| 31 |
|
| 32 |
/* |
| 33 |
* Push as much data into the iqueue as possible. Make sure none of it |
| 34 |
* includes \r\n, hence, it is treated as a single line. Then verify |
| 35 |
* that after more than 16k it bails out with a protocol violation. |
| 36 |
*/ |
| 37 |
{ |
| 38 |
static const char blob[] = TEST_32k; |
| 39 |
UserCharge *charge_fds; |
| 40 |
size_t n, total, *from, to; |
| 41 |
const char *l; |
| 42 |
void *buffer; |
| 43 |
FDList **fds; |
| 44 |
|
| 45 |
total = 0; |
| 46 |
for (;;) { |
| 47 |
r = iqueue_get_cursor(&iq, |
| 48 |
&buffer, |
| 49 |
&from, |
| 50 |
&to, |
| 51 |
&fds, |
| 52 |
&charge_fds); |
| 53 |
if (r == IQUEUE_E_VIOLATION) |
| 54 |
break; |
| 55 |
|
| 56 |
c_assert(!r); |
| 57 |
|
| 58 |
n = c_min(to - *from, sizeof(blob)); |
| 59 |
if (!n) |
| 60 |
break; |
| 61 |
|
| 62 |
c_memcpy(buffer + *from, blob, n); |
| 63 |
*from += n; |
| 64 |
total += n; |
| 65 |
|
| 66 |
r = iqueue_pop_line(&iq, &l, &n); |
| 67 |
c_assert(r == IQUEUE_E_PENDING); |
| 68 |
} |
| 69 |
|
| 70 |
c_assert(total == IQUEUE_LINE_MAX); |
| 71 |
} |
| 72 |
|
| 73 |
iqueue_deinit(&iq); |
| 74 |
iqueue_init(&iq, NULL); |
| 75 |
|
| 76 |
/* |
| 77 |
* Verify `NULL' is correctly handled when fetching FDs. That is, the |
| 78 |
* FDs stay accounted and pinned in the queue and are handed over to |
| 79 |
* the next buffer. |
| 80 |
* |
| 81 |
* We push in this sequence: |
| 82 |
* |
| 83 |
* * 1 byte + 1 FD |
| 84 |
* * 2 bytes + 1 FD |
| 85 |
* |
| 86 |
* We dequeue them as: |
| 87 |
* |
| 88 |
* * 1 byte + NULL |
| 89 |
* * 1 byte + FD |
| 90 |
* * 1 byte + FD |
| 91 |
* |
| 92 |
* That is, we pretend the first read byte wants to delay FD receiption |
| 93 |
* to the next buffer, so we pass NULL. We then verify that the iqueue |
| 94 |
* does the right thing. |
| 95 |
*/ |
| 96 |
{ |
| 97 |
char data[128]; |
| 98 |
UserCharge *charge_fds; |
| 99 |
size_t *from, to; |
| 100 |
void *buffer; |
| 101 |
FDList **fds, *f; |
| 102 |
|
| 103 |
/* push in 1 byte with 1 fd */ |
| 104 |
r = iqueue_get_cursor(&iq, |
| 105 |
&buffer, |
| 106 |
&from, |
| 107 |
&to, |
| 108 |
&fds, |
| 109 |
&charge_fds); |
| 110 |
c_assert(!r); |
| 111 |
c_assert(to - *from >= 128); |
| 112 |
|
| 113 |
c_memcpy(buffer + *from, (char [1]){}, 1); |
| 114 |
*from += 1; |
| 115 |
r = fdlist_new_with_fds(fds, (int [1]){}, 1); |
| 116 |
c_assert(!r); |
| 117 |
|
| 118 |
/* verify further pushes are rejected until it is parsed */ |
| 119 |
r = iqueue_get_cursor(&iq, |
| 120 |
&buffer, |
| 121 |
&from, |
| 122 |
&to, |
| 123 |
&fds, |
| 124 |
&charge_fds); |
| 125 |
c_assert(r == IQUEUE_E_PENDING); |
| 126 |
|
| 127 |
/* set 1 byte target and retrieve without fds */ |
| 128 |
r = iqueue_set_target(&iq, data, 1); |
| 129 |
c_assert(!r); |
| 130 |
|
| 131 |
r = iqueue_pop_data(&iq, NULL); |
| 132 |
c_assert(!r); |
| 133 |
c_assert(!*data); |
| 134 |
|
| 135 |
/* set next 1 byte target and verify nothing is pending */ |
| 136 |
r = iqueue_set_target(&iq, data, 1); |
| 137 |
c_assert(!r); |
| 138 |
|
| 139 |
r = iqueue_pop_data(&iq, NULL); |
| 140 |
c_assert(r == IQUEUE_E_PENDING); |
| 141 |
|
| 142 |
/* push in 2 more bytes with FDs */ |
| 143 |
r = iqueue_get_cursor(&iq, |
| 144 |
&buffer, |
| 145 |
&from, |
| 146 |
&to, |
| 147 |
&fds, |
| 148 |
&charge_fds); |
| 149 |
c_assert(!r); |
| 150 |
c_assert(to - *from >= 128); |
| 151 |
|
| 152 |
c_memcpy(buffer + *from, (char [2]){}, 2); |
| 153 |
*from += 2; |
| 154 |
r = fdlist_new_with_fds(fds, (int [1]){ 1 }, 1); |
| 155 |
c_assert(!r); |
| 156 |
|
| 157 |
/* fetch 1 byte target and verify it got the *OLD* fd */ |
| 158 |
r = iqueue_pop_data(&iq, &f); |
| 159 |
c_assert(!r); |
| 160 |
c_assert(!*data); |
| 161 |
c_assert(fdlist_count(f) == 1); |
| 162 |
c_assert(fdlist_get(f, 0) == 0); |
| 163 |
fdlist_free(f); |
| 164 |
|
| 165 |
/* fetch 1 byte target and verify it got the *NEW* fd */ |
| 166 |
r = iqueue_set_target(&iq, data, 1); |
| 167 |
c_assert(!r); |
| 168 |
|
| 169 |
r = iqueue_pop_data(&iq, &f); |
| 170 |
c_assert(!r); |
| 171 |
c_assert(!*data); |
| 172 |
c_assert(fdlist_count(f) == 1); |
| 173 |
c_assert(fdlist_get(f, 0) == 1); |
| 174 |
fdlist_free(f); |
| 175 |
} |
| 176 |
|
| 177 |
iqueue_deinit(&iq); |
| 178 |
iqueue_init(&iq, NULL); |
| 179 |
|
| 180 |
/* |
| 181 |
* Test receival of empty blobs. Here we send 1 byte blob with FDs. We |
| 182 |
* retrieve the byte WITHOUT FD, followed by a request target of 0 |
| 183 |
* bytes *WITH* FDs. |
| 184 |
*/ |
| 185 |
{ |
| 186 |
char data[128]; |
| 187 |
UserCharge *charge_fds; |
| 188 |
size_t *from, to; |
| 189 |
void *buffer; |
| 190 |
FDList **fds, *f; |
| 191 |
|
| 192 |
/* push in 1 byte with 1 fd */ |
| 193 |
r = iqueue_get_cursor(&iq, |
| 194 |
&buffer, |
| 195 |
&from, |
| 196 |
&to, |
| 197 |
&fds, |
| 198 |
&charge_fds); |
| 199 |
c_assert(!r); |
| 200 |
c_assert(to - *from >= 128); |
| 201 |
|
| 202 |
c_memcpy(buffer + *from, (char [1]){}, 1); |
| 203 |
*from += 1; |
| 204 |
r = fdlist_new_with_fds(fds, (int [1]){}, 1); |
| 205 |
c_assert(!r); |
| 206 |
|
| 207 |
/* set 1 byte target and retrieve without fds */ |
| 208 |
r = iqueue_set_target(&iq, data, 1); |
| 209 |
c_assert(!r); |
| 210 |
|
| 211 |
r = iqueue_pop_data(&iq, NULL); |
| 212 |
c_assert(!r); |
| 213 |
c_assert(!*data); |
| 214 |
|
| 215 |
/* set next 0 byte target and retieve FD */ |
| 216 |
r = iqueue_set_target(&iq, data, 0); |
| 217 |
c_assert(!r); |
| 218 |
|
| 219 |
r = iqueue_pop_data(&iq, &f); |
| 220 |
c_assert(!r); |
| 221 |
c_assert(fdlist_count(f) == 1); |
| 222 |
c_assert(fdlist_get(f, 0) == 0); |
| 223 |
fdlist_free(f); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
static void test_in_lines(void) { |
| 228 |
static const char *send = { |
| 229 |
"foo\r\n" |
| 230 |
|
| 231 |
"\r\n" |
| 232 |
|
| 233 |
"bar\r\n" |
| 234 |
|
| 235 |
"\r \n\r\r\n" |
| 236 |
|
| 237 |
"foo bar foo bar foo bar\r\n" |
| 238 |
|
| 239 |
/* 512 + 2 */ |
| 240 |
TEST_512 "\r\n" |
| 241 |
|
| 242 |
"random\r\n" |
| 243 |
|
| 244 |
/* 8k + 2 */ |
| 245 |
TEST_8k "\r\n" |
| 246 |
|
| 247 |
"foobar\r\n" |
| 248 |
}; |
| 249 |
static const char *expect[] = { |
| 250 |
"foo", |
| 251 |
|
| 252 |
"", |
| 253 |
|
| 254 |
"bar", |
| 255 |
|
| 256 |
"\r \n\r", |
| 257 |
|
| 258 |
"foo bar foo bar foo bar", |
| 259 |
|
| 260 |
TEST_512, |
| 261 |
|
| 262 |
"random", |
| 263 |
|
| 264 |
TEST_8k, |
| 265 |
|
| 266 |
"foobar", |
| 267 |
}; |
| 268 |
_c_cleanup_(iqueue_deinit) IQueue iq = IQUEUE_NULL(iq); |
| 269 |
size_t i, n, i_send, i_expect; |
| 270 |
int r; |
| 271 |
|
| 272 |
iqueue_init(&iq, NULL); |
| 273 |
|
| 274 |
/* |
| 275 |
* Test Line Tokenizer |
| 276 |
* |
| 277 |
* Randomly chunk the data in @send and push it into @iq. Verify that |
| 278 |
* the tokenizer ends up with @expect. |
| 279 |
* |
| 280 |
* This verifies that regardless how the data is chunked, we correctly |
| 281 |
* tokenize the lines via iqueue. This also verifies that large lines |
| 282 |
* are correctly handled and end up in the resized line buffer. |
| 283 |
*/ |
| 284 |
|
| 285 |
for (i = 0; i < 0x1fff; ++i) { |
| 286 |
i_send = 0; |
| 287 |
i_expect = 0; |
| 288 |
|
| 289 |
do { |
| 290 |
/* push random chunk from @send into @iq */ |
| 291 |
{ |
| 292 |
UserCharge *charge_fds; |
| 293 |
size_t *from, to; |
| 294 |
void *buffer; |
| 295 |
FDList **fds; |
| 296 |
|
| 297 |
r = iqueue_get_cursor(&iq, |
| 298 |
&buffer, |
| 299 |
&from, |
| 300 |
&to, |
| 301 |
&fds, |
| 302 |
&charge_fds); |
| 303 |
c_assert(!r); |
| 304 |
c_assert(to > *from); |
| 305 |
|
| 306 |
n = to - *from; |
| 307 |
n = rand() % c_min(n, strlen(send) - i_send); |
| 308 |
++n; |
| 309 |
|
| 310 |
c_memcpy(buffer + *from, send + i_send, n); |
| 311 |
i_send += n; |
| 312 |
*from += n; |
| 313 |
} |
| 314 |
|
| 315 |
/* dequeue as many lines from @iq as possible */ |
| 316 |
for (;;) { |
| 317 |
const char *l; |
| 318 |
|
| 319 |
r = iqueue_pop_line(&iq, &l, &n); |
| 320 |
if (r == IQUEUE_E_PENDING) |
| 321 |
break; |
| 322 |
|
| 323 |
c_assert(!r); |
| 324 |
c_assert(n == strlen(expect[i_expect])); |
| 325 |
c_assert(!memcmp(l, expect[i_expect], n)); |
| 326 |
++i_expect; |
| 327 |
} |
| 328 |
} while (i_send < strlen(send)); |
| 329 |
|
| 330 |
c_assert(i_expect == C_ARRAY_SIZE(expect)); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
int main(int argc, char **argv) { |
| 335 |
srand(0xabcdef); |
| 336 |
|
| 337 |
test_in_setup(); |
| 338 |
test_in_special(); |
| 339 |
test_in_lines(); |
| 340 |
|
| 341 |
return 0; |
| 342 |
} |