OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2013 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include <AdblockPlus/tr1_functional.h> |
| 19 |
| 20 #include "BaseJsTest.h" |
| 21 |
| 22 namespace |
| 23 { |
| 24 typedef std::tr1::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; |
| 25 |
| 26 void FindAndReplace(std::string& source, const std::string& find, const std::s
tring& replace) |
| 27 { |
| 28 for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po
s += replace.size()) |
| 29 source.replace(pos, find.size(), replace); |
| 30 } |
| 31 |
| 32 std::string previousRequestUrl; |
| 33 class TestWebRequest : public LazyWebRequest |
| 34 { |
| 35 public: |
| 36 AdblockPlus::ServerResponse response; |
| 37 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H
eaderList& requestHeaders) const |
| 38 { |
| 39 if (url.find("easylist") != std::string::npos) |
| 40 return LazyWebRequest::GET(url, requestHeaders); |
| 41 |
| 42 previousRequestUrl = url; |
| 43 return response; |
| 44 } |
| 45 }; |
| 46 |
| 47 class UpdateCheckTest : public ::testing::Test |
| 48 { |
| 49 protected: |
| 50 AdblockPlus::AppInfo appInfo; |
| 51 TestWebRequest* webRequest; |
| 52 AdblockPlus::WebRequestPtr webRequestPtr; |
| 53 AdblockPlus::JsEnginePtr jsEngine; |
| 54 FilterEnginePtr filterEngine; |
| 55 |
| 56 bool eventCallbackCalled; |
| 57 AdblockPlus::JsValueList eventCallbackParams; |
| 58 static bool updateCallbackCalled; |
| 59 static std::string updateError; |
| 60 |
| 61 void SetUp() |
| 62 { |
| 63 webRequest = new TestWebRequest(); |
| 64 webRequestPtr.reset(webRequest); |
| 65 |
| 66 eventCallbackCalled = false; |
| 67 updateCallbackCalled = false; |
| 68 Reset(); |
| 69 } |
| 70 |
| 71 void Reset() |
| 72 { |
| 73 jsEngine = AdblockPlus::JsEngine::New(appInfo); |
| 74 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem)); |
| 75 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); |
| 76 jsEngine->SetWebRequest(webRequestPtr); |
| 77 jsEngine->SetEventCallback("updateAvailable", |
| 78 std::tr1::bind(&UpdateCheckTest::EventCallback, this, std::tr1::placeh
olders::_1)); |
| 79 |
| 80 filterEngine.reset(new AdblockPlus::FilterEngine(jsEngine)); |
| 81 } |
| 82 |
| 83 void ForceUpdateCheck() |
| 84 { |
| 85 filterEngine->ForceUpdateCheck(&UpdateCheckTest::UpdateCallback); |
| 86 } |
| 87 |
| 88 void EventCallback(AdblockPlus::JsValueList& params) |
| 89 { |
| 90 eventCallbackCalled = true; |
| 91 eventCallbackParams = params; |
| 92 } |
| 93 |
| 94 // This really shouldn't be static but I couldn't make std::bind() work for
it. |
| 95 static void UpdateCallback(const std::string& error) |
| 96 { |
| 97 updateCallbackCalled = true; |
| 98 updateError = error; |
| 99 } |
| 100 }; |
| 101 |
| 102 bool UpdateCheckTest::updateCallbackCalled = false; |
| 103 std::string UpdateCheckTest::updateError; |
| 104 } |
| 105 |
| 106 TEST_F(UpdateCheckTest, RequestFailure) |
| 107 { |
| 108 webRequest->response.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE; |
| 109 |
| 110 appInfo.name = "1"; |
| 111 appInfo.id = "2"; |
| 112 appInfo.version = "3"; |
| 113 appInfo.platform = "4"; |
| 114 appInfo.developmentBuild = false; |
| 115 |
| 116 Reset(); |
| 117 ForceUpdateCheck(); |
| 118 |
| 119 AdblockPlus::Sleep(100); |
| 120 |
| 121 ASSERT_FALSE(eventCallbackCalled); |
| 122 ASSERT_TRUE(updateCallbackCalled); |
| 123 ASSERT_FALSE(updateError.empty()); |
| 124 |
| 125 std::string expectedUrl(filterEngine->GetPref("update_url_release")->AsString(
)); |
| 126 FindAndReplace(expectedUrl, "%NAME%", appInfo.name); |
| 127 FindAndReplace(expectedUrl, "%ID%", appInfo.id); |
| 128 FindAndReplace(expectedUrl, "%VERSION%", appInfo.version); |
| 129 FindAndReplace(expectedUrl, "%APP%", appInfo.platform); |
| 130 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update |
| 131 ASSERT_EQ(expectedUrl, previousRequestUrl); |
| 132 } |
| 133 |
| 134 TEST_F(UpdateCheckTest, UpdateAvailable) |
| 135 { |
| 136 webRequest->response.status = AdblockPlus::WebRequest::NS_OK; |
| 137 webRequest->response.responseStatus = 200; |
| 138 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht
tps://foo.bar/\"}}"; |
| 139 |
| 140 appInfo.name = "1"; |
| 141 appInfo.id = "2"; |
| 142 appInfo.version = "3"; |
| 143 appInfo.platform = "4"; |
| 144 appInfo.developmentBuild = true; |
| 145 |
| 146 Reset(); |
| 147 ForceUpdateCheck(); |
| 148 |
| 149 AdblockPlus::Sleep(100); |
| 150 |
| 151 ASSERT_TRUE(eventCallbackCalled); |
| 152 ASSERT_EQ(1u, eventCallbackParams.size()); |
| 153 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0]->AsString()); |
| 154 ASSERT_TRUE(updateCallbackCalled); |
| 155 ASSERT_TRUE(updateError.empty()); |
| 156 |
| 157 std::string expectedUrl(filterEngine->GetPref("update_url_devbuild")->AsString
()); |
| 158 FindAndReplace(expectedUrl, "%NAME%", appInfo.name); |
| 159 FindAndReplace(expectedUrl, "%ID%", appInfo.id); |
| 160 FindAndReplace(expectedUrl, "%VERSION%", appInfo.version); |
| 161 FindAndReplace(expectedUrl, "%APP%", appInfo.platform); |
| 162 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update |
| 163 ASSERT_EQ(expectedUrl, previousRequestUrl); |
| 164 } |
| 165 |
| 166 TEST_F(UpdateCheckTest, PlatformUpdateAvailable) |
| 167 { |
| 168 webRequest->response.status = AdblockPlus::WebRequest::NS_OK; |
| 169 webRequest->response.responseStatus = 200; |
| 170 webRequest->response.responseText = "{\"1/4\": {\"version\":\"3.1\",\"url\":\"
https://foo.bar/\"}}"; |
| 171 |
| 172 appInfo.name = "1"; |
| 173 appInfo.id = "2"; |
| 174 appInfo.version = "3"; |
| 175 appInfo.platform = "4"; |
| 176 appInfo.developmentBuild = true; |
| 177 |
| 178 Reset(); |
| 179 ForceUpdateCheck(); |
| 180 |
| 181 AdblockPlus::Sleep(100); |
| 182 |
| 183 ASSERT_TRUE(eventCallbackCalled); |
| 184 ASSERT_EQ(1u, eventCallbackParams.size()); |
| 185 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0]->AsString()); |
| 186 ASSERT_TRUE(updateCallbackCalled); |
| 187 ASSERT_TRUE(updateError.empty()); |
| 188 } |
| 189 |
| 190 TEST_F(UpdateCheckTest, WrongPlatform) |
| 191 { |
| 192 webRequest->response.status = AdblockPlus::WebRequest::NS_OK; |
| 193 webRequest->response.responseStatus = 200; |
| 194 webRequest->response.responseText = "{\"1/3\": {\"version\":\"3.1\",\"url\":\"
https://foo.bar/\"}}"; |
| 195 |
| 196 appInfo.name = "1"; |
| 197 appInfo.id = "2"; |
| 198 appInfo.version = "3"; |
| 199 appInfo.platform = "4"; |
| 200 appInfo.developmentBuild = true; |
| 201 |
| 202 Reset(); |
| 203 ForceUpdateCheck(); |
| 204 |
| 205 AdblockPlus::Sleep(100); |
| 206 |
| 207 ASSERT_FALSE(eventCallbackCalled); |
| 208 ASSERT_TRUE(updateCallbackCalled); |
| 209 ASSERT_TRUE(updateError.empty()); |
| 210 } |
| 211 |
| 212 TEST_F(UpdateCheckTest, WrongVersion) |
| 213 { |
| 214 webRequest->response.status = AdblockPlus::WebRequest::NS_OK; |
| 215 webRequest->response.responseStatus = 200; |
| 216 webRequest->response.responseText = "{\"1\": {\"version\":\"3\",\"url\":\"http
s://foo.bar/\"}}"; |
| 217 |
| 218 appInfo.name = "1"; |
| 219 appInfo.id = "2"; |
| 220 appInfo.version = "3"; |
| 221 appInfo.platform = "4"; |
| 222 appInfo.developmentBuild = true; |
| 223 |
| 224 Reset(); |
| 225 ForceUpdateCheck(); |
| 226 |
| 227 AdblockPlus::Sleep(100); |
| 228 |
| 229 ASSERT_FALSE(eventCallbackCalled); |
| 230 ASSERT_TRUE(updateCallbackCalled); |
| 231 ASSERT_TRUE(updateError.empty()); |
| 232 } |
| 233 |
| 234 TEST_F(UpdateCheckTest, WrongURL) |
| 235 { |
| 236 webRequest->response.status = AdblockPlus::WebRequest::NS_OK; |
| 237 webRequest->response.responseStatus = 200; |
| 238 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht
tp://insecure/\"}}"; |
| 239 |
| 240 appInfo.name = "1"; |
| 241 appInfo.id = "2"; |
| 242 appInfo.version = "3"; |
| 243 appInfo.platform = "4"; |
| 244 appInfo.developmentBuild = true; |
| 245 |
| 246 Reset(); |
| 247 ForceUpdateCheck(); |
| 248 |
| 249 AdblockPlus::Sleep(100); |
| 250 |
| 251 ASSERT_FALSE(eventCallbackCalled); |
| 252 ASSERT_TRUE(updateCallbackCalled); |
| 253 ASSERT_FALSE(updateError.empty()); |
| 254 } |
OLD | NEW |