ByteSequenceParser implements Parser<ParseTree> { @Override public ParseTree parse(final String droidExpression) throws ParseException { final StringParseReader reader = new StringParseReader(droidExpression); final List<ParseTree> byteSequenceNodes = new ArrayList<>(); int currentChar; while ((currentChar = reader.read()) >= 0) { switch (currentChar) { case ASTERISK: { byteSequenceNodes.add(REPEAT_ANY); break; } case SPACE: case NEWLINE: case CARRIAGE_RETURN: case TAB: { break; } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': { byteSequenceNodes.add(ByteNode.valueOf(reader.readHexByte(currentChar))); break; } case QUESTION_MARK: { byteSequenceNodes.add(parseAnyNode(reader)); break; } case OPEN_SQUARE_BRACKET: { byteSequenceNodes.add(parseByteSet(reader)); break; } case OPEN_CURLY_BRACKET: { byteSequenceNodes.add(parseGaps(reader)); break; } case OPEN_ROUND_BRACKET: { byteSequenceNodes.add(parseAlternatives(reader)); break; } case SINGLE_QUOTE: { byteSequenceNodes.add(parseString(reader)); break; } default: throw createParseException("Invalid character in droid expression", reader); } } return new ChildrenNode(ParseTreeType.SEQUENCE, byteSequenceNodes); }  @Override ParseTree parse(final String droidExpression); static final ByteSequenceParser PARSER; }
@Test public void testParseEmptyExpression() throws Exception { ParseTree empty = PARSER.parse(""); assertEquals(ParseTreeType.SEQUENCE, empty.getParseTreeType()); assertEquals(0, empty.getNumChildren()); empty = PARSER.parse(" "); assertEquals(ParseTreeType.SEQUENCE, empty.getParseTreeType()); assertEquals(0, empty.getNumChildren()); empty = PARSER.parse(" \n \t \r "); assertEquals(ParseTreeType.SEQUENCE, empty.getParseTreeType()); assertEquals(0, empty.getNumChildren()); }
@Test(expected = ParseException.class) public void testMissingHexDigitException() throws Exception { PARSER.parse("0"); }
@Test(expected = ParseException.class) public void testMissingHexDigitException2() throws Exception { PARSER.parse("0a 34d"); }
@Test(expected = ParseException.class) public void testUnclosedStringException() throws Exception { PARSER.parse(" 'a string which isn't closed"); }
@Test(expected = ParseException.class) public void testUnopenedStringException() throws Exception { PARSER.parse(" a string which isn't opened'"); }
@Test(expected = ParseException.class) public void testAnotherUnclosedStringException() throws Exception { PARSER.parse(" 'first string is closed' 'a string which isn't closed"); }
@Test(expected = ParseException.class) public void testStringWithMissingHexDigitException() throws Exception { PARSER.parse("'there is a string followed by an incomplete hex digit' ff 0"); }
@Test(expected = ParseException.class) public void testEmptyRoundBracketsException() throws Exception { PARSER.parse("( )"); }
@Test(expected = ParseException.class) public void testEmptyAlternativesException() throws Exception { PARSER.parse("(|)"); }
@Test(expected = ParseException.class) public void testEmptyFirstAlternativeException() throws Exception { PARSER.parse("(|01)"); }
@Test(expected = ParseException.class) public void testEmptySecondAlternativeException() throws Exception { PARSER.parse("(ff|)"); }
@Test(expected = ParseException.class) public void testEmptyMiddleAlternativeException() throws Exception { PARSER.parse("(ff||00)"); }
@Test(expected = ParseException.class) public void testUnclosedAlternativeException() throws Exception { PARSER.parse("(ff|fe|00 "); }
@Test(expected = ParseException.class) public void testInvalidCharInAlternatives() throws Exception { PARSER.parse("(01 02 03| 04 05 'string' [&01] | ^ 'not good char before')"); }
@Test(expected = ParseException.class) public void testMissingSecondQuestionMarkException() throws Exception { PARSER.parse("01 02 ? 04"); }
@Test(expected = ParseException.class) public void testUnclosedByteRange() throws Exception { PARSER.parse("[01"); }
@Test(expected = ParseException.class) public void testUnclosedByteRange2() throws Exception { PARSER.parse("[01 04 05 06"); }
@Test(expected = ParseException.class) public void testUnopenedByteRange() throws Exception { PARSER.parse("01]"); }
@Test(expected = ParseException.class) public void testUnclosedInvertedByteRange() throws Exception { PARSER.parse("[!01"); }
@Test(expected = ParseException.class) public void testUnclosedInvertedByteRange2() throws Exception { PARSER.parse("[!01 04 05 06"); }
@Test(expected = ParseException.class) public void testUnopenedInvertedByteRange() throws Exception { PARSER.parse("!01]"); }
@Test(expected = ParseException.class) public void testMissingFirstRangeValueException() throws Exception { PARSER.parse("[:02]"); }
@Test(expected = ParseException.class) public void testMissingSecondRangeValueException() throws Exception { PARSER.parse("[01:]"); }
@Test(expected = ParseException.class) public void testMissingFirstInvertedRangeValueException() throws Exception { PARSER.parse("[!:02]"); }
@Test(expected = ParseException.class) public void testMissingSecondInvertedRangeValueException() throws Exception { PARSER.parse("[!01:]"); }
@Test(expected = ParseException.class) public void testWhiteSpaceNotAllowedInRangesException() throws Exception { PARSER.parse("[01 : 02]"); }
@Test(expected = ParseException.class) public void testUnclosedCurlyBrackets() throws Exception { PARSER.parse("{5"); }
@Test(expected = ParseException.class) public void testUnopenedCurlyBrackets() throws Exception { PARSER.parse("5}"); }
@Test(expected = ParseException.class) public void testUnclosedCurlyBrackets2() throws Exception { PARSER.parse("01 {5 03"); }
@Test(expected = ParseException.class) public void testMissingFirstValue() throws Exception { PARSER.parse("{-56}"); }
@Test(expected = ParseException.class) public void testMissingSecondValue() throws Exception { PARSER.parse("{56-}"); }
@Test(expected = ParseException.class) public void testMissingAllValues() throws Exception { PARSER.parse("{-}"); }
@Test(expected = ParseException.class) public void testMissingCurlyClosingBracket() throws Exception { PARSER.parse("{56-34"); }
@Test(expected = ParseException.class) public void testMissingCurlyOpeningBracket() throws Exception { PARSER.parse("56-34"); }
@Test(expected=ParseException.class) public void testMissingCloseAfterMany() throws Exception { PARSER.parse("{10-*"); }
@Test(expected=ParseException.class) public void testWhitespaceNotAllowedInMinToMany() throws Exception { PARSER.parse("{10 - *}"); }
@Test(expected = ParseException.class) public void testIncorrectCharInSet() throws ParseException { PARSER.parse("[0102 03-04 % FF]"); }
@Test(expected = ParseException.class) public void testEmptySet() throws ParseException { PARSER.parse("[ ]"); }
@Test(expected = ParseException.class) public void testNoFirstRangeValue() throws ParseException { PARSER.parse("[:04]"); }
@Test(expected = ParseException.class) public void testFirstStringRangeValueTooBig() throws ParseException { PARSER.parse("['aa':'z'"); }
@Test(expected = ParseException.class) public void testSecondStringRangeValueTooBig() throws ParseException { PARSER.parse("['a':'zz'"); }
@Test(expected = ParseException.class) public void testFirstCharRangeValueTooBig() throws ParseException { PARSER.parse("['" + (char) 256 + "':'z'"); }
@Test(expected = ParseException.class) public void testSecondCharRangeValueTooBig() throws ParseException { PARSER.parse("['a':" + (char) 256 + ']'); }
@Test(expected = ParseException.class) public void testFirstRangeValueNotByteOrString() throws ParseException { PARSER.parse("[&01:FF]"); }
@Test(expected = ParseException.class) public void testSecondRangeValueNotByteOrString() throws ParseException { PARSER.parse("[01:&01]"); }