Bug76Test.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. $this->assertTrue((strlen(trim($line)) <= 76), $message);
  45. }
  46. }
  47. // -- Creation Methods
  48. private function _fillFileWithRandomBytes($byteCount, $file)
  49. {
  50. // I was going to use dd with if=/dev/random but this way seems more
  51. // cross platform even if a hella expensive!!
  52. file_put_contents($file, '');
  53. $fp = fopen($file, 'wb');
  54. for ($i = 0; $i < $byteCount; ++$i) {
  55. $byteVal = rand(0, 255);
  56. fwrite($fp, pack('i', $byteVal));
  57. }
  58. fclose($fp);
  59. }
  60. private function _createEncoder()
  61. {
  62. return new Swift_Mime_ContentEncoder_Base64ContentEncoder();
  63. }
  64. private function _createStream($file)
  65. {
  66. return new Swift_ByteStream_FileByteStream($file, true);
  67. }
  68. }