objects.cc
5.1 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <qpdf/assert_test.h>
// This program tests miscellaneous object handle functionality
#include <qpdf/QPDF.hh>
#include <qpdf/QIntC.hh>
#include <qpdf/QPDFObjectHandle_private.hh>
#include <qpdf/QUtil.hh>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
static char const* whoami = nullptr;
void
usage()
{
std::cerr << "Usage: " << whoami << " n filename1 [arg2]" << '\n';
exit(2);
}
#define assert_compare_numbers(expected, expr) compare_numbers(#expr, expected, expr)
template <typename T1, typename T2>
static void
compare_numbers(char const* description, T1 const& expected, T2 const& actual)
{
if (expected != actual) {
std::cerr << description << ": expected = " << expected << "; actual = " << actual << '\n';
}
}
static void
test_0(QPDF& pdf, char const* arg2)
{
// Test int size checks. This test will fail if int and long long are the same size.
QPDFObjectHandle t = pdf.getTrailer();
unsigned long long q1_l = 3ULL * QIntC::to_ulonglong(INT_MAX);
long long q1 = QIntC::to_longlong(q1_l);
long long q2_l = 3LL * QIntC::to_longlong(INT_MIN);
long long q2 = QIntC::to_longlong(q2_l);
unsigned int q3_i = UINT_MAX;
long long q3 = QIntC::to_longlong(q3_i);
t.replaceKey("/Q1", QPDFObjectHandle::newInteger(q1));
t.replaceKey("/Q2", QPDFObjectHandle::newInteger(q2));
t.replaceKey("/Q3", QPDFObjectHandle::newInteger(q3));
assert_compare_numbers(q1, t.getKey("/Q1").getIntValue());
assert_compare_numbers(q1_l, t.getKey("/Q1").getUIntValue());
assert_compare_numbers(INT_MAX, t.getKey("/Q1").getIntValueAsInt());
try {
assert_compare_numbers(0u, QPDFObjectHandle::newNull().getUIntValueAsUInt());
std::cerr << "convert null to uint did not throw\n";
} catch (QPDFExc const&) {
std::cerr << "caught expected type error\n";
}
assert_compare_numbers(std::numeric_limits<int8_t>::max(), Integer(q1).value<int8_t>());
assert_compare_numbers(std::numeric_limits<int8_t>::min(), Integer(-q1).value<int8_t>());
try {
int8_t q1_8 = Integer(q1);
std::cerr << "q1_8: " << std::to_string(q1_8) << '\n';
} catch (std::overflow_error const&) {
std::cerr << "caught expected int8_t overflow error\n";
}
try {
int8_t q1_8 = Integer(-q1);
std::cerr << "q1_8: " << std::to_string(q1_8) << '\n';
} catch (std::underflow_error const&) {
std::cerr << "caught expected int8_t underflow error\n";
}
assert_compare_numbers(std::numeric_limits<uint8_t>::max(), Integer(q1).value<uint8_t>());
assert_compare_numbers(0, Integer(-q1).value<uint8_t>());
try {
uint8_t q1_u8 = Integer(q1);
std::cerr << "q1_u8: " << std::to_string(q1_u8) << '\n';
} catch (std::overflow_error const&) {
std::cerr << "caught expected uint8_t overflow error\n";
}
try {
uint8_t q1_u8 = Integer(-q1);
std::cerr << "q1_u8: " << std::to_string(q1_u8) << '\n';
} catch (std::underflow_error const&) {
std::cerr << "caught expected uint8_t underflow error\n";
}
assert_compare_numbers(UINT_MAX, t.getKey("/Q1").getUIntValueAsUInt());
assert_compare_numbers(q2_l, t.getKey("/Q2").getIntValue());
assert_compare_numbers(0U, t.getKey("/Q2").getUIntValue());
assert_compare_numbers(INT_MIN, t.getKey("/Q2").getIntValueAsInt());
assert_compare_numbers(0U, t.getKey("/Q2").getUIntValueAsUInt());
assert_compare_numbers(INT_MAX, t.getKey("/Q3").getIntValueAsInt());
assert_compare_numbers(UINT_MAX, t.getKey("/Q3").getUIntValueAsUInt());
}
void
runtest(int n, char const* filename1, char const* arg2)
{
// Most tests here are crafted to work on specific files. Look at
// the test suite to see how the test is invoked to find the file
// that the test is supposed to operate on.
std::set<int> ignore_filename = {};
QPDF pdf;
std::shared_ptr<char> file_buf;
FILE* filep = nullptr;
if (ignore_filename.contains(n)) {
// Ignore filename argument entirely
} else {
size_t size = 0;
QUtil::read_file_into_memory(filename1, file_buf, size);
pdf.processMemoryFile(filename1, file_buf.get(), size);
}
std::map<int, void (*)(QPDF&, char const*)> test_functions = {
{0, test_0},
};
auto fn = test_functions.find(n);
if (fn == test_functions.end()) {
throw std::runtime_error(std::string("invalid test ") + QUtil::int_to_string(n));
}
(fn->second)(pdf, arg2);
if (filep) {
fclose(filep);
}
std::cout << "test " << n << " done" << '\n';
}
int
main(int argc, char* argv[])
{
QUtil::setLineBuf(stdout);
if ((whoami = strrchr(argv[0], '/')) == nullptr) {
whoami = argv[0];
} else {
++whoami;
}
if ((argc < 3) || (argc > 4)) {
usage();
}
try {
int n = QUtil::string_to_int(argv[1]);
char const* filename1 = argv[2];
char const* arg2 = argv[3];
runtest(n, filename1, arg2);
} catch (std::exception& e) {
std::cerr << e.what() << '\n';
exit(2);
}
return 0;
}