Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / utility / safe_browsing / mac / read_stream_unittest.cc
blobb1d3b78f58e8cc7b3d14986c053c088f136f2a3c
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"
7 #include <vector>
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 {
19 namespace dmg {
21 struct MemoryReadStreamTest {
22 void SetUp() {}
24 const char* TestName() {
25 return "MemoryReadStream";
28 std::vector<uint8_t> data;
31 struct FileReadStreamTest {
32 void SetUp() {
33 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
36 const char* TestName() {
37 return "FileReadStream";
40 base::ScopedTempDir temp_dir;
41 base::File file;
44 template <typename T>
45 class ReadStreamTest : public testing::Test {
46 protected:
47 void SetUp() override {
48 test_helper_.SetUp();
51 void TearDown() override {
52 if (HasFailure())
53 ADD_FAILURE() << "Failing type is " << test_helper_.TestName();
56 scoped_ptr<ReadStream> CreateStream(size_t data_size);
58 private:
59 T test_helper_;
62 template <>
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));
73 template <>
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";
81 return nullptr;
84 for (size_t i = 0; i < data_size; ++i) {
85 char value = i % 255;
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";
95 return nullptr;
98 return make_scoped_ptr(
99 new FileReadStream(test_helper_.file.GetPlatformFile()));
102 using ReadStreamImpls = testing::Types<MemoryReadStreamTest,
103 FileReadStreamTest>;
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};
109 size_t bytes_read;
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};
139 size_t bytes_read;
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};
161 size_t bytes_read;
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};
181 size_t bytes_read;
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));
210 } // namespace dmg
211 } // namespace safe_browsing