QpContentEncoderTest.php 19KB

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