text: /*
    Copyright 2019 Google LLC
  
    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
  
        https://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.
  */
  
  var assert = require("assert"),
    decache = require("decache"),
    path = require("path"),
    fs = require("fs"),
    testPN = "threatProtectionXMLConditionCheck.js",
    debug = require("debug")("bundlelinter:" + testPN),
    Bundle = require("../../lib/package/Bundle.js"),
    util = require("util"),
    bl = require("../../lib/package/bundleLinter.js");
  
  var Policy = require("../../lib/package/Policy.js"),
    Step = require("../../lib/package/Step.js"),
    Flow = require("../../lib/package/Flow.js"),
    plugin = require("../../lib/package/plugins/" + testPN),
    Dom = require("xmldom").DOMParser,
    test = function(exp, stepExp, flowExp, assertion) {
      it(
        "testing " + testPN + '" expected to see ' + assertion + ".",
        function() {
          var pDoc = new Dom().parseFromString(exp),
            sDoc,
            fDoc,
            p = new Policy(pDoc.documentElement, this),
            s,
            f;
  
          p.addMessage = function(msg) {
            debug(msg);
          };
          p.getElement = function() {
            return pDoc.documentElement;
          };
          p.getSteps = function() {
            if (s) return [s];
            return [];
          };
  
          if (flowExp) {
            fDoc = new Dom().parseFromString(flowExp);
            f = new Flow(fDoc.documentElement, null);
          }
  
          if (stepExp) {
            sDoc = new Dom().parseFromString(stepExp);
            s = new Step(sDoc.documentElement, f);
          }
  
          plugin.onPolicy(p, function(err,result) {
            assert.equal(err, undefined, err ? " err " : " no err");
            assert.equal(
              result,
              assertion,
              result
                ? "warning/error was returned"
                : "warning/error was not returned"
            );
          });
        }
      );
    };
  
  //now generate a full report and check the format of the report
  
  describe("testing " + testPN, function() {
  
  
    test(
      `<XMLThreatProtection async="false" continueOnError="false" enabled="true" name="XML-Threat-Protection-1">
       <DisplayName>XML Threat Protection 1</DisplayName>
       <NameLimits>
          <Element>10</Element>
          <Attribute>10</Attribute>
          <NamespacePrefix>10</NamespacePrefix>
          <ProcessingInstructionTarget>10</ProcessingInstructionTarget>
       </NameLimits>
       <Source>request</Source>
       <StructureLimits>
          <NodeDepth>5</NodeDepth>
          <AttributeCountPerElement>2</AttributeCountPerElement>
          <NamespaceCountPerElement>3</NamespaceCountPerElement>
          <ChildCount includeComment="true" includeElement="true" includeProcessingInstruction="true" includeText="true">3</ChildCount>
       </StructureLimits>
       <ValueLimits>
          <Text>15</Text>
          <Attribute>10</Attribute>
          <NamespaceURI>10</NamespaceURI>
          <Comment>10</Comment>
          <ProcessingInstructionData>10</ProcessingInstructionData>
       </ValueLimits>
    </XMLThreatProtection>`,
      null,
      null,
      false //not attached
    );
  
    test(
      `<XMLThreatProtection async="false" continueOnError="false" enabled="true" name="XML-Threat-Protection-1">
       <DisplayName>XML Threat Protection 1</DisplayName>
       <NameLimits>
          <Element>10</Element>
          <Attribute>10</Attribute>
          <NamespacePrefix>10</NamespacePrefix>
          <ProcessingInstructionTarget>10</ProcessingInstructionTarget>
       </NameLimits>
       <Source>request</Source>
       <StructureLimits>
          <NodeDepth>5</NodeDepth>
          <AttributeCountPerElement>2</AttributeCountPerElement>
          <NamespaceCountPerElement>3</NamespaceCountPerElement>
          <ChildCount includeComment="true" includeElement="true" includeProcessingInstruction="true" includeText="true">3</ChildCount>
       </StructureLimits>
       <ValueLimits>
          <Text>15</Text>
          <Attribute>10</Attribute>
          <NamespaceURI>10</NamespaceURI>
          <Comment>10</Comment>
          <ProcessingInstructionData>10</ProcessingInstructionData>
       </ValueLimits>
    </XMLThreatProtection>`,
      `<Step>
        <Condition>message.content != ""</Condition>
        <Name>XML-Threat-Protection-1</Name>
    </Step>`,
      null,
      false //attached good condition
    );
  
    test(
      `<XMLThreatProtection async="false" continueOnError="false" enabled="true" name="XML-Threat-Protection-1">
       <DisplayName>XML Threat Protection 1</DisplayName>
       <NameLimits>
          <Element>10</Element>
          <Attribute>10</Attribute>
          <NamespacePrefix>10</NamespacePrefix>
          <ProcessingInstructionTarget>10</ProcessingInstructionTarget>
       </NameLimits>
       <Source>request</Source>
       <StructureLimits>
          <NodeDepth>5</NodeDepth>
          <AttributeCountPerElement>2</AttributeCountPerElement>
          <NamespaceCountPerElement>3</NamespaceCountPerElement>
          <ChildCount includeComment="true" includeElement="true" includeProcessingInstruction="true" includeText="true">3</ChildCount>
       </StructureLimits>
       <ValueLimits>
          <Text>15</Text>
          <Attribute>10</Attribute>
          <NamespaceURI>10</NamespaceURI>
          <Comment>10</Comment>
          <ProcessingInstructionData>10</ProcessingInstructionData>
       </ValueLimits>
    </XMLThreatProtection>`,
      `<Step>
        <Condition>foo != ""</Condition>
        <Name>XML-Threat-Protection-1</Name>
    </Step>`,
      null,
      true //attached insufficient condition
    );
  
    test(
      `<XMLThreatProtection async="false" continueOnError="false" enabled="true" name="XML-Threat-Protection-1">
       <DisplayName>XML Threat Protection 1</DisplayName>
       <NameLimits>
          <Element>10</Element>
          <Attribute>10</Attribute>
          <NamespacePrefix>10</NamespacePrefix>
          <ProcessingInstructionTarget>10</ProcessingInstructionTarget>
       </NameLimits>
       <Source>request</Source>
       <StructureLimits>
          <NodeDepth>5</NodeDepth>
          <AttributeCountPerElement>2</AttributeCountPerElement>
          <NamespaceCountPerElement>3</NamespaceCountPerElement>
          <ChildCount includeComment="true" includeElement="true" includeProcessingInstruction="true" includeText="true">3</ChildCount>
       </StructureLimits>
       <ValueLimits>
          <Text>15</Text>
          <Attribute>10</Attribute>
          <NamespaceURI>10</NamespaceURI>
          <Comment>10</Comment>
          <ProcessingInstructionData>10</ProcessingInstructionData>
       </ValueLimits>
    </XMLThreatProtection>`,
      `<Step>
        <Condition>foo != ""</Condition>
        <Name>XML-Threat-Protection-1</Name>
    </Step>`,
      `<Flow name="flow2">
            <Step>
                <Condition>foo != ""</Condition>
                <Name>XML-Threat-Protection-1</Name>
            </Step>
            <Condition/>
        </Flow>`,
      true //attached insufficient condition
    );
  
    test(
      `<XMLThreatProtection async="false" continueOnError="false" enabled="true" name="XML-Threat-Protection-1">
       <DisplayName>XML Threat Protection 1</DisplayName>
       <NameLimits>
          <Element>10</Element>
          <Attribute>10</Attribute>
          <NamespacePrefix>10</NamespacePrefix>
          <ProcessingInstructionTarget>10</ProcessingInstructionTarget>
       </NameLimits>
       <Source>request</Source>
       <StructureLimits>
          <NodeDepth>5</NodeDepth>
          <AttributeCountPerElement>2</AttributeCountPerElement>
          <NamespaceCountPerElement>3</NamespaceCountPerElement>
          <ChildCount includeComment="true" includeElement="true" includeProcessingInstruction="true" includeText="true">3</ChildCount>
       </StructureLimits>
       <ValueLimits>
          <Text>15</Text>
          <Attribute>10</Attribute>
          <NamespaceURI>10</NamespaceURI>
          <Comment>10</Comment>
          <ProcessingInstructionData>10</ProcessingInstructionData>
       </ValueLimits>
    </XMLThreatProtection>`,
      ` <Step>
            <Condition>foo != ""</Condition>
            <Name>XML-Threat-Protection-1</Name>
        </Step>`,
      ` <Flow name="flow2">
            <Step>
                <Condition>foo != ""</Condition>
                <Name>XML-Threat-Protection-1</Name>
            </Step>
            <Condition>message.content != ""</Condition>
        </Flow>`,
      false //attached sufficient condition
    );
  
    test(
      '<RegularExpressionProtection async="false" continueOnError="false" enabled="true" name="regExLookAround"><DisplayName>regExLookAround</DisplayName><Source>request</Source><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables><URIPath><Pattern>(?/(@?[w_?w:*]+([[^]]+])*)?)+</Pattern></URIPath></RegularExpressionProtection>',
      null,
      null,
      false //not extractVar
    );
  
  
    var Bundle = require("../../lib/package/Bundle.js"),
      util = require("util"),
      bl = require("../../lib/package/bundleLinter.js");
  
    debug("test configuration: " + JSON.stringify(configuration));
  
    var bundle = new Bundle(configuration);
    bl.executePlugin(testPN, bundle);
  
    //need a case where we are using ref for the key
    //also prefix
  
    describe("Print " + testPN + " plugin results", function() {
      var report = bundle.getReport(),
        jsimpl = bl.getFormatter("json.js");
  
      if (!jsimpl) {
        assert("implementation not defined: " + jsimpl);
      } else {
        it("should create a report object with valid schema", function() {
          var schema = require("./../fixtures/reportSchema.js"),
            Validator = require("jsonschema").Validator,
            v = new Validator(),
            validationResult,
            jsonReport;
  
          var jsonReport = JSON.parse(jsimpl(bundle.getReport()));
          validationResult = v.validate(jsonReport, schema);
          assert.equal(
            validationResult.errors.length,
            0,
            validationResult.errors
          );
        });
      }
    });
  
    var stylimpl = bl.getFormatter("unix.js");
    var stylReport = stylimpl(bundle.getReport());
    debug("unix formatted report: \n" + stylReport);
  });
  