QpContentEncoderTest.php 17KB

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