1 // Copyright 2015 The Chromium 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.
5 #include "chrome/utility/safe_browsing/mac/read_stream.h"
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/path_service.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/utility/safe_browsing/mac/dmg_test_utils.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace safe_browsing
{
21 struct MemoryReadStreamTest
{
24 const char* TestName() {
25 return "MemoryReadStream";
28 std::vector
<uint8_t> data
;
31 struct FileReadStreamTest
{
33 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
36 const char* TestName() {
37 return "FileReadStream";
40 base::ScopedTempDir temp_dir
;
45 class ReadStreamTest
: public testing::Test
{
47 void SetUp() override
{
51 void TearDown() override
{
53 ADD_FAILURE() << "Failing type is " << test_helper_
.TestName();
56 scoped_ptr
<ReadStream
> CreateStream(size_t data_size
);
63 scoped_ptr
<ReadStream
>
64 ReadStreamTest
<MemoryReadStreamTest
>::CreateStream(size_t data_size
) {
65 test_helper_
.data
.resize(data_size
);
66 for (size_t i
= 0; i
< data_size
; ++i
) {
67 test_helper_
.data
[i
] = i
% 255;
69 return make_scoped_ptr(
70 new MemoryReadStream(&test_helper_
.data
[0], data_size
));
74 scoped_ptr
<ReadStream
>
75 ReadStreamTest
<FileReadStreamTest
>::CreateStream(size_t data_size
) {
76 base::FilePath path
= test_helper_
.temp_dir
.path().Append("stream");
77 test_helper_
.file
.Initialize(path
,
78 base::File::FLAG_CREATE
| base::File::FLAG_WRITE
);
79 if (!test_helper_
.file
.IsValid()) {
80 ADD_FAILURE() << "Failed to create temp file";
84 for (size_t i
= 0; i
< data_size
; ++i
) {
86 EXPECT_EQ(1, test_helper_
.file
.WriteAtCurrentPos(&value
, 1));
89 test_helper_
.file
.Close();
91 test_helper_
.file
.Initialize(path
,
92 base::File::FLAG_OPEN
| base::File::FLAG_READ
);
93 if (!test_helper_
.file
.IsValid()) {
94 ADD_FAILURE() << "Failed to open temp file";
98 return make_scoped_ptr(
99 new FileReadStream(test_helper_
.file
.GetPlatformFile()));
102 using ReadStreamImpls
= testing::Types
<MemoryReadStreamTest
,
104 TYPED_TEST_CASE(ReadStreamTest
, ReadStreamImpls
);
106 TYPED_TEST(ReadStreamTest
, Read
) {
107 scoped_ptr
<ReadStream
> stream
= ReadStreamTest
<TypeParam
>::CreateStream(128);
108 uint8_t buf
[128] = {0};
112 EXPECT_TRUE(stream
->Read(buf
, 4, &bytes_read
));
113 EXPECT_EQ(4u, bytes_read
);
114 uint8_t expected
[] = { 0, 1, 2, 3, 0, 0, 0 };
115 EXPECT_EQ(0, memcmp(expected
, buf
, sizeof(expected
)));
119 EXPECT_TRUE(stream
->Read(buf
, 9, &bytes_read
));
120 EXPECT_EQ(9u, bytes_read
);
121 uint8_t expected
[] = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0 };
122 EXPECT_EQ(0, memcmp(expected
, buf
, sizeof(expected
)));
126 TYPED_TEST(ReadStreamTest
, ReadAll
) {
127 const size_t kStreamSize
= 4242;
128 scoped_ptr
<ReadStream
> stream
=
129 ReadStreamTest
<TypeParam
>::CreateStream(kStreamSize
);
131 std::vector
<uint8_t> data
;
132 EXPECT_TRUE(test::ReadEntireStream(stream
.get(), &data
));
133 EXPECT_EQ(kStreamSize
, data
.size());
136 TYPED_TEST(ReadStreamTest
, SeekSet
) {
137 scoped_ptr
<ReadStream
> stream
= ReadStreamTest
<TypeParam
>::CreateStream(255);
138 uint8_t buf
[32] = {0};
142 EXPECT_EQ(250, stream
->Seek(250, SEEK_SET
));
143 EXPECT_TRUE(stream
->Read(buf
, sizeof(buf
), &bytes_read
));
144 EXPECT_EQ(5u, bytes_read
);
145 uint8_t expected
[] = { 250, 251, 252, 253, 254, 0, 0 };
146 EXPECT_EQ(0, memcmp(expected
, buf
, sizeof(expected
)));
150 EXPECT_EQ(5, stream
->Seek(5, SEEK_SET
));
151 EXPECT_TRUE(stream
->Read(buf
, 3, &bytes_read
));
152 EXPECT_EQ(3u, bytes_read
);
153 uint8_t expected
[] = { 5, 6, 7, 253, 254, 0, 0 };
154 EXPECT_EQ(0, memcmp(expected
, buf
, sizeof(expected
)));
158 TYPED_TEST(ReadStreamTest
, SeekEnd
) {
159 scoped_ptr
<ReadStream
> stream
= ReadStreamTest
<TypeParam
>::CreateStream(32);
160 uint8_t buf
[32] = {0};
164 EXPECT_EQ(32, stream
->Seek(0, SEEK_END
));
165 EXPECT_TRUE(stream
->Read(buf
, sizeof(buf
), &bytes_read
));
166 EXPECT_EQ(0u, bytes_read
);
170 EXPECT_EQ(28, stream
->Seek(-4, SEEK_END
));
171 EXPECT_TRUE(stream
->Read(buf
, sizeof(buf
), &bytes_read
));
172 EXPECT_EQ(4u, bytes_read
);
173 uint8_t expected
[] = { 28, 29, 30, 31, 0, 0, 0 };
174 EXPECT_EQ(0, memcmp(expected
, buf
, sizeof(expected
)));
178 TYPED_TEST(ReadStreamTest
, SeekCur
) {
179 scoped_ptr
<ReadStream
> stream
= ReadStreamTest
<TypeParam
>::CreateStream(100);
180 uint8_t buf
[32] = {0};
184 EXPECT_EQ(0, stream
->Seek(0, SEEK_CUR
));
188 EXPECT_TRUE(stream
->Read(buf
, sizeof(buf
), &bytes_read
));
189 EXPECT_EQ(sizeof(buf
), bytes_read
);
190 for (size_t i
= 0; i
< sizeof(buf
); ++i
) {
191 EXPECT_EQ(i
, buf
[i
]);
193 EXPECT_EQ(32, stream
->Seek(0, SEEK_CUR
));
197 EXPECT_EQ(30, stream
->Seek(-2, SEEK_CUR
));
198 EXPECT_TRUE(stream
->Read(buf
, 3, &bytes_read
));
199 EXPECT_EQ(3u, bytes_read
);
200 uint8_t expected
[] = { 30, 31, 32 };
201 EXPECT_EQ(0, memcmp(expected
, buf
, sizeof(expected
)));
205 EXPECT_EQ(100, stream
->Seek(0, SEEK_END
));
206 EXPECT_EQ(100, stream
->Seek(0, SEEK_CUR
));
211 } // namespace safe_browsing