test_457441-import-export-corrupt-bookmarks-html.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Bug 457441 code.
17  *
18  * The Initial Developer of the Original Code is Mozilla Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2008
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Marco Bonardo <mak77@bonardo.net> (Original Author)
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
39 /*
40  * This test ensures that importing/exporting to HTML does not stop
41  * if a malformed uri is found.
42  */
43 
44 // Get Services
45 var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
46  getService(Ci.nsINavHistoryService);
47 var dbConn = hs.QueryInterface(Ci.nsPIPlacesDatabase).DBConnection;
48 var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
49  getService(Ci.nsINavBookmarksService);
50 var as = Cc["@mozilla.org/browser/annotation-service;1"].
51  getService(Ci.nsIAnnotationService);
52 var lms = Cc["@mozilla.org/browser/livemark-service;2"].
53  getService(Ci.nsILivemarkService);
54 var icos = Cc["@mozilla.org/browser/favicon-service;1"].
55  getService(Ci.nsIFaviconService);
56 var ps = Cc["@mozilla.org/preferences-service;1"].
57  getService(Ci.nsIPrefBranch);
58 var ies = Cc["@mozilla.org/browser/places/import-export-service;1"].
59  getService(Ci.nsIPlacesImportExportService);
60 
61 const DESCRIPTION_ANNO = "bookmarkProperties/description";
62 const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar";
63 const POST_DATA_ANNO = "bookmarkProperties/POSTData";
64 
65 const TEST_FAVICON_PAGE_URL = "http://en-US.www.mozilla.com/en-US/firefox/central/";
66 const TEST_FAVICON_DATA_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg==";
67 
68 // main
69 function run_test() {
70  // avoid creating the places smart folder during tests
71  ps.setIntPref("browser.places.smartBookmarksVersion", -1);
72 
73  // import bookmarks from corrupt file
74  var corruptBookmarksFile = do_get_file("bookmarks.corrupt.html");
75  try {
76  ies.importHTMLFromFile(corruptBookmarksFile, true);
77  } catch(ex) { do_throw("couldn't import corrupt bookmarks file: " + ex); }
78 
79  // Check that every bookmark is correct
80  // Corrupt bookmarks should not have been imported
82 
83  // Create corruption in database
84  var corruptItemId = bs.insertBookmark(bs.toolbarFolder,
85  uri("http://test.mozilla.org"),
86  bs.DEFAULT_INDEX, "We love belugas");
87  var stmt = dbConn.createStatement("UPDATE moz_bookmarks SET fk = NULL WHERE id = :itemId");
88  stmt.params.itemId = corruptItemId;
89  stmt.execute();
90  stmt.finalize();
91 
92  // Export bookmarks
93  var bookmarksFile = dirSvc.get("ProfD", Ci.nsILocalFile);
94  bookmarksFile.append("bookmarks.exported.html");
95  if (bookmarksFile.exists())
96  bookmarksFile.remove(false);
97  bookmarksFile.create(Ci.nsILocalFile.NORMAL_FILE_TYPE, 0600);
98  if (!bookmarksFile.exists())
99  do_throw("couldn't create file: bookmarks.exported.html");
100  try {
101  ies.exportHTMLToFile(bookmarksFile);
102  } catch(ex) { do_throw("couldn't export to bookmarks.exported.html: " + ex); }
103 
104  // Clear all bookmarks
106 
107  // Import bookmarks
108  try {
109  ies.importHTMLFromFile(bookmarksFile, true);
110  } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
111 
112  // Check that every bookmark is correct
113  database_check();
114 }
115 
116 /*
117  * Check for imported bookmarks correctness
118  */
119 function database_check() {
120  // BOOKMARKS MENU
121  var query = hs.getNewQuery();
122  query.setFolders([bs.bookmarksMenuFolder], 1);
123  var options = hs.getNewQueryOptions();
124  options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
125  var result = hs.executeQuery(query, options);
126  var rootNode = result.root;
127  rootNode.containerOpen = true;
128  do_check_eq(rootNode.childCount, 4);
129 
130  // get test folder
131  var testFolder = rootNode.getChild(3);
132  do_check_eq(testFolder.type, testFolder.RESULT_TYPE_FOLDER);
133  do_check_eq(testFolder.title, "test");
134  // add date
135  do_check_eq(bs.getItemDateAdded(testFolder.itemId)/1000000, 1177541020);
136  // last modified
137  do_check_eq(bs.getItemLastModified(testFolder.itemId)/1000000, 1177541050);
138  testFolder = testFolder.QueryInterface(Ci.nsINavHistoryQueryResultNode);
139  do_check_eq(testFolder.hasChildren, true);
140  // folder description
141  do_check_true(as.itemHasAnnotation(testFolder.itemId,
143  do_check_eq("folder test comment",
144  as.getItemAnnotation(testFolder.itemId, DESCRIPTION_ANNO));
145  // open test folder, and test the children
146  testFolder.containerOpen = true;
147  var cc = testFolder.childCount;
148  do_check_eq(cc, 1);
149 
150  // test bookmark 1
151  var testBookmark1 = testFolder.getChild(0);
152  // url
153  do_check_eq("http://test/post", testBookmark1.uri);
154  // title
155  do_check_eq("test post keyword", testBookmark1.title);
156  // keyword
157  do_check_eq("test", bs.getKeywordForBookmark(testBookmark1.itemId));
158  // sidebar
159  do_check_true(as.itemHasAnnotation(testBookmark1.itemId,
161  // add date
162  do_check_eq(testBookmark1.dateAdded/1000000, 1177375336);
163  // last modified
164  do_check_eq(testBookmark1.lastModified/1000000, 1177375423);
165  // post data
166  do_check_true(as.itemHasAnnotation(testBookmark1.itemId,
167  POST_DATA_ANNO));
168  do_check_eq("hidden1%3Dbar&text1%3D%25s",
169  as.getItemAnnotation(testBookmark1.itemId, POST_DATA_ANNO));
170  // last charset
171  var testURI = uri(testBookmark1.uri);
172  do_check_eq("ISO-8859-1", hs.getCharsetForURI(testURI));
173  // description
174  do_check_true(as.itemHasAnnotation(testBookmark1.itemId,
176  do_check_eq("item description",
177  as.getItemAnnotation(testBookmark1.itemId,
179 
180  // clean up
181  testFolder.containerOpen = false;
182  rootNode.containerOpen = false;
183 
184  // BOOKMARKS TOOLBAR
185  query.setFolders([bs.toolbarFolder], 1);
186  result = hs.executeQuery(query, hs.getNewQueryOptions());
187  var toolbar = result.root;
188  toolbar.containerOpen = true;
189  do_check_eq(toolbar.childCount, 3);
190 
191  // livemark
192  var livemark = toolbar.getChild(1);
193  // title
194  do_check_eq("Latest Headlines", livemark.title);
195  // livemark check
196  do_check_true(lms.isLivemark(livemark.itemId));
197  // site url
198  do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/livebookmarks/",
199  lms.getSiteURI(livemark.itemId).spec);
200  // feed url
201  do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/headlines.xml",
202  lms.getFeedURI(livemark.itemId).spec);
203 
204  // cleanup
205  toolbar.containerOpen = false;
206 
207  // UNFILED BOOKMARKS
208  query.setFolders([bs.unfiledBookmarksFolder], 1);
209  result = hs.executeQuery(query, hs.getNewQueryOptions());
210  var unfiledBookmarks = result.root;
211  unfiledBookmarks.containerOpen = true;
212  do_check_eq(unfiledBookmarks.childCount, 1);
213  unfiledBookmarks.containerOpen = false;
214 
215  // favicons
216  var faviconURI = icos.getFaviconForPage(uri(TEST_FAVICON_PAGE_URL));
217  var dataURL = icos.getFaviconDataAsDataURL(faviconURI);
219 }
const Cc
var faviconURI
Definition: FeedWriter.js:1371
do_check_eq(typeof PlacesUtils,"object")
var dirSvc
var testFolder
function remove_all_bookmarks()
getService(Ci.sbIFaceplateManager)
var uri
Definition: FeedWriter.js:1135
foldersync options
Definition: options.js:13
const Ci