Correction to file remove utility
[ Merge of http://go/wvgerrit/84989 ] FileUtils::Remove() when used with wildcards would ignore a prefix specified before the asterisk and delete all files with the same extension. Fix proposed by broadcom. Bug: 120039689 Test: WV unit/integration tests Change-Id: Iddc6c6b1983c41b501b21f34626f56c0b74af6c8
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
// 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 "file_utils.h"
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "file_store.h"
|
||||
#include "file_utils.h"
|
||||
#include "test_vectors.h"
|
||||
|
||||
namespace wvcdm {
|
||||
@@ -15,8 +13,10 @@ namespace {
|
||||
const std::string kTestDirName = "test_dir";
|
||||
const std::string kTestFileName = "test.txt";
|
||||
const std::string kTestFileName2 = "test2.txt";
|
||||
const std::string kTestFileNameWithDifferentExt = "test.log";
|
||||
const std::string kTestFileNameWithDifferentPrefix = "file.txt";
|
||||
const std::string kTestFileNamePrefix = "test";
|
||||
const std::string kTestFileNameExt = ".txt";
|
||||
const std::string kWildcard = "*";
|
||||
} // namespace
|
||||
|
||||
class FileUtilsTest : public testing::Test {
|
||||
@@ -38,6 +38,16 @@ class FileUtilsTest : public testing::Test {
|
||||
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
|
||||
}
|
||||
|
||||
void CreateTestFile(const std::string file_path) {
|
||||
std::string write_data = GenerateRandomData(600);
|
||||
size_t write_data_size = write_data.size();
|
||||
std::unique_ptr<File> file =
|
||||
file_system.Open(file_path, FileSystem::kCreate);
|
||||
EXPECT_TRUE(file);
|
||||
EXPECT_EQ(file->Write(write_data.data(), write_data_size), write_data_size);
|
||||
EXPECT_TRUE(file_system.Exists(file_path));
|
||||
}
|
||||
|
||||
std::string GenerateRandomData(uint32_t len) {
|
||||
std::string data(len, 0);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
@@ -105,7 +115,7 @@ TEST_F(FileUtilsTest, CopyFile) {
|
||||
read_data.resize(file_system.FileSize(path_copy));
|
||||
size_t read_data_size = read_data.size();
|
||||
std::unique_ptr<File> rd_file =
|
||||
file_system.Open(path_copy, FileSystem::kReadOnly);
|
||||
file_system.Open(path_copy, FileSystem::kReadOnly);
|
||||
EXPECT_TRUE(rd_file);
|
||||
EXPECT_EQ(rd_file->Read(&read_data[0], read_data_size), read_data_size);
|
||||
EXPECT_EQ(write_data, read_data);
|
||||
@@ -148,4 +158,157 @@ TEST_F(FileUtilsTest, ListFiles) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveDirectory) {
|
||||
std::string path = test_vectors::kTestDir;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(path));
|
||||
EXPECT_TRUE(FileUtils::Exists(path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(path));
|
||||
EXPECT_FALSE(FileUtils::Exists(path));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveTopLevelDirectory) {
|
||||
std::string base_path = test_vectors::kTestDir;
|
||||
std::string sub_dir_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(sub_dir_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(sub_dir_path));
|
||||
|
||||
std::string file_path_1 = base_path + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = sub_dir_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(base_path));
|
||||
|
||||
EXPECT_FALSE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(sub_dir_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveFilesUsingWildcard) {
|
||||
std::string base_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(base_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
|
||||
std::string file_path_1 = base_path + kDirectoryDelimiter + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = base_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
std::string file_path_with_different_ext =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentExt;
|
||||
CreateTestFile(file_path_with_different_ext);
|
||||
|
||||
std::string file_path_with_different_prefix =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentPrefix;
|
||||
CreateTestFile(file_path_with_different_prefix);
|
||||
|
||||
std::string wildcard_path = base_path + kDirectoryDelimiter + kWildcard;
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(wildcard_path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_with_different_ext));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_with_different_prefix));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveFilesUsingWildcardAndExtension) {
|
||||
std::string base_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(base_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
|
||||
std::string file_path_1 = base_path + kDirectoryDelimiter + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = base_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
std::string file_path_with_different_ext =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentExt;
|
||||
CreateTestFile(file_path_with_different_ext);
|
||||
|
||||
std::string file_path_with_different_prefix =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentPrefix;
|
||||
CreateTestFile(file_path_with_different_prefix);
|
||||
|
||||
std::string wildcard_path =
|
||||
base_path + kDirectoryDelimiter + kWildcard + kTestFileNameExt;
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(wildcard_path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
EXPECT_TRUE(FileUtils::Exists(file_path_with_different_ext));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_with_different_prefix));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveFilesUsingPrefixAndWildcard) {
|
||||
std::string base_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(base_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
|
||||
std::string file_path_1 = base_path + kDirectoryDelimiter + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = base_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
std::string file_path_with_different_ext =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentExt;
|
||||
CreateTestFile(file_path_with_different_ext);
|
||||
|
||||
std::string file_path_with_different_prefix =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentPrefix;
|
||||
CreateTestFile(file_path_with_different_prefix);
|
||||
|
||||
std::string wildcard_path =
|
||||
base_path + kDirectoryDelimiter + kTestFileNamePrefix + kWildcard;
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(wildcard_path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_with_different_ext));
|
||||
EXPECT_TRUE(FileUtils::Exists(file_path_with_different_prefix));
|
||||
}
|
||||
|
||||
TEST_F(FileUtilsTest, RemoveFilesUsingPrefixWildcardAndExtension) {
|
||||
std::string base_path = test_vectors::kTestDir + kTestDirName;
|
||||
EXPECT_TRUE(FileUtils::CreateDirectory(base_path));
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
|
||||
std::string file_path_1 = base_path + kDirectoryDelimiter + kTestFileName;
|
||||
CreateTestFile(file_path_1);
|
||||
|
||||
std::string file_path_2 = base_path + kDirectoryDelimiter + kTestFileName2;
|
||||
CreateTestFile(file_path_2);
|
||||
|
||||
std::string file_path_with_different_ext =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentExt;
|
||||
CreateTestFile(file_path_with_different_ext);
|
||||
|
||||
std::string file_path_with_different_prefix =
|
||||
base_path + kDirectoryDelimiter + kTestFileNameWithDifferentPrefix;
|
||||
CreateTestFile(file_path_with_different_prefix);
|
||||
|
||||
std::string wildcard_path = base_path + kDirectoryDelimiter +
|
||||
kTestFileNamePrefix + kWildcard +
|
||||
kTestFileNameExt;
|
||||
|
||||
EXPECT_TRUE(FileUtils::Remove(wildcard_path));
|
||||
|
||||
EXPECT_TRUE(FileUtils::Exists(base_path));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_1));
|
||||
EXPECT_FALSE(FileUtils::Exists(file_path_2));
|
||||
EXPECT_TRUE(FileUtils::Exists(file_path_with_different_ext));
|
||||
EXPECT_TRUE(FileUtils::Exists(file_path_with_different_prefix));
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
Reference in New Issue
Block a user