Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: core/fxcodec/codec/fx_codec_a85_unittest.cpp

Issue 2612243005: Add postscript path (Closed)
Patch Set: Add fuzzers and fix unit tests Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fxcodec/codec/fx_codec.cpp ('k') | core/fxcodec/codec/fx_codec_rle_unittest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 PDFium Authors. All rights reserved.
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 <stdint.h>
6
7 #include <limits>
8
9 #include "core/fxcodec/codec/ccodec_basicmodule.h"
10 #include "core/fxcodec/fx_codec.h"
11 #include "testing/fx_string_testhelpers.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 TEST(fxcodec, A85TestBadInputs) {
15 uint8_t src_buf[4] = {1, 2, 3, 4};
16 uint8_t* dest_buf = nullptr;
17 uint32_t src_size = 4;
18 uint32_t dest_size = 0;
19
20 CCodec_BasicModule* pEncoders = CCodec_ModuleMgr().GetBasicModule();
21 EXPECT_TRUE(pEncoders);
22
23 // Error codes, not segvs, should callers pass us a nullptr pointer.
24 EXPECT_FALSE(pEncoders->A85Encode(src_buf, src_size, &dest_buf, nullptr));
25 EXPECT_FALSE(pEncoders->A85Encode(src_buf, src_size, nullptr, &dest_size));
26 EXPECT_FALSE(pEncoders->A85Encode(src_buf, 0, &dest_buf, &dest_size));
27 EXPECT_FALSE(pEncoders->A85Encode(nullptr, src_size, &dest_buf, &dest_size));
28 }
29
30 // No leftover bytes, just translate 2 sets of symbols.
31 TEST(fxcodec, A85TestBasic) {
32 // Make sure really big values don't break.
33 uint8_t src_buf[8] = {1, 2, 3, 4, 255, 255, 255, 255};
34 uint8_t* dest_buf = nullptr;
35 uint32_t src_size = 8;
36 uint32_t dest_size = 0;
37
38 CCodec_BasicModule* pEncoders = CCodec_ModuleMgr().GetBasicModule();
39 EXPECT_TRUE(pEncoders);
40
41 // Should succeed.
42 EXPECT_TRUE(pEncoders->A85Encode(src_buf, src_size, &dest_buf, &dest_size));
43
44 // Should have 5 chars for each set of 4 and 2 terminators.
45 uint32_t expected_size = 12;
46 EXPECT_EQ(dest_size, expected_size);
47 uint8_t expected_out[12] = {33, 60, 78, 63, 43, 115, 56, 87, 45, 33, 126, 62};
48
49 // Check the output
50 for (uint32_t i = 0; i < expected_size; i++)
51 EXPECT_EQ(dest_buf[i], expected_out[i]) << " at " << i;
52 FX_Free(dest_buf);
53 }
54
55 // Leftover bytes.
56 TEST(fxcodec, A85TestLeftoverBytes) {
57 // 1 Leftover Byte:
58 uint8_t src_buf_1leftover[5] = {1, 2, 3, 4, 255};
59 uint8_t* dest_buf = nullptr;
60 uint32_t src_size = 5;
61 uint32_t dest_size = 0;
62
63 CCodec_BasicModule* pEncoders = CCodec_ModuleMgr().GetBasicModule();
64 EXPECT_TRUE(pEncoders);
65
66 // Should succeed
67 EXPECT_TRUE(
68 pEncoders->A85Encode(src_buf_1leftover, src_size, &dest_buf, &dest_size));
69 uint32_t expected_size = 9; // 5 chars for first symbol + 2 + 2 terminators.
70 EXPECT_EQ(dest_size, expected_size);
Tom Sepez 2017/01/11 21:09:08 nit: typically we write this as EXPECT_EQ(9u, d
rbpotter 2017/01/11 22:55:59 Done.
71 uint8_t expected_out_1leftover[9] = {33, 60, 78, 63, 43, 114, 114, 126, 62};
72
73 // Check the output
74 for (uint32_t i = 0; i < expected_size; i++)
75 EXPECT_EQ(dest_buf[i], expected_out_1leftover[i]) << " at " << i;
76 FX_Free(dest_buf);
77
78 // 2 Leftover bytes:
79 src_size++;
80 dest_buf = nullptr;
81 dest_size = 0;
82 uint8_t src_buf_2leftover[6] = {1, 2, 3, 4, 255, 254};
83 // Should succeed
84 EXPECT_TRUE(
85 pEncoders->A85Encode(src_buf_2leftover, src_size, &dest_buf, &dest_size));
86 expected_size = 10; // 5 chars for first symbol + 3 + 2 terminators.
87 EXPECT_EQ(dest_size, expected_size);
88 uint8_t expected_out_2leftover[10] = {33, 60, 78, 63, 43,
89 115, 56, 68, 126, 62};
90
91 // Check the output
92 for (uint32_t i = 0; i < expected_size; i++)
93 EXPECT_EQ(dest_buf[i], expected_out_2leftover[i]) << " at " << i;
94 FX_Free(dest_buf);
95
96 // 3 Leftover bytes:
97 src_size++;
98 dest_buf = nullptr;
99 dest_size = 0;
100 uint8_t src_buf_3leftover[7] = {1, 2, 3, 4, 255, 254, 253};
101 // Should succeed
102 EXPECT_TRUE(
103 pEncoders->A85Encode(src_buf_3leftover, src_size, &dest_buf, &dest_size));
104 expected_size = 11; // 5 chars for first symbol + 4 + 2 terminators.
105 EXPECT_EQ(dest_size, expected_size);
106 uint8_t expected_out_3leftover[11] = {33, 60, 78, 63, 43, 115,
107 56, 77, 114, 126, 62};
108
109 // Check the output
110 for (uint32_t i = 0; i < expected_size; i++)
111 EXPECT_EQ(dest_buf[i], expected_out_3leftover[i]) << " at " << i;
112 FX_Free(dest_buf);
113 }
114
115 // Test all zeros comes through as "z".
116 TEST(fxcodec, A85TestZeros) {
117 // Make sure really big values don't break.
118 uint8_t src_buf[8] = {1, 2, 3, 4, 0, 0, 0, 0};
119 uint8_t* dest_buf = nullptr;
120 uint32_t src_size = 8;
121 uint32_t dest_size = 0;
122
123 CCodec_BasicModule* pEncoders = CCodec_ModuleMgr().GetBasicModule();
124 EXPECT_TRUE(pEncoders);
125
126 // Should succeed.
127 EXPECT_TRUE(pEncoders->A85Encode(src_buf, src_size, &dest_buf, &dest_size));
128
129 // Should have 5 chars for first set of 4 + 1 for z + 2 terminators.
130 uint32_t expected_size = 8;
131 EXPECT_EQ(dest_size, expected_size);
132 uint8_t expected_out[8] = {33, 60, 78, 63, 43, 122, 126, 62};
133
134 // Check the output
135 for (uint32_t i = 0; i < 8; i++)
136 EXPECT_EQ(dest_buf[i], expected_out[i]) << " at " << i;
137 FX_Free(dest_buf);
138
139 // Should also work if it is at the start:
140 dest_buf = nullptr;
141 dest_size = 0;
142 uint8_t src_buf_2[8] = {0, 0, 0, 0, 1, 2, 3, 4};
143
144 // Should succeed.
145 EXPECT_TRUE(pEncoders->A85Encode(src_buf_2, src_size, &dest_buf, &dest_size));
146
147 // Should have 5 chars for set of 4 + 1 for z + 2 terminators.
148 EXPECT_EQ(dest_size, expected_size);
149 uint8_t expected_out_2[8] = {122, 33, 60, 78, 63, 43, 126, 62};
150
151 // Check the output
152 for (uint32_t i = 0; i < expected_size; i++)
153 EXPECT_EQ(dest_buf[i], expected_out_2[i]) << " at " << i;
154 FX_Free(dest_buf);
155
156 // Try with 2 leftover zero bytes. Make sure we don't get a "z".
157 src_size = 6; // Cut off the last 2 zeros.
158 dest_buf = nullptr;
159 dest_size = 0;
160
161 // Should succeed.
162 EXPECT_TRUE(pEncoders->A85Encode(src_buf, src_size, &dest_buf, &dest_size));
163
164 // Should have 5 chars for set of 4 + 3 for last 2 + 2 terminators.
165 expected_size = 10;
166 EXPECT_EQ(dest_size, expected_size);
167 uint8_t expected_out_leftover[10] = {33, 60, 78, 63, 43, 33, 33, 33, 126, 62};
168
169 // Check the output
170 for (uint32_t i = 0; i < expected_size; i++)
171 EXPECT_EQ(dest_buf[i], expected_out_leftover[i]) << " at " << i;
172 FX_Free(dest_buf);
173 }
174
175 // Make sure we get returns in the expected locations.
176 TEST(fxcodec, A85TestLineBreaks) {
177 // Make sure really big values don't break.
178 uint8_t src_buf[131] = {0};
179 // 1 full line + most of a line of normal symbols.
180 for (int k = 0; k < 116; k += 4) {
181 src_buf[k] = 1;
182 src_buf[k + 1] = 2;
183 src_buf[k + 2] = 3;
184 src_buf[k + 3] = 4;
185 }
186 // Fill in the end, leaving an all zero gap + 3 extra zeros at the end.
187 for (int k = 120; k < 128; k++) {
188 src_buf[k] = 1;
189 src_buf[k + 1] = 2;
190 src_buf[k + 2] = 3;
191 src_buf[k + 3] = 4;
192 }
193 uint8_t* dest_buf = nullptr;
194 uint32_t src_size = 131;
195 uint32_t dest_size = 0;
196
197 CCodec_BasicModule* pEncoders = CCodec_ModuleMgr().GetBasicModule();
198 EXPECT_TRUE(pEncoders);
199
200 // Should succeed.
201 EXPECT_TRUE(pEncoders->A85Encode(src_buf, src_size, &dest_buf, &dest_size));
202
203 // Should have 75 chars in the first row plus 2 char return,
204 // 76 chars in the second row plus 2 char return,
205 // and 9 chars in the last row with 2 terminators.
206 uint32_t expected_size = 166;
207 EXPECT_EQ(dest_size, expected_size);
208
209 // Check for the returns.
210 EXPECT_EQ(dest_buf[75], 13);
211 EXPECT_EQ(dest_buf[76], 10);
212 EXPECT_EQ(dest_buf[153], 13);
213 EXPECT_EQ(dest_buf[154], 10);
214 }
OLDNEW
« no previous file with comments | « core/fxcodec/codec/fx_codec.cpp ('k') | core/fxcodec/codec/fx_codec_rle_unittest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698