Skip to content

Commit

Permalink
Add tests for POSIX fstat, lseek, and read. (#2602)
Browse files Browse the repository at this point in the history
b/302715109

Test-On-Device: true

Change-Id: I4db736278c54cdf1af85ba03836361f68796aa8f
(cherry picked from commit ca46052)
  • Loading branch information
yjzhang111 authored and anonymous1-me committed May 7, 2024
1 parent f41cbf1 commit f85fe2f
Show file tree
Hide file tree
Showing 30 changed files with 988 additions and 267 deletions.
6 changes: 6 additions & 0 deletions starboard/android/shared/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
'PosixDirectoryOpenTest.SunnyDayStaticContent',
'PosixFileGetPathInfoTest.WorksOnStaticContentDirectories',

# These POSIX tests should be disabled until asset manager starboard
# extension is implemented.
'PosixFileGetInfoTest.WorksOnStaticContentFiles',
'PosixFileReadTest/*.ReadStaticContent',
'PosixFileSeekTest.FromEndInStaticContentWorks',

# These tests are disabled due to not receiving the kEndOfStream
# player state update within the specified timeout.
'SbPlayerGetAudioConfigurationTests/SbPlayerGetAudioConfigurationTest.NoInput/*',
Expand Down
22 changes: 22 additions & 0 deletions starboard/common/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "starboard/common/file.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <cstring>
#include <string>
Expand All @@ -40,6 +42,26 @@ bool DirectoryCloseLogFailure(const char* path, SbDirectory dir) {

} // namespace

ssize_t ReadAll(int fd, void* data, int size) {
if (fd < 0 || size < 0) {
return -1;
}
ssize_t bytes_read = 0;
ssize_t rv;
do {
// Needs to cast void* to char* as MSVC returns error for pointer
// arithmetic.
rv =
read(fd, reinterpret_cast<char*>(data) + bytes_read, size - bytes_read);
if (rv <= 0) {
break;
}
bytes_read += rv;
} while (bytes_read < size);

return bytes_read ? bytes_read : rv;
}

void RecordFileWriteStat(int write_file_result) {
auto& stats_tracker = StatsTrackerContainer::GetInstance()->stats_tracker();
if (write_file_result <= 0) {
Expand Down
4 changes: 4 additions & 0 deletions starboard/common/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@

#include "starboard/file.h"

#include <sys/types.h>

namespace starboard {

ssize_t ReadAll(int fd, void* data, int size);

void RecordFileWriteStat(int write_file_result);

// Deletes the file, symlink or directory at |path|. When |path| is a directory,
Expand Down
7 changes: 7 additions & 0 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "starboard/mutex.h"
#include "starboard/player.h"
#if SB_API_VERSION >= 16
#include "starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h"
#include "starboard/shared/modular/starboard_layer_posix_mmap_abi_wrappers.h"
#include "starboard/shared/modular/starboard_layer_posix_pthread_abi_wrappers.h"
#include "starboard/shared/modular/starboard_layer_posix_stat_abi_wrappers.h"
Expand Down Expand Up @@ -461,17 +462,20 @@ ExportedSymbols::ExportedSymbols() {
REGISTER_SYMBOL(free);
REGISTER_SYMBOL(freeaddrinfo);
REGISTER_SYMBOL(freeifaddrs);
REGISTER_SYMBOL(fstat);
REGISTER_SYMBOL(getaddrinfo);
REGISTER_SYMBOL(getifaddrs);
REGISTER_SYMBOL(getsockname);
REGISTER_SYMBOL(listen);
REGISTER_SYMBOL(lseek);
REGISTER_SYMBOL(malloc);
REGISTER_SYMBOL(mkdir);
REGISTER_SYMBOL(mprotect);
REGISTER_SYMBOL(msync);
REGISTER_SYMBOL(munmap);
REGISTER_SYMBOL(open);
REGISTER_SYMBOL(posix_memalign);
REGISTER_SYMBOL(read);
REGISTER_SYMBOL(realloc);
REGISTER_SYMBOL(recv);
REGISTER_SYMBOL(send);
Expand All @@ -495,9 +499,11 @@ ExportedSymbols::ExportedSymbols() {
// TODO: b/316603042 - Detect via NPLB and only add the wrapper if needed.
map_["clock_gettime"] =
reinterpret_cast<const void*>(&__abi_wrap_clock_gettime);
map_["fstat"] = reinterpret_cast<const void*>(&__abi_wrap_fstat);
map_["gettimeofday"] =
reinterpret_cast<const void*>(&__abi_wrap_gettimeofday);
map_["gmtime_r"] = reinterpret_cast<const void*>(&__abi_wrap_gmtime_r);
map_["lseek"] = reinterpret_cast<const void*>(&__abi_wrap_lseek);
map_["mmap"] = reinterpret_cast<const void*>(&__abi_wrap_mmap);
map_["pthread_cond_broadcast"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_cond_broadcast);
Expand Down Expand Up @@ -553,6 +559,7 @@ ExportedSymbols::ExportedSymbols() {
reinterpret_cast<const void*>(&__abi_wrap_pthread_setspecific);
map_["pthread_setname_np"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_setname_np);
map_["read"] = reinterpret_cast<const void*>(&__abi_wrap_read);
map_["stat"] = reinterpret_cast<const void*>(&__abi_wrap_stat);
map_["time"] = reinterpret_cast<const void*>(&__abi_wrap_time);

Expand Down
3 changes: 3 additions & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,11 @@ target(gtest_target_type, "nplb") {
"posix_compliance/posix_directory_can_open_test.cc",
"posix_compliance/posix_directory_create_test.cc",
"posix_compliance/posix_file_close_test.cc",
"posix_compliance/posix_file_get_info_test.cc",
"posix_compliance/posix_file_get_path_info_test.cc",
"posix_compliance/posix_file_open_test.cc",
"posix_compliance/posix_file_read_test.cc",
"posix_compliance/posix_file_seek_test.cc",
"posix_compliance/posix_memory_allocate_aligned_test.cc",
"posix_compliance/posix_memory_allocate_test.cc",
"posix_compliance/posix_memory_deallocate_aligned_test.cc",
Expand Down
4 changes: 0 additions & 4 deletions starboard/nplb/posix_compliance/posix_file_close_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION >= 16

// close is partially tested in posix_file_open_test.cc.

#include <unistd.h>
Expand All @@ -32,5 +30,3 @@ TEST(PosixFileCloseTest, CloseInvalidFails) {
} // namespace
} // namespace nplb
} // namespace starboard

#endif // SB_API_VERSION >= 16
97 changes: 97 additions & 0 deletions starboard/nplb/posix_compliance/posix_file_get_info_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2024 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 <fcntl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>

#include <iostream>
#include <string>

#include "starboard/common/time.h"
#include "starboard/file.h"
#include "starboard/nplb/file_helpers.h"
#include "starboard/system.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

inline int64_t TimeTToWindowsUsec(time_t time) {
int64_t posix_usec = static_cast<int64_t>(time) * 1000000;
return PosixTimeToWindowsTime(posix_usec);
}

TEST(PosixFileGetInfoTest, InvalidFileErrors) {
struct stat info;
int result = fstat(-1, &info);
EXPECT_FALSE(result == 0);
}

TEST(PosixFileGetInfoTest, WorksOnARegularFile) {
// This test is potentially flaky because it's comparing times. So, building
// in extra sensitivity to make flakiness more apparent.
const int kTrials = 100;
for (int i = 0; i < kTrials; ++i) {
// We can't assume filesystem timestamp precision, so go back a minute
// for a better chance to contain the imprecision and rounding errors.
const int64_t kOneMinuteInMicroseconds = 60'000'000;
int64_t time =
PosixTimeToWindowsTime(CurrentPosixTime()) - kOneMinuteInMicroseconds;

const int kFileSize = 12;
starboard::nplb::ScopedRandomFile random_file(kFileSize);
const std::string& filename = random_file.filename();

int file = open(filename.c_str(), O_RDONLY);
ASSERT_TRUE(file >= 0);

{
struct stat info;
int result = fstat(file, &info);
EXPECT_EQ(kFileSize, info.st_size);
EXPECT_FALSE(S_ISDIR(info.st_mode));
EXPECT_FALSE(S_ISLNK(info.st_mode));
EXPECT_LE(time, TimeTToWindowsUsec(info.st_atime));
EXPECT_LE(time, TimeTToWindowsUsec(info.st_atime));
EXPECT_LE(time, TimeTToWindowsUsec(info.st_ctime));
}

int result = close(file);
EXPECT_TRUE(result == 0);
}
}

TEST(PosixFileGetInfoTest, WorksOnStaticContentFiles) {
int count = 1;
for (auto filename : GetFileTestsFilePaths()) {
int file = open(filename.c_str(), O_RDONLY);
ASSERT_TRUE(file >= 0);

struct stat info;
EXPECT_TRUE(fstat(file, &info) == 0);
size_t content_length = GetTestFileExpectedContent(filename).length();
EXPECT_EQ(content_length, info.st_size);
EXPECT_FALSE(S_ISDIR(info.st_mode));
EXPECT_FALSE(S_ISLNK(info.st_mode));

EXPECT_TRUE(close(file) == 0);
}
}

} // namespace
} // namespace nplb
} // namespace starboard
18 changes: 0 additions & 18 deletions starboard/nplb/posix_compliance/posix_file_get_path_info_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ TEST(PosixFileGetPathInfoTest, WorksOnARegularFile) {
// for a better chance to contain the imprecision and rounding errors.
const int64_t kOneSecondInMicroseconds = 1'000'000;
int64_t time = PosixTimeToWindowsTime(CurrentPosixTime());
#if !SB_HAS_QUIRK(FILESYSTEM_ZERO_FILEINFO_TIME)
#if SB_HAS_QUIRK(FILESYSTEM_COARSE_ACCESS_TIME)
// On platforms with coarse access time, we assume 1 day precision and go
// back 2 days to avoid rounding issues.
const int64_t kOneDayInMicroseconds = 1'000'000LL * 60LL * 60LL * 24LL;
#endif // FILESYSTEM_COARSE_ACCESS_TIME
#endif // FILESYSTEM_ZERO_FILEINFO_TIME

const int kFileSize = 12;
ScopedRandomFile random_file(kFileSize);
Expand All @@ -69,23 +62,12 @@ TEST(PosixFileGetPathInfoTest, WorksOnARegularFile) {
EXPECT_EQ(kFileSize, file_info.st_size);
EXPECT_FALSE(S_ISDIR(file_info.st_mode));
EXPECT_FALSE(S_ISLNK(file_info.st_mode));
#if SB_HAS_QUIRK(FILESYSTEM_ZERO_FILEINFO_TIME)
EXPECT_LE(0, TimeTToWindowsUsecTest(file_info.at_ctime));
EXPECT_LE(0, TimeTToWindowsUsecTest(file_info.mt_ctime));
EXPECT_LE(0, TimeTToWindowsUsecTest(file_info.st_ctime));
#else
EXPECT_NEAR(time, TimeTToWindowsUsecTest(file_info.st_mtime),
kOneSecondInMicroseconds);
#if SB_HAS_QUIRK(FILESYSTEM_COARSE_ACCESS_TIME)
EXPECT_NEAR(time, TimeTToWindowsUsecTest(file_info.at_ctime),
2 * kOneDayInMicroseconds);
#else
EXPECT_NEAR(time, TimeTToWindowsUsecTest(file_info.st_atime),
kOneSecondInMicroseconds);
#endif // FILESYSTEM_COARSE_ACCESS_TIME
EXPECT_NEAR(time, TimeTToWindowsUsecTest(file_info.st_ctime),
kOneSecondInMicroseconds);
#endif // FILESYSTEM_ZERO_FILEINFO_TIME
}
}
}
Expand Down
70 changes: 0 additions & 70 deletions starboard/nplb/posix_compliance/posix_file_helpers.cc

This file was deleted.

Loading

0 comments on commit f85fe2f

Please sign in to comment.