Home | History | Annotate | Download | only in parser
      1 // Copyright 2016 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "core/fpdfapi/parser/cpdf_simple_parser.h"
      6 
      7 #include <string>
      8 
      9 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "testing/test_support.h"
     12 
     13 TEST(SimpleParserTest, GetWord) {
     14   static const pdfium::StrFuncTestData test_data[] = {
     15       // Empty src string.
     16       STR_IN_OUT_CASE("", ""),
     17       // Content with whitespaces only.
     18       STR_IN_OUT_CASE(" \t \0 \n", ""),
     19       // Content with comments only.
     20       STR_IN_OUT_CASE("%this is a test case\r\n%2nd line", ""),
     21       // Mixed whitespaces and comments.
     22       STR_IN_OUT_CASE(" \t \0%try()%haha\n %another line \aa", ""),
     23       // Name.
     24       STR_IN_OUT_CASE(" /Tester ", "/Tester"),
     25       // String.
     26       STR_IN_OUT_CASE("\t(nice day)!\n ", "(nice day)"),
     27       // String with nested braces.
     28       STR_IN_OUT_CASE("\t(It is a (long) day)!\n ", "(It is a (long) day)"),
     29       // String with escaped chars.
     30       STR_IN_OUT_CASE("\t(It is a \\(long\\) day!)hi\n ",
     31                       "(It is a \\(long\\) day!)"),
     32       // Hex string.
     33       STR_IN_OUT_CASE(" \n<4545acdfedertt>abc ", "<4545acdfedertt>"),
     34       STR_IN_OUT_CASE(" \n<4545a<ed>ertt>abc ", "<4545a<ed>"),
     35       // Dictionary.
     36       STR_IN_OUT_CASE("<</oc 234 /color 2 3 R>>", "<<"),
     37       STR_IN_OUT_CASE("\t\t<< /abc>>", "<<"),
     38       // Handling ending delimiters.
     39       STR_IN_OUT_CASE("> little bear", ">"),
     40       STR_IN_OUT_CASE(") another bear", ")"), STR_IN_OUT_CASE(">> end ", ">>"),
     41       // No ending delimiters.
     42       STR_IN_OUT_CASE("(sdfgfgbcv", "(sdfgfgbcv"),
     43       // Regular cases.
     44       STR_IN_OUT_CASE("apple pear", "apple"),
     45       STR_IN_OUT_CASE(" pi=3.1415 ", "pi=3.1415"),
     46       STR_IN_OUT_CASE(" p t x c ", "p"), STR_IN_OUT_CASE(" pt\0xc ", "pt"),
     47       STR_IN_OUT_CASE(" $^&&*\t\0sdff ", "$^&&*"),
     48       STR_IN_OUT_CASE("\n\r+3.5656 -11.0", "+3.5656"),
     49   };
     50   for (size_t i = 0; i < FX_ArraySize(test_data); ++i) {
     51     const pdfium::StrFuncTestData& data = test_data[i];
     52     CPDF_SimpleParser parser(data.input, data.input_size);
     53     ByteStringView word = parser.GetWord();
     54     EXPECT_EQ(data.expected_size, word.GetLength()) << " for case " << i;
     55     if (data.expected_size != word.GetLength())
     56       continue;
     57     EXPECT_EQ(
     58         0, memcmp(data.expected, word.unterminated_c_str(), data.expected_size))
     59         << " for case " << i;
     60   }
     61 }
     62 
     63 TEST(SimpleParserTest, FindTagParamFromStart) {
     64   static const struct FindTagTestStruct {
     65     const unsigned char* input;
     66     unsigned int input_size;
     67     const char* token;
     68     int num_params;
     69     bool result;
     70     unsigned int result_pos;
     71   } test_data[] = {
     72       // Empty strings.
     73       STR_IN_TEST_CASE("", "Tj", 1, false, 0),
     74       STR_IN_TEST_CASE("", "", 1, false, 0),
     75       // Empty token.
     76       STR_IN_TEST_CASE("  T j", "", 1, false, 5),
     77       // No parameter.
     78       STR_IN_TEST_CASE("Tj", "Tj", 1, false, 2),
     79       STR_IN_TEST_CASE("(Tj", "Tj", 1, false, 3),
     80       // Partial token match.
     81       STR_IN_TEST_CASE("\r12\t34  56 78Tj", "Tj", 1, false, 15),
     82       // Regular cases with various parameters.
     83       STR_IN_TEST_CASE("\r\0abd Tj", "Tj", 1, true, 0),
     84       STR_IN_TEST_CASE("12 4 Tj 3 46 Tj", "Tj", 1, true, 2),
     85       STR_IN_TEST_CASE("er^ 2 (34) (5667) Tj", "Tj", 2, true, 5),
     86       STR_IN_TEST_CASE("<344> (232)\t343.4\n12 45 Tj", "Tj", 3, true, 11),
     87       STR_IN_TEST_CASE("1 2 3 4 5 6 7 8 cm", "cm", 6, true, 3),
     88   };
     89   for (size_t i = 0; i < FX_ArraySize(test_data); ++i) {
     90     const FindTagTestStruct& data = test_data[i];
     91     CPDF_SimpleParser parser(data.input, data.input_size);
     92     EXPECT_EQ(data.result,
     93               parser.FindTagParamFromStart(data.token, data.num_params))
     94         << " for case " << i;
     95     EXPECT_EQ(data.result_pos, parser.GetCurPos()) << " for case " << i;
     96   }
     97 }
     98