browser_principalSerialization_json.js (5391B)
1 "use strict"; 2 3 /* Any copyright is dedicated to the Public Domain. 4 * http://creativecommons.org/publicdomain/zero/1.0/ */ 5 6 /* 7 This test file exists to ensure whenever changes to principal serialization happens, 8 we guarantee that the data can be restored and generated into a new principal. 9 10 The tests are written to be brittle so we encode all versions of the changes into the tests. 11 */ 12 13 add_task(async function test_nullPrincipal() { 14 const nullId = "0"; 15 // fields 16 const uri = 0; 17 const suffix = 1; 18 19 const nullReplaceRegex = 20 /moz-nullprincipal:{[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}}/; 21 const NULL_REPLACE = "NULL_PRINCIPAL_URL"; 22 23 /* 24 This test should NOT be resilient to changes in versioning, 25 however it exists purely to verify the code doesn't unintentionally change without updating versioning and migration code. 26 */ 27 let tests = [ 28 { 29 input: { OA: {} }, 30 expected: `{"${nullId}":{"${uri}":"${NULL_REPLACE}"}}`, 31 }, 32 { 33 input: { OA: {} }, 34 expected: `{"${nullId}":{"${uri}":"${NULL_REPLACE}"}}`, 35 }, 36 { 37 input: { OA: { userContextId: 0 } }, 38 expected: `{"${nullId}":{"${uri}":"${NULL_REPLACE}"}}`, 39 }, 40 { 41 input: { OA: { userContextId: 2 } }, 42 expected: `{"${nullId}":{"${uri}":"${NULL_REPLACE}","${suffix}":"^userContextId=2"}}`, 43 }, 44 { 45 input: { OA: { privateBrowsingId: 1 } }, 46 expected: `{"${nullId}":{"${uri}":"${NULL_REPLACE}","${suffix}":"^privateBrowsingId=1"}}`, 47 }, 48 { 49 input: { OA: { privateBrowsingId: 0 } }, 50 expected: `{"${nullId}":{"${uri}":"${NULL_REPLACE}"}}`, 51 }, 52 ]; 53 54 for (let test of tests) { 55 let p = Services.scriptSecurityManager.createNullPrincipal(test.input.OA); 56 let sp = E10SUtils.serializePrincipal(p); 57 // Not sure why cppjson is adding a \n here 58 let spr = sp.replace(nullReplaceRegex, NULL_REPLACE); 59 is( 60 test.expected, 61 spr, 62 "Expected serialized object for " + JSON.stringify(test.input) 63 ); 64 let dp = E10SUtils.deserializePrincipal(sp); 65 66 // Check all the origin attributes 67 for (let key in test.input.OA) { 68 is( 69 dp.originAttributes[key], 70 test.input.OA[key], 71 "Ensure value of " + key + " is " + test.input.OA[key] 72 ); 73 } 74 } 75 }); 76 77 add_task(async function test_contentPrincipal() { 78 const contentId = "1"; 79 // fields 80 const content = 0; 81 // const domain = 1; 82 const suffix = 2; 83 // const csp = 3; 84 85 /* 86 This test should NOT be resilient to changes in versioning, 87 however it exists purely to verify the code doesn't unintentionally change without updating versioning and migration code. 88 */ 89 let tests = [ 90 { 91 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 92 input: { uri: "http://example.com/", OA: {} }, 93 expected: `{"${contentId}":{"${content}":"http://example.com/"}}`, 94 }, 95 { 96 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 97 input: { uri: "http://mozilla1.com/", OA: {} }, 98 expected: `{"${contentId}":{"${content}":"http://mozilla1.com/"}}`, 99 }, 100 { 101 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 102 input: { uri: "http://mozilla2.com/", OA: { userContextId: 0 } }, 103 expected: `{"${contentId}":{"${content}":"http://mozilla2.com/"}}`, 104 }, 105 { 106 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 107 input: { uri: "http://mozilla3.com/", OA: { userContextId: 2 } }, 108 expected: `{"${contentId}":{"${content}":"http://mozilla3.com/","${suffix}":"^userContextId=2"}}`, 109 }, 110 { 111 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 112 input: { uri: "http://mozilla4.com/", OA: { privateBrowsingId: 1 } }, 113 expected: `{"${contentId}":{"${content}":"http://mozilla4.com/","${suffix}":"^privateBrowsingId=1"}}`, 114 }, 115 { 116 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 117 input: { uri: "http://mozilla5.com/", OA: { privateBrowsingId: 0 } }, 118 expected: `{"${contentId}":{"${content}":"http://mozilla5.com/"}}`, 119 }, 120 ]; 121 122 for (let test of tests) { 123 let uri = Services.io.newURI(test.input.uri); 124 let p = Services.scriptSecurityManager.createContentPrincipal( 125 uri, 126 test.input.OA 127 ); 128 let sp = E10SUtils.serializePrincipal(p); 129 is(test.expected, sp, "Expected serialized object for " + test.input.uri); 130 let dp = E10SUtils.deserializePrincipal(sp); 131 is(dp.URI.spec, test.input.uri, "Ensure spec is the same"); 132 133 // Check all the origin attributes 134 for (let key in test.input.OA) { 135 is( 136 dp.originAttributes[key], 137 test.input.OA[key], 138 "Ensure value of " + key + " is " + test.input.OA[key] 139 ); 140 } 141 } 142 }); 143 144 add_task(async function test_systemPrincipal() { 145 const systemId = "3"; 146 /* 147 This test should NOT be resilient to changes in versioning, 148 however it exists purely to verify the code doesn't unintentionally change without updating versioning and migration code. 149 */ 150 const expected = `{"${systemId}":{}}`; 151 152 let p = Services.scriptSecurityManager.getSystemPrincipal(); 153 let sp = E10SUtils.serializePrincipal(p); 154 is(expected, sp, "Expected serialized object for system principal"); 155 let dp = E10SUtils.deserializePrincipal(sp); 156 is( 157 dp, 158 Services.scriptSecurityManager.getSystemPrincipal(), 159 "Deserialized the system principal" 160 ); 161 });