Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fpdf_signature_embeddertest.cpp
Go to the documentation of this file.
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 <vector>
6
7#include "public/fpdf_signature.h"
8#include "testing/embedder_test.h"
9#include "testing/fx_string_testhelpers.h"
10
12
14 ASSERT_TRUE(OpenDocument("two_signatures.pdf"));
15 EXPECT_EQ(2, FPDF_GetSignatureCount(document()));
16}
17
19 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
20 EXPECT_EQ(0, FPDF_GetSignatureCount(document()));
21
22 // Provide no document.
23 EXPECT_EQ(-1, FPDF_GetSignatureCount(nullptr));
24}
25
27 ASSERT_TRUE(OpenDocument("two_signatures.pdf"));
28 // Different, non-null signature objects are returned.
29 FPDF_SIGNATURE signature1 = FPDF_GetSignatureObject(document(), 0);
30 EXPECT_TRUE(signature1);
31 FPDF_SIGNATURE signature2 = FPDF_GetSignatureObject(document(), 1);
32 EXPECT_TRUE(signature2);
33 EXPECT_NE(signature1, signature2);
34
35 // Out of bounds.
36 EXPECT_FALSE(FPDF_GetSignatureObject(document(), -1));
37 EXPECT_FALSE(FPDF_GetSignatureObject(document(), 2));
38
39 // Provide no document.
40 EXPECT_FALSE(FPDF_GetSignatureObject(nullptr, 0));
41}
42
44 ASSERT_TRUE(OpenDocument("two_signatures.pdf"));
45 FPDF_SIGNATURE signature = FPDF_GetSignatureObject(document(), 0);
46 EXPECT_TRUE(signature);
47
48 // FPDFSignatureObj_GetContents() positive testing.
49 unsigned long size = FPDFSignatureObj_GetContents(signature, nullptr, 0);
50 const uint8_t kExpectedContents[] = {0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48,
51 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02, 0xA0,
52 0x80, 0x30, 0x80, 0x02, 0x01, 0x01};
53 ASSERT_EQ(sizeof(kExpectedContents), size);
54 std::vector<char> contents(size);
55 ASSERT_EQ(size,
56 FPDFSignatureObj_GetContents(signature, contents.data(), size));
57 ASSERT_EQ(0, memcmp(kExpectedContents, contents.data(), size));
58
59 // FPDFSignatureObj_GetContents() negative testing.
60 ASSERT_EQ(0U, FPDFSignatureObj_GetContents(nullptr, nullptr, 0));
61
62 contents.resize(2);
63 contents[0] = 'x';
64 contents[1] = '\0';
65 size =
66 FPDFSignatureObj_GetContents(signature, contents.data(), contents.size());
67 ASSERT_EQ(sizeof(kExpectedContents), size);
68 EXPECT_EQ('x', contents[0]);
69 EXPECT_EQ('\0', contents[1]);
70}
71
73 ASSERT_TRUE(OpenDocument("two_signatures.pdf"));
74 FPDF_SIGNATURE signature = FPDF_GetSignatureObject(document(), 0);
75 EXPECT_TRUE(signature);
76
77 // FPDFSignatureObj_GetByteRange() positive testing.
78 unsigned long size = FPDFSignatureObj_GetByteRange(signature, nullptr, 0);
79 const std::vector<int> kExpectedByteRange{0, 10, 30, 10};
80 ASSERT_EQ(kExpectedByteRange.size(), size);
81 std::vector<int> byte_range(size);
82 ASSERT_EQ(size,
83 FPDFSignatureObj_GetByteRange(signature, byte_range.data(), size));
84 ASSERT_EQ(kExpectedByteRange, byte_range);
85
86 // FPDFSignatureObj_GetByteRange() negative testing.
87 ASSERT_EQ(0U, FPDFSignatureObj_GetByteRange(nullptr, nullptr, 0));
88
89 byte_range.resize(2);
90 byte_range[0] = 0;
91 byte_range[1] = 1;
92 size = FPDFSignatureObj_GetByteRange(signature, byte_range.data(),
93 byte_range.size());
94 ASSERT_EQ(kExpectedByteRange.size(), size);
95 EXPECT_EQ(0, byte_range[0]);
96 EXPECT_EQ(1, byte_range[1]);
97}
98
100 ASSERT_TRUE(OpenDocument("two_signatures.pdf"));
101 FPDF_SIGNATURE signature = FPDF_GetSignatureObject(document(), 0);
102 EXPECT_TRUE(signature);
103
104 // FPDFSignatureObj_GetSubFilter() positive testing.
105 unsigned long size = FPDFSignatureObj_GetSubFilter(signature, nullptr, 0);
106 const char kExpectedSubFilter[] = "ETSI.CAdES.detached";
107 ASSERT_EQ(sizeof(kExpectedSubFilter), size);
108 std::vector<char> sub_filter(size);
109 ASSERT_EQ(size,
110 FPDFSignatureObj_GetSubFilter(signature, sub_filter.data(), size));
111 ASSERT_EQ(0, memcmp(kExpectedSubFilter, sub_filter.data(), size));
112
113 // FPDFSignatureObj_GetSubFilter() negative testing.
114 ASSERT_EQ(0U, FPDFSignatureObj_GetSubFilter(nullptr, nullptr, 0));
115
116 sub_filter.resize(2);
117 sub_filter[0] = 'x';
118 sub_filter[1] = '\0';
119 size = FPDFSignatureObj_GetSubFilter(signature, sub_filter.data(),
120 sub_filter.size());
121 ASSERT_EQ(sizeof(kExpectedSubFilter), size);
122 EXPECT_EQ('x', sub_filter[0]);
123 EXPECT_EQ('\0', sub_filter[1]);
124}
125
127 ASSERT_TRUE(OpenDocument("signature_no_sub_filter.pdf"));
128 FPDF_SIGNATURE signature = FPDF_GetSignatureObject(document(), 0);
129 EXPECT_TRUE(signature);
130
131 // FPDFSignatureObj_GetSubFilter() negative testing: no SubFilter
132 ASSERT_EQ(0U, FPDFSignatureObj_GetSubFilter(signature, nullptr, 0));
133}
134
136 ASSERT_TRUE(OpenDocument("signature_reason.pdf"));
137 FPDF_SIGNATURE signature = FPDF_GetSignatureObject(document(), 0);
138 EXPECT_TRUE(signature);
139
140 // FPDFSignatureObj_GetReason() positive testing.
141 constexpr char kReason[] = "test reason";
142 // Return value includes the terminating NUL that is provided.
143 constexpr unsigned long kReasonUTF16Size = std::size(kReason) * 2;
144 constexpr wchar_t kReasonWide[] = L"test reason";
145 unsigned long size = FPDFSignatureObj_GetReason(signature, nullptr, 0);
146 ASSERT_EQ(kReasonUTF16Size, size);
147
148 std::vector<unsigned short> buffer(size);
149 ASSERT_EQ(size, FPDFSignatureObj_GetReason(signature, buffer.data(), size));
150 ASSERT_EQ(kReasonWide, GetPlatformWString(buffer.data()));
151
152 // FPDFSignatureObj_GetReason() negative testing.
153 ASSERT_EQ(0U, FPDFSignatureObj_GetReason(nullptr, nullptr, 0));
154
155 // Buffer is too small, ensure it's not modified.
156 buffer.resize(2);
157 buffer[0] = 'x';
158 buffer[1] = '\0';
159 size = FPDFSignatureObj_GetReason(signature, buffer.data(), buffer.size());
160 ASSERT_EQ(kReasonUTF16Size, size);
161 EXPECT_EQ('x', buffer[0]);
162 EXPECT_EQ('\0', buffer[1]);
163}
164
166 ASSERT_TRUE(OpenDocument("two_signatures.pdf"));
167 FPDF_SIGNATURE signature = FPDF_GetSignatureObject(document(), 0);
168 EXPECT_TRUE(signature);
169
170 // FPDFSignatureObj_GetTime() positive testing.
171 unsigned long size = FPDFSignatureObj_GetTime(signature, nullptr, 0);
172 const char kExpectedTime[] = "D:20200624093114+02'00'";
173 ASSERT_EQ(sizeof(kExpectedTime), size);
174 std::vector<char> time_buffer(size);
175 ASSERT_EQ(size,
176 FPDFSignatureObj_GetTime(signature, time_buffer.data(), size));
177 ASSERT_EQ(0, memcmp(kExpectedTime, time_buffer.data(), size));
178
179 // FPDFSignatureObj_GetTime() negative testing.
180 ASSERT_EQ(0U, FPDFSignatureObj_GetTime(nullptr, nullptr, 0));
181
182 time_buffer.resize(2);
183 time_buffer[0] = 'x';
184 time_buffer[1] = '\0';
185 size = FPDFSignatureObj_GetTime(signature, time_buffer.data(),
186 time_buffer.size());
187 ASSERT_EQ(sizeof(kExpectedTime), size);
188 EXPECT_EQ('x', time_buffer[0]);
189 EXPECT_EQ('\0', time_buffer[1]);
190}
191
193 ASSERT_TRUE(OpenDocument("docmdp.pdf"));
194 FPDF_SIGNATURE signature = FPDF_GetSignatureObject(document(), 0);
195 ASSERT_NE(nullptr, signature);
196
197 // FPDFSignatureObj_GetDocMDPPermission() positive testing.
198 unsigned int permission = FPDFSignatureObj_GetDocMDPPermission(signature);
199 EXPECT_EQ(1U, permission);
200
201 // FPDFSignatureObj_GetDocMDPPermission() negative testing.
202 EXPECT_EQ(0U, FPDFSignatureObj_GetDocMDPPermission(nullptr));
203}
FPDF_EXPORT unsigned int FPDF_CALLCONV FPDFSignatureObj_GetDocMDPPermission(FPDF_SIGNATURE signature)
FPDF_EXPORT FPDF_SIGNATURE FPDF_CALLCONV FPDF_GetSignatureObject(FPDF_DOCUMENT document, int index)
FPDF_EXPORT int FPDF_CALLCONV FPDF_GetSignatureCount(FPDF_DOCUMENT document)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFSignatureObj_GetTime(FPDF_SIGNATURE signature, char *buffer, unsigned long length)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFSignatureObj_GetReason(FPDF_SIGNATURE signature, void *buffer, unsigned long length)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFSignatureObj_GetByteRange(FPDF_SIGNATURE signature, int *buffer, unsigned long length)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFSignatureObj_GetContents(FPDF_SIGNATURE signature, void *buffer, unsigned long length)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFSignatureObj_GetSubFilter(FPDF_SIGNATURE signature, char *buffer, unsigned long length)
TEST_F(FPDFSignatureEmbedderTest, GetSignatureCount)