Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | function getPostDataString(aIS) { |
michael@0 | 2 | if (!aIS) |
michael@0 | 3 | return null; |
michael@0 | 4 | |
michael@0 | 5 | var sis = Cc["@mozilla.org/scriptableinputstream;1"]. |
michael@0 | 6 | createInstance(Ci.nsIScriptableInputStream); |
michael@0 | 7 | sis.init(aIS); |
michael@0 | 8 | var dataLines = sis.read(aIS.available()).split("\n"); |
michael@0 | 9 | |
michael@0 | 10 | // only want the last line |
michael@0 | 11 | return dataLines[dataLines.length-1]; |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | function keywordResult(aURL, aPostData, aIsUnsafe) { |
michael@0 | 15 | this.url = aURL; |
michael@0 | 16 | this.postData = aPostData; |
michael@0 | 17 | this.isUnsafe = aIsUnsafe; |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | function keyWordData() {} |
michael@0 | 21 | keyWordData.prototype = { |
michael@0 | 22 | init: function(aKeyWord, aURL, aPostData, aSearchWord) { |
michael@0 | 23 | this.keyword = aKeyWord; |
michael@0 | 24 | this.uri = makeURI(aURL); |
michael@0 | 25 | this.postData = aPostData; |
michael@0 | 26 | this.searchWord = aSearchWord; |
michael@0 | 27 | |
michael@0 | 28 | this.method = (this.postData ? "POST" : "GET"); |
michael@0 | 29 | } |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | function bmKeywordData(aKeyWord, aURL, aPostData, aSearchWord) { |
michael@0 | 33 | this.init(aKeyWord, aURL, aPostData, aSearchWord); |
michael@0 | 34 | } |
michael@0 | 35 | bmKeywordData.prototype = new keyWordData(); |
michael@0 | 36 | |
michael@0 | 37 | function searchKeywordData(aKeyWord, aURL, aPostData, aSearchWord) { |
michael@0 | 38 | this.init(aKeyWord, aURL, aPostData, aSearchWord); |
michael@0 | 39 | } |
michael@0 | 40 | searchKeywordData.prototype = new keyWordData(); |
michael@0 | 41 | |
michael@0 | 42 | var testData = [ |
michael@0 | 43 | [new bmKeywordData("bmget", "http://bmget/search=%s", null, "foo"), |
michael@0 | 44 | new keywordResult("http://bmget/search=foo", null)], |
michael@0 | 45 | |
michael@0 | 46 | [new bmKeywordData("bmpost", "http://bmpost/", "search=%s", "foo2"), |
michael@0 | 47 | new keywordResult("http://bmpost/", "search=foo2")], |
michael@0 | 48 | |
michael@0 | 49 | [new bmKeywordData("bmpostget", "http://bmpostget/search1=%s", "search2=%s", "foo3"), |
michael@0 | 50 | new keywordResult("http://bmpostget/search1=foo3", "search2=foo3")], |
michael@0 | 51 | |
michael@0 | 52 | [new bmKeywordData("bmget-nosearch", "http://bmget-nosearch/", null, ""), |
michael@0 | 53 | new keywordResult("http://bmget-nosearch/", null)], |
michael@0 | 54 | |
michael@0 | 55 | [new searchKeywordData("searchget", "http://searchget/?search={searchTerms}", null, "foo4"), |
michael@0 | 56 | new keywordResult("http://searchget/?search=foo4", null, true)], |
michael@0 | 57 | |
michael@0 | 58 | [new searchKeywordData("searchpost", "http://searchpost/", "search={searchTerms}", "foo5"), |
michael@0 | 59 | new keywordResult("http://searchpost/", "search=foo5", true)], |
michael@0 | 60 | |
michael@0 | 61 | [new searchKeywordData("searchpostget", "http://searchpostget/?search1={searchTerms}", "search2={searchTerms}", "foo6"), |
michael@0 | 62 | new keywordResult("http://searchpostget/?search1=foo6", "search2=foo6", true)], |
michael@0 | 63 | |
michael@0 | 64 | // Bookmark keywords that don't take parameters should not be activated if a |
michael@0 | 65 | // parameter is passed (bug 420328). |
michael@0 | 66 | [new bmKeywordData("bmget-noparam", "http://bmget-noparam/", null, "foo7"), |
michael@0 | 67 | new keywordResult(null, null, true)], |
michael@0 | 68 | [new bmKeywordData("bmpost-noparam", "http://bmpost-noparam/", "not_a=param", "foo8"), |
michael@0 | 69 | new keywordResult(null, null, true)], |
michael@0 | 70 | |
michael@0 | 71 | // Test escaping (%s = escaped, %S = raw) |
michael@0 | 72 | // UTF-8 default |
michael@0 | 73 | [new bmKeywordData("bmget-escaping", "http://bmget/?esc=%s&raw=%S", null, "foé"), |
michael@0 | 74 | new keywordResult("http://bmget/?esc=fo%C3%A9&raw=foé", null)], |
michael@0 | 75 | // Explicitly-defined ISO-8859-1 |
michael@0 | 76 | [new bmKeywordData("bmget-escaping2", "http://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", null, "foé"), |
michael@0 | 77 | new keywordResult("http://bmget/?esc=fo%E9&raw=foé", null)], |
michael@0 | 78 | |
michael@0 | 79 | // Bug 359809: Test escaping +, /, and @ |
michael@0 | 80 | // UTF-8 default |
michael@0 | 81 | [new bmKeywordData("bmget-escaping", "http://bmget/?esc=%s&raw=%S", null, "+/@"), |
michael@0 | 82 | new keywordResult("http://bmget/?esc=%2B%2F%40&raw=+/@", null)], |
michael@0 | 83 | // Explicitly-defined ISO-8859-1 |
michael@0 | 84 | [new bmKeywordData("bmget-escaping2", "http://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", null, "+/@"), |
michael@0 | 85 | new keywordResult("http://bmget/?esc=%2B%2F%40&raw=+/@", null)], |
michael@0 | 86 | |
michael@0 | 87 | // Test using a non-bmKeywordData object, to test the behavior of |
michael@0 | 88 | // getShortcutOrURIAndPostData for non-keywords (setupKeywords only adds keywords for |
michael@0 | 89 | // bmKeywordData objects) |
michael@0 | 90 | [{keyword: "http://gavinsharp.com"}, |
michael@0 | 91 | new keywordResult(null, null, true)] |
michael@0 | 92 | ]; |
michael@0 | 93 | |
michael@0 | 94 | function test() { |
michael@0 | 95 | waitForExplicitFinish(); |
michael@0 | 96 | |
michael@0 | 97 | setupKeywords(); |
michael@0 | 98 | |
michael@0 | 99 | Task.spawn(function() { |
michael@0 | 100 | for each (var item in testData) { |
michael@0 | 101 | let [data, result] = item; |
michael@0 | 102 | |
michael@0 | 103 | let query = data.keyword; |
michael@0 | 104 | if (data.searchWord) |
michael@0 | 105 | query += " " + data.searchWord; |
michael@0 | 106 | let returnedData = yield new Promise( |
michael@0 | 107 | resolve => getShortcutOrURIAndPostData(query, resolve)); |
michael@0 | 108 | // null result.url means we should expect the same query we sent in |
michael@0 | 109 | let expected = result.url || query; |
michael@0 | 110 | is(returnedData.url, expected, "got correct URL for " + data.keyword); |
michael@0 | 111 | is(getPostDataString(returnedData.postData), result.postData, "got correct postData for " + data.keyword); |
michael@0 | 112 | is(returnedData.mayInheritPrincipal, !result.isUnsafe, "got correct mayInheritPrincipal for " + data.keyword); |
michael@0 | 113 | } |
michael@0 | 114 | cleanupKeywords(); |
michael@0 | 115 | }).then(finish); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | var gBMFolder = null; |
michael@0 | 119 | var gAddedEngines = []; |
michael@0 | 120 | function setupKeywords() { |
michael@0 | 121 | gBMFolder = Application.bookmarks.menu.addFolder("keyword-test"); |
michael@0 | 122 | for each (var item in testData) { |
michael@0 | 123 | var data = item[0]; |
michael@0 | 124 | if (data instanceof bmKeywordData) { |
michael@0 | 125 | var bm = gBMFolder.addBookmark(data.keyword, data.uri); |
michael@0 | 126 | bm.keyword = data.keyword; |
michael@0 | 127 | if (data.postData) |
michael@0 | 128 | bm.annotations.set("bookmarkProperties/POSTData", data.postData, Ci.nsIAnnotationService.EXPIRE_SESSION); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | if (data instanceof searchKeywordData) { |
michael@0 | 132 | Services.search.addEngineWithDetails(data.keyword, "", data.keyword, "", data.method, data.uri.spec); |
michael@0 | 133 | var addedEngine = Services.search.getEngineByName(data.keyword); |
michael@0 | 134 | if (data.postData) { |
michael@0 | 135 | var [paramName, paramValue] = data.postData.split("="); |
michael@0 | 136 | addedEngine.addParam(paramName, paramValue, null); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | gAddedEngines.push(addedEngine); |
michael@0 | 140 | } |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | function cleanupKeywords() { |
michael@0 | 145 | gBMFolder.remove(); |
michael@0 | 146 | gAddedEngines.map(Services.search.removeEngine); |
michael@0 | 147 | } |