1 // Copyright 2018 The PDFium Authors
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/fpdfdoc/cpdf_defaultappearance.h"
6
7 #include <iterator>
8
9 #include "core/fpdfapi/parser/cpdf_simple_parser.h"
10 #include "core/fxcrt/compiler_specific.h"
11 #include "core/fxcrt/span.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/test_support.h"
14
15 namespace {
16
17 struct FindTagTestStruct {
input_span__anon5d811a390111::FindTagTestStruct18 pdfium::span<const uint8_t> input_span() const {
19 // SAFETY: STR_IN_TEST_CASE macro extracts size of literal.
20 return UNSAFE_BUFFERS(pdfium::make_span(input, input_size));
21 }
22 const unsigned char* input;
23 unsigned int input_size;
24 const char* token;
25 int num_params;
26 bool result;
27 unsigned int result_position;
28 };
29
30 } // namespace
31
TEST(CPDFDefaultAppearanceTest,FindTagParamFromStart)32 TEST(CPDFDefaultAppearanceTest, FindTagParamFromStart) {
33 const FindTagTestStruct test_data[] = {
34 // Empty strings.
35 STR_IN_TEST_CASE("", "Tj", 1, false, 0),
36 STR_IN_TEST_CASE("", "", 1, false, 0),
37 // Empty token.
38 STR_IN_TEST_CASE(" T j", "", 1, false, 5),
39 // No parameter.
40 STR_IN_TEST_CASE("Tj", "Tj", 1, false, 2),
41 STR_IN_TEST_CASE("(Tj", "Tj", 1, false, 3),
42 // Partial token match.
43 STR_IN_TEST_CASE("\r12\t34 56 78Tj", "Tj", 1, false, 15),
44 // Regular cases with various parameters.
45 STR_IN_TEST_CASE("\r\0abd Tj", "Tj", 1, true, 0),
46 STR_IN_TEST_CASE("12 4 Tj 3 46 Tj", "Tj", 1, true, 2),
47 STR_IN_TEST_CASE("er^ 2 (34) (5667) Tj", "Tj", 2, true, 5),
48 STR_IN_TEST_CASE("<344> (232)\t343.4\n12 45 Tj", "Tj", 3, true, 11),
49 STR_IN_TEST_CASE("1 2 3 4 5 6 7 8 cm", "cm", 6, true, 3),
50 };
51
52 for (const auto& item : test_data) {
53 CPDF_SimpleParser parser(item.input_span());
54 EXPECT_EQ(item.result,
55 CPDF_DefaultAppearance::FindTagParamFromStartForTesting(
56 &parser, item.token, item.num_params))
57 << " for case " << item.input;
58 EXPECT_EQ(item.result_position, parser.GetCurrentPosition())
59 << " for case " << item.input;
60 }
61 }
62