OLD | NEW |
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 <limits> | 5 #include <limits> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "core/fpdfapi/parser/cpdf_parser.h" | 8 #include "core/fpdfapi/parser/cpdf_parser.h" |
9 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" | 9 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" |
| 10 #include "core/fxcrt/cfx_retain_ptr.h" |
10 #include "core/fxcrt/fx_ext.h" | 11 #include "core/fxcrt/fx_ext.h" |
11 #include "core/fxcrt/fx_stream.h" | 12 #include "core/fxcrt/fx_stream.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "testing/utils/path_service.h" | 14 #include "testing/utils/path_service.h" |
14 | 15 |
15 // Provide a way to read test data from a buffer instead of a file. | 16 // Provide a way to read test data from a buffer instead of a file. |
16 class CFX_TestBufferRead : public IFX_SeekableReadStream { | 17 class CFX_TestBufferRead : public IFX_SeekableReadStream { |
17 public: | 18 public: |
18 CFX_TestBufferRead(const unsigned char* buffer_in, size_t buf_size) | 19 static CFX_RetainPtr<CFX_TestBufferRead> Create( |
19 : buffer_(buffer_in), total_size_(buf_size) {} | 20 const unsigned char* buffer_in, |
20 | 21 size_t buf_size) { |
21 // IFX_ReadStream: | 22 return CFX_RetainPtr<CFX_TestBufferRead>( |
22 void Release() override { delete this; } | 23 new CFX_TestBufferRead(buffer_in, buf_size)); |
| 24 } |
23 | 25 |
24 // IFX_SeekableReadStream: | 26 // IFX_SeekableReadStream: |
25 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { | 27 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { |
26 if (offset < 0 || offset + size > total_size_) | 28 if (offset < 0 || offset + size > total_size_) |
27 return false; | 29 return false; |
28 | 30 |
29 memcpy(buffer, buffer_ + offset, size); | 31 memcpy(buffer, buffer_ + offset, size); |
30 return true; | 32 return true; |
31 } | 33 } |
32 | 34 |
33 FX_FILESIZE GetSize() override { return (FX_FILESIZE)total_size_; }; | 35 FX_FILESIZE GetSize() override { return (FX_FILESIZE)total_size_; }; |
34 | 36 |
35 protected: | 37 protected: |
| 38 CFX_TestBufferRead(const unsigned char* buffer_in, size_t buf_size) |
| 39 : buffer_(buffer_in), total_size_(buf_size) {} |
| 40 |
36 const unsigned char* buffer_; | 41 const unsigned char* buffer_; |
37 size_t total_size_; | 42 size_t total_size_; |
38 }; | 43 }; |
39 | 44 |
40 // A wrapper class to help test member functions of CPDF_Parser. | 45 // A wrapper class to help test member functions of CPDF_Parser. |
41 class CPDF_TestParser : public CPDF_Parser { | 46 class CPDF_TestParser : public CPDF_Parser { |
42 public: | 47 public: |
43 CPDF_TestParser() {} | 48 CPDF_TestParser() {} |
44 ~CPDF_TestParser() {} | 49 ~CPDF_TestParser() {} |
45 | 50 |
46 // Setup reading from a file and initial states. | 51 // Setup reading from a file and initial states. |
47 bool InitTestFromFile(const FX_CHAR* path) { | 52 bool InitTestFromFile(const FX_CHAR* path) { |
48 IFX_SeekableReadStream* pFileAccess = | 53 CFX_RetainPtr<IFX_SeekableReadStream> pFileAccess = |
49 IFX_SeekableReadStream::CreateFromFilename(path); | 54 IFX_SeekableReadStream::CreateFromFilename(path); |
50 if (!pFileAccess) | 55 if (!pFileAccess) |
51 return false; | 56 return false; |
52 | 57 |
53 // For the test file, the header is set at the beginning. | 58 // For the test file, the header is set at the beginning. |
54 m_pSyntax->InitParser(pFileAccess, 0); | 59 m_pSyntax->InitParser(pFileAccess, 0); |
55 return true; | 60 return true; |
56 } | 61 } |
57 | 62 |
58 // Setup reading from a buffer and initial states. | 63 // Setup reading from a buffer and initial states. |
59 bool InitTestFromBuffer(const unsigned char* buffer, size_t len) { | 64 bool InitTestFromBuffer(const unsigned char* buffer, size_t len) { |
60 CFX_TestBufferRead* buffer_reader = new CFX_TestBufferRead(buffer, len); | 65 CFX_RetainPtr<CFX_TestBufferRead> buffer_reader = |
| 66 CFX_TestBufferRead::Create(buffer, len); |
61 | 67 |
62 // For the test file, the header is set at the beginning. | 68 // For the test file, the header is set at the beginning. |
63 m_pSyntax->InitParser(buffer_reader, 0); | 69 m_pSyntax->InitParser(buffer_reader, 0); |
64 return true; | 70 return true; |
65 } | 71 } |
66 | 72 |
67 private: | 73 private: |
68 // Add test cases here as private friend so that protected members in | 74 // Add test cases here as private friend so that protected members in |
69 // CPDF_Parser can be accessed by test cases. | 75 // CPDF_Parser can be accessed by test cases. |
70 // Need to access RebuildCrossRef. | 76 // Need to access RebuildCrossRef. |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 200 |
195 ASSERT_TRUE(parser.LoadCrossRefV4(0, 0, false)); | 201 ASSERT_TRUE(parser.LoadCrossRefV4(0, 0, false)); |
196 const FX_FILESIZE offsets[] = {0, 23, 0, 0, 0, 45, 179}; | 202 const FX_FILESIZE offsets[] = {0, 23, 0, 0, 0, 45, 179}; |
197 const uint8_t types[] = {0, 1, 0, 0, 0, 1, 1}; | 203 const uint8_t types[] = {0, 1, 0, 0, 0, 1, 1}; |
198 for (size_t i = 0; i < FX_ArraySize(offsets); ++i) { | 204 for (size_t i = 0; i < FX_ArraySize(offsets); ++i) { |
199 EXPECT_EQ(offsets[i], parser.m_ObjectInfo[i].pos); | 205 EXPECT_EQ(offsets[i], parser.m_ObjectInfo[i].pos); |
200 EXPECT_EQ(types[i], parser.m_ObjectInfo[i].type); | 206 EXPECT_EQ(types[i], parser.m_ObjectInfo[i].type); |
201 } | 207 } |
202 } | 208 } |
203 } | 209 } |
OLD | NEW |