This source file includes following definitions.
- GetRandomFilename
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <windows.h>
#include <wincrypt.h>
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/installer/util/self_cleaning_temp_dir.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
std::wstring GetRandomFilename() {
uint8 data[4];
HCRYPTPROV crypt_ctx = NULL;
EXPECT_NE(FALSE, CryptAcquireContext(&crypt_ctx, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT));
EXPECT_NE(FALSE, CryptGenRandom(crypt_ctx, arraysize(data), &data[0]));
EXPECT_NE(FALSE, CryptReleaseContext(crypt_ctx, 0));
std::string result(base::HexEncode(&data[0], arraysize(data)));
EXPECT_EQ(8, result.size());
result[0] = 'R';
return base::ASCIIToWide(result);
}
}
namespace installer {
class SelfCleaningTempDirTest : public testing::Test {
};
TEST_F(SelfCleaningTempDirTest, TopLevel) {
base::FilePath base_dir;
SelfCleaningTempDir::GetTopDirToCreate(base::FilePath(L"C:\\"), &base_dir);
EXPECT_TRUE(base_dir.empty());
}
TEST_F(SelfCleaningTempDirTest, TopLevelPlusOne) {
base::FilePath base_dir;
base::FilePath parent_dir(L"C:\\");
parent_dir = parent_dir.Append(GetRandomFilename());
SelfCleaningTempDir::GetTopDirToCreate(parent_dir, &base_dir);
EXPECT_EQ(parent_dir, base_dir);
}
TEST_F(SelfCleaningTempDirTest, RemoveUnusedOnDelete) {
base::ScopedTempDir work_dir;
EXPECT_TRUE(work_dir.CreateUniqueTempDir());
base::FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
SelfCleaningTempDir temp_dir;
EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three"));
EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path());
EXPECT_TRUE(base::DirectoryExists(temp_dir.path()));
EXPECT_TRUE(temp_dir.Delete());
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.Append(L"Three")));
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir));
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName()));
EXPECT_TRUE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
EXPECT_TRUE(work_dir.Delete());
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
}
TEST_F(SelfCleaningTempDirTest, TwoClients) {
base::ScopedTempDir work_dir;
EXPECT_TRUE(work_dir.CreateUniqueTempDir());
base::FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
SelfCleaningTempDir temp_dir1;
SelfCleaningTempDir temp_dir2;
EXPECT_TRUE(temp_dir1.Initialize(parent_temp_dir, L"Three"));
EXPECT_TRUE(temp_dir2.Initialize(parent_temp_dir, L"Three"));
EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir1.path());
EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir2.path());
EXPECT_TRUE(base::DirectoryExists(temp_dir1.path()));
EXPECT_TRUE(base::DirectoryExists(temp_dir2.path()));
EXPECT_TRUE(temp_dir2.Delete());
EXPECT_FALSE(base::DirectoryExists(temp_dir1.path()));
EXPECT_TRUE(base::DirectoryExists(parent_temp_dir));
EXPECT_TRUE(temp_dir1.Delete());
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.Append(L"Three")));
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir));
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName()));
EXPECT_TRUE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
EXPECT_TRUE(work_dir.Delete());
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
}
TEST_F(SelfCleaningTempDirTest, RemoveUnusedOnDestroy) {
base::ScopedTempDir work_dir;
EXPECT_TRUE(work_dir.CreateUniqueTempDir());
base::FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
{
SelfCleaningTempDir temp_dir;
EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three"));
EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path());
EXPECT_TRUE(base::DirectoryExists(temp_dir.path()));
}
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.Append(L"Three")));
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir));
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName()));
EXPECT_TRUE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
EXPECT_TRUE(work_dir.Delete());
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
}
TEST_F(SelfCleaningTempDirTest, LeaveUsedOnDestroy) {
static const char kHiHon[] = "hi, hon";
base::ScopedTempDir work_dir;
EXPECT_TRUE(work_dir.CreateUniqueTempDir());
base::FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
{
SelfCleaningTempDir temp_dir;
EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three"));
EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path());
EXPECT_TRUE(base::DirectoryExists(temp_dir.path()));
EXPECT_EQ(arraysize(kHiHon) - 1,
base::WriteFile(parent_temp_dir.Append(GetRandomFilename()),
kHiHon, arraysize(kHiHon) - 1));
}
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.Append(L"Three")));
EXPECT_TRUE(base::DirectoryExists(parent_temp_dir));
EXPECT_TRUE(work_dir.Delete());
EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
}
}