1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "components/paint_preview/common/file_stream.h"
#include "base/files/scoped_temp_dir.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace paint_preview {
// Test reading and writing from a file.
TEST(PaintPreviewFileStreamTest, TestWriteRead) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path = temp_dir.GetPath().AppendASCII("test_file");
std::vector<uint8_t> test_data = {0, 1, 2, 3, 4, 5, 8, 9};
base::File write_file(
file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
FileWStream wstream(std::move(write_file));
EXPECT_TRUE(wstream.write(test_data.data(), test_data.size()));
wstream.flush();
wstream.Close();
EXPECT_EQ(wstream.bytesWritten(), test_data.size());
EXPECT_EQ(wstream.ActualBytesWritten(), test_data.size());
EXPECT_FALSE(wstream.DidWriteFail());
base::File read_file(file_path, base::File::FLAG_OPEN |
base::File::FLAG_READ |
base::File::FLAG_WIN_EXCLUSIVE_READ);
FileRStream rstream(std::move(read_file));
EXPECT_FALSE(rstream.isAtEnd());
std::vector<uint8_t> read_data(test_data.size(), 0xFF);
EXPECT_EQ(rstream.read(read_data.data(), read_data.size()), read_data.size());
EXPECT_THAT(read_data,
testing::ElementsAreArray(test_data.begin(), test_data.end()));
EXPECT_TRUE(rstream.isAtEnd());
}
// Test writing failure.
TEST(PaintPreviewFileStreamTest, TestWriteFail) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path = temp_dir.GetPath().AppendASCII("test_file");
std::vector<uint8_t> test_data = {0, 1, 2, 3, 4, 5, 8, 9};
base::File bad_write_file(
file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ);
FileWStream wstream(std::move(bad_write_file));
EXPECT_FALSE(wstream.write(test_data.data(), test_data.size()));
EXPECT_EQ(wstream.bytesWritten(), test_data.size());
EXPECT_EQ(wstream.ActualBytesWritten(), 0U);
EXPECT_TRUE(wstream.DidWriteFail());
}
// Test writing beyond max.
TEST(PaintPreviewFileStreamTest, TestWriteFailCapped) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path = temp_dir.GetPath().AppendASCII("test_file");
std::vector<uint8_t> test_data = {0, 1, 2, 3, 4, 5, 8, 9};
base::File write_file(
file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
FileWStream wstream(std::move(write_file), test_data.size());
EXPECT_TRUE(wstream.write(test_data.data(), test_data.size()));
EXPECT_FALSE(wstream.DidWriteFail());
EXPECT_EQ(wstream.bytesWritten(), test_data.size());
EXPECT_EQ(wstream.ActualBytesWritten(), test_data.size());
EXPECT_FALSE(wstream.write(test_data.data(), test_data.size()));
EXPECT_EQ(wstream.bytesWritten(), test_data.size() * 2);
EXPECT_EQ(wstream.ActualBytesWritten(), test_data.size());
EXPECT_TRUE(wstream.DidWriteFail());
}
// Test writing beyond max on first write.
TEST(PaintPreviewFileStreamTest, TestWriteFailCappedFirstWrite) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path = temp_dir.GetPath().AppendASCII("test_file");
std::vector<uint8_t> test_data = {0, 1, 2, 3, 4, 5, 8, 9};
base::File write_file(
file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
FileWStream wstream(std::move(write_file), 4);
EXPECT_FALSE(wstream.write(test_data.data(), test_data.size()));
EXPECT_TRUE(wstream.DidWriteFail());
}
// Test writing beyond max (with overflow).
TEST(PaintPreviewFileStreamTest, TestWriteFailOverflow) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path = temp_dir.GetPath().AppendASCII("test_file");
std::vector<uint8_t> test_data = {0, 1, 2, 3, 4, 5, 8, 9};
base::File write_file(
file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
FileWStream wstream(std::move(write_file),
std::numeric_limits<size_t>::max());
EXPECT_TRUE(wstream.write(test_data.data(), test_data.size()));
EXPECT_FALSE(wstream.DidWriteFail());
EXPECT_FALSE(
wstream.write(test_data.data(), std::numeric_limits<size_t>::max()));
EXPECT_TRUE(wstream.DidWriteFail());
}
// Test reading to skip.
TEST(PaintPreviewFileStreamTest, TestSkip) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path = temp_dir.GetPath().AppendASCII("test_file");
std::vector<uint8_t> test_data = {0, 1, 2, 3, 4, 5, 8, 9};
base::File write_file(
file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
FileWStream wstream(std::move(write_file));
// Write half the data.
EXPECT_TRUE(wstream.write(test_data.data(), 4));
EXPECT_EQ(wstream.bytesWritten(), 4U);
EXPECT_TRUE(wstream.write(test_data.data() + 4, 4));
EXPECT_EQ(wstream.bytesWritten(), test_data.size());
wstream.Close();
base::File read_file(file_path, base::File::FLAG_OPEN |
base::File::FLAG_READ |
base::File::FLAG_WIN_EXCLUSIVE_READ);
FileRStream rstream(std::move(read_file));
EXPECT_FALSE(rstream.isAtEnd());
EXPECT_EQ(rstream.read(nullptr, test_data.size()), test_data.size());
EXPECT_TRUE(rstream.isAtEnd());
}
// Test read and skip.
TEST(PaintPreviewFileStreamTest, TestReadAndSkip) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path = temp_dir.GetPath().AppendASCII("test_file");
std::vector<uint8_t> test_data = {0, 1, 2, 3, 4, 5, 8, 9};
base::File write_file(
file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
FileWStream wstream(std::move(write_file));
EXPECT_TRUE(wstream.write(test_data.data(), test_data.size()));
wstream.Close();
base::File read_file(file_path, base::File::FLAG_OPEN |
base::File::FLAG_READ |
base::File::FLAG_WIN_EXCLUSIVE_READ);
const size_t kSkipBytes = 3;
FileRStream rstream(std::move(read_file));
EXPECT_FALSE(rstream.isAtEnd());
EXPECT_EQ(rstream.read(nullptr, kSkipBytes), kSkipBytes);
EXPECT_FALSE(rstream.isAtEnd());
EXPECT_EQ(rstream.read(nullptr, kSkipBytes), kSkipBytes);
EXPECT_FALSE(rstream.isAtEnd());
const size_t kReadBytes = test_data.size() - kSkipBytes * 2;
std::vector<uint8_t> read_data(kReadBytes, 0xFF);
EXPECT_EQ(rstream.read(read_data.data(), read_data.size()), kReadBytes);
EXPECT_THAT(read_data,
testing::ElementsAreArray(test_data.begin() + kSkipBytes * 2,
test_data.end()));
EXPECT_TRUE(rstream.isAtEnd());
}
// Test reading failure.
TEST(PaintPreviewFileStreamTest, TestReadFail) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path = temp_dir.GetPath().AppendASCII("test_file");
std::vector<uint8_t> test_data = {0, 1, 2, 3, 4, 5, 8, 9};
base::File write_file(
file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
FileWStream wstream(std::move(write_file));
EXPECT_TRUE(wstream.write(test_data.data(), test_data.size()));
base::File read_file(file_path,
base::File::FLAG_OPEN | base::File::FLAG_WRITE);
FileRStream rstream(std::move(read_file));
EXPECT_FALSE(rstream.isAtEnd());
std::vector<uint8_t> read_data(test_data.size(), 0xFF);
EXPECT_EQ(rstream.read(read_data.data(), read_data.size()), 0U);
std::vector<uint8_t> expected(test_data.size(), 0xFF);
EXPECT_THAT(read_data,
testing::ElementsAreArray(expected.begin(), expected.end()));
}
} // namespace paint_preview
|