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
a85_unittest.cpp
Go to the documentation of this file.
1// Copyright 2016 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 <stdint.h>
6
7#include <array>
8#include <iterator>
9#include <limits>
10#include <memory>
11
12#include "core/fxcodec/basic/basicmodule.h"
13#include "core/fxcrt/data_vector.h"
14#include "testing/gmock/include/gmock/gmock.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17using ::testing::ElementsAre;
18
19TEST(fxcodec, A85EmptyInput) {
20 EXPECT_TRUE(BasicModule::A85Encode({}).empty());
21}
22
23// No leftover bytes, just translate 2 sets of symbols.
24TEST(fxcodec, A85Basic) {
25 // Make sure really big values don't break.
26 const uint8_t src_buf[] = {1, 2, 3, 4, 255, 255, 255, 255};
27 DataVector<uint8_t> dest_buf = BasicModule::A85Encode(src_buf);
28
29 // Should have 5 chars for each set of 4 and 2 terminators.
30 EXPECT_THAT(dest_buf,
31 ElementsAre(33, 60, 78, 63, 43, 115, 56, 87, 45, 33, 126, 62));
32}
33
34// Leftover bytes.
35TEST(fxcodec, A85LeftoverBytes) {
36 {
37 // 1 Leftover Byte:
38 const uint8_t src_buf_1leftover[] = {1, 2, 3, 4, 255};
39 DataVector<uint8_t> dest_buf = BasicModule::A85Encode(src_buf_1leftover);
40
41 // 5 chars for first symbol + 2 + 2 terminators.
42 EXPECT_THAT(dest_buf, ElementsAre(33, 60, 78, 63, 43, 114, 114, 126, 62));
43 }
44 {
45 // 2 Leftover bytes:
46 const uint8_t src_buf_2leftover[] = {1, 2, 3, 4, 255, 254};
47 DataVector<uint8_t> dest_buf = BasicModule::A85Encode(src_buf_2leftover);
48 // 5 chars for first symbol + 3 + 2 terminators.
49 EXPECT_THAT(dest_buf,
50 ElementsAre(33, 60, 78, 63, 43, 115, 56, 68, 126, 62));
51 }
52 {
53 // 3 Leftover bytes:
54 const uint8_t src_buf_3leftover[] = {1, 2, 3, 4, 255, 254, 253};
55 DataVector<uint8_t> dest_buf = BasicModule::A85Encode(src_buf_3leftover);
56 // 5 chars for first symbol + 4 + 2 terminators.
57 EXPECT_THAT(dest_buf,
58 ElementsAre(33, 60, 78, 63, 43, 115, 56, 77, 114, 126, 62));
59 }
60}
61
62// Test all zeros comes through as "z".
63TEST(fxcodec, A85Zeros) {
64 {
65 // Make sure really big values don't break.
66 const uint8_t src_buf[] = {1, 2, 3, 4, 0, 0, 0, 0};
67 DataVector<uint8_t> dest_buf = BasicModule::A85Encode(src_buf);
68
69 // Should have 5 chars for first set of 4 + 1 for z + 2 terminators.
70 EXPECT_THAT(dest_buf, ElementsAre(33, 60, 78, 63, 43, 122, 126, 62));
71 }
72 {
73 // Should also work if it is at the start:
74 const uint8_t src_buf_2[] = {0, 0, 0, 0, 1, 2, 3, 4};
75 DataVector<uint8_t> dest_buf = BasicModule::A85Encode(src_buf_2);
76
77 // Should have 5 chars for set of 4 + 1 for z + 2 terminators.
78 EXPECT_THAT(dest_buf, ElementsAre(122, 33, 60, 78, 63, 43, 126, 62));
79 }
80 {
81 // Try with 2 leftover zero bytes. Make sure we don't get a "z".
82 const uint8_t src_buf_3[] = {1, 2, 3, 4, 0, 0};
83 DataVector<uint8_t> dest_buf = BasicModule::A85Encode(src_buf_3);
84
85 // Should have 5 chars for set of 4 + 3 for last 2 + 2 terminators.
86 EXPECT_THAT(dest_buf, ElementsAre(33, 60, 78, 63, 43, 33, 33, 33, 126, 62));
87 }
88}
89
90// Make sure we get returns in the expected locations.
91TEST(fxcodec, A85LineBreaks) {
92 // Make sure really big values don't break.
93 std::array<uint8_t, 131> src_buf = {};
94 // 1 full line + most of a line of normal symbols.
95 for (int k = 0; k < 116; k += 4) {
96 src_buf[k] = 1;
97 src_buf[k + 1] = 2;
98 src_buf[k + 2] = 3;
99 src_buf[k + 3] = 4;
100 }
101 // Fill in the end, leaving an all zero gap + 3 extra zeros at the end.
102 for (int k = 120; k < 128; k++) {
103 src_buf[k] = 1;
104 src_buf[k + 1] = 2;
105 src_buf[k + 2] = 3;
106 src_buf[k + 3] = 4;
107 }
108
109 // Should succeed.
110 DataVector<uint8_t> dest_buf = BasicModule::A85Encode(src_buf);
111
112 // Should have 75 chars in the first row plus 2 char return,
113 // 76 chars in the second row plus 2 char return,
114 // and 9 chars in the last row with 2 terminators.
115 ASSERT_EQ(166u, dest_buf.size());
116
117 // Check for the returns.
118 EXPECT_EQ(13, dest_buf[75]);
119 EXPECT_EQ(10, dest_buf[76]);
120 EXPECT_EQ(13, dest_buf[153]);
121 EXPECT_EQ(10, dest_buf[154]);
122}
TEST(FXCRYPT, CryptToBase16)