/* Copyright 2017 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { isEmptyObj } from "./test_utils.js";
import { Metadata } from "../../src/display/metadata.js";
import { MetadataParser } from "../../src/core/metadata_parser.js";
function createMetadata(data) {
  const metadataParser = new MetadataParser(data);
  return new Metadata(metadataParser.serializable);
}
describe("metadata", function () {
  it("should handle valid metadata", function () {
    const data =
      "" +
      "" +
      "" +
      'Foo bar baz' +
      "";
    const metadata = createMetadata(data);
    expect(metadata.has("dc:title")).toBeTruthy();
    expect(metadata.has("dc:qux")).toBeFalsy();
    expect(metadata.get("dc:title")).toEqual("Foo bar baz");
    expect(metadata.get("dc:qux")).toEqual(null);
    expect(metadata.getAll()).toEqual({ "dc:title": "Foo bar baz" });
  });
  it("should repair and handle invalid metadata", function () {
    const data =
      "" +
      "" +
      "" +
      "\\376\\377\\000P\\000D\\000F\\000&" +
      "";
    const metadata = createMetadata(data);
    expect(metadata.has("dc:title")).toBeTruthy();
    expect(metadata.has("dc:qux")).toBeFalsy();
    expect(metadata.get("dc:title")).toEqual("PDF&");
    expect(metadata.get("dc:qux")).toEqual(null);
    expect(metadata.getAll()).toEqual({ "dc:title": "PDF&" });
  });
  it("should repair and handle invalid metadata (bug 1424938)", function () {
    const data =
      "" +
      "" +
      "" +
      "" +
      "\\376\\377\\000P\\000D\\000F\\000C\\000r\\000e\\000a" +
      "\\000t\\000o\\000r\\000 \\000V\\000e\\000r\\000s\\000i\\000o\\000n" +
      "\\000 \\0000\\000.\\0009\\000.\\0006" +
      "" +
      "" +
      "\\376\\377\\000L\\000'\\000O\\000d" +
      "\\000i\\000s\\000s\\000e\\000e\\000 \\000t\\000h\\000\\351\\000m\\000a" +
      "\\000t\\000i\\000q\\000u\\000e\\000 \\000l\\000o\\000g\\000o\\000 " +
      "\\000O\\000d\\000i\\000s\\000s\\000\\351\\000\\351\\000 \\000-\\000 " +
      "\\000d\\000\\351\\000c\\000e\\000m\\000b\\000r\\000e\\000 \\0002\\0000" +
      "\\0000\\0008\\000.\\000p\\000u\\000b" +
      "\\376\\377\\000O\\000D\\000I\\000S" +
      "" +
      "";
    const metadata = createMetadata(data);
    expect(metadata.has("dc:title")).toBeTruthy();
    expect(metadata.has("dc:qux")).toBeFalsy();
    expect(metadata.get("dc:title")).toEqual(
      "L'Odissee thématique logo Odisséé - décembre 2008.pub"
    );
    expect(metadata.get("dc:qux")).toEqual(null);
    expect(metadata.getAll()).toEqual({
      "dc:creator": ["ODIS"],
      "dc:title": "L'Odissee thématique logo Odisséé - décembre 2008.pub",
      "xap:creatortool": "PDFCreator Version 0.9.6",
    });
  });
  it("should gracefully handle incomplete tags (issue 8884)", function () {
    const data =
      '' +
      '' +
      '' +
      "" +
      '' +
      "2010-03-25T11:20:09-04:00" +
      "2010-03-25T11:20:09-04:00" +
      "2010-03-25T11:20:09-04:00" +
      "" +
      '' +
      "application/pdf" +
      "" +
      '' +
      "1" +
      "A" +
      "" +
      "" +
      "" +
      '';
    const metadata = createMetadata(data);
    expect(isEmptyObj(metadata.getAll())).toEqual(true);
  });
  it('should gracefully handle "junk" before the actual metadata (issue 10395)', function () {
    const data =
      '' +
      '' +
      '' +
      '' +
      "PDFKit.NET 4.0.102.0" +
      "" +
      "1.7" +
      '' +
      "2018-12-27T13:50:36-08:00" +
      "2018-12-27T13:50:38-08:00" +
      "" +
      "2018-12-27T13:50:38-08:00" +
      '' +
      "" +
      "" +
      '' +
      "" +
      '' +
      "application/pdf" +
      '';
    const metadata = createMetadata(data);
    expect(metadata.has("dc:title")).toBeTruthy();
    expect(metadata.has("dc:qux")).toBeFalsy();
    expect(metadata.get("dc:title")).toEqual("");
    expect(metadata.get("dc:qux")).toEqual(null);
    expect(metadata.getAll()).toEqual({
      "dc:creator": [""],
      "dc:description": "",
      "dc:format": "application/pdf",
      "dc:subject": [],
      "dc:title": "",
      "pdf:keywords": "",
      "pdf:pdfversion": "1.7",
      "pdf:producer": "PDFKit.NET 4.0.102.0",
      "xap:createdate": "2018-12-27T13:50:36-08:00",
      "xap:creatortool": "",
      "xap:metadatadate": "2018-12-27T13:50:38-08:00",
      "xap:modifydate": "2018-12-27T13:50:38-08:00",
    });
  });
  it('should correctly handle metadata containing "&apos" (issue 10407)', function () {
    const data =
      "" +
      "" +
      "" +
      "" +
      ''Foo bar baz'' +
      "";
    const metadata = createMetadata(data);
    expect(metadata.has("dc:title")).toBeTruthy();
    expect(metadata.has("dc:qux")).toBeFalsy();
    expect(metadata.get("dc:title")).toEqual("'Foo bar baz'");
    expect(metadata.get("dc:qux")).toEqual(null);
    expect(metadata.getAll()).toEqual({ "dc:title": "'Foo bar baz'" });
  });
  it("should gracefully handle unbalanced end tags (issue 10410)", function () {
    const data =
      '' +
      '' +
      '' +
      "Soda PDF 5" +
      '' +
      "2018-10-02T08:14:49-05:00" +
      "Soda PDF 5" +
      "2018-10-02T08:14:49-05:00 " +
      "2018-10-02T08:14:49-05:00" +
      '' +
      "uuid:00000000-1c84-3cf9-89ba-bef0e729c831" +
      "" +
      '';
    const metadata = createMetadata(data);
    expect(isEmptyObj(metadata.getAll())).toEqual(true);
  });
  it("should not be vulnerable to the billion laughs attack", function () {
    const data =
      '' +
      "' +
      '  ' +
      '  ' +
      '  ' +
      '  ' +
      '  ' +
      '  ' +
      '  ' +
      '  ' +
      '  ' +
      "]>" +
      '' +
      '  ' +
      "    " +
      "      " +
      '        a&lol9;b' +
      "      " +
      "    " +
      "  " +
      "";
    const metadata = createMetadata(data);
    expect(metadata.has("dc:title")).toBeTruthy();
    expect(metadata.has("dc:qux")).toBeFalsy();
    expect(metadata.get("dc:title")).toEqual("a&lol9;b");
    expect(metadata.get("dc:qux")).toEqual(null);
    expect(metadata.getAll()).toEqual({ "dc:title": "a&lol9;b" });
  });
});