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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
|
/*
SPDX-FileCopyrightText: 2006 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "contenttest.h"
#include <QDebug>
#include <QTest>
#include "content.h"
#include "headers.h"
#include "message.h"
#include <type_traits>
using namespace KMime;
using namespace Qt::Literals;
QTEST_MAIN(ContentTest)
void ContentTest::testGetHeaderInstance()
{
// stuff that looks trivial but breaks if you mess with virtual method signatures (see r534381)
auto myfrom = new Headers::From();
QCOMPARE(myfrom->type(), "From");
Headers::Base *mybase = myfrom;
QCOMPARE(mybase->type(), "From");
delete myfrom;
// getHeaderInstance() is protected, so we need to test it via KMime::Message
auto c = new Message();
Headers::From *f1 = c->from(true);
Headers::From *f2 = c->from(true);
QCOMPARE(f1, f2);
delete c;
}
void ContentTest::testHeaderAddRemove()
{
// Add a Content-Description header to a content.
auto c = new Content;
QVERIFY(!c->contentDescription(false));
c->contentDescription()->from7BitString("description");
// The content must now have the header.
QVERIFY(c->contentDescription(false));
QCOMPARE(c->contentDescription()->as7BitString(false), QByteArray("description"));
// The content's head must also have the header. Save the head.
c->assemble();
QByteArray head = c->head();
// Clear the content. It must now forget the cached header.
c->clear();
QVERIFY(c->head().isEmpty());
QVERIFY(!c->contentDescription(false));
// Put the head back. It must now remember the header.
c->setHead(head);
QVERIFY(!c->contentDescription(false));
c->parse();
QVERIFY(c->contentDescription(false));
c->contentDescription()->from7BitString("description");
// Now remove the header explicitly.
bool ret = c->removeHeader("Content-Description");
QVERIFY(ret);
// The content must have forgotten the header now.
QVERIFY(!c->contentDescription(false));
// And after assembly, the header should stay gone.
c->assemble();
//QVERIFY(c->head().isEmpty());
QVERIFY(!c->contentDescription(false));
delete c;
// test template versions
Content c2;
QVERIFY(!c2.hasHeader(KMime::Headers::Lines::staticType()));
QVERIFY(!c2.header<KMime::Headers::Lines>(false));
QVERIFY(c2.header<KMime::Headers::Lines>(true));
QVERIFY(c2.hasHeader(KMime::Headers::Lines::staticType()));
c2.removeHeader<KMime::Headers::Lines>();
QVERIFY(!c2.hasHeader(KMime::Headers::Lines::staticType()));
}
void ContentTest::testHeaderAppend()
{
auto c = new Content;
QByteArray d1("Resent-From: test1@example.com");
QByteArray d2("Resent-From: test2@example.com");
auto h1 = new Headers::Generic("Resent-From");
h1->from7BitString("test1@example.com");
auto h2 = new Headers::Generic("Resent-From");
h2->from7BitString("test2@example.com");
c->appendHeader(h1);
c->appendHeader(h2);
c->assemble();
QByteArray head = d1 + '\n' + d2 + '\n';
QCOMPARE(c->head(), head);
QByteArray d3("Resent-From: test3@example.com");
auto h3 = new Headers::Generic("Resent-From");
h3->from7BitString("test3@example.com");
c->appendHeader(h3);
c->assemble();
head.append(d3 + '\n');
QCOMPARE(c->head(), head);
delete c;
}
void ContentTest::testExplicitMultipartGeneration()
{
auto c1 = new Content();
c1->contentType()->from7BitString("multipart/mixed");
auto c2 = new Content();
c2->contentType()->from7BitString("text/plain");
c2->setBody("textpart");
auto c3 = new Content();
c3->contentType()->from7BitString("text/html");
c3->setBody("htmlpart");
c1->appendContent(c2);
c1->appendContent(c3);
// c1 should not have been changed.
QCOMPARE(c1->contentType()->mimeType(), QByteArray("multipart/mixed"));
QVERIFY(c1->body().isEmpty());
QCOMPARE(c1->contents().count(), 2);
QCOMPARE(c1->contents().at(0), c2);
QCOMPARE(c1->contents().at(1), c3);
c1->takeContent(c3);
QCOMPARE(c1->contents().count(), 1);
QCOMPARE(c1->contentType()->mimeType(), QByteArray("multipart/mixed"));
QVERIFY(c1->body().isEmpty());
QCOMPARE(c1->contents().at(0), c2);
QCOMPARE(c2->body(), QByteArray("textpart"));
QCOMPARE(c3->parent(), nullptr);
// Clean up.
delete c1;
// c2 was deleted when c1 turned itself single-part.
delete c3;
}
void ContentTest::testSetContent()
{
auto c = new Content();
QVERIFY(!c->hasContent());
// head and body present
c->setContent("head1\nhead2\n\nbody1\n\nbody2\n");
QVERIFY(c->hasContent());
QCOMPARE(c->head(), QByteArray("head1\nhead2\n"));
QCOMPARE(c->body(), QByteArray("body1\n\nbody2\n"));
// empty content
c->setContent(QByteArray());
QVERIFY(!c->hasContent());
QVERIFY(c->head().isEmpty());
QVERIFY(c->body().isEmpty());
// empty head
c->setContent("\nbody1\n\nbody2\n");
QVERIFY(c->hasContent());
QVERIFY(c->head().isEmpty());
QCOMPARE(c->body(), QByteArray("body1\n\nbody2\n"));
// empty body
c->setContent("head1\nhead2\n\n");
QVERIFY(c->hasContent());
QCOMPARE(c->head(), QByteArray("head1\nhead2\n"));
QVERIFY(c->body().isEmpty());
delete c;
}
void ContentTest::testEncodedContent()
{
// Example taken from RFC 2046, section 5.1.1.
// Removed "preamble" and "epilogue", which KMime loses.
QByteArray data =
"From: Nathaniel Borenstein <nsb@bellcore.com>\n"
"To: Ned Freed <ned@innosoft.com>\n"
"Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
"Subject: Sample message\n"
"MIME-Version: 1.0\n"
"Content-type: multipart/mixed; boundary=\"simple boundary\"\n"
"\n"
"\n"
"--simple boundary\n"
"\n"
"This is implicitly typed plain US-ASCII text.\n"
"It does NOT end with a linebreak.\n"
"--simple boundary\n"
"Content-type: text/plain; charset=us-ascii\n"
"\n"
"This is explicitly typed plain US-ASCII text.\n"
"It DOES end with a linebreak.\n"
"\n"
"--simple boundary--\n";
auto msg = new Message;
msg->setContent(data);
msg->parse();
// Test that multiple calls do not corrupt anything.
//QByteArray encc = msg->encodedContent();
//qDebug() << "original data" << data;
//qDebug() << "encodedContent" << encc;
QCOMPARE(msg->encodedContent(), data);
QCOMPARE(msg->encodedContent(), data);
QCOMPARE(msg->encodedContent(), data);
delete msg;
// RFC 2822 3.5: lines are limited to 1000 characters (998 + CRLF)
// (bug #187345)
msg = new Message();
data =
"Subject:"
"test test test test test test test test test test test test test test test test test test test test "
"test test test test test test test test test test test test test test test test test test test test "
"test test test test test test test test test test test test test test test test test test test test "
"test test test test test test test test test test test test test test test test test test test test "
"test test test test test test test test test test test test test test test test test test test test "
"test test test test test test test test test test test test test test test test test test test test "
"test test test test test test test test test test test test test test test test test test test test "
"test test test test test test test test test test test test test test test test test test test test "
"test test test test test test test test test test test test test test test test test test test test "
"test test test test test test test test test test test test test test test test test test test test"
"\n"
"References: "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> "
"<test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com> <test1@example.com>"
"\n\n"
"body\n";
msg->setContent(data);
QByteArray content = msg->encodedContent(true /* use CRLF */);
const QStringList lines = QString::fromLatin1(content).split(QStringLiteral("\r\n"));
for (const QString &line : lines) {
QEXPECT_FAIL("", "KMime does not fold lines longer than 998 characters", Continue);
QVERIFY(line.length() < 998 && !line.isEmpty() &&
line != QLatin1StringView("body"));
// The test should be (after the expected failure disappears):
//QVERIFY( line.length() < 998 );
}
delete msg;
}
void ContentTest::testDecodedContent()
{
auto c = new Content();
c->setBody("\0");
QVERIFY(c->decodedBody() == QByteArray());
c->setBody(QByteArray());
QVERIFY(c->decodedBody() == QByteArray());
c->setBody(" ");
QVERIFY(c->decodedBody() == QByteArray(" "));
delete c;
}
void ContentTest::testDecodedText()
{
{
Content c{};
c.setEncodedBody("plain text");
QCOMPARE(c.decodedText(), u"plain text"_s);
}
{
Content c{};
auto cte = std::make_unique<Headers::ContentTransferEncoding>();
cte->setEncoding(Headers::CEbase64);
c.setHeader(cte.release());
c.setEncodedBody("YmFzZTY0LWVuY29kZWQgdGV4dA==");
QCOMPARE(c.decodedText(), u"base64-encoded text"_s);
QCOMPARE(c.decodedBody(), "base64-encoded text\n");
}
{
Content c{};
auto cte = std::make_unique<Headers::ContentTransferEncoding>();
cte->setEncoding(Headers::CEbase64);
c.setHeader(cte.release());
auto ct = std::make_unique<Headers::ContentType>();
ct->setMimeType("text/pgp");
c.setHeader(ct.release());
c.setEncodedBody("YmFzZTY0LWVuY29kZWQgYmluYXJ5IGJsb2Igb2YgZW5jcnlwdGVkIHRleHQ=");
// content of type text/pgp might be a binary blob of encrypted text; it must not be decoded as text
QCOMPARE(c.decodedText(), QString{});
QCOMPARE(c.decodedBody(), "base64-encoded binary blob of encrypted text");
}
}
void ContentTest::testMultipleHeaderExtraction()
{
QByteArray data =
"From: Nathaniel Borenstein <nsb@bellcore.com>\n"
"To: Ned Freed <ned@innosoft.com>\n"
"Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
"Subject: Sample message\n"
"Received: from ktown.kde.org ([192.168.100.1])\n"
"Received: from dev1.kde.org ([192.168.100.2])\n"
"\t by ktown.kde.org ([192.168.100.1])\n"
"Received: from dev2.kde.org ([192.168.100.3])\n"
" by ktown.kde.org ([192.168.100.1])\n";
auto msg = new Message();
msg->setContent(data);
// FAILS identically to ContentTest::testMultipartMixed
// QCOMPARE( msg->encodedContent(), data );
msg->parse();
auto result = msg->headersByType("Received");
QCOMPARE(result.count(), 3);
QCOMPARE(result[0]->asUnicodeString(), QString::fromLatin1("from ktown.kde.org ([192.168.100.1])"));
QCOMPARE(result[1]->asUnicodeString(), QString::fromLatin1("from dev1.kde.org ([192.168.100.2]) by ktown.kde.org ([192.168.100.1])"));
QCOMPARE(result[2]->asUnicodeString(), QString::fromLatin1("from dev2.kde.org ([192.168.100.3]) by ktown.kde.org ([192.168.100.1])"));
delete msg;
}
void ContentTest::testMultipartMixed()
{
// example taken from RFC 2046, section 5.1.1.
QByteArray data =
"From: Nathaniel Borenstein <nsb@bellcore.com>\n"
"To: Ned Freed <ned@innosoft.com>\n"
"Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
"Subject: Sample message\n"
"MIME-Version: 1.0\n"
"Content-type: multipart/mixed; boundary=\"simple boundary\"\n"
"\n"
"This is the preamble. It is to be ignored, though it\n"
"is a handy place for composition agents to include an\n"
"explanatory note to non-MIME conformant readers.\n"
"\n"
"--simple boundary\n"
"\n"
"This is implicitly typed plain US-ASCII text.\n"
"It does NOT end with a linebreak.\n"
"--simple boundary\n"
"Content-type: text/plain; charset=us-ascii\n"
"\n"
"This is explicitly typed plain US-ASCII text.\n"
"It DOES end with a linebreak.\n"
"\n"
"--simple boundary--\n"
"\n"
"This is the epilogue. It is also to be ignored.\n";
QByteArray part1 =
"This is implicitly typed plain US-ASCII text.\n"
"It does NOT end with a linebreak.";
QByteArray part2 =
"This is explicitly typed plain US-ASCII text.\n"
"It DOES end with a linebreak.\n";
// What we expect KMime to parse the above data into.
QByteArray parsedWithPreambleAndEpilogue =
"From: Nathaniel Borenstein <nsb@bellcore.com>\n"
"To: Ned Freed <ned@innosoft.com>\n"
"Date: Sun, 21 Mar 1993 23:56:48 -0800\n"
"Subject: Sample message\n"
"MIME-Version: 1.0\n"
"Content-Type: multipart/mixed; boundary=\"simple boundary\"\n"
"\n"
"This is the preamble. It is to be ignored, though it\n"
"is a handy place for composition agents to include an\n"
"explanatory note to non-MIME conformant readers.\n"
"\n"
"--simple boundary\n"
"Content-Type: text/plain; charset=\"us-ascii\"\n\n"
"This is implicitly typed plain US-ASCII text.\n"
"It does NOT end with a linebreak.\n"
"--simple boundary\n"
"Content-Type: text/plain; charset=\"us-ascii\"\n"
"\n"
"This is explicitly typed plain US-ASCII text.\n"
"It DOES end with a linebreak.\n"
"\n"
"--simple boundary--\n"
"\n"
"This is the epilogue. It is also to be ignored.\n";
// What we expect KMime to assemble the above data into.
QByteArray assembled =
"From: Nathaniel Borenstein <nsb@bellcore.com>\n"
"To: Ned Freed <ned@innosoft.com>\n"
"Date: Sun, 21 Mar 1993 23:56:48 -0800\n"
"Subject: Sample message\n"
"MIME-Version: 1.0\n"
"Content-Type: multipart/mixed; boundary=\"simple boundary\"\n"
"Content-Transfer-Encoding: 7Bit\n"
"\n"
"--simple boundary\n"
"\n"
"This is implicitly typed plain US-ASCII text.\n"
"It does NOT end with a linebreak.\n"
"--simple boundary\n"
"Content-Type: text/plain; charset=\"us-ascii\"\n"
"\n"
"This is explicitly typed plain US-ASCII text.\n"
"It DOES end with a linebreak.\n"
"\n"
"--simple boundary--\n";
// test parsing
auto msg = new Message();
msg->setContent(data);
QCOMPARE(msg->encodedContent(), data);
msg->parse();
QVERIFY(msg->contentType()->isMultipart());
auto list = msg->contents();
QCOMPARE(list.count(), 2);
Content *c = list.takeFirst();
QCOMPARE(c->body(), part1);
c = list.takeFirst();
QCOMPARE(c->body(), part2);
// assemble again
msg->assemble();
//qDebug() << "expected assembled content" << parsedWithPreambleAndEpilogue;
//qDebug() << "actual new encoded content" << msg->encodedContent();
QCOMPARE(msg->encodedContent(), parsedWithPreambleAndEpilogue);
delete msg;
// assembling from scratch
// (The headers have to be in the same order, as we compare with the above assembled.)
msg = new Message();
msg->from()->from7BitString("Nathaniel Borenstein <nsb@bellcore.com>");
msg->to()->from7BitString("Ned Freed <ned@innosoft.com>");
msg->date()->from7BitString("Sun, 21 Mar 1993 23:56:48 -0800 (PST)");
msg->subject()->from7BitString("Sample message");
// HACK to make MIME-Version appear before Content-Type, as in the expected message.
auto header = new Headers::MIMEVersion;
header->from7BitString("1.234");
msg->setHeader(header);
msg->contentType()->from7BitString("multipart/mixed");
msg->contentTransferEncoding()->setEncoding(KMime::Headers::CE7Bit);
c = new Content();
c->setBody(part1);
msg->appendContent(c);
c = new Content();
c->setBody(part2);
c->contentType()->setMimeType("text/plain");
c->contentType()->setCharset("us-ascii");
msg->appendContent(c);
msg->contentType()->setBoundary("simple boundary");
list = msg->contents();
QCOMPARE(list.count(), 2);
c = list.takeFirst();
QCOMPARE(c->body(), part1);
c = list.takeFirst();
QCOMPARE(c->body(), part2);
msg->assemble();
//QByteArray encc = msg->encodedContent();
//qDebug().noquote() << "expected assembled content" << assembled;
//qDebug().noquote() << "actual encoded content" << encc;
QCOMPARE(msg->encodedContent(), assembled);
delete msg;
}
void ContentTest::testParsingUuencoded()
{
const QByteArray body =
"This is a test message that should appears as a text/plain part\n"
"once this message is parsed and convert to a MIME tree.\n"
"\n"
"\n";
const QString imageName = QStringLiteral("Name of the encoded file (oxygen 22x22 kde.png)");
const QByteArray imageBase64 =
"\n"
"iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n"
"AAADdgAAA3YBfdWCzAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAU4SURB\n"
"VBgZjcFbiF1XGcDx/7fW2pdznTOXNJlJc4WWVK3RqiC2FOyTiGCs+lKkohWTIl5QWrAp9ckLKpYi\n"
"KFjwodgXoRDMk1VECj7UpkIqUZuCTUycyWXOzJxz9tnXtdfnxNqnvvj7iaryNhGxgPBOAh/gLa+y\n"
"S3kn3dXyP6KqyEe+1Rm6tSc6nYVHO+loOXYR1hisFYRAIOBljtecyPaItEMkXeK4S2QTVAxVOZ1t\n"
"TzaeG6//9fTWuR9MnOxyx7/xzaWjB548cvgAUeyJbGDYj9mzPGJl1GdpocOwlxCCMs1qtrKSrZ2c\n"
"ze0Z126O2ZkWSJoO0rDylUabREROOsCoG3z58JEDrK4NIFQMBz0WBl2G3ZReGiNi+debO6gKC3sH\n"
"DAcxNu6QpF1GiwtsTzMm04wrVyeY7upngEcdYIy4pSgVJtmMYb+HmBiVGE9Eo47ZdsHJj3eJnOHp\n"
"M3P6exbIJxmffr/ibMK58zN+M4nwlGCTPmAMu8QYKasCFYd1CWoSgkT4YGmCoWggTRLiOKH0UFTK\n"
"A8csdx0ZcnBfl/PXIuJ+j253gBED3CEGDluxVtqgGBcTJCKIZboxJq9bssozLxqiKMJZS1G3LIct\n"
"7nvfAs5FvPDSjHlnEbER3f4AsUZYG1rD2t3GGIu4GIhosUSzCd9/5HZOvKtldnmd7evbRM7hnEOz\n"
"CV/8xCrOWv52qeKVGx0CBpUIF3cwxsLwdmtYPGSMtaLW0WIIKuStIXLCh9+9wE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OH/o7xlE7gOeN31H1IDp2dl3tAoAAAAAElFTkSuQmCC\n"
;
const QByteArray uuencodedMsg =
"Path: news.example.net!not-for-mail\n"
"From: Coin coin <meuh@example.net>\n"
"Newsgroups: test.kmime.uuencoded\n"
"Subject: Kmime test\n"
"Date: Thu, 14 Apr 2005 20:12:47 -0700\n"
"Message-ID: <xxxxxxxxxxxxxxxxxx@xxxxx.kmime.example.net>\n"
"X-Newsreader: Forte Agent 2.0/32.640\n"
"Lines: 1283\n"
"Organization: Ament\n"
"Xref: news.example.net test.kmime.uuencoded:4584\n"
"\n"
"This is a test message that should appears as a text/plain part\n"
"once this message is parsed and convert to a MIME tree.\n"
"\n"
"begin 644 Name of the encoded file (oxygen 22x22 kde.png)\n"
"MB5!.1PT*&@H````-24A$4@```!8````6\"`8```#$M&P[````!'-\"250(\"`@(\n"
"M?`ADB`````EP2%ES```#=@```W8!?=6\"S````!ET15AT4V]F='=A<F4`=W=W\n"
"M+FEN:W-C87!E+F]R9YON/!H```4X241!5!@9C<%;B%U7&<#Q_[?6VI=SG3.7\n"
"M-)E)<X665*W1JB\"V%.R3B&\"L^E*DHA63(EY06K`I]<D+*I8B*%CPH=@7H1#,\n"
"MDU5$\"C[4ID(J49N\"34R<R67.S)QS]MG7M=?GQ-JGOOC[B:KR-A&Q@/!.`A_@\n"
"M+:^R2WDGW=7R/Z*JR$>^U1FZM2<ZG85'.^EH.781UABL%81`(.!ECM><R/:(\n"
"MM$,D7>*X2V035`Q5.9UM3S:>&Z__]?36N1],G.QRQ[_QS:6C!YX\\<O@`4>R)\n"
"M;C]FS/&)EU&=IH<.PEQ\"\",LUJMK*2K9V<S>T9UVZ.V9D62)H.TK#RE4:;\n"
"M1$1..L\"H&WSY\\)$#K*X-(%0,!ST6!EV&W91>&B-B^=>;.Z@*\"WL'#`<Q-NZ0\n"
"MI%U&BPML3S,FTXPK5R>8[NIG@$<=8(RXI2@5)MF,8;^'F!B5&$]$HX[9=L')\n"
"MCW>)G.'I,W/Z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@B+U+$(*B.L(Y(8T\"3WZNSQ._*O%U35W,\n"
"MJ7U#\\%Z9_\\4[0$/KU6\"IRSE557)NH^6K/[Q$D>YG>>4`YY^_RM-?2Q`G//:S\n"
"M$G5=/GMOP>5_YV1YH*X*JBJGJAM0KURYTCI`0U/FO;3?&X]WF$ZVH85_^\"6&\n"
"M@S%-\\$0FYOHX1Q6F+*!MRT]_ZYG/\"Z;9#CN3+>99AK,)6L\\K=CD@A.FE%WQY\n"
"M[-3RXAK7;ZY3YSEU65`6<[+NA#3I<O+'%<88Z$[Q34-9Y>3YC%FVPW2Z1613\n"
"M]HSV<77]E3\\\"K:@JLO]#*]W%X\\_W]]YS?[>_)XU=A#,6:P0DH-KB38;7G,CV\n"
"MB;1+)%WBN$MD$U2$?+[57+OTTJLW7S_SI6I\\X8*H*K>(B`-&P'Y``.'_H[QE\n"
";$[@.>-WU'U(#IV=EWM`H`````$E%3D2N0F\"\"\n"
"`\n"
"end\n"
"\n";
auto msg = new Message();
msg->setContent(uuencodedMsg);
msg->parse();
auto contents = msg->contents();
// text + image
QCOMPARE(contents.size(), 2);
Content *c = nullptr;
// Check the first text part
c = contents.at(0);
QVERIFY(c->contentType()->isPlainText());
QCOMPARE(c->body(), body);
// Check the image part
c = contents.at(1);
QVERIFY(!c->contentType()->isText());
QCOMPARE(c->contentType()->name(), imageName);
// The uuencoded content as been recoded as base64
QCOMPARE(c->encodedContent().data(), imageBase64.data());
delete msg;
}
void ContentTest::testParent()
{
auto c1 = new Content();
c1->contentType()->from7BitString("multipart/mixed");
auto c2 = new Content();
c2->contentType()->from7BitString("text/plain");
c2->setBody("textpart");
auto c3 = new Content();
c3->contentType()->from7BitString("text/html");
c3->setBody("htmlpart");
auto c4 = new Content();
c4->contentType()->from7BitString("text/html");
c4->setBody("htmlpart2");
auto c5 = new Content();
c5->contentType()->from7BitString("multipart/mixed");
//c2 doesn't have a parent yet
QCOMPARE(c2->parent(), (Content *)nullptr);
c1->appendContent(c2);
c1->appendContent(c3);
c1->appendContent(c4);
QCOMPARE(c1->contents().size(), 3);
// c1 is the parent of those
QCOMPARE(c2->parent(), c1);
QCOMPARE(c3->parent(), c1);
//test removal
c1->takeContent(c2);
QCOMPARE(c2->parent(), (Content *)nullptr);
QCOMPARE(c1->contents().at(0), c3);
QCOMPARE(c1->contents().size(), 2);
//check if the content is moved correctly to another parent
c5->appendContent(c4);
QCOMPARE(c4->parent(), c5);
QCOMPARE(c1->contents().size(), 1);
QCOMPARE(c1->contents().at(0), c3);
QCOMPARE(c5->contents().size(), 1);
QCOMPARE(c5->contents().at(0), c4);
// example taken from RFC 2046, section 5.1.1.
QByteArray data =
"From: Nathaniel Borenstein <nsb@bellcore.com>\n"
"To: Ned Freed <ned@innosoft.com>\n"
"Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
"Subject: Sample message\n"
"MIME-Version: 1.0\n"
"Content-type: multipart/mixed; boundary=\"simple boundary\"\n"
"\n"
"This is the preamble. It is to be ignored, though it\n"
"is a handy place for composition agents to include an\n"
"explanatory note to non-MIME conformant readers.\n"
"\n"
"--simple boundary\n"
"\n"
"This is implicitly typed plain US-ASCII text.\n"
"It does NOT end with a linebreak.\n"
"--simple boundary\n"
"Content-type: text/plain; charset=us-ascii\n"
"\n"
"This is explicitly typed plain US-ASCII text.\n"
"It DOES end with a linebreak.\n"
"\n"
"--simple boundary--\n"
"\n"
"This is the epilogue. It is also to be ignored.\n";
// test parsing
auto msg = new Message();
msg->setContent(data);
msg->parse();
QCOMPARE(msg->parent(), (Content *)nullptr);
QCOMPARE(msg->contents().at(0)->parent(), msg);
QCOMPARE(msg->contents().at(1)->parent(), msg);
delete msg;
delete c1;
delete c2;
delete c5;
}
void ContentTest::testFreezing()
{
// Example taken from RFC 2046, section 5.1.1.
QByteArray data =
"From: Nathaniel Borenstein <nsb@bellcore.com>\n"
"To: Ned Freed <ned@innosoft.com>\n"
"Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
"Subject: Sample message\n"
"MIME-Version: 1.0\n"
"Content-type: multipart/mixed; boundary=\"simple boundary\"\n"
"\n"
"This is the preamble. It is to be ignored, though it\n"
"is a handy place for composition agents to include an\n"
"explanatory note to non-MIME conformant readers.\n"
"\n"
"--simple boundary\n"
"\n"
"This is implicitly typed plain US-ASCII text.\n"
"It does NOT end with a linebreak.\n"
"--simple boundary\n"
"Content-type: text/plain; charset=us-ascii\n"
"\n"
"This is explicitly typed plain US-ASCII text.\n"
"It DOES end with a linebreak.\n"
"\n"
"--simple boundary--\n"
"\n"
"This is the epilogue. It is also to be ignored.\n";
auto msg = new Message;
msg->setContent(data);
msg->setFrozen(true);
// The data should be untouched before parsing.
//qDebug() << "original data" << data;
//qDebug() << "data from message" << msg->encodedContent();
QCOMPARE(msg->encodedContent(), data);
// The data should remain untouched after parsing.
msg->parse();
QVERIFY(msg->contentType()->isMultipart());
QCOMPARE(msg->contents().count(), 2);
QCOMPARE(msg->encodedContent(), data);
// Calling assemble() should not alter the data.
msg->assemble();
QCOMPARE(msg->encodedContent(), data);
delete msg;
}
void ContentTest::testContentTypeMimetype_data()
{
QTest::addColumn<QByteArray>("data");
QTest::addColumn<QByteArray>("mimetype");
QTest::addColumn<int>("contentCount");
QTest::addColumn<QList<QByteArray> >("contentMimeType");
QByteArray data =
"From: Nathaniel Borenstein <nsb@bellcore.com>\n"
"To: Ned Freed <ned@innosoft.com>\n"
"Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
"Subject: Sample message\n"
"MIME-Version: 1.0\n"
"Content-type: multipart/mixed; boundary=\"simple boundary\"\n"
"\n"
"This is the preamble. It is to be ignored, though it\n"
"is a handy place for composition agents to include an\n"
"explanatory note to non-MIME conformant readers.\n"
"\n"
"--simple boundary\n"
"\n"
"This is implicitly typed plain US-ASCII text.\n"
"It does NOT end with a linebreak.\n"
"--simple boundary\n"
"Content-type: text/plain; charset=us-ascii\n"
"\n"
"This is explicitly typed plain US-ASCII text.\n"
"It DOES end with a linebreak.\n"
"\n"
"--simple boundary--\n"
"\n"
"This is the epilogue. It is also to be ignored.\n";
QList<QByteArray> contentMimes = { "text/plain", "text/plain"};
QTest::newRow("multipart") << data << QByteArrayLiteral("multipart/mixed") << 2 << contentMimes;
data =
"From: Montel Laurent <null@kde.org>\n"
"To: kde-commits@kde.org\n"
"Content-Type: text/plain; charset=\"utf-8\"\n"
"MIME-Version: 1.0\n"
"Content-Transfer-Encoding: quoted-printable\n"
"Subject: [libksieve] src/ksieveui: coding style\n"
"Date: Tue, 30 May 2017 19:25:59 +0000\n"
"\n"
"Git commit 5410a2b3b6eef29ba32d0ac5e363fd38a56f535b by Montel Laurent.\n"
"Committed on 30/05/2017 at 19:25.\n";
QTest::newRow("text/plain") << data << QByteArrayLiteral("text/plain") << 0 << QList<QByteArray>();
data =
"From: Montel Laurent <null@kde.org>\n"
"To: kde-commits@kde.org\n"
"Content-Type: %22%22%22%22%22%22%22%22%22%22%22%22%22%22%22%22=?windows-1252?q?ap%22/pdf;\n"
"name=\"file.pdf\"\n"
"MIME-Version: 1.0\n"
"Content-Transfer-Encoding: quoted-printable\n"
"Subject: [libksieve] src/ksieveui: coding style\n"
"Date: Tue, 30 May 2017 19:25:59 +0000\n"
"\n"
"Git commit 5410a2b3b6eef29ba32d0ac5e363fd38a56f535b by Montel Laurent.\n"
"Committed on 30/05/2017 at 19:25.\n";
QTest::newRow("broken") << data << QByteArrayLiteral("text/plain") << 0 << QList<QByteArray>();
data =
"From: Montel Laurent <null@kde.org>\n"
"To: kde-commits@kde.org\n"
"Content-Type: application/pdf;\n"
"name=\"file.pdf\"\n"
"MIME-Version: 1.0\n"
"Content-Transfer-Encoding: quoted-printable\n"
"Subject: [libksieve] src/ksieveui: coding style\n"
"Date: Tue, 30 May 2017 19:25:59 +0000\n"
"\n"
"Git commit 5410a2b3b6eef29ba32d0ac5e363fd38a56f535b by Montel Laurent.\n"
"Committed on 30/05/2017 at 19:25.\n";
QTest::newRow("not broken") << data << QByteArrayLiteral("application/pdf") << 0 << QList<QByteArray>();
data =
"From kde@kde.org Fri Jan 29 19:19:26 2010\n"
"Return-Path: <kde@kde.org>\n"
"Delivered-To: kde@kde.org\n"
"Received: from localhost (localhost [127.0.0.1])\n"
" by mail.kde.org (Postfix) with ESMTP id 8A6FF2B23D\n"
" for <kde@kde.org>; Fri, 29 Jan 2010 19:19:27 +0100 (CET)\n"
"From: KDE <kde@kde.org>\n"
"To: kde@kde.org\n"
"Subject: Attachment test Outlook\n"
"Date: Fri, 29 Jan 2010 19:19:26 +0100\n"
"User-Agent: KMail/1.13.0 (Linux/2.6.32-gentoo-r1; KDE/4.3.95; x86_64; ; )\n"
"MIME-Version: 1.0\n"
"Content-Type: Multipart/Mixed;\n"
" boundary=\"Boundary-00=_uayYLfn6pjD7iFW\"\n"
"Message-Id: <201001291919.26518.kde@kde.org>\n"
"X-Length: 10467\n"
"X-UID: 17\n"
"\n"
"--Boundary-00=_uayYLfn6pjD7iFW\n"
"Content-Type: Text/Plain;\n"
" charset=\"us-ascii\"\n"
"Content-Transfer-Encoding: 7bit\n"
"\n"
"Attachment test Outlook\n"
"\n"
"--Boundary-00=_uayYLfn6pjD7iFW\n"
"Content-Type: text/x-patch;\n"
" charset=\"UTF-8\";\n"
" name*=UTF-8''%C3%A5%2Ediff\n"
"Content-Transfer-Encoding: 7bit\n"
"Content-Disposition: attachment;\n"
" filename=\"=?iso-8859-1?q?=E5=2Ediff?=\"\n"
"\n"
"Index: kmime/kmime_headers_p.h\n"
"===================================================================\n"
"--- kmime/kmime_headers_p.h (revision 1068282)\n"
"+++ kmime/kmime_headers_p.h (working copy)\n"
"@@ -170,6 +170,7 @@\n"
" int lines;\n"
" };\n"
"\n"
"+kmime_mk_empty_private( ContentID, Generics::SingleIdent )\n"
" }\n"
"\n"
" }\n"
"\n"
"--Boundary-00=_uayYLfn6pjD7iFW--";
contentMimes = { "text/plain", "text/x-patch"};
QTest::newRow("example1") << data << QByteArrayLiteral("multipart/mixed") << 2 << contentMimes;
}
void ContentTest::testContentTypeMimetype()
{
QFETCH(QByteArray, data);
QFETCH(QByteArray, mimetype);
QFETCH(int, contentCount);
QFETCH(QList<QByteArray> , contentMimeType);
// test parsing
Message msg;
msg.setContent(data);
msg.parse();
//QEXPECT_FAIL("broken", "Problem with content type", Continue);
QCOMPARE(msg.contentType(false)->mimeType(), mimetype);
QCOMPARE(msg.contents().count(), contentCount);
for (int i = 0; i < msg.contents().count(); ++i) {
QVERIFY(msg.contents().at(i)->contentType(false));
QCOMPARE(contentMimeType.count(), contentCount);
QCOMPARE(msg.contents().at(i)->contentType(false)->mimeType(), contentMimeType.at(i));
}
}
void ContentTest::testConstChildren()
{
QFile file(QLatin1StringView(TEST_DATA_DIR "/simple-encapsulated.mbox"));
QVERIFY(file.open(QIODevice::ReadOnly));
const QByteArray data = KMime::CRLFtoLF(file.readAll());
auto msg = std::make_unique<KMime::Message>();
msg->setContent(data);
msg->parse();
QVERIFY(!msg->contents().isEmpty());
QVERIFY(!msg->attachments().isEmpty());
QVERIFY(!msg->headers().isEmpty());
// iteration over non-const Content
for (auto content : msg->contents()) {
static_assert(std::is_same_v<decltype(content), KMime::Content*>);
content->contentTransferEncoding(false); // using the non-const API if this compiles
}
// iteration over const Content
const KMime::Content* const constMsg = msg.get();
QVERIFY(!msg->contents().empty());
QVERIFY(!msg->attachments().empty());
QVERIFY(!msg->headers().empty());
for (auto content : constMsg->contents()) {
static_assert(std::is_same_v<decltype(content), const KMime::Content*>);
// content->contentTransferEncoding(false); // must not compile
QCOMPARE(content->contentLocation(), nullptr); // using the const API if this doesn't create
}
// non-const attachments() returns a temporary, test if the const view keeps that alive
const auto constAtt = constMsg->attachments();
QCOMPARE(constAtt.size(), 1);
for (auto content : constAtt) {
static_assert(std::is_same_v<decltype(content), const KMime::Content*>);
}
}
#include "moc_contenttest.cpp"
|