QpEncoderTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Encoder/QpEncoder.php';
  4. require_once 'Swift/CharacterStream.php';
  5. class Swift_Encoder_QpEncoderTest extends Swift_Tests_SwiftUnitTestCase
  6. {
  7. /* -- RFC 2045, 6.7 --
  8. (1) (General 8bit representation) Any octet, except a CR or
  9. LF that is part of a CRLF line break of the canonical
  10. (standard) form of the data being encoded, may be
  11. represented by an "=" followed by a two digit
  12. hexadecimal representation of the octet's value. The
  13. digits of the hexadecimal alphabet, for this purpose,
  14. are "0123456789ABCDEF". Uppercase letters must be
  15. used; lowercase letters are not allowed. Thus, for
  16. example, the decimal value 12 (US-ASCII form feed) can
  17. be represented by "=0C", and the decimal value 61 (US-
  18. ASCII EQUAL SIGN) can be represented by "=3D". This
  19. rule must be followed except when the following rules
  20. allow an alternative encoding.
  21. */
  22. public function testPermittedCharactersAreNotEncoded()
  23. {
  24. /* -- RFC 2045, 6.7 --
  25. (2) (Literal representation) Octets with decimal values of
  26. 33 through 60 inclusive, and 62 through 126, inclusive,
  27. MAY be represented as the US-ASCII characters which
  28. correspond to those octets (EXCLAMATION POINT through
  29. LESS THAN, and GREATER THAN through TILDE,
  30. respectively).
  31. */
  32. foreach (array_merge(range(33, 60), range(62, 126)) as $ordinal) {
  33. $char = chr($ordinal);
  34. $charStream = $this->_createCharStream();
  35. $this->_checking(Expectations::create()
  36. -> one($charStream)->flushContents()
  37. -> one($charStream)->importString($char)
  38. -> one($charStream)->readBytes(optional()) -> returns(array($ordinal))
  39. -> atLeast(1)->of($charStream)->readBytes(optional()) -> returns(false)
  40. );
  41. $encoder = new Swift_Encoder_QpEncoder($charStream);
  42. $this->assertIdenticalBinary($char, $encoder->encodeString($char));
  43. }
  44. }
  45. public function testWhiteSpaceAtLineEndingIsEncoded()
  46. {
  47. /* -- RFC 2045, 6.7 --
  48. (3) (White Space) Octets with values of 9 and 32 MAY be
  49. represented as US-ASCII TAB (HT) and SPACE characters,
  50. respectively, but MUST NOT be so represented at the end
  51. of an encoded line. Any TAB (HT) or SPACE characters
  52. on an encoded line MUST thus be followed on that line
  53. by a printable character. In particular, an "=" at the
  54. end of an encoded line, indicating a soft line break
  55. (see rule #5) may follow one or more TAB (HT) or SPACE
  56. characters. It follows that an octet with decimal
  57. value 9 or 32 appearing at the end of an encoded line
  58. must be represented according to Rule #1. This rule is
  59. necessary because some MTAs (Message Transport Agents,
  60. programs which transport messages from one user to
  61. another, or perform a portion of such transfers) are
  62. known to pad lines of text with SPACEs, and others are
  63. known to remove "white space" characters from the end
  64. of a line. Therefore, when decoding a Quoted-Printable
  65. body, any trailing white space on a line must be
  66. deleted, as it will necessarily have been added by
  67. intermediate transport agents.
  68. */
  69. $HT = chr(0x09); //9
  70. $SPACE = chr(0x20); //32
  71. //HT
  72. $string = 'a' . $HT . $HT . "\r\n" . 'b';
  73. $seq = $this->_mockery()->sequence('byte-sequence');
  74. $charStream = $this->_createCharStream();
  75. $this->_checking(Expectations::create()
  76. -> one($charStream)->flushContents()
  77. -> one($charStream)->importString($string)
  78. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('a')))
  79. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x09))
  80. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x09))
  81. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0D))
  82. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0A))
  83. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('b')))
  84. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(false)
  85. );
  86. $encoder = new Swift_Encoder_QpEncoder($charStream);
  87. $this->assertEqual(
  88. 'a' . $HT . '=09' . "\r\n" . 'b',
  89. $encoder->encodeString($string)
  90. );
  91. //SPACE
  92. $string = 'a' . $SPACE . $SPACE . "\r\n" . 'b';
  93. $seq = $this->_mockery()->sequence('byte-sequence');
  94. $charStream = $this->_createCharStream();
  95. $this->_checking(Expectations::create()
  96. -> one($charStream)->flushContents()
  97. -> one($charStream)->importString($string)
  98. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('a')))
  99. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x20))
  100. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x20))
  101. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0D))
  102. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0A))
  103. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('b')))
  104. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(false)
  105. );
  106. $encoder = new Swift_Encoder_QpEncoder($charStream);
  107. $this->assertEqual(
  108. 'a' . $SPACE . '=20' . "\r\n" . 'b',
  109. $encoder->encodeString($string)
  110. );
  111. }
  112. public function testCRLFIsLeftAlone()
  113. {
  114. /*
  115. (4) (Line Breaks) A line break in a text body, represented
  116. as a CRLF sequence in the text canonical form, must be
  117. represented by a (RFC 822) line break, which is also a
  118. CRLF sequence, in the Quoted-Printable encoding. Since
  119. the canonical representation of media types other than
  120. text do not generally include the representation of
  121. line breaks as CRLF sequences, no hard line breaks
  122. (i.e. line breaks that are intended to be meaningful
  123. and to be displayed to the user) can occur in the
  124. quoted-printable encoding of such types. Sequences
  125. like "=0D", "=0A", "=0A=0D" and "=0D=0A" will routinely
  126. appear in non-text data represented in quoted-
  127. printable, of course.
  128. Note that many implementations may elect to encode the
  129. local representation of various content types directly
  130. rather than converting to canonical form first,
  131. encoding, and then converting back to local
  132. representation. In particular, this may apply to plain
  133. text material on systems that use newline conventions
  134. other than a CRLF terminator sequence. Such an
  135. implementation optimization is permissible, but only
  136. when the combined canonicalization-encoding step is
  137. equivalent to performing the three steps separately.
  138. */
  139. $string = 'a' . "\r\n" . 'b' . "\r\n" . 'c' . "\r\n";
  140. $seq = $this->_mockery()->sequence('byte-sequence');
  141. $charStream = $this->_createCharStream();
  142. $this->_checking(Expectations::create()
  143. -> one($charStream)->flushContents()
  144. -> one($charStream)->importString($string)
  145. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('a')))
  146. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0D))
  147. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0A))
  148. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('b')))
  149. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0D))
  150. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0A))
  151. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('c')))
  152. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0D))
  153. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(0x0A))
  154. -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(false)
  155. );
  156. $encoder = new Swift_Encoder_QpEncoder($charStream);
  157. $this->assertEqual($string, $encoder->encodeString($string));
  158. }
  159. public function testLinesLongerThan76CharactersAreSoftBroken()
  160. {
  161. /*
  162. (5) (Soft Line Breaks) The Quoted-Printable encoding
  163. REQUIRES that encoded lines be no more than 76
  164. characters long. If longer lines are to be encoded
  165. with the Quoted-Printable encoding, "soft" line breaks
  166. must be used. An equal sign as the last character on a
  167. encoded line indicates such a non-significant ("soft")
  168. line break in the encoded text.
  169. */
  170. $input = str_repeat('a', 140);
  171. $charStream = $this->_createCharStream();
  172. $seq = $this->_mockery()->sequence('byte-sequence');
  173. $exps = Expectations::create();
  174. $exps -> one($charStream)->flushContents()
  175. -> one($charStream)->importString($input)
  176. ;
  177. $output = '';
  178. for ($i = 0; $i < 140; ++$i) {
  179. $exps -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('a')));
  180. if (75 == $i) {
  181. $output .= "=\r\n";
  182. }
  183. $output .= 'a';
  184. }
  185. $exps -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(false);
  186. $this->_checking($exps);
  187. $encoder = new Swift_Encoder_QpEncoder($charStream);
  188. $this->assertEqual($output, $encoder->encodeString($input));
  189. }
  190. public function testMaxLineLengthCanBeSpecified()
  191. {
  192. $input = str_repeat('a', 100);
  193. $charStream = $this->_createCharStream();
  194. $seq = $this->_mockery()->sequence('byte-sequence');
  195. $exps = Expectations::create();
  196. $exps -> one($charStream)->flushContents()
  197. -> one($charStream)->importString($input)
  198. ;
  199. $output = '';
  200. for ($i = 0; $i < 100; ++$i) {
  201. $exps -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('a')));
  202. if (53 == $i) {
  203. $output .= "=\r\n";
  204. }
  205. $output .= 'a';
  206. }
  207. $exps -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(false);
  208. $this->_checking($exps);
  209. $encoder = new Swift_Encoder_QpEncoder($charStream);
  210. $this->assertEqual($output, $encoder->encodeString($input, 0, 54));
  211. }
  212. public function testBytesBelowPermittedRangeAreEncoded()
  213. {
  214. /*
  215. According to Rule (1 & 2)
  216. */
  217. foreach (range(0, 32) as $ordinal) {
  218. $char = chr($ordinal);
  219. $charStream = $this->_createCharStream();
  220. $this->_checking(Expectations::create()
  221. -> one($charStream)->flushContents()
  222. -> one($charStream)->importString($char)
  223. -> one($charStream)->readBytes(optional()) -> returns(array($ordinal))
  224. -> atLeast(1)->of($charStream)->readBytes(optional()) -> returns(false)
  225. );
  226. $encoder = new Swift_Encoder_QpEncoder($charStream);
  227. $this->assertEqual(
  228. sprintf('=%02X', $ordinal), $encoder->encodeString($char)
  229. );
  230. }
  231. }
  232. public function testDecimalByte61IsEncoded()
  233. {
  234. /*
  235. According to Rule (1 & 2)
  236. */
  237. $char = '=';
  238. $charStream = $this->_createCharStream();
  239. $this->_checking(Expectations::create()
  240. -> one($charStream)->flushContents()
  241. -> one($charStream)->importString($char)
  242. -> one($charStream)->readBytes(optional()) -> returns(array(61))
  243. -> atLeast(1)->of($charStream)->readBytes(optional()) -> returns(false)
  244. );
  245. $encoder = new Swift_Encoder_QpEncoder($charStream);
  246. $this->assertEqual('=3D', $encoder->encodeString('='));
  247. }
  248. public function testBytesAbovePermittedRangeAreEncoded()
  249. {
  250. /*
  251. According to Rule (1 & 2)
  252. */
  253. foreach (range(127, 255) as $ordinal) {
  254. $char = chr($ordinal);
  255. $charStream = $this->_createCharStream();
  256. $this->_checking(Expectations::create()
  257. -> one($charStream)->flushContents()
  258. -> one($charStream)->importString($char)
  259. -> one($charStream)->readBytes(optional()) -> returns(array($ordinal))
  260. -> atLeast(1)->of($charStream)->readBytes(optional()) -> returns(false)
  261. );
  262. $encoder = new Swift_Encoder_QpEncoder($charStream);
  263. $this->assertEqual(
  264. sprintf('=%02X', $ordinal), $encoder->encodeString($char)
  265. );
  266. }
  267. }
  268. public function testFirstLineLengthCanBeDifferent()
  269. {
  270. $input = str_repeat('a', 140);
  271. $charStream = $this->_createCharStream();
  272. $seq = $this->_mockery()->sequence('byte-sequence');
  273. $exps = Expectations::create();
  274. $exps -> one($charStream)->flushContents();
  275. $exps -> one($charStream)->importString($input);
  276. $output = '';
  277. for ($i = 0; $i < 140; ++$i) {
  278. $exps -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(array(ord('a')));
  279. if (53 == $i || 53 + 75 == $i) {
  280. $output .= "=\r\n";
  281. }
  282. $output .= 'a';
  283. }
  284. $exps -> one($charStream)->readBytes(optional()) -> inSequence($seq) -> returns(false);
  285. $this->_checking($exps);
  286. $encoder = new Swift_Encoder_QpEncoder($charStream);
  287. $this->assertEqual(
  288. $output, $encoder->encodeString($input, 22),
  289. '%s: First line should start at offset 22 so can only have max length 54'
  290. );
  291. }
  292. // -- Creation methods
  293. private function _createCharStream()
  294. {
  295. return $this->_mock('Swift_CharacterStream');
  296. }
  297. }