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