QpEncoderTest.php 15 KB

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