1 // Copyright 2020 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 "fpdfsdk/cpdfsdk_helpers.h"
6
7 #include "core/fxcrt/compiler_specific.h"
8 #include "core/fxcrt/fx_memcpy_wrappers.h"
9 #include "core/fxcrt/stl_util.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 using ::testing::ElementsAre;
14 using ::testing::IsEmpty;
15
TEST(CPDFSDKHelpersTest,NulTerminateMaybeCopyAndReturnLength)16 TEST(CPDFSDKHelpersTest, NulTerminateMaybeCopyAndReturnLength) {
17 {
18 const ByteString to_be_copied("toBeCopied");
19 constexpr size_t kExpectedToBeCopiedLen = 10;
20 ASSERT_EQ(kExpectedToBeCopiedLen, to_be_copied.GetLength());
21 EXPECT_EQ(kExpectedToBeCopiedLen + 1,
22 NulTerminateMaybeCopyAndReturnLength(to_be_copied,
23 pdfium::span<char>()));
24
25 // Buffer should not change if declared length is too short.
26 char buf[kExpectedToBeCopiedLen + 1];
27 fxcrt::Fill(buf, 0x42);
28 ASSERT_EQ(kExpectedToBeCopiedLen + 1,
29 NulTerminateMaybeCopyAndReturnLength(
30 to_be_copied,
31 pdfium::make_span(buf).first(kExpectedToBeCopiedLen)));
32 for (char c : buf)
33 EXPECT_EQ(0x42, c);
34
35 // Buffer should copy over if long enough.
36 ASSERT_EQ(kExpectedToBeCopiedLen + 1,
37 NulTerminateMaybeCopyAndReturnLength(to_be_copied,
38 pdfium::make_span(buf)));
39 EXPECT_EQ(to_be_copied, ByteString(buf));
40 }
41 {
42 // Empty ByteString should still copy NUL terminator.
43 const ByteString empty;
44 char buf[1];
45 ASSERT_EQ(1u, NulTerminateMaybeCopyAndReturnLength(empty,
46 pdfium::make_span(buf)));
47 EXPECT_EQ(empty, ByteString(buf));
48 }
49 }
50
TEST(CPDFSDKHelpersTest,ParsePageRangeString)51 TEST(CPDFSDKHelpersTest, ParsePageRangeString) {
52 EXPECT_THAT(ParsePageRangeString("", 1), IsEmpty());
53 EXPECT_THAT(ParsePageRangeString(" ", 1), IsEmpty());
54 EXPECT_THAT(ParsePageRangeString("clams", 1), IsEmpty());
55 EXPECT_THAT(ParsePageRangeString("0", 0), IsEmpty());
56 EXPECT_THAT(ParsePageRangeString("1", 0), IsEmpty());
57 EXPECT_THAT(ParsePageRangeString(",1", 10), IsEmpty());
58 EXPECT_THAT(ParsePageRangeString("1,", 10), IsEmpty());
59 EXPECT_THAT(ParsePageRangeString("1,clams", 1), IsEmpty());
60 EXPECT_THAT(ParsePageRangeString("clams,1", 1), IsEmpty());
61 EXPECT_THAT(ParsePageRangeString("0-1", 10), IsEmpty());
62 EXPECT_THAT(ParsePageRangeString("1-0", 10), IsEmpty());
63 EXPECT_THAT(ParsePageRangeString("1-5", 4), IsEmpty());
64 EXPECT_THAT(ParsePageRangeString("1-11,", 10), IsEmpty());
65 EXPECT_THAT(ParsePageRangeString(",1-1", 10), IsEmpty());
66 EXPECT_THAT(ParsePageRangeString("1-", 10), IsEmpty());
67 EXPECT_THAT(ParsePageRangeString("1-,", 10), IsEmpty());
68 EXPECT_THAT(ParsePageRangeString("-2,", 10), IsEmpty());
69 EXPECT_THAT(ParsePageRangeString("1-clams", 10), IsEmpty());
70 EXPECT_THAT(ParsePageRangeString("clams-1,", 10), IsEmpty());
71 EXPECT_THAT(ParsePageRangeString("1-2clams", 10), IsEmpty());
72 EXPECT_THAT(ParsePageRangeString("0,1", 10), IsEmpty());
73 EXPECT_THAT(ParsePageRangeString("1,0", 10), IsEmpty());
74 EXPECT_THAT(ParsePageRangeString("1-2,,,,3-4", 10), IsEmpty());
75 EXPECT_THAT(ParsePageRangeString("1-2-", 10), IsEmpty());
76
77 EXPECT_THAT(ParsePageRangeString("1-1", 10), ElementsAre(0));
78 EXPECT_THAT(ParsePageRangeString("1", 1), ElementsAre(0));
79 EXPECT_THAT(ParsePageRangeString("1-4", 4), ElementsAre(0, 1, 2, 3));
80 EXPECT_THAT(ParsePageRangeString("1- 4", 4), ElementsAre(0, 1, 2, 3));
81 EXPECT_THAT(ParsePageRangeString("1 -4", 4), ElementsAre(0, 1, 2, 3));
82 EXPECT_THAT(ParsePageRangeString("1,2", 10), ElementsAre(0, 1));
83 EXPECT_THAT(ParsePageRangeString("2,1", 10), ElementsAre(1, 0));
84 EXPECT_THAT(ParsePageRangeString("1,50,2", 100), ElementsAre(0, 49, 1));
85 EXPECT_THAT(ParsePageRangeString("1-4,50", 100), ElementsAre(0, 1, 2, 3, 49));
86 EXPECT_THAT(ParsePageRangeString("50,1-2", 100), ElementsAre(49, 0, 1));
87 EXPECT_THAT(ParsePageRangeString("5 0, 1-2 ", 100),
88 ElementsAre(49, 0, 1)); // ???
89 EXPECT_THAT(ParsePageRangeString("1-3,4-6", 10),
90 ElementsAre(0, 1, 2, 3, 4, 5));
91 EXPECT_THAT(ParsePageRangeString("1-4,3-6", 10),
92 ElementsAre(0, 1, 2, 3, 2, 3, 4, 5));
93 }
94