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

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_decode_unittest.cpp

Issue 1666663004: Add unit tests for ascii85 and hex decoders. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 10 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
OLDNEW
1 // Copyright 2015 PDFium Authors. All rights reserved. 1 // Copyright 2015 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/include/fpdfapi/fpdf_parser.h" 5 #include "core/include/fpdfapi/fpdf_parser.h"
6 #include "testing/gtest/include/gtest/gtest.h" 6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "testing/test_support.h"
8
9 TEST(fpdf_parser_decode, A85Decode) {
10 pdfium::DecodeTestData test_data[] = {
11 // Empty src string.
12 DECODE_TEST_CASE("", "", 0),
13 // Empty content in src string.
14 DECODE_TEST_CASE("~>", "", 0),
15 // Regular conversion.
16 DECODE_TEST_CASE("FCfN8~>", "test", 7),
17 // End at the ending mark.
18 DECODE_TEST_CASE("FCfN8~>FCfN8", "test", 7),
19 // Skip whitespaces.
20 DECODE_TEST_CASE("\t F C\r\n \tf N 8 ~>", "test", 17),
21 // No ending mark.
22 DECODE_TEST_CASE("@3B0)DJj_BF*)>@Gp#-s", "a funny story :)", 20),
23 // Non-multiple length.
24 DECODE_TEST_CASE("12A", "2k", 3),
25 // Stop at unknown characters.
26 DECODE_TEST_CASE("FCfN8FCfN8vw", "testtest", 11),
27 };
28 for (size_t i = 0; i < FX_ArraySize(test_data); ++i) {
29 pdfium::DecodeTestData* ptr = &test_data[i];
30 uint8_t* result = nullptr;
31 FX_DWORD result_size;
32 EXPECT_EQ(ptr->processed_size,
33 A85Decode(ptr->input, ptr->input_size, result, result_size))
34 << "for case " << i;
35 EXPECT_EQ(ptr->expected_size, result_size);
Lei Zhang 2016/02/05 01:25:34 ASSERT_EQ() or we may get out of bound access belo
Wei Li 2016/02/06 00:11:44 Done.
36 for (size_t j = 0; j < result_size; ++j) {
37 EXPECT_EQ(ptr->expected[j], result[j]) << "for case " << i << " char "
38 << j;
39 }
40 FX_Free(result);
41 }
42 }
7 43
8 TEST(fpdf_parser_decode, HexDecode) { 44 TEST(fpdf_parser_decode, HexDecode) {
9 { 45 pdfium::DecodeTestData test_data[] = {
10 // Empty src string. 46 // Empty src string.
11 uint8_t* dest = nullptr; 47 DECODE_TEST_CASE("", "", 0),
12 FX_DWORD dest_size; 48 // Empty content in src string.
13 uint8_t src[] = ""; 49 DECODE_TEST_CASE(">", "", 1),
14 EXPECT_EQ(0, HexDecode(src, 0, dest, dest_size)); 50 // Only whitespaces in src string.
15 EXPECT_EQ(0, dest_size); 51 DECODE_TEST_CASE("\t \r\n>", "", 7),
16 EXPECT_EQ('\0', dest[0]); 52 // Regular conversion.
17 FX_Free(dest); 53 DECODE_TEST_CASE("12Ac>zzz", "\x12\xac", 5),
18 } 54 // Skip whitespaces.
19 55 DECODE_TEST_CASE("12 Ac\t02\r\nBF>zzz>", "\x12\xac\x02\xbf", 13),
20 { 56 // Non-multiple length.
21 // Regular conversion. 57 DECODE_TEST_CASE("12A>zzz", "\x12\xa0", 4),
22 uint8_t* dest = nullptr; 58 // Skips unknown characters.
23 FX_DWORD dest_size; 59 DECODE_TEST_CASE("12tk \tAc>zzz", "\x12\xac", 10),
24 uint8_t src[] = "12Ac>zzz"; 60 // No ending mark.
25 EXPECT_EQ(5, HexDecode(src, 8, dest, dest_size)); 61 DECODE_TEST_CASE("12AcED3c3456", "\x12\xac\xed\x3c\x34\x56", 12),
26 EXPECT_EQ(2, dest_size); 62 };
27 EXPECT_EQ(18, dest[0]); 63 for (size_t i = 0; i < FX_ArraySize(test_data); ++i) {
28 EXPECT_EQ(172, dest[1]); 64 pdfium::DecodeTestData* ptr = &test_data[i];
29 FX_Free(dest); 65 uint8_t* result = nullptr;
30 } 66 FX_DWORD result_size;
31 67 EXPECT_EQ(ptr->processed_size,
32 { 68 HexDecode(ptr->input, ptr->input_size, result, result_size))
33 // Non-multiple length. 69 << "for case " << i;
34 uint8_t* dest = nullptr; 70 EXPECT_EQ(ptr->expected_size, result_size);
35 FX_DWORD dest_size; 71 for (size_t j = 0; j < result_size; ++j) {
36 uint8_t src[] = "12A>zzz"; 72 EXPECT_EQ(ptr->expected[j], result[j]) << "for case " << i << " char "
37 EXPECT_EQ(4, HexDecode(src, 8, dest, dest_size)); 73 << j;
38 EXPECT_EQ(2, dest_size); 74 }
39 EXPECT_EQ(18, dest[0]); 75 FX_Free(result);
40 EXPECT_EQ(160, dest[1]);
41 FX_Free(dest);
42 }
43
44 {
45 // Skips unknown characters.
46 uint8_t* dest = nullptr;
47 FX_DWORD dest_size;
48 uint8_t src[] = "12tk \tAc>zzz";
49 EXPECT_EQ(10, HexDecode(src, 13, dest, dest_size));
50 EXPECT_EQ(2, dest_size);
51 EXPECT_EQ(18, dest[0]);
52 EXPECT_EQ(172, dest[1]);
53 FX_Free(dest);
54 } 76 }
55 } 77 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698