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 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 5 | |
michael@0 | 6 | const NETWORK_STATUS_READY = 0; |
michael@0 | 7 | const NETWORK_STATUS_STANDBY = 1; |
michael@0 | 8 | const NETWORK_STATUS_AWAY = 2; |
michael@0 | 9 | |
michael@0 | 10 | const QUEUE_TYPE_UPDATE_STATS = 0; |
michael@0 | 11 | |
michael@0 | 12 | var wifiId = '00'; |
michael@0 | 13 | |
michael@0 | 14 | function getNetworks(callback) { |
michael@0 | 15 | NetworkStatsService._db.getAvailableNetworks(function onGetNetworks(aError, aResult) { |
michael@0 | 16 | callback(aError, aResult); |
michael@0 | 17 | }); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | add_test(function test_clearDB() { |
michael@0 | 21 | getNetworks(function onGetNetworks(error, result) { |
michael@0 | 22 | do_check_eq(error, null); |
michael@0 | 23 | var networks = result; |
michael@0 | 24 | networks.forEach(function(network, index) { |
michael@0 | 25 | networks[index] = {network: network, networkId: NetworkStatsService.getNetworkId(network.id, network.type)}; |
michael@0 | 26 | }, this); |
michael@0 | 27 | |
michael@0 | 28 | NetworkStatsService._db.clearStats(networks, function onDBCleared(error, result) { |
michael@0 | 29 | do_check_eq(error, null); |
michael@0 | 30 | run_next_test(); |
michael@0 | 31 | }); |
michael@0 | 32 | }); |
michael@0 | 33 | }); |
michael@0 | 34 | |
michael@0 | 35 | function getNetworkId(callback) { |
michael@0 | 36 | getNetworks(function onGetNetworks(error, result) { |
michael@0 | 37 | do_check_eq(error, null); |
michael@0 | 38 | var netId = NetworkStatsService.getNetworkId(result[0].id, result[0].type); |
michael@0 | 39 | callback(null, netId); |
michael@0 | 40 | }); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | add_test(function test_networkStatsAvailable_ok() { |
michael@0 | 44 | getNetworkId(function onGetId(error, result) { |
michael@0 | 45 | do_check_eq(error, null); |
michael@0 | 46 | var netId = result; |
michael@0 | 47 | NetworkStatsService.networkStatsAvailable(function (success, msg) { |
michael@0 | 48 | do_check_eq(success, true); |
michael@0 | 49 | run_next_test(); |
michael@0 | 50 | }, netId, true, 1234, 4321, new Date()); |
michael@0 | 51 | }); |
michael@0 | 52 | }); |
michael@0 | 53 | |
michael@0 | 54 | add_test(function test_networkStatsAvailable_failure() { |
michael@0 | 55 | getNetworkId(function onGetId(error, result) { |
michael@0 | 56 | do_check_eq(error, null); |
michael@0 | 57 | var netId = result; |
michael@0 | 58 | NetworkStatsService.networkStatsAvailable(function (success, msg) { |
michael@0 | 59 | do_check_eq(success, false); |
michael@0 | 60 | run_next_test(); |
michael@0 | 61 | }, netId, false, 1234, 4321, new Date()); |
michael@0 | 62 | }); |
michael@0 | 63 | }); |
michael@0 | 64 | |
michael@0 | 65 | add_test(function test_update_invalidNetwork() { |
michael@0 | 66 | NetworkStatsService.update(-1, function (success, msg) { |
michael@0 | 67 | do_check_eq(success, false); |
michael@0 | 68 | do_check_eq(msg, "Invalid network -1"); |
michael@0 | 69 | run_next_test(); |
michael@0 | 70 | }); |
michael@0 | 71 | }); |
michael@0 | 72 | |
michael@0 | 73 | add_test(function test_update() { |
michael@0 | 74 | getNetworkId(function onGetId(error, result) { |
michael@0 | 75 | do_check_eq(error, null); |
michael@0 | 76 | var netId = result; |
michael@0 | 77 | NetworkStatsService.update(netId, function (success, msg) { |
michael@0 | 78 | do_check_eq(success, true); |
michael@0 | 79 | run_next_test(); |
michael@0 | 80 | }); |
michael@0 | 81 | }); |
michael@0 | 82 | }); |
michael@0 | 83 | |
michael@0 | 84 | add_test(function test_updateQueueIndex() { |
michael@0 | 85 | NetworkStatsService.updateQueue = [{netId: 0, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, |
michael@0 | 86 | {netId: 1, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, |
michael@0 | 87 | {netId: 2, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, |
michael@0 | 88 | {netId: 3, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, |
michael@0 | 89 | {netId: 4, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}]; |
michael@0 | 90 | var index = NetworkStatsService.updateQueueIndex(3); |
michael@0 | 91 | do_check_eq(index, 3); |
michael@0 | 92 | index = NetworkStatsService.updateQueueIndex(10); |
michael@0 | 93 | do_check_eq(index, -1); |
michael@0 | 94 | |
michael@0 | 95 | NetworkStatsService.updateQueue = []; |
michael@0 | 96 | run_next_test(); |
michael@0 | 97 | }); |
michael@0 | 98 | |
michael@0 | 99 | add_test(function test_updateAllStats() { |
michael@0 | 100 | NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY; |
michael@0 | 101 | NetworkStatsService.updateAllStats(function(success, msg) { |
michael@0 | 102 | do_check_eq(success, true); |
michael@0 | 103 | NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY; |
michael@0 | 104 | NetworkStatsService.updateAllStats(function(success, msg) { |
michael@0 | 105 | do_check_eq(success, true); |
michael@0 | 106 | NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_AWAY; |
michael@0 | 107 | NetworkStatsService.updateAllStats(function(success, msg) { |
michael@0 | 108 | do_check_eq(success, true); |
michael@0 | 109 | run_next_test(); |
michael@0 | 110 | }); |
michael@0 | 111 | }); |
michael@0 | 112 | }); |
michael@0 | 113 | }); |
michael@0 | 114 | |
michael@0 | 115 | add_test(function test_updateStats_ok() { |
michael@0 | 116 | getNetworkId(function onGetId(error, result) { |
michael@0 | 117 | do_check_eq(error, null); |
michael@0 | 118 | var netId = result; |
michael@0 | 119 | NetworkStatsService.updateStats(netId, function(success, msg){ |
michael@0 | 120 | do_check_eq(success, true); |
michael@0 | 121 | run_next_test(); |
michael@0 | 122 | }); |
michael@0 | 123 | }); |
michael@0 | 124 | }); |
michael@0 | 125 | |
michael@0 | 126 | add_test(function test_updateStats_failure() { |
michael@0 | 127 | NetworkStatsService.updateStats(-1, function(success, msg){ |
michael@0 | 128 | do_check_eq(success, false); |
michael@0 | 129 | run_next_test(); |
michael@0 | 130 | }); |
michael@0 | 131 | }); |
michael@0 | 132 | |
michael@0 | 133 | // Define Mockup function to simulate a request to netd |
michael@0 | 134 | function MockNetdRequest(aCallback) { |
michael@0 | 135 | var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
michael@0 | 136 | var event = { |
michael@0 | 137 | notify: function (timer) { |
michael@0 | 138 | aCallback(); |
michael@0 | 139 | } |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | timer.initWithCallback(event, 100, Ci.nsITimer.TYPE_ONE_SHOT); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | add_test(function test_queue() { |
michael@0 | 146 | |
michael@0 | 147 | // Overwrite update function of NetworkStatsService to avoid netd errors due to use |
michael@0 | 148 | // fake interfaces. First, original function is stored to restore it at the end of the |
michael@0 | 149 | // test. |
michael@0 | 150 | var updateFunctionBackup = NetworkStatsService.update; |
michael@0 | 151 | |
michael@0 | 152 | NetworkStatsService.update = function update(aNetId, aCallback) { |
michael@0 | 153 | MockNetdRequest(function () { |
michael@0 | 154 | if (aCallback) { |
michael@0 | 155 | aCallback(true, "ok"); |
michael@0 | 156 | } |
michael@0 | 157 | }); |
michael@0 | 158 | }; |
michael@0 | 159 | |
michael@0 | 160 | // Fill networks with fake network interfaces to enable netd async requests. |
michael@0 | 161 | var network = {id: "1234", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE}; |
michael@0 | 162 | var netId1 = NetworkStatsService.getNetworkId(network.id, network.type); |
michael@0 | 163 | NetworkStatsService._networks[netId1] = { network: network, |
michael@0 | 164 | interfaceName: "net1" }; |
michael@0 | 165 | |
michael@0 | 166 | network = {id: "5678", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE}; |
michael@0 | 167 | var netId2 = NetworkStatsService.getNetworkId(network.id, network.type); |
michael@0 | 168 | NetworkStatsService._networks[netId2] = { network: network, |
michael@0 | 169 | interfaceName: "net2" }; |
michael@0 | 170 | |
michael@0 | 171 | NetworkStatsService.updateStats(netId1); |
michael@0 | 172 | NetworkStatsService.updateStats(netId2); |
michael@0 | 173 | do_check_eq(NetworkStatsService.updateQueue.length, 2); |
michael@0 | 174 | do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 1); |
michael@0 | 175 | |
michael@0 | 176 | var i = 0; |
michael@0 | 177 | var updateCount = 0; |
michael@0 | 178 | var callback = function(success, msg) { |
michael@0 | 179 | i++; |
michael@0 | 180 | if (i >= updateCount) { |
michael@0 | 181 | NetworkStatsService.update = updateFunctionBackup; |
michael@0 | 182 | run_next_test(); |
michael@0 | 183 | } |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | NetworkStatsService.updateStats(netId1, callback); |
michael@0 | 187 | updateCount++; |
michael@0 | 188 | NetworkStatsService.updateStats(netId2, callback); |
michael@0 | 189 | updateCount++; |
michael@0 | 190 | |
michael@0 | 191 | do_check_eq(NetworkStatsService.updateQueue.length, 2); |
michael@0 | 192 | do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 2); |
michael@0 | 193 | do_check_eq(NetworkStatsService.updateQueue[0].callbacks[0], null); |
michael@0 | 194 | do_check_neq(NetworkStatsService.updateQueue[0].callbacks[1], null); |
michael@0 | 195 | }); |
michael@0 | 196 | |
michael@0 | 197 | add_test(function test_getAlarmQuota() { |
michael@0 | 198 | let alarm = { networkId: wifiId, absoluteThreshold: 10000 }; |
michael@0 | 199 | |
michael@0 | 200 | NetworkStatsService._getAlarmQuota(alarm, function onSet(error, quota){ |
michael@0 | 201 | do_check_eq(error, null); |
michael@0 | 202 | do_check_neq(quota, undefined); |
michael@0 | 203 | do_check_eq(alarm.absoluteThreshold, alarm.relativeThreshold); |
michael@0 | 204 | run_next_test(); |
michael@0 | 205 | }); |
michael@0 | 206 | }); |
michael@0 | 207 | |
michael@0 | 208 | var testPageURL = "http://test.com"; |
michael@0 | 209 | var testManifestURL = "http://test.com/manifest.webapp"; |
michael@0 | 210 | |
michael@0 | 211 | add_test(function test_setAlarm() { |
michael@0 | 212 | let alarm = { id: null, |
michael@0 | 213 | networkId: wifiId, |
michael@0 | 214 | threshold: 10000, |
michael@0 | 215 | absoluteThreshold: null, |
michael@0 | 216 | alarmStart: null, |
michael@0 | 217 | alarmEnd: null, |
michael@0 | 218 | data: null, |
michael@0 | 219 | pageURL: testPageURL, |
michael@0 | 220 | manifestURL: testManifestURL }; |
michael@0 | 221 | |
michael@0 | 222 | NetworkStatsService._setAlarm(alarm, function onSet(error, result) { |
michael@0 | 223 | do_check_eq(result, 1); |
michael@0 | 224 | run_next_test(); |
michael@0 | 225 | }); |
michael@0 | 226 | }); |
michael@0 | 227 | |
michael@0 | 228 | add_test(function test_setAlarm_invalid_threshold() { |
michael@0 | 229 | let alarm = { id: null, |
michael@0 | 230 | networkId: wifiId, |
michael@0 | 231 | threshold: -10000, |
michael@0 | 232 | absoluteThreshold: null, |
michael@0 | 233 | alarmStart: null, |
michael@0 | 234 | alarmEnd: null, |
michael@0 | 235 | data: null, |
michael@0 | 236 | pageURL: testPageURL, |
michael@0 | 237 | manifestURL: testManifestURL }; |
michael@0 | 238 | |
michael@0 | 239 | NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY; |
michael@0 | 240 | |
michael@0 | 241 | NetworkStatsService._setAlarm(alarm, function onSet(error, result) { |
michael@0 | 242 | do_check_eq(error, "InvalidStateError"); |
michael@0 | 243 | run_next_test(); |
michael@0 | 244 | }); |
michael@0 | 245 | }); |
michael@0 | 246 | |
michael@0 | 247 | add_test(function test_fireAlarm() { |
michael@0 | 248 | // Add a fake alarm into database. |
michael@0 | 249 | let alarm = { id: null, |
michael@0 | 250 | networkId: wifiId, |
michael@0 | 251 | threshold: 10000, |
michael@0 | 252 | absoluteThreshold: null, |
michael@0 | 253 | alarmStart: null, |
michael@0 | 254 | alarmEnd: null, |
michael@0 | 255 | data: null, |
michael@0 | 256 | pageURL: testPageURL, |
michael@0 | 257 | manifestURL: testManifestURL }; |
michael@0 | 258 | |
michael@0 | 259 | // Set wifi status to standby to avoid connecting to netd when adding an alarm. |
michael@0 | 260 | NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY; |
michael@0 | 261 | |
michael@0 | 262 | NetworkStatsService._db.addAlarm(alarm, function addSuccessCb(error, newId) { |
michael@0 | 263 | NetworkStatsService._db.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, |
michael@0 | 264 | testManifestURL, function onGet(error, result) { |
michael@0 | 265 | do_check_eq(error, null); |
michael@0 | 266 | do_check_eq(result.length, 1); |
michael@0 | 267 | |
michael@0 | 268 | // Result of getAlarms is based on expected child's data format, so |
michael@0 | 269 | // some changes are needed to be able to use it. |
michael@0 | 270 | result[0].networkId = wifiId; |
michael@0 | 271 | result[0].pageURL = testPageURL; |
michael@0 | 272 | result[0].manifestURL = testManifestURL; |
michael@0 | 273 | |
michael@0 | 274 | NetworkStatsService._fireAlarm(result[0], false); |
michael@0 | 275 | NetworkStatsService._db.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, |
michael@0 | 276 | testManifestURL, function onGet(error, result) { |
michael@0 | 277 | do_check_eq(error, undefined); |
michael@0 | 278 | do_check_eq(result.length, 0); |
michael@0 | 279 | run_next_test(); |
michael@0 | 280 | }); |
michael@0 | 281 | }); |
michael@0 | 282 | }); |
michael@0 | 283 | }); |
michael@0 | 284 | |
michael@0 | 285 | function run_test() { |
michael@0 | 286 | do_get_profile(); |
michael@0 | 287 | |
michael@0 | 288 | Cu.import("resource://gre/modules/NetworkStatsService.jsm"); |
michael@0 | 289 | run_next_test(); |
michael@0 | 290 | } |