QpHeaderEncoderTest.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. {
  125. $char = pack('C', $byte);
  126. $charStream = $this->_createCharacterStream();
  127. $this->_checking(Expectations::create()
  128. -> one($charStream)->readBytes(any()) -> returns(array($byte))
  129. -> allowing($charStream)->readBytes(any()) -> returns(false)
  130. -> ignoring($charStream)
  131. );
  132. $encoder = $this->_createEncoder($charStream);
  133. $encodedChar = $encoder->encodeString($char);
  134. if (in_array($byte, $allowedBytes))
  135. {
  136. $this->assertEqual($char, $encodedChar,
  137. '%s: Character ' . $char . ' should not be encoded.'
  138. );
  139. }
  140. elseif (0x20 == $byte) //Special case
  141. {
  142. $this->assertEqual('_', $encodedChar,
  143. '%s: Space character should be replaced.'
  144. );
  145. }
  146. else
  147. {
  148. $this->assertEqual(sprintf('=%02X', $byte), $encodedChar,
  149. '%s: Byte ' . $byte . ' should be encoded.'
  150. );
  151. }
  152. }
  153. }
  154. public function testEqualsNeverAppearsAtEndOfLine()
  155. {
  156. /* -- RFC 2047, 5 (3).
  157. The 'encoded-text' in an 'encoded-word' must be self-contained;
  158. 'encoded-text' MUST NOT be continued from one 'encoded-word' to
  159. another. This implies that the 'encoded-text' portion of a "B"
  160. 'encoded-word' will be a multiple of 4 characters long; for a "Q"
  161. 'encoded-word', any "=" character that appears in the 'encoded-text'
  162. portion will be followed by two hexadecimal characters.
  163. */
  164. $input = str_repeat('a', 140);
  165. $charStream = $this->_createCharacterStream();
  166. $output = '';
  167. $seq = 0;
  168. for (; $seq < 140; ++$seq)
  169. {
  170. $this->_checking(Expectations::create()
  171. -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
  172. );
  173. if (75 == $seq)
  174. {
  175. $output .= "\r\n"; // =\r\n
  176. }
  177. $output .= 'a';
  178. }
  179. $this->_checking(Expectations::create()
  180. -> allowing($charStream)->readBytes(any()) -> returns(false)
  181. -> ignoring($charStream)
  182. );
  183. $encoder = $this->_createEncoder($charStream);
  184. $this->assertEqual($output, $encoder->encodeString($input));
  185. }
  186. // -- Creation Methods
  187. private function _createEncoder($charStream)
  188. {
  189. return new Swift_Mime_HeaderEncoder_QpHeaderEncoder($charStream);
  190. }
  191. private function _createCharacterStream($stub = false)
  192. {
  193. return $stub
  194. ? $this->_stub('Swift_CharacterStream')
  195. : $this->_mock('Swift_CharacterStream')
  196. ;
  197. }
  198. }