Bug51Test.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. class Swift_Bug51Test extends Swift_Tests_SwiftUnitTestCase
  4. {
  5. private $_attachmentFile;
  6. private $_outputFile;
  7. public function skip()
  8. {
  9. $this->skipUnless(
  10. is_writable(SWIFT_TMP_DIR),
  11. '%s: This test requires tests/acceptance.conf.php to specify a ' .
  12. 'writable SWIFT_TMP_DIR'
  13. );
  14. }
  15. public function setUp()
  16. {
  17. $this->_attachmentFile = SWIFT_TMP_DIR . '/attach.rand.bin';
  18. file_put_contents($this->_attachmentFile, '');
  19. $this->_outputFile = SWIFT_TMP_DIR . '/attach.out.bin';
  20. file_put_contents($this->_outputFile, '');
  21. }
  22. public function tearDown()
  23. {
  24. unlink($this->_attachmentFile);
  25. unlink($this->_outputFile);
  26. }
  27. public function testAttachmentsDoNotGetTruncatedUsingToByteStream()
  28. {
  29. //Run 100 times with 10KB attachments
  30. for ($i = 0; $i < 10; ++$i) {
  31. $message = $this->_createMessageWithRandomAttachment(
  32. 10000, $this->_attachmentFile
  33. );
  34. file_put_contents($this->_outputFile, '');
  35. $message->toByteStream(
  36. new Swift_ByteStream_FileByteStream($this->_outputFile, true)
  37. );
  38. $emailSource = file_get_contents($this->_outputFile);
  39. $this->assertAttachmentFromSourceMatches(
  40. file_get_contents($this->_attachmentFile),
  41. $emailSource
  42. );
  43. }
  44. }
  45. public function testAttachmentsDoNotGetTruncatedUsingToString()
  46. {
  47. //Run 100 times with 10KB attachments
  48. for ($i = 0; $i < 10; ++$i) {
  49. $message = $this->_createMessageWithRandomAttachment(
  50. 10000, $this->_attachmentFile
  51. );
  52. $emailSource = $message->toString();
  53. $this->assertAttachmentFromSourceMatches(
  54. file_get_contents($this->_attachmentFile),
  55. $emailSource
  56. );
  57. }
  58. }
  59. // -- Custom Assertions
  60. public function assertAttachmentFromSourceMatches($attachmentData, $source)
  61. {
  62. $encHeader = 'Content-Transfer-Encoding: base64';
  63. $base64declaration = strpos($source, $encHeader);
  64. $attachmentDataStart = strpos($source, "\r\n\r\n", $base64declaration);
  65. $attachmentDataEnd = strpos($source, "\r\n--", $attachmentDataStart);
  66. if (false === $attachmentDataEnd) {
  67. $attachmentBase64 = trim(substr($source, $attachmentDataStart));
  68. } else {
  69. $attachmentBase64 = trim(substr(
  70. $source, $attachmentDataStart,
  71. $attachmentDataEnd - $attachmentDataStart
  72. ));
  73. }
  74. $this->assertIdenticalBinary($attachmentData, base64_decode($attachmentBase64));
  75. }
  76. // -- Creation Methods
  77. private function _fillFileWithRandomBytes($byteCount, $file)
  78. {
  79. // I was going to use dd with if=/dev/random but this way seems more
  80. // cross platform even if a hella expensive!!
  81. file_put_contents($file, '');
  82. $fp = fopen($file, 'wb');
  83. for ($i = 0; $i < $byteCount; ++$i) {
  84. $byteVal = rand(0, 255);
  85. fwrite($fp, pack('i', $byteVal));
  86. }
  87. fclose($fp);
  88. }
  89. private function _createMessageWithRandomAttachment($size, $attachmentPath)
  90. {
  91. $this->_fillFileWithRandomBytes($size, $attachmentPath);
  92. $message = Swift_Message::newInstance()
  93. ->setSubject('test')
  94. ->setBody('test')
  95. ->setFrom('a@b.c')
  96. ->setTo('d@e.f')
  97. ->attach(Swift_Attachment::fromPath($attachmentPath))
  98. ;
  99. return $message;
  100. }
  101. }