Bug 1257137 - Handle bad args in downloads.{pause,resume,cancel}. r=kmag
authorAndrew Swan <aswan@mozilla.com>
Sat, 26 Mar 2016 14:49:51 -0700
changeset 290866 4772ee550675da7ea644d8cf41ca0021caf18c65
parent 290865 089d27ec5d7c0dfeb1b553a39db431e393000f77
child 290867 3df6a745a563855868ce0dfaba1c74f0f263307f
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1257137
milestone48.0a1
Bug 1257137 - Handle bad args in downloads.{pause,resume,cancel}. r=kmag MozReview-Commit-ID: HKYqlKUMp91
toolkit/components/extensions/ext-downloads.js
toolkit/components/extensions/test/mochitest/test_chrome_ext_downloads_misc.html
--- a/toolkit/components/extensions/ext-downloads.js
+++ b/toolkit/components/extensions/ext-downloads.js
@@ -492,46 +492,52 @@ extensions.registerSchemaAPI("downloads"
 
       search(query) {
         return queryHelper(query)
           .then(items => items.map(item => item.serialize()));
       },
 
       pause(id) {
         return DownloadMap.lazyInit().then(() => {
-          let item = DownloadMap.fromId(id);
-          if (!item) {
+          let item;
+          try {
+            item = DownloadMap.fromId(id);
+          } catch (err) {
             return Promise.reject({message: `Invalid download id ${id}`});
           }
           if (item.state != "in_progress") {
             return Promise.reject({message: `Download ${id} cannot be paused since it is in state ${item.state}`});
           }
 
           return item.download.cancel();
         });
       },
 
       resume(id) {
         return DownloadMap.lazyInit().then(() => {
-          let item = DownloadMap.fromId(id);
-          if (!item) {
+          let item;
+          try {
+            item = DownloadMap.fromId(id);
+          } catch (err) {
             return Promise.reject({message: `Invalid download id ${id}`});
           }
           if (!item.canResume) {
             return Promise.reject({message: `Download ${id} cannot be resumed`});
           }
 
           return item.download.start();
         });
       },
 
       cancel(id) {
         return DownloadMap.lazyInit().then(() => {
-          let item = DownloadMap.fromId(id);
-          if (!item) {
+          let item;
+          try {
+            item = DownloadMap.fromId(id);
+          } catch (err) {
             return Promise.reject({message: `Invalid download id ${id}`});
           }
           if (item.download.succeeded) {
             return Promise.reject({message: `Download ${id} is already complete`});
           }
           return item.download.finalize(true);
         });
       },
--- a/toolkit/components/extensions/test/mochitest/test_chrome_ext_downloads_misc.html
+++ b/toolkit/components/extensions/test/mochitest/test_chrome_ext_downloads_misc.html
@@ -534,16 +534,33 @@ add_task(function* test_pausecancel() {
   is(msg.result[0].state, "interrupted", "download.state is correct");
   is(msg.result[0].paused, false, "download.paused is correct");
   is(msg.result[0].canResume, false, "download.canResume is correct");
   is(msg.result[0].error, "USER_CANCELED", "download.error is correct");
   is(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
   is(msg.result[0].exists, false, "download.exists is correct");
 });
 
+add_task(function* test_pause_resume_cancel_badargs() {
+  let BAD_ID = 1000;
+
+  let msg = yield runInExtension("pause", BAD_ID);
+  info(JSON.stringify(msg));
+  is(msg.status, "error", "pause() failed with a bad download id");
+  ok(/Invalid download id/.test(msg.errmsg), "error message is descriptive");
+
+  msg = yield runInExtension("resume", BAD_ID);
+  is(msg.status, "error", "resume() failed with a bad download id");
+  ok(/Invalid download id/.test(msg.errmsg), "error message is descriptive");
+
+  msg = yield runInExtension("cancel", BAD_ID);
+  is(msg.status, "error", "cancel() failed with a bad download id");
+  ok(/Invalid download id/.test(msg.errmsg), "error message is descriptive");
+});
+
 add_task(function* test_file_removal() {
   let msg = yield runInExtension("download", {url: TXT_URL});
   is(msg.status, "success", "download() succeeded");
   const id = msg.result;
 
   msg = yield runInExtension("waitForEvents", [
     {type: "onCreated", data: {id, url: TXT_URL}},
     {