forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_delete_recursive_test.cc
129 lines (98 loc) · 3.67 KB
/
file_delete_recursive_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2020 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <sys/stat.h>
#include <string>
#include "starboard/common/file.h"
#include "starboard/configuration_constants.h"
#include "starboard/nplb/file_helpers.h"
#include "starboard/types.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace nplb {
namespace {
const size_t kDirectoryCount = 3;
const size_t kFileCount = 3;
const char kRoot[] = {"SbFileDeleteRecursiveTest"};
const char* kDirectories[kDirectoryCount] = {
"",
"test1",
"test2",
};
const char* kFiles[kFileCount] = {
"file1",
"test1/file2",
"test2/file3",
};
bool FileExists(const char* path) {
struct stat info;
return stat(path, &info) == 0;
}
bool DirectoryExists(const char* path) {
struct stat info;
return stat(path, &info) == 0 && S_ISDIR(info.st_mode);
}
TEST(SbFileDeleteRecursiveTest, SunnyDayDeleteExistingPath) {
std::string path;
const std::string& tmp = GetTempDir();
// Create the directory tree.
for (size_t i = 0; i < kDirectoryCount; ++i) {
path = tmp + kSbFileSepString + kRoot + kSbFileSepString + kDirectories[i];
EXPECT_FALSE(FileExists(path.c_str()));
EXPECT_TRUE(mkdir(path.c_str(), 0700) == 0 ||
DirectoryExists(path.c_str()));
EXPECT_TRUE(DirectoryExists(path.c_str()));
}
int file = -1;
// Create files in our directory tree.
for (size_t i = 0; i < kFileCount; ++i) {
path = tmp + kSbFileSepString + kRoot + kSbFileSepString + kFiles[i];
EXPECT_FALSE(FileExists(path.c_str()));
file = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
EXPECT_TRUE(file >= 0);
EXPECT_EQ(close(file), 0);
EXPECT_TRUE(FileExists(path.c_str()));
}
path = tmp + kSbFileSepString + kRoot;
EXPECT_TRUE(SbFileDeleteRecursive(path.c_str(), false));
EXPECT_FALSE(FileExists(path.c_str()));
}
TEST(SbFileDeleteRecursiveTest, SunnyDayDeletePreserveRoot) {
const std::string root = GetTempDir() + kSbFileSepString + kRoot;
EXPECT_FALSE(FileExists(root.c_str()));
EXPECT_TRUE(mkdir(root.c_str(), 0700) == 0 || DirectoryExists(root.c_str()));
EXPECT_TRUE(DirectoryExists(root.c_str()));
EXPECT_TRUE(SbFileDeleteRecursive(root.c_str(), true));
EXPECT_TRUE(FileExists(root.c_str()));
EXPECT_TRUE(SbFileDeleteRecursive(root.c_str(), false));
EXPECT_FALSE(FileExists(root.c_str()));
}
TEST(SbFileDeleteRecursiveTest, RainyDayDeleteFileIgnoresPreserveRoot) {
const std::string& path = GetTempDir() + kSbFileSepString + "file1";
EXPECT_FALSE(FileExists(path.c_str()));
int file = -1;
file = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
EXPECT_TRUE(file >= 0);
EXPECT_EQ(close(file), 0);
EXPECT_TRUE(FileExists(path.c_str()));
EXPECT_TRUE(SbFileDeleteRecursive(path.c_str(), true));
EXPECT_FALSE(FileExists(path.c_str()));
}
TEST(SbFileDeleteRecursiveTest, RainyDayNonExistentPathErrors) {
ScopedRandomFile file(ScopedRandomFile::kDontCreate);
EXPECT_FALSE(FileExists(file.filename().c_str()));
EXPECT_FALSE(SbFileDeleteRecursive(file.filename().c_str(), false));
}
} // namespace
} // namespace nplb
} // namespace starboard