Bug 1373610 - Clarify BookmarkJSONUtils' handling of invalid bookmark types and add a test. r?mak draft
authorMark Banner <standard8@mozilla.com>
Tue, 21 Nov 2017 16:36:28 +0000
changeset 701428 79e86a8d7027e35a51932c68542f937f6c7ec5a1
parent 701250 72ee4800d4156931c89b58bd807af4a3083702bb
child 741156 53cc98f105b68d69fdcc8080ddc1ed17524a5ee7
push id90151
push userbmo:standard8@mozilla.com
push dateTue, 21 Nov 2017 16:38:50 +0000
reviewersmak
bugs1373610
milestone59.0a1
Bug 1373610 - Clarify BookmarkJSONUtils' handling of invalid bookmark types and add a test. r?mak MozReview-Commit-ID: 4IckKeQsgzo
toolkit/components/places/BookmarkJSONUtils.jsm
toolkit/components/places/tests/unit/bookmarks_corrupt.json
toolkit/components/places/tests/unit/test_bookmarks_json_corrupt.js
toolkit/components/places/tests/unit/xpcshell.ini
--- a/toolkit/components/places/BookmarkJSONUtils.jsm
+++ b/toolkit/components/places/BookmarkJSONUtils.jsm
@@ -437,17 +437,17 @@ function translateTreeTypes(node) {
       break;
     case PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR:
       node.type = PlacesUtils.bookmarks.TYPE_SEPARATOR;
       if ("title" in node) {
         delete node.title;
       }
       break;
     default:
-      // TODO We should handle this in a more robust fashion, see bug 1373610.
+      // No need to throw/reject here, insertTree will remove this node automatically.
       Cu.reportError(`Unexpected bookmark type ${node.type}`);
       break;
   }
 
   if (node.dateAdded) {
     node.dateAdded = PlacesUtils.toDate(node.dateAdded);
   }
 
new file mode 100644
--- /dev/null
+++ b/toolkit/components/places/tests/unit/bookmarks_corrupt.json
@@ -0,0 +1,42 @@
+{
+  "guid": "root________",
+  "title": "",
+  "id": 1,
+  "dateAdded": 1361551978957783,
+  "lastModified": 1361551978957783,
+  "type": "text/x-moz-place-container",
+  "root": "placesRoot",
+  "children": [
+    {
+      "guid": "menu________",
+      "title": "Bookmarks Menu",
+      "id": 2,
+      "parent": 1,
+      "dateAdded": 1361551978957783,
+      "lastModified": 1361551979382837,
+      "type": "text/x-moz-place-container",
+      "root": "bookmarksMenuFolder",
+      "children": [
+        {
+          "guid": "OCyeUO5uu9FG",
+          "title": "Help and Tutorials",
+          "id": 7,
+          "dateAdded": 1361551979356436,
+          "lastModified": 1361551979362718,
+          "type": "x/invalid",
+          "uri": "http://en-us.www.mozilla.com/en-US/firefox/help/"
+        },
+        {
+          "guid": "OCyeUO5uu9FH",
+          "index": 1,
+          "title": "Customize Firefox",
+          "id": 8,
+          "dateAdded": 1361551979365662,
+          "lastModified": 1361551979368077,
+          "type": "text/x-moz-place",
+          "uri": "http://en-us.www.mozilla.com/en-US/firefox/customize/"
+        }
+      ]
+    }
+  ]
+}
new file mode 100644
--- /dev/null
+++ b/toolkit/components/places/tests/unit/test_bookmarks_json_corrupt.js
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+Cu.import("resource://gre/modules/BookmarkJSONUtils.jsm");
+
+// Exported bookmarks file pointer.
+var bookmarksExportedFile;
+
+add_task(async function test_import_bookmarks() {
+  let bookmarksFile = OS.Path.join(do_get_cwd().path, "bookmarks_corrupt.json");
+
+  await BookmarkJSONUtils.importFromFile(bookmarksFile, true);
+  await PlacesTestUtils.promiseAsyncUpdates();
+
+  let bookmarks = await PlacesUtils.promiseBookmarksTree(PlacesUtils.bookmarks.menuGuid);
+
+  Assert.equal(bookmarks.children.length, 1, "should only be one bookmark");
+  let bookmark = bookmarks.children[0];
+  Assert.equal(bookmark.guid, "OCyeUO5uu9FH", "should have correct guid");
+  Assert.equal(bookmark.title, "Customize Firefox", "should have correct title");
+  Assert.equal(bookmark.uri, "http://en-us.www.mozilla.com/en-US/firefox/customize/",
+    "should have correct uri");
+});
--- a/toolkit/components/places/tests/unit/xpcshell.ini
+++ b/toolkit/components/places/tests/unit/xpcshell.ini
@@ -1,14 +1,15 @@
 [DEFAULT]
 head = head_bookmarks.js
 firefox-appdir = browser
 support-files =
   bookmarks.corrupt.html
   bookmarks.json
+  bookmarks_corrupt.json
   bookmarks.preplaces.html
   bookmarks_html_singleframe.html
   bug476292.sqlite
   corruptDB.sqlite
   default.sqlite
   livemark.xml
   mobile_bookmarks_folder_import.json
   mobile_bookmarks_folder_merge.json
@@ -57,16 +58,17 @@ skip-if = os == "linux"
 [test_adaptive_bug527311.js]
 [test_annotations.js]
 [test_asyncExecuteLegacyQueries.js]
 [test_async_in_batchmode.js]
 [test_async_transactions.js]
 skip-if = (os == "win" && os_version == "5.1") # Bug 1158887
 [test_bookmark_catobs.js]
 [test_bookmarks_json.js]
+[test_bookmarks_json_corrupt.js]
 [test_bookmarks_html.js]
 [test_bookmarks_html_corrupt.js]
 [test_bookmarks_html_escape_entities.js]
 [test_bookmarks_html_import_tags.js]
 [test_bookmarks_html_singleframe.js]
 [test_bookmarks_restore_notification.js]
 [test_broken_folderShortcut_result.js]
 [test_browserhistory.js]