@Test
 public void testAddition() {
   assertEquals(3, Parser.parse("1+2"), 0.0001);
   assertEquals(2, Parser.parse("0+2"), 0.0001);
   assertEquals(0, Parser.parse("0+0"), 0.0001);
   assertEquals(27.34, Parser.parse("16.88+10.46"), 0.00001);
 }
 @Test
 public void testPower() {
   assertEquals(1, Parser.parse("3^0"), 0.0001);
   assertEquals(3, Parser.parse("3^1"), 0.0001);
   assertEquals(9, Parser.parse("3^2"), 0.0001);
   assertEquals(27, Parser.parse("3^3"), 0.0001);
 }
 @Test
 public void testMultiplication() {
   assertEquals(2, Parser.parse("2*1"), 0.0001);
   assertEquals(0, Parser.parse("2*0"), 0.0001);
   assertEquals(3, Parser.parse("1*3"), 0.0001);
   assertEquals(176.5648, Parser.parse("16.88*10.46"), 0.0001);
 }
 @Test
 public void testSubstraction() {
   assertEquals(1, Parser.parse("2-1"), 0.0);
   assertEquals(2, Parser.parse("2-0"), 0.0);
   assertEquals(-2, Parser.parse("1-3"), 0.0);
   assertEquals(6.42, Parser.parse("16.88-10.46"), 0.0001);
 }
Esempio n. 5
0
  @Test
  public void testWarnings() throws ParseException, TokeniserException {
    ArrayList<Warning> warnings = new ArrayList<>();
    Policy p1 = Parser.parse("frame-src aaa", "https://origin", warnings);

    assertEquals("frame-src aaa", p1.show());
    assertEquals(1, warnings.size());
    assertEquals(
        "The frame-src directive is deprecated as of CSP version 1.1. Authors who wish to govern nested browsing contexts SHOULD use the child-src directive instead.",
        warnings.iterator().next().message);
  }
Esempio n. 6
0
  @Test
  public void testParseTag() {
    Element elemento = new Element("p");
    Attr atributo = new Attr("name", "parrafo");
    Text texto = new Text("texto");

    elemento.setAttributeNode(atributo);
    elemento.appendChild(texto);
    Element elementoAComparar = parser.parse("<p name:\"parrafo\">texto</p>");
    assertEquals(elemento, elementoAComparar);
  }
