/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { E10SUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/E10SUtils.sys.mjs"
);

add_task(function () {
  let pre136 = E10SUtils.deserializeCSP(
    "CdntGuXUQAS/4CfOuSPZrAAAAAAAAAAAwAAAAAAAAEYB3pRy0IA0EdOTmQAQS6D9QJIHOlRteE8wkTq4cYEyCMYAAAAC/////wAAAbsBAAAAP2h0dHBzOi8vY2hhdC5tb3ppbGxhLm9yZy8jL3Jvb20vIWVhRmVUa2lTdHZyYnhqQmJ4Uzptb3ppbGxhLm9yZwAAAAAAAAAFAAAACAAAABAAAAAI/////wAAAAj/////AAAACAAAABAAAAAYAAAAJwAAABgAAAABAAAAGAAAAAEAAAAZAAAAAAAAABn/////AAAAAP////8AAAAY/////wAAABoAAAAlAQAAAAAAAAAAAGR7IjEiOnsiMCI6Imh0dHBzOi8vY2hhdC5tb3ppbGxhLm9yZy8jL3Jvb20vIWVhRmVUa2lTdHZyYnhqQmJ4Uzptb3ppbGxhLm9yZyIsIjIiOiJedXNlckNvbnRleHRJZD02In19AAAAAgAAABYAZgByAGEAbQBlAC0AYQBuAGMAZQBzAHQAbwByAHMAIAAnAHMAZQBsAGYAJwAAAAABcgBkAGUAZgBhAHUAbAB0AC0AcwByAGMAIAAnAG4AbwBuAGUAJwA7ACAAcwB0AHkAbABlAC0AcwByAGMAIAAnAHMAZQBsAGYAJwAgACcAdQBuAHMAYQBmAGUALQBpAG4AbABpAG4AZQAnADsAIABzAGMAcgBpAHAAdAAtAHMAcgBjACAAJwBzAGUAbABmACcAIAAnAHcAYQBzAG0ALQB1AG4AcwBhAGYAZQAtAGUAdgBhAGwAJwAgAGgAdAB0AHAAcwA6AC8ALwB3AHcAdwAuAHIAZQBjAGEAcAB0AGMAaABhAC4AbgBlAHQALwByAGUAYwBhAHAAdABjAGgAYQAvACAAaAB0AHQAcABzADoALwAvAHcAdwB3AC4AZwBzAHQAYQB0AGkAYwAuAGMAbwBtAC8AcgBlAGMAYQBwAHQAYwBoAGEALwA7ACAAaQBtAGcALQBzAHIAYwAgACoAIABiAGwAbwBiADoAIABkAGEAdABhADoAOwAgAGMAbwBuAG4AZQBjAHQALQBzAHIAYwAgACoAIABiAGwAbwBiADoAOwAgAGYAbwBuAHQALQBzAHIAYwAgACcAcwBlAGwAZgAnACAAZABhAHQAYQA6ADsAIABtAGUAZABpAGEALQBzAHIAYwAgACoAIABiAGwAbwBiADoAIABkAGEAdABhADoAOwAgAGMAaABpAGwAZAAtAHMAcgBjACAAKgAgAGIAbABvAGIAOgAgAGQAYQB0AGEAOgA7ACAAdwBvAHIAawBlAHIALQBzAHIAYwAgACcAcwBlAGwAZgAnACAAYgBsAG8AYgA6ADsAIABmAHIAYQBtAGUALQBzAHIAYwAgACoAIABiAGwAbwBiADoAIABkAGEAdABhADoAOwAgAGYAbwByAG0ALQBhAGMAdABpAG8AbgAgACcAcwBlAGwAZgAnADsAIABtAGEAbgBpAGYAZQBzAHQALQBzAHIAYwAgACcAcwBlAGwAZgAnAAE="
  );
  let json = JSON.parse(pre136.toJSON());
  Assert.equal(json["csp-policies"].length, 2);
  Assert.equal(json["csp-policies"][0]["report-only"], false);
  Assert.equal(json["csp-policies"][1]["worker-src"][1], "blob:");
  Assert.equal(json["csp-policies"][1]["default-src"][0], "'none'");

  const DATA_v136 =
    "CdntGuXUQAS/4CfOuSPZrAAAAAAAAAAAwAAAAAAAAEYB3pRy0IA0EdOTmQAQS6D9QJIHOlRteE8wkTq4cYEyCMYAAAAC/////wAAAbsBAAAAFGh0dHBzOi8vZXhhbXBsZS5vcmcvAAAAAAAAAAUAAAAIAAAACwAAAAD/////AAAAAP////8AAAAIAAAACwAAABMAAAABAAAAEwAAAAEAAAATAAAAAQAAABQAAAAAAAAAAP////8AAAAA/////wAAAAD/////AAAAAP////8BAAAAAAAAAAAAInsiMSI6eyIwIjoiaHR0cHM6Ly9leGFtcGxlLm9yZy8ifX0AAAACAAAAEgBkAGUAZgBhAHUAbAB0AC0AcwByAGMAIAAnAHMAZQBsAGYAJwABAAAAAA4AaQBtAGcALQBzAHIAYwAgACcAbgBvAG4AZQAnAAEA";
  let v136 = E10SUtils.deserializeCSP(DATA_v136);
  Assert.equal(
    v136.toJSON(),
    `{"csp-policies":[{"default-src":["'self'"],"report-only":false},{"img-src":["'none'"],"report-only":false}]}`
  );

  let v138 = E10SUtils.deserializeCSP(
    "CdntGuXUQAS/4CfOuSPZrAAAAAAAAAAAwAAAAAAAAEYB3pRy0IA0EdOTmQAQS6D9QJIHOlRteE8wkTq4cYEyCMYAAAAC/////wAAAbsBAAAAGGh0dHBzOi8vd3d3LnlvdXR1YmUuY29tLwAAAAAAAAAFAAAACAAAAA8AAAAA/////wAAAAD/////AAAACAAAAA8AAAAXAAAAAQAAABcAAAABAAAAFwAAAAEAAAAYAAAAAAAAAAD/////AAAAAP////8AAAAA/////wAAAAD/////AQAAAAAAAAAAACZ7IjEiOnsiMCI6Imh0dHBzOi8vd3d3LnlvdXR1YmUuY29tLyJ9fQAAAAA="
  );
  Assert.equal(v138.toJSON(), `{"csp-policies":[]}`);

  let v138_2 = E10SUtils.deserializeCSP(
    "[encoded data, 7140 characters]"
  );
  json = JSON.parse(v138_2.toJSON());
  Assert.equal(json["csp-policies"].length, 4);
  Assert.equal(
    Object.keys(json["csp-policies"][0]).join("|"),
    "report-only|report-uri"
  );
  Assert.equal(json["csp-policies"][2]["script-src"][0], "'unsafe-inline'");
  Assert.ok(
    json["csp-policies"][3]["script-src"].includes(
      "https://apis.google.com/js/client.js"
    )
  );

  Assert.equal(
    E10SUtils.serializeCSP(v136),
    DATA_v136,
    "DANGER: serialization format might have changed!"
  );
});
