diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/push/test/xpcshell/test_service_child.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/push/test/xpcshell/test_service_child.js')
-rw-r--r-- | dom/push/test/xpcshell/test_service_child.js | 354 |
1 files changed, 354 insertions, 0 deletions
diff --git a/dom/push/test/xpcshell/test_service_child.js b/dom/push/test/xpcshell/test_service_child.js new file mode 100644 index 0000000000..2c96200902 --- /dev/null +++ b/dom/push/test/xpcshell/test_service_child.js @@ -0,0 +1,354 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +Cu.importGlobalProperties(["crypto"]); + +var db; + +function done() { + do_test_finished(); + run_next_test(); +} + +function generateKey() { + return crypto.subtle + .generateKey( + { + name: "ECDSA", + namedCurve: "P-256", + }, + true, + ["sign", "verify"] + ) + .then(cryptoKey => crypto.subtle.exportKey("raw", cryptoKey.publicKey)) + .then(publicKey => new Uint8Array(publicKey)); +} + +function run_test() { + if (isParent) { + do_get_profile(); + } + run_next_test(); +} + +if (isParent) { + add_test(function setUp() { + db = PushServiceWebSocket.newPushDB(); + registerCleanupFunction(() => { + return db.drop().then(_ => db.close()); + }); + setUpServiceInParent(PushService, db).then(run_next_test, run_next_test); + }); +} + +add_test(function test_subscribe_success() { + do_test_pending(); + PushServiceComponent.subscribe( + "https://example.com/sub/ok", + Services.scriptSecurityManager.getSystemPrincipal(), + (result, subscription) => { + ok(Components.isSuccessCode(result), "Error creating subscription"); + ok(subscription.isSystemSubscription, "Expected system subscription"); + ok( + subscription.endpoint.startsWith("https://example.org/push"), + "Wrong endpoint prefix" + ); + equal(subscription.pushCount, 0, "Wrong push count"); + equal(subscription.lastPush, 0, "Wrong last push time"); + equal(subscription.quota, -1, "Wrong quota for system subscription"); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_subscribeWithKey_error() { + do_test_pending(); + + let invalidKey = [0, 1]; + PushServiceComponent.subscribeWithKey( + "https://example.com/sub-key/invalid", + Services.scriptSecurityManager.getSystemPrincipal(), + invalidKey, + (result, subscription) => { + ok( + !Components.isSuccessCode(result), + "Expected error creating subscription with invalid key" + ); + equal( + result, + Cr.NS_ERROR_DOM_PUSH_INVALID_KEY_ERR, + "Wrong error code for invalid key" + ); + strictEqual(subscription, null, "Unexpected subscription"); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_subscribeWithKey_success() { + do_test_pending(); + + generateKey().then( + key => { + PushServiceComponent.subscribeWithKey( + "https://example.com/sub-key/ok", + Services.scriptSecurityManager.getSystemPrincipal(), + key, + (result, subscription) => { + ok( + Components.isSuccessCode(result), + "Error creating subscription with key" + ); + notStrictEqual(subscription, null, "Expected subscription"); + done(); + } + ); + }, + error => { + ok(false, "Error generating app server key"); + done(); + } + ); +}); + +add_test(function test_subscribeWithKey_conflict() { + do_test_pending(); + + generateKey().then( + differentKey => { + PushServiceComponent.subscribeWithKey( + "https://example.com/sub-key/ok", + Services.scriptSecurityManager.getSystemPrincipal(), + differentKey, + (result, subscription) => { + ok( + !Components.isSuccessCode(result), + "Expected error creating subscription with conflicting key" + ); + equal( + result, + Cr.NS_ERROR_DOM_PUSH_MISMATCHED_KEY_ERR, + "Wrong error code for mismatched key" + ); + strictEqual(subscription, null, "Unexpected subscription"); + done(); + } + ); + }, + error => { + ok(false, "Error generating different app server key"); + done(); + } + ); +}); + +add_test(function test_subscribe_error() { + do_test_pending(); + PushServiceComponent.subscribe( + "https://example.com/sub/fail", + Services.scriptSecurityManager.getSystemPrincipal(), + (result, subscription) => { + ok( + !Components.isSuccessCode(result), + "Expected error creating subscription" + ); + strictEqual(subscription, null, "Unexpected subscription"); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_getSubscription_exists() { + do_test_pending(); + PushServiceComponent.getSubscription( + "https://example.com/get/ok", + Services.scriptSecurityManager.getSystemPrincipal(), + (result, subscription) => { + ok(Components.isSuccessCode(result), "Error getting subscription"); + + equal( + subscription.endpoint, + "https://example.org/push/get", + "Wrong endpoint" + ); + equal(subscription.pushCount, 10, "Wrong push count"); + equal(subscription.lastPush, 1438360548322, "Wrong last push"); + equal(subscription.quota, 16, "Wrong quota for subscription"); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_getSubscription_missing() { + do_test_pending(); + PushServiceComponent.getSubscription( + "https://example.com/get/missing", + Services.scriptSecurityManager.getSystemPrincipal(), + (result, subscription) => { + ok( + Components.isSuccessCode(result), + "Error getting nonexistent subscription" + ); + strictEqual( + subscription, + null, + "Nonexistent subscriptions should return null" + ); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_getSubscription_error() { + do_test_pending(); + PushServiceComponent.getSubscription( + "https://example.com/get/fail", + Services.scriptSecurityManager.getSystemPrincipal(), + (result, subscription) => { + ok( + !Components.isSuccessCode(result), + "Expected error getting subscription" + ); + strictEqual(subscription, null, "Unexpected subscription"); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_unsubscribe_success() { + do_test_pending(); + PushServiceComponent.unsubscribe( + "https://example.com/unsub/ok", + Services.scriptSecurityManager.getSystemPrincipal(), + (result, success) => { + ok(Components.isSuccessCode(result), "Error unsubscribing"); + strictEqual(success, true, "Expected successful unsubscribe"); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_unsubscribe_nonexistent() { + do_test_pending(); + PushServiceComponent.unsubscribe( + "https://example.com/unsub/ok", + Services.scriptSecurityManager.getSystemPrincipal(), + (result, success) => { + ok( + Components.isSuccessCode(result), + "Error removing nonexistent subscription" + ); + strictEqual( + success, + false, + "Nonexistent subscriptions should return false" + ); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_unsubscribe_error() { + do_test_pending(); + PushServiceComponent.unsubscribe( + "https://example.com/unsub/fail", + Services.scriptSecurityManager.getSystemPrincipal(), + (result, success) => { + ok(!Components.isSuccessCode(result), "Expected error unsubscribing"); + strictEqual(success, false, "Unexpected successful unsubscribe"); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_subscribe_origin_principal() { + let scope = "https://example.net/origin-principal"; + let principal = + Services.scriptSecurityManager.createContentPrincipalFromOrigin(scope); + + do_test_pending(); + PushServiceComponent.subscribe(scope, principal, (result, subscription) => { + ok( + Components.isSuccessCode(result), + "Expected error creating subscription with origin principal" + ); + ok( + !subscription.isSystemSubscription, + "Unexpected system subscription for origin principal" + ); + equal(subscription.quota, 16, "Wrong quota for origin subscription"); + + do_test_finished(); + run_next_test(); + }); +}); + +add_test(function test_subscribe_null_principal() { + do_test_pending(); + PushServiceComponent.subscribe( + "chrome://push/null-principal", + Services.scriptSecurityManager.createNullPrincipal({}), + (result, subscription) => { + ok( + !Components.isSuccessCode(result), + "Expected error creating subscription with null principal" + ); + strictEqual( + subscription, + null, + "Unexpected subscription with null principal" + ); + + do_test_finished(); + run_next_test(); + } + ); +}); + +add_test(function test_subscribe_missing_principal() { + do_test_pending(); + PushServiceComponent.subscribe( + "chrome://push/missing-principal", + null, + (result, subscription) => { + ok( + !Components.isSuccessCode(result), + "Expected error creating subscription without principal" + ); + strictEqual( + subscription, + null, + "Unexpected subscription without principal" + ); + + do_test_finished(); + run_next_test(); + } + ); +}); + +if (isParent) { + add_test(function tearDown() { + tearDownServiceInParent(db).then(run_next_test, run_next_test); + }); +} |