Esempio n. 7
0
  @Test
  public void testParseMulti() throws ParseException, TokeniserException {
    List<Policy> pl;
    ArrayList<Warning> warnings;

    pl =
        Parser.parseMulti(
            "script-src a; script-src b, , script-src c; script-src d", "https://origin.com");
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("script-src c", pl.get(1).show());

    pl = Parser.parseMulti("script-src a,", URI.parse("https://origin.com"));
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("", pl.get(1).show());

    warnings = new ArrayList<>();
    pl = Parser.parseMulti("script-src a,", URI.parse("https://origin.com"), warnings);
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("", pl.get(1).show());
    assertEquals(0, warnings.size());

    warnings = new ArrayList<>();
    pl = Parser.parseMulti("script-src a, sandbox", "https://origin.com", warnings);
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("sandbox", pl.get(1).show());
    assertEquals(0, warnings.size());

    warnings = new ArrayList<>();
    pl =
        ParserWithLocation.parseMulti(
            "   plugin-types  a/b  , script-src 'unsafe-redirect'", "https://origin.com", warnings);
    assertEquals(2, pl.size());
    assertEquals("plugin-types a/b", pl.get(0).show());
    assertEquals("script-src 'unsafe-redirect'", pl.get(1).show());
    assertEquals(1, warnings.size());
    assertEquals(
        "1:36: 'unsafe-redirect' has been removed from CSP as of version 2.0",
        warnings.get(0).show());

    warnings = new ArrayList<>();
    pl =
        ParserWithLocation.parseMulti(
            "script-src a, frame-src b", URI.parse("https://origin.com"), warnings);
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("frame-src b", pl.get(1).show());
    assertEquals(1, warnings.size());
    assertEquals(
        "1:15: The frame-src directive is deprecated as of CSP version 1.1. Authors who wish to govern nested browsing contexts SHOULD use the child-src directive instead.",
        warnings.get(0).show());

    try {
      pl.clear();
      pl = Parser.parseMulti("script-src a,b", "https://origin.com");
      fail();
    } catch (IllegalArgumentException e1) {
      assertEquals(0, pl.size());
      assertEquals("Unrecognised directive name: b", e1.getMessage());
    }

    try {
      ParserWithLocation.parse(
          "script-src a, script-src b", "https://origin.com", new ArrayList<>());
      fail();
    } catch (ParseException e1) {
      assertEquals(0, pl.size());
      assertEquals("1:13: expecting end of policy but found ,", e1.getMessage());
    }

    try {
      Parser.parse("script-src a, script-src b", "https://origin.com");
      fail();
    } catch (ParseException e1) {
      assertEquals(0, pl.size());
      assertEquals("expecting end of policy but found ,", e1.getMessage());
    }

    try {
      pl.clear();
      pl = ParserWithLocation.parseMulti("allow 'none', options", "https://origin.com");
      fail();
    } catch (ParseException e1) {
      assertEquals(0, pl.size());
      assertEquals(
          "1:1: The allow directive has been replaced with default-src and is not in the CSP specification.",
          e1.getMessage());
    }

    try {
      pl.clear();
      pl = ParserWithLocation.parseMulti("allow 'none', referrer", URI.parse("https://origin.com"));
      fail();
    } catch (ParseException e1) {
      assertEquals(0, pl.size());
      assertEquals(
          "1:1: The allow directive has been replaced with default-src and is not in the CSP specification.",
          e1.getMessage());
    }

    failsToParse("script-src *, ");
  }
 @Test
 public void testNegatives() {
   assertEquals(-3, Parser.parse("-3"), 0.0001);
   assertEquals(12, Parser.parse("-3*-4"), 0.0001);
   assertEquals(-13, Parser.parse("-5+-8"), 0.0001);
 }
 @Test
 public void testOperatorPriority() {
   assertEquals(22, Parser.parse("4+3*6"), 0.0001);
   assertEquals(9, Parser.parse("4+10/2"), 0.0001);
 }
Esempio n. 10
0
 @Test
 public void testBracketParsing() {
   assertEquals(13, Parser.parse("((2+1)*3)+4"), 0.0001);
   assertEquals(-1, Parser.parse("(2-5)/3"), 0.0001);
 }
Esempio n. 11
0
 @Test
 public void testSpaceStripping() {
   assertEquals(3, Parser.parse(" ( 3 ) "), 0.0001);
   assertEquals(3, Parser.parse(" ( 2 + 1 ) "), 0.0001);
   assertEquals(-3, Parser.parse(" ( 2 - 5 ) "), 0.0001);
 }
Esempio n. 12
0
 @Test(expected = IllegalArgumentException.class)
 public void testInvalidOpeningBracket() {
   Parser.parse("((2+1))");
 }
Esempio n. 13
0
 @Test
 public void testBracketStripping() {
   assertEquals(3, Parser.parse("(2+1)"), 0.0001);
   assertEquals(-3, Parser.parse("(2-5)"), 0.0001);
   assertEquals(4, Parser.parse("(2*5) - (3*2)"), 0.0001);
 }
Esempio n. 14
0
 @Test(expected = IllegalArgumentException.class)
 public void testDivisionByZeroAfterManyOperation() {
   String zeroFormula = "((((3*4)/2)/2)/3)*(17/3)/(3/17))-1";
   assertEquals(0, Parser.parse(zeroFormula), 0.000000001);
   Parser.parse("2/" + zeroFormula);
 }
Esempio n. 15
0
 @Test(expected = IllegalArgumentException.class)
 public void testDivisionByZero() {
   Parser.parse("2/0");
 }
Esempio n. 16
0
 @Test
 public void testDivision() {
   assertEquals(2, Parser.parse("2/1"), 0.0001);
   assertEquals(0.333333, Parser.parse("1/3"), 0.0001);
   assertEquals(1.6137, Parser.parse("16.88/10.46"), 0.0001);
 }