2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-08-21 22:40:44 +00:00
|
|
|
#include <gtest/gtest.h>
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/file_util.h"
|
|
|
|
#include "packager/media/file/file.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
const int kDataSize = 1024;
|
|
|
|
}
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-10-11 21:44:55 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class LocalFileTest : public testing::Test {
|
|
|
|
protected:
|
|
|
|
virtual void SetUp() {
|
|
|
|
data_.resize(kDataSize);
|
|
|
|
for (int i = 0; i < kDataSize; ++i)
|
|
|
|
data_[i] = i % 256;
|
|
|
|
|
|
|
|
// Test file path for file_util API.
|
2015-07-22 07:23:11 +00:00
|
|
|
ASSERT_TRUE(base::CreateTemporaryFile(&test_file_path_));
|
|
|
|
local_file_name_no_prefix_ = test_file_path_.value();
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
// Local file name with prefix for File API.
|
|
|
|
local_file_name_ = kLocalFilePrefix;
|
2015-07-22 07:23:11 +00:00
|
|
|
local_file_name_ += local_file_name_no_prefix_;
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void TearDown() {
|
|
|
|
// Remove test file if created.
|
2015-07-22 07:23:11 +00:00
|
|
|
base::DeleteFile(base::FilePath(local_file_name_no_prefix_), false);
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string data_;
|
2015-07-22 07:23:11 +00:00
|
|
|
|
|
|
|
// Path to the temporary file for this test.
|
2013-10-11 21:44:55 +00:00
|
|
|
base::FilePath test_file_path_;
|
2015-07-22 07:23:11 +00:00
|
|
|
// Same as |test_file_path_| but in string form.
|
|
|
|
std::string local_file_name_no_prefix_;
|
|
|
|
|
|
|
|
// Same as |local_file_name_no_prefix_| but with the file prefix.
|
2013-10-11 21:44:55 +00:00
|
|
|
std::string local_file_name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(LocalFileTest, ReadNotExist) {
|
|
|
|
// Remove test file if it exists.
|
2015-07-22 07:23:11 +00:00
|
|
|
base::DeleteFile(base::FilePath(local_file_name_no_prefix_), false);
|
2013-10-11 21:44:55 +00:00
|
|
|
ASSERT_TRUE(File::Open(local_file_name_.c_str(), "r") == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LocalFileTest, Size) {
|
|
|
|
ASSERT_EQ(kDataSize,
|
|
|
|
file_util::WriteFile(test_file_path_, data_.data(), kDataSize));
|
|
|
|
ASSERT_EQ(kDataSize, File::GetFileSize(local_file_name_.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LocalFileTest, Write) {
|
|
|
|
// Write file using File API.
|
|
|
|
File* file = File::Open(local_file_name_.c_str(), "w");
|
|
|
|
ASSERT_TRUE(file != NULL);
|
|
|
|
EXPECT_EQ(kDataSize, file->Write(&data_[0], kDataSize));
|
|
|
|
EXPECT_EQ(kDataSize, file->Size());
|
|
|
|
EXPECT_TRUE(file->Close());
|
|
|
|
|
|
|
|
// Read file using file_util API.
|
|
|
|
std::string read_data(kDataSize, 0);
|
|
|
|
ASSERT_EQ(kDataSize,
|
2014-02-26 23:55:01 +00:00
|
|
|
base::ReadFile(test_file_path_, &read_data[0], kDataSize));
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
// Compare data written and read.
|
|
|
|
EXPECT_EQ(data_, read_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LocalFileTest, Read_And_Eof) {
|
|
|
|
// Write file using file_util API.
|
|
|
|
ASSERT_EQ(kDataSize,
|
|
|
|
file_util::WriteFile(test_file_path_, data_.data(), kDataSize));
|
|
|
|
|
|
|
|
// Read file using File API.
|
|
|
|
File* file = File::Open(local_file_name_.c_str(), "r");
|
|
|
|
ASSERT_TRUE(file != NULL);
|
|
|
|
|
2015-03-20 22:32:59 +00:00
|
|
|
// Read half of the file.
|
2013-10-11 21:44:55 +00:00
|
|
|
const int kFirstReadBytes = kDataSize / 2;
|
|
|
|
std::string read_data(kFirstReadBytes + kDataSize, 0);
|
|
|
|
EXPECT_EQ(kFirstReadBytes, file->Read(&read_data[0], kFirstReadBytes));
|
|
|
|
|
2015-03-20 22:32:59 +00:00
|
|
|
// Read the remaining half of the file and verify EOF.
|
2013-10-11 21:44:55 +00:00
|
|
|
EXPECT_EQ(kDataSize - kFirstReadBytes,
|
|
|
|
file->Read(&read_data[kFirstReadBytes], kDataSize));
|
2015-03-20 22:32:59 +00:00
|
|
|
uint8_t single_byte;
|
|
|
|
EXPECT_EQ(0, file->Read(&single_byte, sizeof(single_byte)));
|
2013-10-11 21:44:55 +00:00
|
|
|
EXPECT_TRUE(file->Close());
|
|
|
|
|
|
|
|
// Compare data written and read.
|
|
|
|
read_data.resize(kDataSize);
|
|
|
|
EXPECT_EQ(data_, read_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LocalFileTest, WriteRead) {
|
|
|
|
// Write file using File API, using file name directly (without prefix).
|
2015-07-22 07:23:11 +00:00
|
|
|
File* file = File::Open(local_file_name_no_prefix_.c_str(), "w");
|
2013-10-11 21:44:55 +00:00
|
|
|
ASSERT_TRUE(file != NULL);
|
|
|
|
EXPECT_EQ(kDataSize, file->Write(&data_[0], kDataSize));
|
|
|
|
EXPECT_EQ(kDataSize, file->Size());
|
|
|
|
EXPECT_TRUE(file->Close());
|
|
|
|
|
|
|
|
// Read file using File API, using local file prefix + file name.
|
|
|
|
file = File::Open(local_file_name_.c_str(), "r");
|
|
|
|
ASSERT_TRUE(file != NULL);
|
|
|
|
|
|
|
|
// Read half of the file and verify that Eof is not true.
|
|
|
|
std::string read_data(kDataSize, 0);
|
|
|
|
EXPECT_EQ(kDataSize, file->Read(&read_data[0], kDataSize));
|
|
|
|
EXPECT_TRUE(file->Close());
|
|
|
|
|
|
|
|
// Compare data written and read.
|
|
|
|
EXPECT_EQ(data_, read_data);
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:10:42 +00:00
|
|
|
TEST_F(LocalFileTest, WriteFlushCheckSize) {
|
|
|
|
const uint32 kNumCycles(10);
|
|
|
|
const uint32 kNumWrites(10);
|
|
|
|
|
|
|
|
for (uint32 cycle_idx = 0; cycle_idx < kNumCycles; ++cycle_idx) {
|
|
|
|
// Write file using File API, using file name directly (without prefix).
|
|
|
|
File* file = File::Open(local_file_name_no_prefix_.c_str(), "w");
|
|
|
|
ASSERT_TRUE(file != NULL);
|
|
|
|
for (uint32 write_idx = 0; write_idx < kNumWrites; ++write_idx)
|
|
|
|
EXPECT_EQ(kDataSize, file->Write(data_.data(), kDataSize));
|
|
|
|
ASSERT_NO_FATAL_FAILURE(file->Flush());
|
|
|
|
EXPECT_TRUE(file->Close());
|
|
|
|
|
|
|
|
file = File::Open(local_file_name_.c_str(), "r");
|
|
|
|
ASSERT_TRUE(file != NULL);
|
|
|
|
EXPECT_EQ(static_cast<int64_t>(data_.size() * kNumWrites), file->Size());
|
|
|
|
|
|
|
|
EXPECT_TRUE(file->Close());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|