Skip to content

Commit

Permalink
Fix file tests for cobalt. (#2252)
Browse files Browse the repository at this point in the history
b/316404107
  • Loading branch information
aee-google authored Feb 28, 2024
1 parent 6266533 commit 7c115b0
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 47 deletions.
25 changes: 12 additions & 13 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ component("base") {
"debug/debugger_starboard.cc",
"debug/stack_trace_starboard.cc",
"files/file_enumerator_starboard.cc",
"files/file_path_watcher_stub.cc",
"files/file_starboard.cc",
"files/file_util_starboard.cc",
"files/memory_mapped_file_starboard.cc",
Expand All @@ -972,6 +973,7 @@ component("base") {
"message_loop/message_pump_io_starboard.h",
"message_loop/message_pump_ui_starboard.cc",
"message_loop/message_pump_ui_starboard.h",
"process/launch_starboard.cc",
"process/memory_starboard.cc",
"process/process_starboard.cc",
"profiler/module_cache_starboard.cc",
Expand Down Expand Up @@ -1742,12 +1744,6 @@ component("base") {
]
}

if (use_cobalt_customizations) {
sources += [
"process/launch_starboard.cc",
]
}

if (!use_cobalt_customizations && is_posix && !is_android) {
sources += [ "debug/stack_trace_posix.cc" ]
}
Expand Down Expand Up @@ -3231,16 +3227,15 @@ test("base_unittests") {
"environment_unittest.cc",
"feature_list_unittest.cc",

# "files/file_enumerator_unittest.cc",
"files/file_enumerator_unittest.cc",
"files/file_error_or_unittest.cc",

# "files/file_path_unittest.cc",
"files/file_path_unittest.cc",

# "files/file_path_watcher_unittest.cc",
"files/file_proxy_unittest.cc",

# "files/file_unittest.cc",
# "files/file_util_unittest.cc",
"files/file_path_watcher_unittest.cc",
# "files/file_proxy_unittest.cc",
"files/file_unittest.cc",
"files/file_util_unittest.cc",
"files/important_file_writer_cleaner_unittest.cc",
"files/important_file_writer_unittest.cc",
"files/memory_mapped_file_unittest.cc",
Expand Down Expand Up @@ -4123,6 +4118,10 @@ test("base_unittests") {
sources += [ "files/file_locking_unittest.cc" ]
}

if (use_cobalt_customizations) {
sources -= [ "files/file_path_watcher_unittest.cc" ]
}

if (is_android) {
deps += [ "//testing/android/native_test:native_test_native_code" ]
sources += [
Expand Down
1 change: 1 addition & 0 deletions base/files/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ class BASE_EXPORT File {
bool async_ = false;

#if defined(STARBOARD)
bool delete_on_close_;
std::string file_name_;
bool append_ = false;
#endif
Expand Down
8 changes: 6 additions & 2 deletions base/files/file_enumerator_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ FileEnumerator::~FileEnumerator() = default;
std::vector<FileEnumerator::FileInfo> FileEnumerator::ReadDirectory(
const FilePath& source) {
internal::AssertBlockingAllowed();
SbDirectory dir = SbDirectoryOpen(source.value().c_str(), NULL);
SbFileError error;
SbDirectory dir = SbDirectoryOpen(source.value().c_str(), &error);
if (!SbDirectoryIsValid(dir)) {
error_ = static_cast<File::Error>(error);
return std::vector<FileEnumerator::FileInfo>();
}

Expand Down Expand Up @@ -189,7 +191,8 @@ FilePath FileEnumerator::Next() {
}

if ((file_info.sb_info_.is_directory && (file_type_ & DIRECTORIES)) ||
(!file_info.sb_info_.is_directory && (file_type_ & FILES))) {
(!file_info.sb_info_.is_directory && (file_type_ & FILES)) ||
(file_type_ & NAMES_ONLY)) {
directory_entries_.push_back(file_info);
}
}
Expand All @@ -200,6 +203,7 @@ FilePath FileEnumerator::Next() {
}

FileEnumerator::FileInfo FileEnumerator::GetInfo() const {
DCHECK(!(file_type_ & FileType::NAMES_ONLY));
return directory_entries_[current_directory_entry_];
}

Expand Down
14 changes: 13 additions & 1 deletion base/files/file_enumerator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ TEST(FileEnumerator, SingleFileInFolderForDirSearch) {
}
}

// Starboard does not support patterns.
#if !defined(STARBOARD)
TEST(FileEnumerator, SingleFileInFolderWithFiltering) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Expand All @@ -202,7 +204,10 @@ TEST(FileEnumerator, SingleFileInFolderWithFiltering) {
EXPECT_THAT(files, IsEmpty());
}
}
#endif // !defined(STARBOARD)

// Starboard does not support patterns.
#if !defined(STARBOARD)
TEST(FileEnumerator, TwoFilesInFolder) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Expand Down Expand Up @@ -231,6 +236,7 @@ TEST(FileEnumerator, TwoFilesInFolder) {
EXPECT_THAT(files, UnorderedElementsAre(foo_txt, bar_txt));
}
}
#endif // !defined(STARBOARD)

TEST(FileEnumerator, SingleFolderInFolderForFileSearch) {
ScopedTempDir temp_dir;
Expand Down Expand Up @@ -264,6 +270,8 @@ TEST(FileEnumerator, SingleFolderInFolderForDirSearch) {
}
}

// Starboard does not support patterns.
#if !defined(STARBOARD)
TEST(FileEnumerator, TwoFoldersInFolder) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Expand All @@ -285,6 +293,7 @@ TEST(FileEnumerator, TwoFoldersInFolder) {
EXPECT_THAT(files, ElementsAre(subdir_foo));
}
}
#endif // !defined(STARBOARD)

TEST(FileEnumerator, FolderAndFileInFolder) {
ScopedTempDir temp_dir;
Expand Down Expand Up @@ -354,6 +363,8 @@ TEST(FileEnumerator, FileInSubfolder) {
}
}

// Starboard does not support patterns.
#if !defined(STARBOARD)
TEST(FileEnumerator, FilesInSubfoldersWithFiltering) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Expand Down Expand Up @@ -391,6 +402,7 @@ TEST(FileEnumerator, FilesInSubfoldersWithFiltering) {
FileEnumerator::FolderSearchPolicy::ALL);
EXPECT_THAT(files, UnorderedElementsAre(subdir_foo, foo_foo, bar_foo));
}
#endif // !defined(STARBOARD)

TEST(FileEnumerator, InvalidDirectory) {
ScopedTempDir temp_dir;
Expand All @@ -415,7 +427,7 @@ TEST(FileEnumerator, InvalidDirectory) {
#endif
}

#if BUILDFLAG(IS_POSIX)
#if BUILDFLAG(IS_POSIX) && !defined(STARBOARD)
TEST(FileEnumerator, SymLinkLoops) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Expand Down
2 changes: 1 addition & 1 deletion base/files/file_path_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ TEST_F(FilePathTest, Append) {
// handle the case when AppendASCII is passed UTF8
#if BUILDFLAG(IS_WIN)
std::string ascii = WideToUTF8(leaf);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || defined(STARBOARD)
std::string ascii = leaf;
#endif
observed_str = root.AppendASCII(ascii);
Expand Down
27 changes: 19 additions & 8 deletions base/files/file_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

#include "base/files/file_starboard.h"

#include <errno.h>

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
#include "starboard/common/metrics/stats_tracker.h"
#include "starboard/common/log.h"
Expand Down Expand Up @@ -63,6 +66,9 @@ void File::Close() {
SCOPED_FILE_TRACE("Close");
internal::AssertBlockingAllowed();
file_.reset();
if (delete_on_close_) {
SbFileDelete(file_name_.c_str());
}
}

int64_t File::Seek(Whence whence, int64_t offset) {
Expand Down Expand Up @@ -267,8 +273,18 @@ bool File::GetInfo(Info* info) {
}

File::Error File::GetLastFileError() {
SB_NOTIMPLEMENTED();
return File::FILE_ERROR_MAX;
int err = errno;
switch (err) {
case ENOENT:
return static_cast<File::Error>(kSbFileErrorNotFound);
case EACCES:
return static_cast<File::Error>(kSbFileErrorAccessDenied);
case EEXIST:
return static_cast<File::Error>(kSbFileErrorExists);
default:
NOTREACHED() << base::StringPrintf("Unhandled errno: %d", err);
}
return FILE_ERROR_FAILED;
}

// Static.
Expand Down Expand Up @@ -338,8 +354,6 @@ void File::DoInitialize(const FilePath& path, uint32_t flags) {
error_details_ = FILE_ERROR_FAILED;
}

DCHECK(flags & FLAG_WRITE || flags & FLAG_READ || flags & FLAG_APPEND);

if (flags & FLAG_READ) {
open_flags |= kSbFileRead;
}
Expand All @@ -361,10 +375,7 @@ void File::DoInitialize(const FilePath& path, uint32_t flags) {
}
}

if (flags & FLAG_DELETE_ON_CLOSE) {
NOTREACHED() << "Not supported on Starboard platforms right now.";
}

delete_on_close_ = (flags & FLAG_DELETE_ON_CLOSE) == FLAG_DELETE_ON_CLOSE;
async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
}

Expand Down
8 changes: 6 additions & 2 deletions base/files/file_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ TEST(FileTest, DISABLED_TouchGetInfo) {
EXPECT_FALSE(info.is_symbolic_link);

// ext2/ext3 and HPS/HPS+ seem to have a timestamp granularity of 1s.
#if BUILDFLAG(IS_POSIX)
#if BUILDFLAG(IS_POSIX) && !defined(STARBOARD)
EXPECT_EQ(info.last_accessed.ToTimeVal().tv_sec,
new_last_accessed.ToTimeVal().tv_sec);
EXPECT_EQ(info.last_modified.ToTimeVal().tv_sec,
Expand Down Expand Up @@ -540,6 +540,7 @@ TEST(FileTest, Seek) {
EXPECT_EQ(kOffset, file.Seek(base::File::FROM_END, -kOffset));
}

#if !defined(STARBOARD)
TEST(FileTest, Duplicate) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Expand All @@ -566,7 +567,9 @@ TEST(FileTest, Duplicate) {
ASSERT_EQ(kDataLen, file2.Read(0, &buf[0], kDataLen));
ASSERT_EQ(std::string(kData, kDataLen), std::string(&buf[0], kDataLen));
}
#endif

#if !defined(STARBOARD)
TEST(FileTest, DuplicateDeleteOnClose) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Expand All @@ -582,6 +585,7 @@ TEST(FileTest, DuplicateDeleteOnClose) {
file2.Close();
ASSERT_FALSE(base::PathExists(file_path));
}
#endif

#if BUILDFLAG(ENABLE_BASE_TRACING)
TEST(FileTest, TracedValueSupport) {
Expand Down Expand Up @@ -857,4 +861,4 @@ TEST(FileDeathTest, InvalidFlags) {
},
"FLAG_WIN_NO_EXECUTE");
}
#endif // BUILDFLAG(IS_WIN)
#endif // BUILDFLAG(IS_WIN)
6 changes: 5 additions & 1 deletion base/files/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ absl::optional<std::vector<uint8_t>> ReadFileToBytes(const FilePath& path) {
return absl::nullopt;
}
#else
std::vector<uint8_t> bytes;
std::string contents;
if (!ReadFileToString(path, &contents)) {
return absl::nullopt;
}
std::vector<uint8_t> bytes(contents.begin(), contents.end());
#endif
return bytes;
}
Expand Down
1 change: 0 additions & 1 deletion base/files/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#include <limits>
#include <set>
Expand Down
19 changes: 19 additions & 0 deletions base/files/file_util_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ bool PathExists(const FilePath &path) {
return SbFileExists(path.value().c_str());
}

bool PathIsReadable(const FilePath &path) {
internal::AssertBlockingAllowed();
return SbFileCanOpen(path.value().c_str(), kSbFileOpenAlways | kSbFileRead);
}

bool PathIsWritable(const FilePath &path) {
internal::AssertBlockingAllowed();
return SbFileCanOpen(path.value().c_str(), kSbFileOpenAlways | kSbFileWrite);
Expand Down Expand Up @@ -380,6 +385,16 @@ bool CreateDirectoryAndGetError(const FilePath &full_path, File::Error* error) {
return true;
}

bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) {
internal::AssertBlockingAllowed();
// Only absolute paths are supported in Starboard.
if (!path.IsAbsolute()) {
return false;
}
*normalized_path = path;
return true;
}

bool IsLink(const FilePath &file_path) {
internal::AssertBlockingAllowed();
SbFileInfo info;
Expand Down Expand Up @@ -455,6 +470,10 @@ bool AppendToFile(const FilePath &filename, const char *data, int size) {
return file.WriteAtCurrentPos(data, size) == size;
}

bool AppendToFile(const FilePath& filename, StringPiece data) {
return AppendToFile(filename, data.data(), data.size());
}

bool HasFileBeenModifiedSince(const FileEnumerator::FileInfo &file_info,
const base::Time &cutoff_time) {
return file_info.GetLastModifiedTime() >= cutoff_time;
Expand Down
Loading

0 comments on commit 7c115b0

Please sign in to comment.