PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Swift-4.1.7/tests/bug/Swift/Bug51Test.php

https://gitlab.com/vince.omega/mcb-nov-build
PHP | 133 lines | 101 code | 26 blank | 6 comment | 4 complexity | 7dbc2a7bad57877a9727e00fe578d0f3 MD5 | raw file
  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. {
  32. $message = $this->_createMessageWithRandomAttachment(
  33. 10000, $this->_attachmentFile
  34. );
  35. file_put_contents($this->_outputFile, '');
  36. $message->toByteStream(
  37. new Swift_ByteStream_FileByteStream($this->_outputFile, true)
  38. );
  39. $emailSource = file_get_contents($this->_outputFile);
  40. $this->assertAttachmentFromSourceMatches(
  41. file_get_contents($this->_attachmentFile),
  42. $emailSource
  43. );
  44. }
  45. }
  46. public function testAttachmentsDoNotGetTruncatedUsingToString()
  47. {
  48. //Run 100 times with 10KB attachments
  49. for ($i = 0; $i < 10; ++$i)
  50. {
  51. $message = $this->_createMessageWithRandomAttachment(
  52. 10000, $this->_attachmentFile
  53. );
  54. $emailSource = $message->toString();
  55. $this->assertAttachmentFromSourceMatches(
  56. file_get_contents($this->_attachmentFile),
  57. $emailSource
  58. );
  59. }
  60. }
  61. // -- Custom Assertions
  62. public function assertAttachmentFromSourceMatches($attachmentData, $source)
  63. {
  64. $encHeader = 'Content-Transfer-Encoding: base64';
  65. $base64declaration = strpos($source, $encHeader);
  66. $attachmentDataStart = strpos($source, "\r\n\r\n", $base64declaration);
  67. $attachmentDataEnd = strpos($source, "\r\n--", $attachmentDataStart);
  68. if (false === $attachmentDataEnd)
  69. {
  70. $attachmentBase64 = trim(substr($source, $attachmentDataStart));
  71. }
  72. else
  73. {
  74. $attachmentBase64 = trim(substr(
  75. $source, $attachmentDataStart,
  76. $attachmentDataEnd - $attachmentDataStart
  77. ));
  78. }
  79. $this->assertIdenticalBinary($attachmentData, base64_decode($attachmentBase64));
  80. }
  81. // -- Creation Methods
  82. private function _fillFileWithRandomBytes($byteCount, $file)
  83. {
  84. // I was going to use dd with if=/dev/random but this way seems more
  85. // cross platform even if a hella expensive!!
  86. file_put_contents($file, '');
  87. $fp = fopen($file, 'wb');
  88. for ($i = 0; $i < $byteCount; ++$i)
  89. {
  90. $byteVal = rand(0, 255);
  91. fwrite($fp, pack('i', $byteVal));
  92. }
  93. fclose($fp);
  94. }
  95. private function _createMessageWithRandomAttachment($size, $attachmentPath)
  96. {
  97. $this->_fillFileWithRandomBytes($size, $attachmentPath);
  98. $message = Swift_Message::newInstance()
  99. ->setSubject('test')
  100. ->setBody('test')
  101. ->setFrom('a@b.c')
  102. ->setTo('d@e.f')
  103. ->attach(Swift_Attachment::fromPath($attachmentPath))
  104. ;
  105. return $message;
  106. }
  107. }