PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/swiftmailer/swiftmailer/tests/unit/Swift/Mime/HeaderEncoder/QpHeaderEncoderTest.php

https://bitbucket.org/Zizaco/nodegear
PHP | 223 lines | 144 code | 29 blank | 50 comment | 6 complexity | d4519a3caf59cd5b75c63a3b99ed69c3 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mime/HeaderEncoder/QpHeaderEncoder.php';
  4. require_once 'Swift/CharacterStream.php';
  5. class Swift_Mime_HeaderEncoder_QpHeaderEncoderTest
  6. extends Swift_Tests_SwiftUnitTestCase
  7. {
  8. //Most tests are already covered in QpEncoderTest since this subclass only
  9. // adds a getName() method
  10. public function testNameIsQ()
  11. {
  12. $encoder = $this->_createEncoder(
  13. $this->_createCharacterStream(true)
  14. );
  15. $this->assertEqual('Q', $encoder->getName());
  16. }
  17. public function testSpaceAndTabNeverAppear()
  18. {
  19. /* -- RFC 2047, 4.
  20. Only a subset of the printable ASCII characters may be used in
  21. 'encoded-text'. Space and tab characters are not allowed, so that
  22. the beginning and end of an 'encoded-word' are obvious.
  23. */
  24. $charStream = $this->_createCharacterStream();
  25. $this->_checking(Expectations::create()
  26. -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
  27. -> one($charStream)->readBytes(any()) -> returns(array(0x20))
  28. -> one($charStream)->readBytes(any()) -> returns(array(0x09))
  29. -> one($charStream)->readBytes(any()) -> returns(array(0x20))
  30. -> one($charStream)->readBytes(any()) -> returns(array(ord('b')))
  31. -> allowing($charStream)->readBytes(any()) -> returns(false)
  32. -> ignoring($charStream)
  33. );
  34. $encoder = $this->_createEncoder($charStream);
  35. $this->assertNoPattern('~[ \t]~', $encoder->encodeString("a \t b"),
  36. '%s: encoded-words in headers cannot contain LWSP as per RFC 2047.'
  37. );
  38. }
  39. public function testSpaceIsRepresentedByUnderscore()
  40. {
  41. /* -- RFC 2047, 4.2.
  42. (2) The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be
  43. represented as "_" (underscore, ASCII 95.). (This character may
  44. not pass through some internetwork mail gateways, but its use
  45. will greatly enhance readability of "Q" encoded data with mail
  46. readers that do not support this encoding.) Note that the "_"
  47. always represents hexadecimal 20, even if the SPACE character
  48. occupies a different code position in the character set in use.
  49. */
  50. $charStream = $this->_createCharacterStream();
  51. $this->_checking(Expectations::create()
  52. -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
  53. -> one($charStream)->readBytes(any()) -> returns(array(0x20))
  54. -> one($charStream)->readBytes(any()) -> returns(array(ord('b')))
  55. -> allowing($charStream)->readBytes(any()) -> returns(false)
  56. -> ignoring($charStream)
  57. );
  58. $encoder = $this->_createEncoder($charStream);
  59. $this->assertEqual('a_b', $encoder->encodeString('a b'),
  60. '%s: Spaces can be represented by more readable underscores as per RFC 2047.'
  61. );
  62. }
  63. public function testEqualsAndQuestionAndUnderscoreAreEncoded()
  64. {
  65. /* -- RFC 2047, 4.2.
  66. (3) 8-bit values which correspond to printable ASCII characters other
  67. than "=", "?", and "_" (underscore), MAY be represented as those
  68. characters. (But see section 5 for restrictions.) In
  69. particular, SPACE and TAB MUST NOT be represented as themselves
  70. within encoded words.
  71. */
  72. $charStream = $this->_createCharacterStream();
  73. $this->_checking(Expectations::create()
  74. -> one($charStream)->readBytes(any()) -> returns(array(ord('=')))
  75. -> one($charStream)->readBytes(any()) -> returns(array(ord('?')))
  76. -> one($charStream)->readBytes(any()) -> returns(array(ord('_')))
  77. -> allowing($charStream)->readBytes(any()) -> returns(false)
  78. -> ignoring($charStream)
  79. );
  80. $encoder = $this->_createEncoder($charStream);
  81. $this->assertEqual('=3D=3F=5F', $encoder->encodeString('=?_'),
  82. '%s: Chars =, ? and _ (underscore) may not appear as per RFC 2047.'
  83. );
  84. }
  85. public function testParensAndQuotesAreEncoded()
  86. {
  87. /* -- RFC 2047, 5 (2).
  88. A "Q"-encoded 'encoded-word' which appears in a 'comment' MUST NOT
  89. contain the characters "(", ")" or "
  90. */
  91. $charStream = $this->_createCharacterStream();
  92. $this->_checking(Expectations::create()
  93. -> one($charStream)->readBytes(any()) -> returns(array(ord('(')))
  94. -> one($charStream)->readBytes(any()) -> returns(array(ord('"')))
  95. -> one($charStream)->readBytes(any()) -> returns(array(ord(')')))
  96. -> allowing($charStream)->readBytes(any()) -> returns(false)
  97. -> ignoring($charStream)
  98. );
  99. $encoder = $this->_createEncoder($charStream);
  100. $this->assertEqual('=28=22=29', $encoder->encodeString('(")'),
  101. '%s: Chars (, " (DQUOTE) and ) may not appear as per RFC 2047.'
  102. );
  103. }
  104. public function testOnlyCharactersAllowedInPhrasesAreUsed()
  105. {
  106. /* -- RFC 2047, 5.
  107. (3) As a replacement for a 'word' entity within a 'phrase', for example,
  108. one that precedes an address in a From, To, or Cc header. The ABNF
  109. definition for 'phrase' from RFC 822 thus becomes:
  110. phrase = 1*( encoded-word / word )
  111. In this case the set of characters that may be used in a "Q"-encoded
  112. 'encoded-word' is restricted to: <upper and lower case ASCII
  113. letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
  114. (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
  115. 'phrase' MUST be separated from any adjacent 'word', 'text' or
  116. 'special' by 'linear-white-space'.
  117. */
  118. $allowedBytes = array_merge(
  119. range(ord('a'), ord('z')), range(ord('A'), ord('Z')),
  120. range(ord('0'), ord('9')),
  121. array(ord('!'), ord('*'), ord('+'), ord('-'), ord('/'))
  122. );
  123. foreach (range(0x00, 0xFF) as $byte) {
  124. $char = pack('C', $byte);
  125. $charStream = $this->_createCharacterStream();
  126. $this->_checking(Expectations::create()
  127. -> one($charStream)->readBytes(any()) -> returns(array($byte))
  128. -> allowing($charStream)->readBytes(any()) -> returns(false)
  129. -> ignoring($charStream)
  130. );
  131. $encoder = $this->_createEncoder($charStream);
  132. $encodedChar = $encoder->encodeString($char);
  133. if (in_array($byte, $allowedBytes)) {
  134. $this->assertEqual($char, $encodedChar,
  135. '%s: Character ' . $char . ' should not be encoded.'
  136. );
  137. }
  138. elseif (0x20 == $byte) //Special case
  139. {
  140. $this->assertEqual('_', $encodedChar,
  141. '%s: Space character should be replaced.'
  142. );
  143. } else {
  144. $this->assertEqual(sprintf('=%02X', $byte), $encodedChar,
  145. '%s: Byte ' . $byte . ' should be encoded.'
  146. );
  147. }
  148. }
  149. }
  150. public function testEqualsNeverAppearsAtEndOfLine()
  151. {
  152. /* -- RFC 2047, 5 (3).
  153. The 'encoded-text' in an 'encoded-word' must be self-contained;
  154. 'encoded-text' MUST NOT be continued from one 'encoded-word' to
  155. another. This implies that the 'encoded-text' portion of a "B"
  156. 'encoded-word' will be a multiple of 4 characters long; for a "Q"
  157. 'encoded-word', any "=" character that appears in the 'encoded-text'
  158. portion will be followed by two hexadecimal characters.
  159. */
  160. $input = str_repeat('a', 140);
  161. $charStream = $this->_createCharacterStream();
  162. $output = '';
  163. $seq = 0;
  164. for (; $seq < 140; ++$seq) {
  165. $this->_checking(Expectations::create()
  166. -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
  167. );
  168. if (75 == $seq) {
  169. $output .= "\r\n"; // =\r\n
  170. }
  171. $output .= 'a';
  172. }
  173. $this->_checking(Expectations::create()
  174. -> allowing($charStream)->readBytes(any()) -> returns(false)
  175. -> ignoring($charStream)
  176. );
  177. $encoder = $this->_createEncoder($charStream);
  178. $this->assertEqual($output, $encoder->encodeString($input));
  179. }
  180. // -- Creation Methods
  181. private function _createEncoder($charStream)
  182. {
  183. return new Swift_Mime_HeaderEncoder_QpHeaderEncoder($charStream);
  184. }
  185. private function _createCharacterStream($stub = false)
  186. {
  187. return $stub
  188. ? $this->_stub('Swift_CharacterStream')
  189. : $this->_mock('Swift_CharacterStream')
  190. ;
  191. }
  192. }