2015-12-22 00:20:34 +00:00
|
|
|
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/memory_file.h"
|
2015-12-22 00:20:34 +00:00
|
|
|
#include <gtest/gtest.h>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file.h"
|
|
|
|
#include "packager/file/file_closer.h"
|
2015-12-22 00:20:34 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-12-22 00:20:34 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const uint8_t kWriteBuffer[] = {1, 2, 3, 4, 5, 6, 7, 8};
|
|
|
|
const int64_t kWriteBufferSize = sizeof(kWriteBuffer);
|
|
|
|
|
2017-07-10 18:26:22 +00:00
|
|
|
} // namespace
|
2015-12-22 00:20:34 +00:00
|
|
|
|
|
|
|
class MemoryFileTest : public testing::Test {
|
|
|
|
protected:
|
2017-07-10 18:26:22 +00:00
|
|
|
void TearDown() override { MemoryFile::DeleteAll(); }
|
2015-12-22 00:20:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(MemoryFileTest, ModifiesSameFile) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> writer(File::Open("memory://file1", "w"));
|
2015-12-22 00:33:55 +00:00
|
|
|
ASSERT_TRUE(writer);
|
2015-12-22 00:20:34 +00:00
|
|
|
ASSERT_EQ(kWriteBufferSize, writer->Write(kWriteBuffer, kWriteBufferSize));
|
|
|
|
|
|
|
|
// Since File::Open should not create a ThreadedIoFile so there should be
|
|
|
|
// no cache.
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> reader(File::Open("memory://file1", "r"));
|
2015-12-22 00:33:55 +00:00
|
|
|
ASSERT_TRUE(reader);
|
2015-12-22 00:20:34 +00:00
|
|
|
|
|
|
|
uint8_t read_buffer[kWriteBufferSize];
|
|
|
|
ASSERT_EQ(kWriteBufferSize, reader->Read(read_buffer, kWriteBufferSize));
|
|
|
|
EXPECT_EQ(0, memcmp(kWriteBuffer, read_buffer, kWriteBufferSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryFileTest, SupportsDifferentFiles) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> writer(File::Open("memory://file1", "w"));
|
|
|
|
std::unique_ptr<File, FileCloser> reader(File::Open("memory://file2", "w"));
|
2015-12-22 00:33:55 +00:00
|
|
|
ASSERT_TRUE(writer);
|
|
|
|
ASSERT_TRUE(reader);
|
2015-12-22 00:20:34 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(kWriteBufferSize, writer->Write(kWriteBuffer, kWriteBufferSize));
|
|
|
|
ASSERT_EQ(0, reader->Size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryFileTest, SeekAndTell) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> file(File::Open("memory://file1", "w"));
|
2015-12-22 00:33:55 +00:00
|
|
|
ASSERT_TRUE(file);
|
|
|
|
|
2015-12-22 00:20:34 +00:00
|
|
|
ASSERT_EQ(kWriteBufferSize, file->Write(kWriteBuffer, kWriteBufferSize));
|
|
|
|
ASSERT_TRUE(file->Seek(0));
|
|
|
|
|
|
|
|
const int64_t seek_pos = kWriteBufferSize / 2;
|
|
|
|
ASSERT_TRUE(file->Seek(seek_pos));
|
|
|
|
|
|
|
|
uint64_t size;
|
|
|
|
ASSERT_TRUE(file->Tell(&size));
|
|
|
|
EXPECT_EQ(seek_pos, static_cast<int64_t>(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryFileTest, EndOfFile) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> file(File::Open("memory://file1", "w"));
|
2015-12-22 00:33:55 +00:00
|
|
|
ASSERT_TRUE(file);
|
|
|
|
|
2015-12-22 00:20:34 +00:00
|
|
|
ASSERT_EQ(kWriteBufferSize, file->Write(kWriteBuffer, kWriteBufferSize));
|
|
|
|
ASSERT_TRUE(file->Seek(0));
|
|
|
|
|
|
|
|
uint8_t read_buffer[kWriteBufferSize];
|
|
|
|
const int64_t seek_pos = kWriteBufferSize / 2;
|
|
|
|
const int64_t read_size = kWriteBufferSize - seek_pos;
|
|
|
|
ASSERT_TRUE(file->Seek(seek_pos));
|
|
|
|
EXPECT_EQ(read_size, file->Read(read_buffer, kWriteBufferSize));
|
|
|
|
EXPECT_EQ(0, memcmp(read_buffer, kWriteBuffer + seek_pos, read_size));
|
|
|
|
EXPECT_EQ(0, file->Read(read_buffer, kWriteBufferSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryFileTest, ExtendsSize) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> file(File::Open("memory://file1", "w"));
|
2015-12-22 00:33:55 +00:00
|
|
|
ASSERT_TRUE(file);
|
2015-12-22 00:20:34 +00:00
|
|
|
ASSERT_EQ(kWriteBufferSize, file->Write(kWriteBuffer, kWriteBufferSize));
|
|
|
|
|
|
|
|
ASSERT_EQ(kWriteBufferSize, file->Size());
|
|
|
|
ASSERT_EQ(kWriteBufferSize, file->Write(kWriteBuffer, kWriteBufferSize));
|
|
|
|
EXPECT_EQ(2 * kWriteBufferSize, file->Size());
|
|
|
|
|
|
|
|
uint64_t size;
|
|
|
|
ASSERT_TRUE(file->Tell(&size));
|
|
|
|
EXPECT_EQ(2 * kWriteBufferSize, static_cast<int64_t>(size));
|
|
|
|
}
|
|
|
|
|
2015-12-22 00:33:55 +00:00
|
|
|
TEST_F(MemoryFileTest, ReadMissingFileFails) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> file(File::Open("memory://file1", "r"));
|
2015-12-22 00:33:55 +00:00
|
|
|
EXPECT_FALSE(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryFileTest, WriteExistingFileDeletes) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> file1(File::Open("memory://file1", "w"));
|
2015-12-22 00:33:55 +00:00
|
|
|
ASSERT_TRUE(file1);
|
|
|
|
ASSERT_EQ(kWriteBufferSize, file1->Write(kWriteBuffer, kWriteBufferSize));
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> file2(File::Open("memory://file1", "w"));
|
2015-12-22 00:33:55 +00:00
|
|
|
ASSERT_TRUE(file2);
|
|
|
|
EXPECT_EQ(0, file2->Size());
|
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|