browser_principalSerialization_version1.js (5783B)
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(function test_nullPrincipal() { 14 /* 15 As Null principals are designed to be non deterministic we just need to ensure that 16 a previous serialized version matches what it was generated as. 17 18 This test should be resilient to changes in versioning, however it should also be duplicated for a new serialization change. 19 */ 20 // Principal created with: E10SUtils.serializePrincipal(Services.scriptSecurityManager.createNullPrincipal({ })); 21 let p = E10SUtils.deserializePrincipal( 22 "vQZuXxRvRHKDMXv9BbHtkAAAAAAAAAAAwAAAAAAAAEYAAAA4bW96LW51bGxwcmluY2lwYWw6ezU2Y2FjNTQwLTg2NGQtNDdlNy04ZTI1LTE2MTRlYWI1MTU1ZX0AAAAA" 23 ); 24 is( 25 "moz-nullprincipal:{56cac540-864d-47e7-8e25-1614eab5155e}", 26 p.URI.spec, 27 "Deserialized principal doesn't have the correct URI" 28 ); 29 30 // Principal created with: E10SUtils.serializePrincipal(Services.scriptSecurityManager.createNullPrincipal({ userContextId: 2 })); 31 let p2 = E10SUtils.deserializePrincipal( 32 "vQZuXxRvRHKDMXv9BbHtkAAAAAAAAAAAwAAAAAAAAEYAAAA4bW96LW51bGxwcmluY2lwYWw6ezA1ZjllN2JhLWIwODMtNDJhMi1iNDdkLTZiODRmNmYwYTM3OX0AAAAQXnVzZXJDb250ZXh0SWQ9Mg==" 33 ); 34 is( 35 "moz-nullprincipal:{05f9e7ba-b083-42a2-b47d-6b84f6f0a379}", 36 p2.URI.spec, 37 "Deserialized principal doesn't have the correct URI" 38 ); 39 is(p2.originAttributes.userContextId, 2, "Expected a userContextId of 2"); 40 }); 41 42 add_task(async function test_realHistoryCheck() { 43 /* 44 This test should be resilient to changes in principal serialization, if these are failing then it's likely the code will break session storage. 45 To recreate this for another version, copy the function into the browser console, browse some pages and printHistory. 46 47 Generated with: 48 function printHistory() { 49 let tests = []; 50 let entries = SessionStore.getSessionHistory(gBrowser.selectedTab).entries.map((entry) => { return entry.triggeringPrincipal_base64 }); 51 entries.push(E10SUtils.serializePrincipal(gBrowser.selectedTab.linkedBrowser._contentPrincipal)); 52 for (let entry of entries) { 53 console.log(entry); 54 let testData = {}; 55 testData.input = entry; 56 let principal = E10SUtils.deserializePrincipal(testData.input); 57 testData.output = {}; 58 if (principal.URI === null) { 59 testData.output.URI = false; 60 } else { 61 testData.output.URISpec = principal.URI.spec; 62 } 63 testData.output.originAttributes = principal.originAttributes; 64 65 tests.push(testData); 66 } 67 return tests; 68 } 69 printHistory(); // Copy this into: serializedPrincipalsFromFirefox 70 */ 71 72 let serializedPrincipalsFromFirefox = [ 73 { 74 input: "SmIS26zLEdO3ZQBgsLbOywAAAAAAAAAAwAAAAAAAAEY=", 75 output: { 76 URI: false, 77 originAttributes: { 78 firstPartyDomain: "", 79 inIsolatedMozBrowser: false, 80 privateBrowsingId: 0, 81 userContextId: 0, 82 geckoViewSessionContextId: "", 83 partitionKey: "", 84 }, 85 }, 86 }, 87 { 88 input: 89 "ZT4OTT7kRfqycpfCC8AeuAAAAAAAAAAAwAAAAAAAAEYB3pRy0IA0EdOTmQAQS6D9QJIHOlRteE8wkTq4cYEyCMYAAAAC/////wAAAbsBAAAAe2h0dHBzOi8vZGV2ZWxvcGVyLm1vemlsbGEub3JnL2VuLVVTLz91dG1fc291cmNlPXd3dy5tb3ppbGxhLm9yZyZ1dG1fbWVkaXVtPXJlZmVycmFsJnV0bV9jYW1wYWlnbj1uYXYmdXRtX2NvbnRlbnQ9ZGV2ZWxvcGVycwAAAAAAAAAFAAAACAAAABUAAAAA/////wAAAAD/////AAAACAAAABUAAAAdAAAAXgAAAB0AAAAHAAAAHQAAAAcAAAAkAAAAAAAAAAD/////AAAAAP////8AAAAlAAAAVgAAAAD/////AQAAAAAAAAAAAAAAAA==", 90 output: { 91 URISpec: 92 "https://developer.mozilla.org/en-US/?utm_source=www.mozilla.org&utm_medium=referral&utm_campaign=nav&utm_content=developers", 93 originAttributes: { 94 firstPartyDomain: "", 95 inIsolatedMozBrowser: false, 96 privateBrowsingId: 0, 97 userContextId: 0, 98 geckoViewSessionContextId: "", 99 partitionKey: "", 100 }, 101 }, 102 }, 103 { 104 input: "SmIS26zLEdO3ZQBgsLbOywAAAAAAAAAAwAAAAAAAAEY=", 105 output: { 106 URI: false, 107 originAttributes: { 108 firstPartyDomain: "", 109 inIsolatedMozBrowser: false, 110 privateBrowsingId: 0, 111 userContextId: 0, 112 geckoViewSessionContextId: "", 113 partitionKey: "", 114 }, 115 }, 116 }, 117 { 118 input: 119 "vQZuXxRvRHKDMXv9BbHtkAAAAAAAAAAAwAAAAAAAAEYAAAA4bW96LW51bGxwcmluY2lwYWw6ezA0NWNhMThkLTQzNmMtNDc0NC1iYmI2LWIxYTE1MzY2ZGY3OX0AAAAA", 120 output: { 121 URISpec: "moz-nullprincipal:{045ca18d-436c-4744-bbb6-b1a15366df79}", 122 originAttributes: { 123 firstPartyDomain: "", 124 inIsolatedMozBrowser: false, 125 privateBrowsingId: 0, 126 userContextId: 0, 127 geckoViewSessionContextId: "", 128 partitionKey: "", 129 }, 130 }, 131 }, 132 ]; 133 134 for (let test of serializedPrincipalsFromFirefox) { 135 let principal = E10SUtils.deserializePrincipal(test.input); 136 137 for (let key in principal.originAttributes) { 138 is( 139 principal.originAttributes[key], 140 test.output.originAttributes[key], 141 `Ensure value of ${key} is ${test.output.originAttributes[key]}` 142 ); 143 } 144 145 if ("URI" in test.output && test.output.URI === false) { 146 is( 147 principal.isContentPrincipal, 148 false, 149 "Should have not have a URI for system" 150 ); 151 } else { 152 is( 153 principal.spec, 154 test.output.URISpec, 155 `Should have spec ${test.output.URISpec}` 156 ); 157 } 158 } 159 });