1 // Copyright 2019 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 "core/fxcrt/cfx_seekablestreamproxy.h"
6
7 #include <memory>
8 #include <vector>
9
10 #include "core/fxcrt/cfx_readonlymemorystream.h"
11 #include "core/fxcrt/retain_ptr.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/base/ptr_util.h"
14 #include "third_party/base/span.h"
15
TEST(SeekableStreamProxyTest,NullStream)16 TEST(SeekableStreamProxyTest, NullStream) {
17 auto proxy_stream = pdfium::MakeRetain<CFX_SeekableStreamProxy>(
18 pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(
19 pdfium::make_span<const uint8_t>(nullptr, 0)));
20
21 wchar_t buffer[16];
22 EXPECT_EQ(0u, proxy_stream->ReadBlock(buffer, FX_ArraySize(buffer)));
23 }
24
TEST(SeekableStreamProxyTest,DefaultStreamBOMNotRecognized)25 TEST(SeekableStreamProxyTest, DefaultStreamBOMNotRecognized) {
26 const char data[] = "abcd";
27 auto proxy_stream = pdfium::MakeRetain<CFX_SeekableStreamProxy>(
28 pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(pdfium::make_span(
29 reinterpret_cast<const uint8_t*>(data), sizeof(data) - 1)));
30
31 wchar_t buffer[16];
32 EXPECT_EQ(0u, proxy_stream->ReadBlock(buffer, FX_ArraySize(buffer)));
33 }
34
TEST(SeekableStreamProxyTest,UTF8Stream)35 TEST(SeekableStreamProxyTest, UTF8Stream) {
36 const char data[] = "\xEF\xBB\xBF*\xC2\xA2*";
37 auto proxy_stream = pdfium::MakeRetain<CFX_SeekableStreamProxy>(
38 pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(pdfium::make_span(
39 reinterpret_cast<const uint8_t*>(data), sizeof(data) - 1)));
40
41 wchar_t buffer[16];
42 EXPECT_EQ(3u, proxy_stream->ReadBlock(buffer, FX_ArraySize(buffer)));
43 EXPECT_EQ(L'*', buffer[0]);
44 EXPECT_EQ(L'\u00A2', buffer[1]);
45 EXPECT_EQ(L'*', buffer[2]);
46 }
47
TEST(SeekableStreamProxyTest,UTF16LEStream)48 TEST(SeekableStreamProxyTest, UTF16LEStream) {
49 const char data[] = "\xFF\xFE\x41\x00\x42\x01";
50 auto proxy_stream = pdfium::MakeRetain<CFX_SeekableStreamProxy>(
51 pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(pdfium::make_span(
52 reinterpret_cast<const uint8_t*>(data), sizeof(data) - 1)));
53
54 wchar_t buffer[16];
55 EXPECT_EQ(2u, proxy_stream->ReadBlock(buffer, FX_ArraySize(buffer)));
56 EXPECT_EQ(L'A', buffer[0]);
57 EXPECT_EQ(L'\u0142', buffer[1]);
58 }
59
TEST(SeekableStreamProxyTest,UTF16BEStream)60 TEST(SeekableStreamProxyTest, UTF16BEStream) {
61 const char data[] = "\xFE\xFF\x00\x41\x01\x42";
62 auto proxy_stream = pdfium::MakeRetain<CFX_SeekableStreamProxy>(
63 pdfium::MakeRetain<CFX_ReadOnlyMemoryStream>(pdfium::make_span(
64 reinterpret_cast<const uint8_t*>(data), sizeof(data) - 1)));
65
66 wchar_t buffer[16];
67 EXPECT_EQ(2u, proxy_stream->ReadBlock(buffer, FX_ArraySize(buffer)));
68 EXPECT_EQ(L'A', buffer[0]);
69 EXPECT_EQ(L'\u0142', buffer[1]);
70 }
71