aboutsummaryrefslogtreecommitdiff
path: root/fpdfsdk/cpdfsdk_helpers_unittest.cpp
blob: db5c85af3a481711f8eb8c1fa43243d55d727de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2020 The PDFium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "fpdfsdk/cpdfsdk_helpers.h"

#include "core/fxcrt/compiler_specific.h"
#include "core/fxcrt/fx_memcpy_wrappers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::ElementsAre;
using ::testing::IsEmpty;

TEST(CPDFSDK_HelpersTest, NulTerminateMaybeCopyAndReturnLength) {
  {
    const ByteString to_be_copied("toBeCopied");
    constexpr size_t kExpectedToBeCopiedLen = 10;
    ASSERT_EQ(kExpectedToBeCopiedLen, to_be_copied.GetLength());
    EXPECT_EQ(kExpectedToBeCopiedLen + 1,
              NulTerminateMaybeCopyAndReturnLength(to_be_copied,
                                                   pdfium::span<char>()));

    // Buffer should not change if declared length is too short.
    char buf[kExpectedToBeCopiedLen + 1];
    fxcrt::spanset(pdfium::make_span(buf), 0x42);
    ASSERT_EQ(kExpectedToBeCopiedLen + 1,
              NulTerminateMaybeCopyAndReturnLength(
                  to_be_copied, UNSAFE_BUFFERS(pdfium::make_span(
                                    buf, kExpectedToBeCopiedLen))));
    for (char c : buf)
      EXPECT_EQ(0x42, c);

    // Buffer should copy over if long enough.
    ASSERT_EQ(kExpectedToBeCopiedLen + 1,
              NulTerminateMaybeCopyAndReturnLength(to_be_copied,
                                                   pdfium::make_span(buf)));
    EXPECT_EQ(to_be_copied, ByteString(buf));
  }
  {
    // Empty ByteString should still copy NUL terminator.
    const ByteString empty;
    char buf[1];
    ASSERT_EQ(1u, NulTerminateMaybeCopyAndReturnLength(empty,
                                                       pdfium::make_span(buf)));
    EXPECT_EQ(empty, ByteString(buf));
  }
}

TEST(CPDFSDK_HelpersTest, ParsePageRangeString) {
  EXPECT_THAT(ParsePageRangeString("", 1), IsEmpty());
  EXPECT_THAT(ParsePageRangeString(" ", 1), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("clams", 1), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("0", 0), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1", 0), IsEmpty());
  EXPECT_THAT(ParsePageRangeString(",1", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1,", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1,clams", 1), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("clams,1", 1), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("0-1", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1-0", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1-5", 4), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1-11,", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString(",1-1", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1-", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1-,", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("-2,", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1-clams", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("clams-1,", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1-2clams", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("0,1", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1,0", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1-2,,,,3-4", 10), IsEmpty());
  EXPECT_THAT(ParsePageRangeString("1-2-", 10), IsEmpty());

  EXPECT_THAT(ParsePageRangeString("1-1", 10), ElementsAre(0));
  EXPECT_THAT(ParsePageRangeString("1", 1), ElementsAre(0));
  EXPECT_THAT(ParsePageRangeString("1-4", 4), ElementsAre(0, 1, 2, 3));
  EXPECT_THAT(ParsePageRangeString("1- 4", 4), ElementsAre(0, 1, 2, 3));
  EXPECT_THAT(ParsePageRangeString("1 -4", 4), ElementsAre(0, 1, 2, 3));
  EXPECT_THAT(ParsePageRangeString("1,2", 10), ElementsAre(0, 1));
  EXPECT_THAT(ParsePageRangeString("2,1", 10), ElementsAre(1, 0));
  EXPECT_THAT(ParsePageRangeString("1,50,2", 100), ElementsAre(0, 49, 1));
  EXPECT_THAT(ParsePageRangeString("1-4,50", 100), ElementsAre(0, 1, 2, 3, 49));
  EXPECT_THAT(ParsePageRangeString("50,1-2", 100), ElementsAre(49, 0, 1));
  EXPECT_THAT(ParsePageRangeString("5  0, 1-2 ", 100),
              ElementsAre(49, 0, 1));  // ???
  EXPECT_THAT(ParsePageRangeString("1-3,4-6", 10),
              ElementsAre(0, 1, 2, 3, 4, 5));
  EXPECT_THAT(ParsePageRangeString("1-4,3-6", 10),
              ElementsAre(0, 1, 2, 3, 2, 3, 4, 5));
}