forked from youtube/cobalt_sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_helpers.cc
161 lines (139 loc) · 4.9 KB
/
file_helpers.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2015 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 "starboard/nplb/file_helpers.h"
#include <sstream>
#include <string>
#include <vector>
#include "starboard/configuration_constants.h"
#include "starboard/file.h"
#include "starboard/system.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace nplb {
namespace {
// Size of appropriate path buffer.
const size_t kPathSize = kSbFileMaxPath + 1;
} // namespace
std::string GetTempDir() {
// It seems there's absolutely no way to get to std::string without a copy.
std::vector<char> path(kPathSize, 0);
if (!SbSystemGetPath(kSbSystemPathTempDirectory, path.data(), path.size())) {
return "";
}
return path.data();
}
std::string GetFileTestsDataDir() {
std::vector<char> content_path(kPathSize);
EXPECT_TRUE(SbSystemGetPath(kSbSystemPathContentDirectory,
content_path.data(), kPathSize));
std::string directory_path =
std::string(content_path.data()) + kSbFileSepChar + "test" +
kSbFileSepChar + "starboard" + kSbFileSepChar + "nplb" +
kSbFileSepChar + "file_tests";
SB_CHECK(SbDirectoryCanOpen(directory_path.c_str()));
return directory_path;
}
// Make a vector of absolute paths in our test data from a null-terminated array
// of C-strings with the relative paths. Slashes are converted to the platform's
// delimiter.
std::vector<std::string> MakePathsVector(const char* files[]) {
std::string directory_path = GetFileTestsDataDir();
std::vector<std::string> paths;
for (int i = 0; files[i] != nullptr; i++) {
std::string file_path = directory_path + kSbFileSepChar + files[i];
std::replace(file_path.begin(), file_path.end(), '/', kSbFileSepChar);
paths.push_back(file_path);
}
return paths;
}
std::vector<std::string> GetFileTestsFilePaths() {
const char* kFiles[] = {
// This long file MUST be first -- SbFileSeekTest depends on it!
"file_with_long_name_and_contents_for_seek_testing_1234567890",
"file01",
"dir_with_files/file11",
"dir_with_files/file12",
"dir_with_only_subdir/dir_with_files/file21",
"dir_with_only_subdir/dir_with_files/file22",
nullptr
};
return MakePathsVector(kFiles);
}
std::vector<std::string> GetFileTestsDirectoryPaths() {
const char* kDirs[] = {
"dir_with_files",
"dir_with_only_subdir",
"dir_with_only_subdir/dir_with_files",
nullptr
};
return MakePathsVector(kDirs);
}
std::string GetTestFileExpectedContent(const std::string& path) {
// The test file content matches the basename of the file + a newline.
std::string content(path, path.find_last_of(kSbFileSepChar) + 1);
content += '\n';
return content;
}
// static
std::string ScopedRandomFile::MakeRandomFilename() {
std::ostringstream filename_stream;
filename_stream << "ScopedRandomFile.File_" << SbSystemGetRandomUInt64();
return filename_stream.str();
}
// static
void ScopedRandomFile::ExpectPattern(int pattern_offset,
void* buffer,
int size,
int line) {
char* char_buffer = static_cast<char*>(buffer);
for (int i = 0; i < size; ++i) {
EXPECT_EQ(static_cast<char>((i + pattern_offset) & 0xFF), char_buffer[i])
<< "original line=" << line;
}
}
// static
std::string ScopedRandomFile::MakeRandomFilePath() {
std::ostringstream filename_stream;
filename_stream << GetTempDir();
if (!filename_stream.tellp()) {
return "";
}
filename_stream << kSbFileSepChar << MakeRandomFilename();
return filename_stream.str();
}
std::string ScopedRandomFile::MakeRandomFile(int length) {
std::string filename = MakeRandomFilePath();
if (filename.empty()) {
return filename;
}
SbFile file = SbFileOpen(filename.c_str(), kSbFileCreateOnly | kSbFileWrite,
NULL, NULL);
EXPECT_TRUE(SbFileIsValid(file));
if (!SbFileIsValid(file)) {
return "";
}
char* data = new char[length];
for (int i = 0; i < length; ++i) {
data[i] = static_cast<char>(i & 0xFF);
}
int bytes = SbFileWriteAll(file, data, length);
EXPECT_EQ(bytes, length) << "Failed to write " << length << " bytes to "
<< filename;
bool result = SbFileClose(file);
EXPECT_TRUE(result) << "Failed to close " << filename;
delete[] data;
return filename;
}
} // namespace nplb
} // namespace starboard