PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/test_ext_mailparse.cpp

https://github.com/diegoIta/hiphop-php
C++ | 412 lines | 329 code | 61 blank | 22 comment | 15 complexity | 52fa02682ce69b353e2579aa24150b5a MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010 Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <test/test_ext_mailparse.h>
  17. #include <runtime/ext/ext.h>
  18. using namespace std;
  19. ///////////////////////////////////////////////////////////////////////////////
  20. bool TestExtMailparse::RunTests(const std::string &which) {
  21. bool ret = true;
  22. RUN_TEST(test_mail);
  23. RUN_TEST(test_ezmlm_hash);
  24. RUN_TEST(test_mailparse_msg_create);
  25. RUN_TEST(test_mailparse_msg_free);
  26. RUN_TEST(test_mailparse_msg_parse_file);
  27. RUN_TEST(test_mailparse_msg_parse);
  28. RUN_TEST(test_mailparse_msg_extract_part_file);
  29. RUN_TEST(test_mailparse_msg_extract_whole_part_file);
  30. RUN_TEST(test_mailparse_msg_extract_part);
  31. RUN_TEST(test_mailparse_msg_get_part_data);
  32. RUN_TEST(test_mailparse_msg_get_part);
  33. RUN_TEST(test_mailparse_msg_get_structure);
  34. RUN_TEST(test_mailparse_rfc822_parse_addresses);
  35. RUN_TEST(test_mailparse_stream_encode);
  36. RUN_TEST(test_mailparse_uudecode_all);
  37. RUN_TEST(test_mailparse_determine_best_xfer_encoding);
  38. return ret;
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////
  41. bool TestExtMailparse::test_mail() {
  42. //VCB("<?php ");
  43. return Count(true);
  44. }
  45. bool TestExtMailparse::test_ezmlm_hash() {
  46. VS(f_ezmlm_hash("foo"), 40);
  47. return Count(true);
  48. }
  49. bool TestExtMailparse::test_mailparse_msg_create() {
  50. const char *files[] = { "mime", "phpcvs1", "qp", "uue" };
  51. for (unsigned int i = 0; i < sizeof(files)/sizeof(files[0]); i++) {
  52. string file = files[i];
  53. string testname = "test/test_ext_mailparse." + file + ".txt";
  54. string expname = "test/test_ext_mailparse." + file + ".exp";
  55. Variant mime = f_mailparse_msg_create();
  56. PlainFile input;
  57. input.open(testname, "r");
  58. while (!input.eof()) {
  59. String data = input.read(1024);
  60. if (!data.isNull()) {
  61. f_mailparse_msg_parse(mime, data);
  62. }
  63. }
  64. input.close();
  65. Array arr = f_mailparse_msg_get_structure(mime);
  66. f_ob_start();
  67. echo("Message: "); echo(file.c_str()); echo("\n");
  68. for (ArrayIter iter(arr); iter; ++iter) {
  69. Variant partname = iter.second();
  70. int depth = f_count(f_explode(".", partname)) - 1;
  71. String indent = f_str_repeat(" ", depth * 2);
  72. Variant subpart = f_mailparse_msg_get_part(mime, partname);
  73. if (subpart.isNull()) {
  74. f_var_dump(partname); echo("\n");
  75. f_var_dump(arr);
  76. break;
  77. }
  78. Variant data = f_mailparse_msg_get_part_data(subpart);
  79. echo("\n"); echo(indent); echo("Part "); echo(partname); echo("\n");
  80. f_ksort(ref(data));
  81. for (ArrayIter iter(data); iter; ++iter) {
  82. String key = iter.first().toString();
  83. if (key != "headers" && key != "ending-pos-body") {
  84. echo(indent); echo(key); echo(" => ");
  85. f_var_dump(iter.second());
  86. }
  87. }
  88. }
  89. String output = f_ob_get_contents();
  90. Variant expect = f_file_get_contents(expname.c_str());
  91. VS(output, expect);
  92. f_ob_end_clean();
  93. }
  94. return Count(true);
  95. }
  96. bool TestExtMailparse::test_mailparse_msg_free() {
  97. f_mailparse_msg_free(null);
  98. return Count(true);
  99. }
  100. bool TestExtMailparse::test_mailparse_msg_parse_file() {
  101. return Count(true);
  102. }
  103. bool TestExtMailparse::test_mailparse_msg_parse() {
  104. String text =
  105. "To: fred@bloggs.com\n"
  106. "To: wez@thebrainroom.com\n"
  107. "\n"
  108. "hello, this is some text=hello.";
  109. Variant mime = f_mailparse_msg_create();
  110. f_mailparse_msg_parse(mime, text);
  111. Variant data = f_mailparse_msg_get_part_data(mime);
  112. VS(data["headers"]["to"], "fred@bloggs.com, wez@thebrainroom.com");
  113. return Count(true);
  114. }
  115. bool TestExtMailparse::test_mailparse_msg_extract_part_file() {
  116. // tested in test_mailparse_msg_extract_part()
  117. return Count(true);
  118. }
  119. bool TestExtMailparse::test_mailparse_msg_extract_whole_part_file() {
  120. // tested in test_mailparse_msg_extract_part()
  121. return Count(true);
  122. }
  123. bool TestExtMailparse::test_mailparse_msg_extract_part() {
  124. String text =
  125. "To: fred@bloggs.com\n"
  126. "Mime-Version: 1.0\n"
  127. "Content-Type: text/plain\n"
  128. "Subject: A simple MIME message\n"
  129. "\n"
  130. "hello, this is some text hello.\n"
  131. "blah blah blah.\n";
  132. Variant fp = f_tmpfile();
  133. f_fwrite(fp, text);
  134. f_rewind(fp);
  135. f_ob_start();
  136. Variant mime = f_mailparse_msg_create();
  137. f_mailparse_msg_parse(mime, text);
  138. echo("Extract to output\n");
  139. f_mailparse_msg_extract_part_file(mime, fp);
  140. echo("Extract and return as string\n");
  141. Variant result = f_mailparse_msg_extract_part_file(mime, fp, null);
  142. echo("-->\n");
  143. echo(result);
  144. echo("\nExtract to open file\n");
  145. Variant fpdest = f_tmpfile();
  146. f_mailparse_msg_extract_part_file(mime, fp, fpdest);
  147. echo("\nrewinding\n");
  148. f_rewind(fpdest);
  149. f_fpassthru(fpdest);
  150. echo("\nExtract via user function\n");
  151. f_mailparse_msg_extract_part_file(mime, fp);
  152. echo("\nExtract whole part to output\n");
  153. f_mailparse_msg_extract_whole_part_file(mime, fp);
  154. echo("\nExtract part from string to output\n");
  155. f_mailparse_msg_extract_part(mime, text);
  156. f_fclose(fpdest);
  157. f_fclose(fp);
  158. String output = f_ob_get_contents();
  159. f_ob_end_clean();
  160. VS(output,
  161. "Extract to output\n"
  162. "hello, this is some text hello.\n"
  163. "blah blah blah.\n"
  164. "Extract and return as string\n"
  165. "-->\n"
  166. "hello, this is some text hello.\n"
  167. "blah blah blah.\n"
  168. "\n"
  169. "Extract to open file\n"
  170. "\n"
  171. "rewinding\n"
  172. "hello, this is some text hello.\n"
  173. "blah blah blah.\n"
  174. "\n"
  175. "Extract via user function\n"
  176. "hello, this is some text hello.\n"
  177. "blah blah blah.\n"
  178. "\n"
  179. "Extract whole part to output\n"
  180. "To: fred@bloggs.com\n"
  181. "Mime-Version: 1.0\n"
  182. "Content-Type: text/plain\n"
  183. "Subject: A simple MIME message\n"
  184. "\n"
  185. "hello, this is some text hello.\n"
  186. "blah blah blah.\n"
  187. "\n"
  188. "Extract part from string to output\n"
  189. "hello, this is some text hello.\n"
  190. "blah blah blah.\n");
  191. return Count(true);
  192. }
  193. bool TestExtMailparse::test_mailparse_msg_get_part_data() {
  194. // tested in test_mailparse_msg_parse()
  195. return Count(true);
  196. }
  197. bool TestExtMailparse::test_mailparse_msg_get_part() {
  198. String msg =
  199. "Received: from mail pickup service by hotmail.com with Microsoft\n"
  200. "SMTPSVC;\n"
  201. "Sat, 18 Feb 2006 22:58:14 -0800\n"
  202. "Received: from 66.178.40.49 by BAY116-DAV8.phx.gbl with DAV;\n"
  203. "Sun, 19 Feb 2006 06:58:13 +0000\n"
  204. "\n"
  205. "test";
  206. Variant mail = f_mailparse_msg_create();
  207. f_mailparse_msg_parse(mail, msg);
  208. Array arr = f_mailparse_msg_get_structure(mail);
  209. for (ArrayIter iter(arr); iter; ++iter) {
  210. Variant section = f_mailparse_msg_get_part(mail, iter.second());
  211. Variant info = f_mailparse_msg_get_part_data(section);
  212. Array received =
  213. CREATE_VECTOR2("from mail pickup service by hotmail.com with Microsoft",
  214. "from 66.178.40.49 by BAY116-DAV8.phx.gbl with DAV;");
  215. VS(info,
  216. Array(ArrayInit(11, false).
  217. set(0, "headers", CREATE_MAP1("received", received)).
  218. set(1, "starting-pos", 0).
  219. set(2, "starting-pos-body", 200).
  220. set(3, "ending-pos", 200).
  221. set(4, "ending-pos-body", 200).
  222. set(5, "line-count", 6).
  223. set(6, "body-line-count", 0).
  224. set(7, "charset", "us-ascii").
  225. set(8, "transfer-encoding", "8bit").
  226. set(9, "content-type", "text/plain").
  227. set(10, "content-base", "/").
  228. create()));
  229. }
  230. return Count(true);
  231. }
  232. bool TestExtMailparse::test_mailparse_msg_get_structure() {
  233. // tested in test_mailparse_msg_create()
  234. return Count(true);
  235. }
  236. bool TestExtMailparse::test_mailparse_rfc822_parse_addresses() {
  237. Array addresses =
  238. CREATE_VECTOR2("\":sysmail\"@ Some-Group. Some-Org, Muhammed."
  239. "(I am the greatest) Ali @(the)Vegas.WBA",
  240. "\"strange\":\":sysmail\"@ Some-Group. Some-Org, Muhammed."
  241. "(I am the greatest) Ali @(the)Vegas.WBA;");
  242. f_ob_start();
  243. for (ArrayIter iter(addresses); iter; ++iter) {
  244. Variant parsed = f_mailparse_rfc822_parse_addresses(iter.second());
  245. for (ArrayIter iter2(parsed); iter2; ++iter2) {
  246. Variant pair = iter2.second();
  247. echo(pair["display"]); echo("\n");
  248. echo(pair["address"]); echo("\n");
  249. if (pair["is_group"].toBoolean()) {
  250. Variant sub = f_mailparse_rfc822_parse_addresses
  251. (f_substr(pair["address"], 1, f_strlen(pair["address"]) - 2));
  252. for (ArrayIter iter3(sub); iter3; ++iter3) {
  253. echo(" "); echo(iter3.second()["address"]); echo("\n");
  254. }
  255. }
  256. }
  257. echo("...\n");
  258. }
  259. String output = f_ob_get_contents();
  260. f_ob_end_clean();
  261. VS(output,
  262. ":sysmail@Some-Group.Some-Org\n"
  263. "\":sysmail\"@Some-Group.Some-Org\n"
  264. "I am the greatest the\n"
  265. "Muhammed.Ali@Vegas.WBA\n"
  266. "...\n"
  267. "strange\n"
  268. ":\":sysmail\"@Some-Group.Some-Org,Muhammed.Ali@Vegas.WBA;\n"
  269. " \":sysmail\"@Some-Group.Some-Org\n"
  270. " Muhammed.Ali@Vegas.WBA\n"
  271. "...\n");
  272. return Count(true);
  273. }
  274. bool TestExtMailparse::test_mailparse_stream_encode() {
  275. {
  276. String text = "hello, this is some text=hello.";
  277. Variant fp = f_tmpfile();
  278. f_fwrite(fp, text); f_rewind(fp);
  279. Variant dest = f_tmpfile();
  280. f_mailparse_stream_encode(fp, dest, "quoted-printable");
  281. f_rewind(dest);
  282. Variant data = f_fread(dest, 2048);
  283. VS(data, "hello, this is some text=3Dhello.");
  284. }
  285. {
  286. String text =
  287. "To: fred@bloggs.com\n"
  288. "\n"
  289. "blah blah blah From blah $ \" & £ blah blah blah blah blah\n"
  290. "From the first of the month, things will be different!\n"
  291. "blah blah blah From blah\n"
  292. "Frome is a town in Somerset.";
  293. f_ob_start();
  294. Variant fp = f_tmpfile();
  295. f_fwrite(fp, text);
  296. f_rewind(fp);
  297. Variant fpdest = f_tmpfile();
  298. f_mailparse_stream_encode(fp, fpdest, "quoted-printable");
  299. f_rewind(fpdest);
  300. f_fpassthru(fpdest);
  301. f_fclose(fp);
  302. f_fclose(fpdest);
  303. String output = f_ob_get_contents();
  304. f_ob_end_clean();
  305. VS(output,
  306. "To: fred@bloggs.com\r\n"
  307. "\r\n"
  308. "blah blah blah From blah $ \" & =C2=A3 blah blah blah blah blah\r\n"
  309. "=46rom the first of the month, things will be different!\r\n"
  310. "blah blah blah From blah\r\n"
  311. "Frome is a town in Somerset.");
  312. }
  313. return Count(true);
  314. }
  315. bool TestExtMailparse::test_mailparse_uudecode_all() {
  316. String text =
  317. "To: fred@bloggs.com\n"
  318. "\n"
  319. "hello, this is some text hello.\n"
  320. "blah blah blah.\n"
  321. "\n"
  322. "begin 644 test.txt\n"
  323. "/=&AI<R!I<R!A('1E<W0*\n"
  324. "`\n"
  325. "end";
  326. f_ob_start();
  327. Variant fp = f_tmpfile();
  328. f_fwrite(fp, text);
  329. Variant data = f_mailparse_uudecode_all(fp);
  330. echo("BODY\n");
  331. f_readfile(data[0]["filename"]);
  332. echo("UUE\n");
  333. f_readfile(data[1]["filename"]);
  334. f_unlink(data[0]["filename"]);
  335. f_unlink(data[1]["filename"]);
  336. String output = f_ob_get_contents();
  337. f_ob_end_clean();
  338. VS(output,
  339. "BODY\n"
  340. "To: fred@bloggs.com\n"
  341. "\n"
  342. "hello, this is some text hello.\n"
  343. "blah blah blah.\n"
  344. "\n"
  345. "UUE\n"
  346. "this is a test\n");
  347. return Count(true);
  348. }
  349. bool TestExtMailparse::test_mailparse_determine_best_xfer_encoding() {
  350. return Count(true);
  351. }