Bug76Test.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. class Swift_Bug76Test extends Swift_Tests_SwiftUnitTestCase
  4. {
  5. private $_inputFile;
  6. private $_outputFile;
  7. private $_encoder;
  8. public function skip()
  9. {
  10. $this->skipUnless(
  11. is_writable(SWIFT_TMP_DIR),
  12. '%s: This test requires tests/acceptance.conf.php to specify a ' .
  13. 'writable SWIFT_TMP_DIR'
  14. );
  15. }
  16. public function setUp()
  17. {
  18. $this->_inputFile = SWIFT_TMP_DIR . '/in.bin';
  19. file_put_contents($this->_inputFile, '');
  20. $this->_outputFile = SWIFT_TMP_DIR . '/out.bin';
  21. file_put_contents($this->_outputFile, '');
  22. $this->_encoder = $this->_createEncoder();
  23. }
  24. public function tearDown()
  25. {
  26. unlink($this->_inputFile);
  27. unlink($this->_outputFile);
  28. }
  29. public function testBase64EncodedLineLengthNeverExceeds76CharactersEvenIfArgsDo()
  30. {
  31. $this->_fillFileWithRandomBytes(1000, $this->_inputFile);
  32. $os = $this->_createStream($this->_inputFile);
  33. $is = $this->_createStream($this->_outputFile);
  34. $this->_encoder->encodeByteStream($os, $is, 0, 80); //Exceeds 76
  35. $this->assertMaxLineLength(76, $this->_outputFile,
  36. '%s: Line length should not exceed 76 characters'
  37. );
  38. }
  39. // -- Custom Assertions
  40. public function assertMaxLineLength($length, $filePath, $message = '%s')
  41. {
  42. $lines = file($filePath);
  43. foreach ($lines as $line)
  44. {
  45. $this->assertTrue((strlen(trim($line)) <= 76), $message);
  46. }
  47. }
  48. // -- Creation Methods
  49. private function _fillFileWithRandomBytes($byteCount, $file)
  50. {
  51. // I was going to use dd with if=/dev/random but this way seems more
  52. // cross platform even if a hella expensive!!
  53. file_put_contents($file, '');
  54. $fp = fopen($file, 'wb');
  55. for ($i = 0; $i < $byteCount; ++$i)
  56. {
  57. $byteVal = rand(0, 255);
  58. fwrite($fp, pack('i', $byteVal));
  59. }
  60. fclose($fp);
  61. }
  62. private function _createEncoder()
  63. {
  64. return new Swift_Mime_ContentEncoder_Base64ContentEncoder();
  65. }
  66. private function _createStream($file)
  67. {
  68. return new Swift_ByteStream_FileByteStream($file, true);
  69. }
  70. }