QpEncoderTest.php 14KB

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