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 "xfa/fxfa/xfa_ffapp.h"
6 
7 #include <memory>
8 #include <vector>
9 
10 #include "core/fpdfapi/parser/cpdf_dictionary.h"
11 #include "core/fpdfapi/parser/cpdf_stream.h"
12 #include "core/fxcrt/fx_memory.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/base/ptr_util.h"
15 
TEST(CXFAFileRead,NoStreams)16 TEST(CXFAFileRead, NoStreams) {
17   std::vector<CPDF_Stream*> streams;
18   CFX_RetainPtr<IFX_SeekableReadStream> fileread =
19       MakeSeekableReadStream(streams);
20 
21   uint8_t output_buffer[16];
22   memset(output_buffer, 0xbd, sizeof(output_buffer));
23   EXPECT_FALSE(fileread->ReadBlock(output_buffer, 0, 0));
24   EXPECT_EQ(0xbd, output_buffer[0]);
25 }
26 
TEST(CXFAFileRead,EmptyStreams)27 TEST(CXFAFileRead, EmptyStreams) {
28   std::vector<CPDF_Stream*> streams;
29   auto stream1 = pdfium::MakeUnique<CPDF_Stream>();
30   streams.push_back(stream1.get());
31   CFX_RetainPtr<IFX_SeekableReadStream> fileread =
32       MakeSeekableReadStream(streams);
33 
34   uint8_t output_buffer[16];
35   memset(output_buffer, 0xbd, sizeof(output_buffer));
36   EXPECT_FALSE(fileread->ReadBlock(output_buffer, 0, 0));
37   EXPECT_EQ(0xbd, output_buffer[0]);
38 }
39 
TEST(CXFAFileRead,NormalStreams)40 TEST(CXFAFileRead, NormalStreams) {
41   std::vector<CPDF_Stream*> streams;
42   auto stream1 = pdfium::MakeUnique<CPDF_Stream>();
43   auto stream2 = pdfium::MakeUnique<CPDF_Stream>();
44   auto stream3 = pdfium::MakeUnique<CPDF_Stream>();
45 
46   // 16 chars total.
47   stream1->InitStream(reinterpret_cast<const uint8_t*>("one t"), 5,
48                       pdfium::MakeUnique<CPDF_Dictionary>());
49   stream2->InitStream(reinterpret_cast<const uint8_t*>("wo "), 3,
50                       pdfium::MakeUnique<CPDF_Dictionary>());
51   stream3->InitStream(reinterpret_cast<const uint8_t*>("three!!!"), 8,
52                       pdfium::MakeUnique<CPDF_Dictionary>());
53 
54   streams.push_back(stream1.get());
55   streams.push_back(stream2.get());
56   streams.push_back(stream3.get());
57   CFX_RetainPtr<IFX_SeekableReadStream> fileread =
58       MakeSeekableReadStream(streams);
59 
60   uint8_t output_buffer[16];
61   memset(output_buffer, 0xbd, sizeof(output_buffer));
62   EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, 0));
63   EXPECT_EQ(0xbd, output_buffer[0]);
64 
65   memset(output_buffer, 0xbd, sizeof(output_buffer));
66   EXPECT_TRUE(fileread->ReadBlock(output_buffer, 1, 0));
67   EXPECT_EQ(0xbd, output_buffer[0]);
68 
69   memset(output_buffer, 0xbd, sizeof(output_buffer));
70   EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, 1));
71   EXPECT_EQ(0, memcmp(output_buffer, "o", 1));
72   EXPECT_EQ(0xbd, output_buffer[1]);
73 
74   memset(output_buffer, 0xbd, sizeof(output_buffer));
75   EXPECT_TRUE(fileread->ReadBlock(output_buffer, 0, sizeof(output_buffer)));
76   EXPECT_EQ(0, memcmp(output_buffer, "one two three!!!", 16));
77 
78   memset(output_buffer, 0xbd, sizeof(output_buffer));
79   EXPECT_TRUE(fileread->ReadBlock(output_buffer, 2, 10));
80   EXPECT_EQ(0, memcmp(output_buffer, "e two thre", 10));
81   EXPECT_EQ(0xbd, output_buffer[11]);
82 
83   memset(output_buffer, 0xbd, sizeof(output_buffer));
84   EXPECT_FALSE(fileread->ReadBlock(output_buffer, 1, sizeof(output_buffer)));
85   EXPECT_EQ(0, memcmp(output_buffer, "ne two three!!!", 15));
86   EXPECT_EQ(0xbd, output_buffer[15]);
87 }
88