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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
#include "config.h"
#include "cunit/cyrunit.h"
#include "imap/imap_err.h"
#include "prot.h"
#include "retry.h"
#include "xmalloc.h"
#include "lib/libconfig.h"
#include "lib/libcyr_cfg.h"
#include "imap/spool.h"
#include "xunlink.h"
#define DBDIR "test-dbdir"
#define DELIVERED "Fri, 29 Oct 2010 13:07:07 +1100"
#define FIRST_RX "Fri, 29 Oct 2010 13:05:01 +1100"
#define SECOND_RX "Fri, 29 Oct 2010 13:03:03 +1100"
#define THIRD_RX "Fri, 29 Oct 2010 13:01:01 +1100"
#define SENT "Thu, 28 Oct 2010 18:37:26 +1100"
#define HFROM "Fred Bloggs <fbloggs@fastmail.fm>"
#define HFROMFOLD "Fred Bloggs\r\n <fbloggs@fastmail.fm>"
#define HFROM2 "Antoine Lavoisier <lavoisier@chemistry.fr>"
#define HTO "Sarah Jane Smith <sjsmith@gmail.com>"
#define HDATE SENT
#define HSUBJECT "Simple testing email"
#define HMESSAGEID "<fake1000@fastmail.fm>"
#define HRECEIVED1 "from mail.quux.com (mail.quux.com [10.0.0.1]) by mail.gmail.com (Software); " FIRST_RX
#define HRECEIVED2 "from mail.bar.com (mail.bar.com [10.0.0.1]) by mail.quux.com (Software); " SECOND_RX
#define HRECEIVED3 "from mail.fastmail.fm (mail.fastmail.fm [10.0.0.1]) by mail.bar.com (Software); " THIRD_RX
static int set_up(void)
{
/* need basic configuration for parseheader */
libcyrus_config_setstring(CYRUSOPT_CONFIG_DIR, DBDIR);
config_read_string(
"configdirectory: "DBDIR"/conf\n"
);
return 0;
}
static int tear_down(void)
{
int r;
config_reset();
r = system("rm -rf " DBDIR);
return r;
}
static void test_simple(void)
{
hdrcache_t cache;
const char **val;
cache = spool_new_hdrcache();
CU_ASSERT_PTR_NOT_NULL(cache);
val = spool_getheader(cache, "Nonesuch");
CU_ASSERT_PTR_NULL(val);
val = spool_getheader(cache, "From");
CU_ASSERT_PTR_NULL(val);
val = spool_getheader(cache, "fRoM");
CU_ASSERT_PTR_NULL(val);
val = spool_getheader(cache, "from");
CU_ASSERT_PTR_NULL(val);
spool_cache_header(xstrdup("From"), xstrdup(HFROM), cache);
val = spool_getheader(cache, "Nonesuch");
CU_ASSERT_PTR_NULL(val);
val = spool_getheader(cache, "From");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "fRoM");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "from");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
spool_cache_header(xstrdup("To"), xstrdup(HTO), cache);
spool_cache_header(xstrdup("Date"), xstrdup(HDATE), cache);
spool_cache_header(xstrdup("Subject"), xstrdup(HSUBJECT), cache);
spool_cache_header(xstrdup("Message-ID"), xstrdup(HMESSAGEID), cache);
spool_cache_header(xstrdup("Received"), xstrdup(HRECEIVED1), cache);
spool_cache_header(xstrdup("Received"), xstrdup(HRECEIVED2), cache);
spool_cache_header(xstrdup("Received"), xstrdup(HRECEIVED3), cache);
val = spool_getheader(cache, "Nonesuch");
CU_ASSERT_PTR_NULL(val);
val = spool_getheader(cache, "From");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "fRoM");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "from");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "To");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HTO);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "Subject");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HSUBJECT);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "message-id");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HMESSAGEID);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "received");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HRECEIVED1);
CU_ASSERT_STRING_EQUAL(val[1], HRECEIVED2);
CU_ASSERT_STRING_EQUAL(val[2], HRECEIVED3);
CU_ASSERT_PTR_NULL(val[3]);
spool_free_hdrcache(cache);
}
static void test_fill(void)
{
static const char MSG[] =
"From: " HFROM "\r\n"
"To: " HTO "\r\n"
"Date: " HDATE "\r\n"
"Subject: " HSUBJECT "\r\n"
"Message-ID: " HMESSAGEID "\r\n"
"Received: " HRECEIVED1 "\r\n"
"Received: " HRECEIVED2 "\r\n"
"Received: " HRECEIVED3 "\r\n"
"\r\n"
"Hello, World\r\n";
hdrcache_t cache;
const char **val;
int fd;
char tempfile[32];
int r;
struct protstream *pin;
FILE *fout;
/* Setup @pin to point to the start of a file open for (at least)
* reading containing the message. */
strcpy(tempfile, "/tmp/spooltestAXXXXXX");
fd = mkstemp(tempfile);
CU_ASSERT(fd >= 0);
r = retry_write(fd, MSG, sizeof(MSG)-1);
CU_ASSERT_EQUAL(r, sizeof(MSG)-1);
lseek(fd, SEEK_SET, 0);
pin = prot_new(fd, /*read*/0);
CU_ASSERT_PTR_NOT_NULL(pin);
/* Setup @fout to ignore data written to it */
fout = fopen("/dev/null", "w");
CU_ASSERT_PTR_NOT_NULL(fout);
cache = spool_new_hdrcache();
CU_ASSERT_PTR_NOT_NULL(cache);
/* TODO: test non-NULL skipheaders */
r = spool_fill_hdrcache(pin, fout, cache, NULL);
CU_ASSERT_EQUAL(r, 0);
val = spool_getheader(cache, "Nonesuch");
CU_ASSERT_PTR_NULL(val);
val = spool_getheader(cache, "From");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "fRoM");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "from");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "To");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HTO);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "Subject");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HSUBJECT);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "message-id");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HMESSAGEID);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "received");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HRECEIVED1);
CU_ASSERT_STRING_EQUAL(val[1], HRECEIVED2);
CU_ASSERT_STRING_EQUAL(val[2], HRECEIVED3);
CU_ASSERT_PTR_NULL(val[3]);
spool_free_hdrcache(cache);
fclose(fout);
prot_free(pin);
xunlink(tempfile);
}
static void test_folded_headers(void)
{
static const char MSG[] =
"From: " HFROMFOLD "\r\n" /* mid-value folding */
"Message-ID:\r\n " HMESSAGEID "\r\n" /* leading whitespace is folded */
"\r\n"
"Hello, World\r\n";
hdrcache_t cache;
const char **val;
int fd;
char tempfile[32];
int r;
struct protstream *pin;
FILE *fout;
/* Setup @pin to point to the start of a file open for (at least)
* reading containing the message. */
strcpy(tempfile, "/tmp/spooltestAXXXXXX");
fd = mkstemp(tempfile);
CU_ASSERT(fd >= 0);
r = retry_write(fd, MSG, sizeof(MSG)-1);
CU_ASSERT_EQUAL(r, sizeof(MSG)-1);
lseek(fd, SEEK_SET, 0);
pin = prot_new(fd, /*read*/0);
CU_ASSERT_PTR_NOT_NULL(pin);
/* Setup @fout to ignore data written to it */
fout = fopen("/dev/null", "w");
CU_ASSERT_PTR_NOT_NULL(fout);
cache = spool_new_hdrcache();
CU_ASSERT_PTR_NOT_NULL(cache);
/* TODO: test non-NULL skipheaders */
r = spool_fill_hdrcache(pin, fout, cache, NULL);
CU_ASSERT_EQUAL(r, 0);
val = spool_getheader(cache, "Nonesuch");
CU_ASSERT_PTR_NULL(val);
val = spool_getheader(cache, "From");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "fRoM");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "from");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "message-id");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HMESSAGEID);
CU_ASSERT_PTR_NULL(val[1]);
spool_free_hdrcache(cache);
fclose(fout);
prot_free(pin);
xunlink(tempfile);
}
static void test_empty_headers(void)
{
static const char MSG[] =
"From: " HFROM "\r\n"
"Message-ID: " HMESSAGEID "\r\n"
"Empty1:\r\n" /* not even a leading space */
"Empty2: \r\n" /* leading space */
"Empty3:\r\n \r\n" /* folded leading space */
"Empty4:\r" /* same, with bare CR */
"Empty5: \r"
"Empty6:\r \r"
"Empty7:\n" /* same, with bare LF */
"Empty8: \n"
"Empty9:\n \n"
"\r\n"
"Hello, World\r\n";
hdrcache_t cache;
const char **val;
int fd;
char tempfile[32];
int r;
struct protstream *pin;
FILE *fout;
/* Setup @pin to point to the start of a file open for (at least)
* reading containing the message. */
strcpy(tempfile, "/tmp/spooltestAXXXXXX");
fd = mkstemp(tempfile);
CU_ASSERT(fd >= 0);
r = retry_write(fd, MSG, sizeof(MSG)-1);
CU_ASSERT_EQUAL(r, sizeof(MSG)-1);
lseek(fd, SEEK_SET, 0);
pin = prot_new(fd, /*read*/0);
CU_ASSERT_PTR_NOT_NULL(pin);
/* Setup @fout to ignore data written to it */
fout = fopen("/dev/null", "w");
CU_ASSERT_PTR_NOT_NULL(fout);
cache = spool_new_hdrcache();
CU_ASSERT_PTR_NOT_NULL(cache);
/* TODO: test non-NULL skipheaders */
r = spool_fill_hdrcache(pin, fout, cache, NULL);
CU_ASSERT_EQUAL(r, 0);
val = spool_getheader(cache, "Nonesuch");
CU_ASSERT_PTR_NULL(val);
val = spool_getheader(cache, "From");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "fRoM");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "from");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "message-id");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HMESSAGEID);
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "empty1");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], "");
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "empty2");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], "");
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "empty3");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], "");
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "empty4");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], "");
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "empty5");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], "");
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "empty6");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], "");
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "empty7");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], "");
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "empty8");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], "");
CU_ASSERT_PTR_NULL(val[1]);
val = spool_getheader(cache, "empty9");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], "");
CU_ASSERT_PTR_NULL(val[1]);
spool_free_hdrcache(cache);
fclose(fout);
prot_free(pin);
xunlink(tempfile);
}
/* BZ3640: headers with NULL bytes shall be rejected. */
static void test_fill_null(void)
{
static const char MSG[] =
"From: " HFROM "\r\n"
"To: " HTO "\r\n"
"Date: " HDATE "\r\n"
"Subject: " HSUBJECT "\r\n"
"Message-ID: " HMESSAGEID "\r\n"
"Received: " HRECEIVED1 "\r\n"
"Received:\0" HRECEIVED2 "\r\n"
"Received: " HRECEIVED3 "\r\n"
"\r\n"
"Hello, World\r\n";
hdrcache_t cache;
int fd;
char tempfile[32];
int r;
struct protstream *pin;
FILE *fout;
/* Setup @pin to point to the start of a file open for (at least)
* reading containing the message. */
strcpy(tempfile, "/tmp/spooltestAXXXXXX");
fd = mkstemp(tempfile);
CU_ASSERT(fd >= 0);
r = retry_write(fd, MSG, sizeof(MSG)-1);
CU_ASSERT_EQUAL(r, sizeof(MSG)-1);
lseek(fd, SEEK_SET, 0);
pin = prot_new(fd, /*read*/0);
CU_ASSERT_PTR_NOT_NULL(pin);
/* Setup @fout to ignore data written to it */
fout = fopen("/dev/null", "w");
CU_ASSERT_PTR_NOT_NULL(fout);
cache = spool_new_hdrcache();
CU_ASSERT_PTR_NOT_NULL(cache);
r = spool_fill_hdrcache(pin, fout, cache, NULL);
CU_ASSERT_EQUAL(r, IMAP_MESSAGE_CONTAINSNULL);
spool_free_hdrcache(cache);
fclose(fout);
prot_free(pin);
xunlink(tempfile);
}
/* BZ3386: insert more unique headers than the internal limit of 4009
* headers, and see what happens. */
static void test_bz3386(void)
{
hdrcache_t cache;
#define N 5000
int i;
char name[32];
char body[128];
char body2[128]; /* use a different buffer Just In Case */
const char **val;
cache = spool_new_hdrcache();
CU_ASSERT_PTR_NOT_NULL(cache);
for (i = 0 ; i < N ; i++) {
snprintf(name, sizeof(name), "X-Foo-%d-%c", i, 'A'+(i%26));
snprintf(body, sizeof(body), "value %d %c", i, 'A'+(i%26));
spool_cache_header(xstrdup(name), xstrdup(body), cache);
}
strcpy(body, "Old Buffer");
for (i = 0 ; i < N ; i++) {
snprintf(name, sizeof(name), "X-Foo-%d-%c", i, 'A'+(i%26));
snprintf(body2, sizeof(body2), "value %d %c", i, 'A'+(i%26));
val = spool_getheader(cache, name);
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], body2);
CU_ASSERT_PTR_NULL(val[1]);
}
spool_free_hdrcache(cache);
#undef N
}
static void test_replace(void)
{
#define FOOBAR1 "If music be the foo of love"
#define FOOBAR2 "Foo glorious foo"
#define FOOBAR3 "Foo is an important part of a balanced diet"
/* apologies to Fran Liebowitz */
#define FOOBAR4 "Foo and Drug Administration"
hdrcache_t cache;
const char **val;
cache = spool_new_hdrcache();
CU_ASSERT_PTR_NOT_NULL(cache);
val = spool_getheader(cache, "From");
CU_ASSERT_PTR_NULL(val);
/* replace a single line */
spool_cache_header(xstrdup("From"), xstrdup(HFROM), cache);
val = spool_getheader(cache, "From");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM);
CU_ASSERT_PTR_NULL(val[1]);
spool_replace_header(xstrdup("From"), xstrdup(HFROM2), cache);
val = spool_getheader(cache, "From");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], HFROM2);
CU_ASSERT_PTR_NULL(val[1]);
/* replace multiple lines with one */
spool_cache_header(xstrdup("X-FooBar"), xstrdup(FOOBAR1), cache);
spool_cache_header(xstrdup("X-FooBar"), xstrdup(FOOBAR2), cache);
spool_cache_header(xstrdup("X-FooBar"), xstrdup(FOOBAR3), cache);
val = spool_getheader(cache, "X-FooBar");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], FOOBAR1);
CU_ASSERT_STRING_EQUAL(val[1], FOOBAR2);
CU_ASSERT_STRING_EQUAL(val[2], FOOBAR3);
CU_ASSERT_PTR_NULL(val[3]);
spool_replace_header(xstrdup("X-FooBar"), xstrdup(FOOBAR4), cache);
val = spool_getheader(cache, "X-FooBar");
CU_ASSERT_PTR_NOT_NULL(val);
CU_ASSERT_STRING_EQUAL(val[0], FOOBAR4);
CU_ASSERT_PTR_NULL(val[1]);
spool_free_hdrcache(cache);
#undef FOOBAR1
#undef FOOBAR2
#undef FOOBAR3
#undef FOOBAR4
}
/* vim: set ft=c: */
|