test_service_child.js (9171B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 var db; 7 8 function done() { 9 do_test_finished(); 10 run_next_test(); 11 } 12 13 function generateKey() { 14 return crypto.subtle 15 .generateKey( 16 { 17 name: "ECDSA", 18 namedCurve: "P-256", 19 }, 20 true, 21 ["sign", "verify"] 22 ) 23 .then(cryptoKey => crypto.subtle.exportKey("raw", cryptoKey.publicKey)) 24 .then(publicKey => new Uint8Array(publicKey)); 25 } 26 27 function run_test() { 28 if (isParent) { 29 do_get_profile(); 30 } 31 run_next_test(); 32 } 33 34 if (isParent) { 35 add_test(function setUp() { 36 db = PushServiceWebSocket.newPushDB(); 37 registerCleanupFunction(() => { 38 return db.drop().then(_ => db.close()); 39 }); 40 setUpServiceInParent(PushService, db).then(run_next_test, run_next_test); 41 }); 42 } 43 44 add_test(function test_subscribe_success() { 45 do_test_pending(); 46 PushServiceComponent.subscribe( 47 "https://example.com/sub/ok", 48 Services.scriptSecurityManager.getSystemPrincipal(), 49 (result, subscription) => { 50 ok(Components.isSuccessCode(result), "Error creating subscription"); 51 ok(subscription.isSystemSubscription, "Expected system subscription"); 52 ok( 53 subscription.endpoint.startsWith("https://example.org/push"), 54 "Wrong endpoint prefix" 55 ); 56 equal(subscription.pushCount, 0, "Wrong push count"); 57 equal(subscription.lastPush, 0, "Wrong last push time"); 58 equal(subscription.quota, -1, "Wrong quota for system subscription"); 59 60 do_test_finished(); 61 run_next_test(); 62 } 63 ); 64 }); 65 66 add_test(function test_subscribeWithKey_error() { 67 do_test_pending(); 68 69 let invalidKey = [0, 1]; 70 PushServiceComponent.subscribeWithKey( 71 "https://example.com/sub-key/invalid", 72 Services.scriptSecurityManager.getSystemPrincipal(), 73 invalidKey, 74 (result, subscription) => { 75 ok( 76 !Components.isSuccessCode(result), 77 "Expected error creating subscription with invalid key" 78 ); 79 equal( 80 result, 81 Cr.NS_ERROR_DOM_PUSH_INVALID_KEY_ERR, 82 "Wrong error code for invalid key" 83 ); 84 strictEqual(subscription, null, "Unexpected subscription"); 85 86 do_test_finished(); 87 run_next_test(); 88 } 89 ); 90 }); 91 92 add_test(function test_subscribeWithKey_success() { 93 do_test_pending(); 94 95 generateKey().then( 96 key => { 97 PushServiceComponent.subscribeWithKey( 98 "https://example.com/sub-key/ok", 99 Services.scriptSecurityManager.getSystemPrincipal(), 100 key, 101 (result, subscription) => { 102 ok( 103 Components.isSuccessCode(result), 104 "Error creating subscription with key" 105 ); 106 notStrictEqual(subscription, null, "Expected subscription"); 107 done(); 108 } 109 ); 110 }, 111 () => { 112 ok(false, "Error generating app server key"); 113 done(); 114 } 115 ); 116 }); 117 118 add_test(function test_subscribeWithKey_conflict() { 119 do_test_pending(); 120 121 generateKey().then( 122 differentKey => { 123 PushServiceComponent.subscribeWithKey( 124 "https://example.com/sub-key/ok", 125 Services.scriptSecurityManager.getSystemPrincipal(), 126 differentKey, 127 (result, subscription) => { 128 ok( 129 !Components.isSuccessCode(result), 130 "Expected error creating subscription with conflicting key" 131 ); 132 equal( 133 result, 134 Cr.NS_ERROR_DOM_PUSH_MISMATCHED_KEY_ERR, 135 "Wrong error code for mismatched key" 136 ); 137 strictEqual(subscription, null, "Unexpected subscription"); 138 done(); 139 } 140 ); 141 }, 142 () => { 143 ok(false, "Error generating different app server key"); 144 done(); 145 } 146 ); 147 }); 148 149 add_test(function test_subscribe_error() { 150 do_test_pending(); 151 PushServiceComponent.subscribe( 152 "https://example.com/sub/fail", 153 Services.scriptSecurityManager.getSystemPrincipal(), 154 (result, subscription) => { 155 ok( 156 !Components.isSuccessCode(result), 157 "Expected error creating subscription" 158 ); 159 strictEqual(subscription, null, "Unexpected subscription"); 160 161 do_test_finished(); 162 run_next_test(); 163 } 164 ); 165 }); 166 167 add_test(function test_getSubscription_exists() { 168 do_test_pending(); 169 PushServiceComponent.getSubscription( 170 "https://example.com/get/ok", 171 Services.scriptSecurityManager.getSystemPrincipal(), 172 (result, subscription) => { 173 ok(Components.isSuccessCode(result), "Error getting subscription"); 174 175 equal( 176 subscription.endpoint, 177 "https://example.org/push/get", 178 "Wrong endpoint" 179 ); 180 equal(subscription.pushCount, 10, "Wrong push count"); 181 equal(subscription.lastPush, 1438360548322, "Wrong last push"); 182 equal(subscription.quota, 16, "Wrong quota for subscription"); 183 184 do_test_finished(); 185 run_next_test(); 186 } 187 ); 188 }); 189 190 add_test(function test_getSubscription_missing() { 191 do_test_pending(); 192 PushServiceComponent.getSubscription( 193 "https://example.com/get/missing", 194 Services.scriptSecurityManager.getSystemPrincipal(), 195 (result, subscription) => { 196 ok( 197 Components.isSuccessCode(result), 198 "Error getting nonexistent subscription" 199 ); 200 strictEqual( 201 subscription, 202 null, 203 "Nonexistent subscriptions should return null" 204 ); 205 206 do_test_finished(); 207 run_next_test(); 208 } 209 ); 210 }); 211 212 add_test(function test_getSubscription_error() { 213 do_test_pending(); 214 PushServiceComponent.getSubscription( 215 "https://example.com/get/fail", 216 Services.scriptSecurityManager.getSystemPrincipal(), 217 (result, subscription) => { 218 ok( 219 !Components.isSuccessCode(result), 220 "Expected error getting subscription" 221 ); 222 strictEqual(subscription, null, "Unexpected subscription"); 223 224 do_test_finished(); 225 run_next_test(); 226 } 227 ); 228 }); 229 230 add_test(function test_unsubscribe_success() { 231 do_test_pending(); 232 PushServiceComponent.unsubscribe( 233 "https://example.com/unsub/ok", 234 Services.scriptSecurityManager.getSystemPrincipal(), 235 (result, success) => { 236 ok(Components.isSuccessCode(result), "Error unsubscribing"); 237 strictEqual(success, true, "Expected successful unsubscribe"); 238 239 do_test_finished(); 240 run_next_test(); 241 } 242 ); 243 }); 244 245 add_test(function test_unsubscribe_nonexistent() { 246 do_test_pending(); 247 PushServiceComponent.unsubscribe( 248 "https://example.com/unsub/ok", 249 Services.scriptSecurityManager.getSystemPrincipal(), 250 (result, success) => { 251 ok( 252 Components.isSuccessCode(result), 253 "Error removing nonexistent subscription" 254 ); 255 strictEqual( 256 success, 257 false, 258 "Nonexistent subscriptions should return false" 259 ); 260 261 do_test_finished(); 262 run_next_test(); 263 } 264 ); 265 }); 266 267 add_test(function test_unsubscribe_error() { 268 do_test_pending(); 269 PushServiceComponent.unsubscribe( 270 "https://example.com/unsub/fail", 271 Services.scriptSecurityManager.getSystemPrincipal(), 272 (result, success) => { 273 ok(!Components.isSuccessCode(result), "Expected error unsubscribing"); 274 strictEqual(success, false, "Unexpected successful unsubscribe"); 275 276 do_test_finished(); 277 run_next_test(); 278 } 279 ); 280 }); 281 282 add_test(function test_subscribe_origin_principal() { 283 let scope = "https://example.net/origin-principal"; 284 let principal = 285 Services.scriptSecurityManager.createContentPrincipalFromOrigin(scope); 286 287 do_test_pending(); 288 PushServiceComponent.subscribe(scope, principal, (result, subscription) => { 289 ok( 290 Components.isSuccessCode(result), 291 "Expected error creating subscription with origin principal" 292 ); 293 ok( 294 !subscription.isSystemSubscription, 295 "Unexpected system subscription for origin principal" 296 ); 297 equal(subscription.quota, 16, "Wrong quota for origin subscription"); 298 299 do_test_finished(); 300 run_next_test(); 301 }); 302 }); 303 304 add_test(function test_subscribe_null_principal() { 305 do_test_pending(); 306 PushServiceComponent.subscribe( 307 "chrome://push/null-principal", 308 Services.scriptSecurityManager.createNullPrincipal({}), 309 (result, subscription) => { 310 ok( 311 !Components.isSuccessCode(result), 312 "Expected error creating subscription with null principal" 313 ); 314 strictEqual( 315 subscription, 316 null, 317 "Unexpected subscription with null principal" 318 ); 319 320 do_test_finished(); 321 run_next_test(); 322 } 323 ); 324 }); 325 326 add_test(function test_subscribe_missing_principal() { 327 do_test_pending(); 328 PushServiceComponent.subscribe( 329 "chrome://push/missing-principal", 330 null, 331 (result, subscription) => { 332 ok( 333 !Components.isSuccessCode(result), 334 "Expected error creating subscription without principal" 335 ); 336 strictEqual( 337 subscription, 338 null, 339 "Unexpected subscription without principal" 340 ); 341 342 do_test_finished(); 343 run_next_test(); 344 } 345 ); 346 }); 347 348 if (isParent) { 349 add_test(function tearDown() { 350 tearDownServiceInParent(db).then(run_next_test, run_next_test); 351 }); 352 }