Merge from Widevine repo of http://go/wvgerrit/47860 This CL updates the copyright notice to indicate that files shared with partners are shared under the Widevine Master License Agreement. bug: 77926774 test: comment change only Change-Id: I0423668111578b80fb39a932d763df2827e2dfc3
151 lines
4.8 KiB
C++
151 lines
4.8 KiB
C++
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine Master
|
|
// License Agreement.
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "file_store.h"
|
|
#include "file_utils.h"
|
|
#include "test_vectors.h"
|
|
|
|
namespace wvcdm {
|
|
|
|
namespace {
|
|
const std::string kTestDirName = "test_dir";
|
|
const std::string kTestFileName = "test.txt";
|
|
const std::string kTestFileName2 = "test2.txt";
|
|
const std::string kTestFileNameExt = ".txt";
|
|
const std::string kWildcard = "*";
|
|
} // namespace
|
|
|
|
class FileUtilsTest : public testing::Test {
|
|
protected:
|
|
FileUtilsTest() {}
|
|
virtual ~FileUtilsTest() {}
|
|
|
|
virtual void SetUp() { CreateTestDir(); }
|
|
virtual void TearDown() { RemoveTestDir(); }
|
|
|
|
void CreateTestDir() {
|
|
if (!file_system.Exists(test_vectors::kTestDir)) {
|
|
EXPECT_TRUE(FileUtils::CreateDirectory(test_vectors::kTestDir));
|
|
}
|
|
EXPECT_TRUE(file_system.Exists(test_vectors::kTestDir));
|
|
}
|
|
|
|
void RemoveTestDir() {
|
|
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
|
|
}
|
|
|
|
std::string GenerateRandomData(uint32_t len) {
|
|
std::string data(len, 0);
|
|
for (size_t i = 0; i < len; i++) {
|
|
data[i] = rand() % 256;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
FileSystem file_system;
|
|
};
|
|
|
|
TEST_F(FileUtilsTest, CreateDirectory) {
|
|
std::string dir_wo_delimiter =
|
|
test_vectors::kTestDir.substr(0, test_vectors::kTestDir.size() - 1);
|
|
if (file_system.Exists(dir_wo_delimiter))
|
|
EXPECT_TRUE(file_system.Remove(dir_wo_delimiter));
|
|
EXPECT_FALSE(file_system.Exists(dir_wo_delimiter));
|
|
EXPECT_TRUE(FileUtils::CreateDirectory(dir_wo_delimiter));
|
|
EXPECT_TRUE(file_system.Exists(dir_wo_delimiter));
|
|
EXPECT_TRUE(file_system.Remove(dir_wo_delimiter));
|
|
EXPECT_TRUE(FileUtils::CreateDirectory(test_vectors::kTestDir));
|
|
EXPECT_TRUE(file_system.Exists(test_vectors::kTestDir));
|
|
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
|
|
}
|
|
|
|
TEST_F(FileUtilsTest, IsDir) {
|
|
std::string path = test_vectors::kTestDir + kTestFileName;
|
|
File* file = file_system.Open(path, FileSystem::kCreate);
|
|
EXPECT_TRUE(file);
|
|
file->Close();
|
|
|
|
EXPECT_TRUE(file_system.Exists(path));
|
|
EXPECT_TRUE(file_system.Exists(test_vectors::kTestDir));
|
|
EXPECT_FALSE(FileUtils::IsDirectory(path));
|
|
EXPECT_TRUE(FileUtils::IsDirectory(test_vectors::kTestDir));
|
|
}
|
|
|
|
TEST_F(FileUtilsTest, IsRegularFile) {
|
|
std::string path = test_vectors::kTestDir + kTestFileName;
|
|
File* file = file_system.Open(path, FileSystem::kCreate);
|
|
EXPECT_TRUE(file);
|
|
file->Close();
|
|
|
|
EXPECT_TRUE(file_system.Exists(path));
|
|
EXPECT_TRUE(file_system.Exists(test_vectors::kTestDir));
|
|
EXPECT_TRUE(FileUtils::IsRegularFile(path));
|
|
EXPECT_FALSE(FileUtils::IsRegularFile(test_vectors::kTestDir));
|
|
}
|
|
|
|
TEST_F(FileUtilsTest, CopyFile) {
|
|
std::string path = test_vectors::kTestDir + kTestFileName;
|
|
file_system.Remove(path);
|
|
|
|
std::string write_data = GenerateRandomData(600);
|
|
File* wr_file = file_system.Open(path, FileSystem::kCreate);
|
|
EXPECT_TRUE(wr_file);
|
|
EXPECT_TRUE(wr_file->Write(write_data.data(), write_data.size()));
|
|
wr_file->Close();
|
|
ASSERT_TRUE(file_system.Exists(path));
|
|
|
|
std::string path_copy = test_vectors::kTestDir + kTestFileName2;
|
|
EXPECT_FALSE(file_system.Exists(path_copy));
|
|
EXPECT_TRUE(FileUtils::Copy(path, path_copy));
|
|
|
|
std::string read_data;
|
|
read_data.resize(file_system.FileSize(path_copy));
|
|
File* rd_file = file_system.Open(path_copy, FileSystem::kReadOnly);
|
|
EXPECT_TRUE(rd_file);
|
|
EXPECT_TRUE(rd_file->Read(&read_data[0], read_data.size()));
|
|
rd_file->Close();
|
|
EXPECT_EQ(write_data, read_data);
|
|
EXPECT_EQ(file_system.FileSize(path), file_system.FileSize(path_copy));
|
|
}
|
|
|
|
TEST_F(FileUtilsTest, ListEmptyDirectory) {
|
|
std::vector<std::string> files;
|
|
EXPECT_TRUE(FileUtils::List(test_vectors::kTestDir, &files));
|
|
EXPECT_EQ(0u, files.size());
|
|
}
|
|
|
|
TEST_F(FileUtilsTest, ListFiles) {
|
|
std::string path = test_vectors::kTestDir + kTestDirName;
|
|
EXPECT_TRUE(FileUtils::CreateDirectory(path));
|
|
|
|
path = test_vectors::kTestDir + kTestFileName;
|
|
std::string write_data = GenerateRandomData(600);
|
|
File* file = file_system.Open(path, FileSystem::kCreate);
|
|
EXPECT_TRUE(file);
|
|
EXPECT_TRUE(file->Write(write_data.data(), write_data.size()));
|
|
file->Close();
|
|
EXPECT_TRUE(file_system.Exists(path));
|
|
|
|
path = test_vectors::kTestDir + kTestFileName2;
|
|
write_data = GenerateRandomData(600);
|
|
file = file_system.Open(path, FileSystem::kCreate);
|
|
EXPECT_TRUE(file);
|
|
EXPECT_TRUE(file->Write(write_data.data(), write_data.size()));
|
|
file->Close();
|
|
EXPECT_TRUE(file_system.Exists(path));
|
|
|
|
std::vector<std::string> files;
|
|
EXPECT_TRUE(FileUtils::List(test_vectors::kTestDir, &files));
|
|
EXPECT_EQ(3u, files.size());
|
|
|
|
for (size_t i = 0; i < files.size(); ++i) {
|
|
EXPECT_TRUE(files[i] == kTestDirName || files[i] == kTestFileName ||
|
|
files[i] == kTestFileName2);
|
|
}
|
|
}
|
|
|
|
} // namespace wvcdm
|