browser_post_file.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Tests for bug 1241100: Post to local file should not overwrite the file.
  3. */
  4. Components.utils.import("resource://gre/modules/osfile.jsm");
  5. function* createTestFile(filename, content) {
  6. let path = OS.Path.join(OS.Constants.Path.tmpDir, filename);
  7. yield OS.File.writeAtomic(path, content);
  8. return path;
  9. }
  10. function* readFile(path) {
  11. var array = yield OS.File.read(path);
  12. var decoder = new TextDecoder();
  13. return decoder.decode(array);
  14. }
  15. function frameScript() {
  16. addMessageListener("Test:WaitForIFrame", function() {
  17. var check = function() {
  18. if (content) {
  19. var frame = content.document.getElementById("frame");
  20. if (frame) {
  21. var okBox = frame.contentDocument.getElementById("action_file_ok");
  22. if (okBox) {
  23. sendAsyncMessage("Test:IFrameLoaded");
  24. return;
  25. }
  26. }
  27. }
  28. setTimeout(check, 100);
  29. };
  30. check();
  31. });
  32. }
  33. add_task(function*() {
  34. var postFilename = "post_file.html";
  35. var actionFilename = "action_file.html";
  36. var postFileContent = `
  37. <!DOCTYPE html>
  38. <html>
  39. <head>
  40. <meta charset="utf-8">
  41. <title>post file</title>
  42. </head>
  43. <body onload="document.getElementById('form').submit();">
  44. <form id="form" action="${actionFilename}" method="post" enctype="text/plain" target="frame">
  45. <input type="hidden" name="foo" value="bar">
  46. <input type="submit">
  47. </form>
  48. <iframe id="frame" name="frame"></iframe>
  49. </body>
  50. </html>
  51. `;
  52. var actionFileContent = `
  53. <!DOCTYPE html>
  54. <html>
  55. <head>
  56. <meta charset="utf-8">
  57. <title>action file</title>
  58. </head>
  59. <body>
  60. <div id="action_file_ok">ok</div>
  61. </body>
  62. </html>
  63. `;
  64. var postPath = yield* createTestFile(postFilename, postFileContent);
  65. var actionPath = yield* createTestFile(actionFilename, actionFileContent);
  66. var postURI = OS.Path.toFileURI(postPath);
  67. gBrowser.selectedTab = gBrowser.addTab(postURI);
  68. let browser = gBrowser.selectedBrowser;
  69. browser.messageManager.loadFrameScript("data:,(" + frameScript.toString() + ")();", true);
  70. yield new Promise(resolve => {
  71. let manager = browser.messageManager;
  72. function listener() {
  73. manager.removeMessageListener("Test:IFrameLoaded", listener);
  74. resolve();
  75. }
  76. manager.addMessageListener("Test:IFrameLoaded", listener);
  77. manager.sendAsyncMessage("Test:WaitForIFrame");
  78. });
  79. var actionFileContentAfter = yield* readFile(actionPath);
  80. is(actionFileContentAfter, actionFileContent, "action file is not modified");
  81. yield OS.File.remove(postPath);
  82. yield OS.File.remove(actionPath);
  83. gBrowser.removeCurrentTab();
  84. });