Skip to content

Commit

Permalink
Remove musl implementations.
Browse files Browse the repository at this point in the history
Change-Id: I20e7215432d8172c585fe4e0d96427661cc9bf79
  • Loading branch information
yjzhang111 committed Feb 24, 2024
1 parent 5a0486f commit da11c5a
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 0 deletions.
2 changes: 2 additions & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ target(gtest_target_type, "nplb") {
"player_test_util.h",
"player_write_sample_test.cc",
"posix_compliance/posix_arpa_inet_test.cc",
"posix_compliance/posix_file_close_test.cc",
"posix_compliance/posix_file_open_test.cc",
"posix_compliance/posix_memory_map_test.cc",
"posix_compliance/posix_mutex_acquire_test.cc",
"posix_compliance/posix_mutex_acquire_try_test.cc",
Expand Down
36 changes: 36 additions & 0 deletions starboard/nplb/posix_compliance/posix_file_close_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.

#if SB_API_VERSION >= 16

// close is partially tested in posix_file_open_test.cc.

#include <unistd.h>

#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

TEST(PosixFileCloseTest, CloseInvalidFails) {
int fd = -1;
EXPECT_FALSE(close(fd) == 0);
}

} // namespace
} // namespace nplb
} // namespace starboard

#endif // SB_API_VERSION >= 16
69 changes: 69 additions & 0 deletions starboard/nplb/posix_compliance/posix_file_helpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 "starboard/nplb/posix_compliance/posix_file_helpers.h"

#include <fcntl.h>
#include <unistd.h>

#include <sstream>
#include <string>
#include <vector>

#include "starboard/configuration_constants.h"
#include "starboard/file.h"
#include "starboard/shared/posix/file_internal.h"
#include "starboard/system.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {

// 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;
}

int file = open(filename.c_str(), O_CREAT | O_WRONLY);
EXPECT_TRUE(fcntl(file, F_GETFD));
if (!fcntl(file, F_GETFD)) {
return "";
}

char* data = new char[length];
for (int i = 0; i < length; ++i) {
data[i] = static_cast<char>(i & 0xFF);
}

bool result = close(file);
EXPECT_TRUE(result) << "Failed to close " << filename;
delete[] data;
return filename;
}

} // namespace nplb
} // namespace starboard
101 changes: 101 additions & 0 deletions starboard/nplb/posix_compliance/posix_file_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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.

#ifndef STARBOARD_NPLB_POSIX_COMPLIANCE_POSIX_FILE_HELPERS_H_
#define STARBOARD_NPLB_POSIX_COMPLIANCE_POSIX_FILE_HELPERS_H_

#include <string>
#include <vector>

#include "starboard/file.h"

namespace starboard {
namespace nplb {

// Gets the temporary directory in which ScopedRandomFile places its files.
std::string GetTempDir();

// Creates a random file of the given length, and deletes it when the instance
// falls out of scope.
class ScopedRandomFile {
public:
enum {
kDefaultLength = 64,
};

enum Create {
kCreate,
kDontCreate,
};

// Will create a file of |kDefaultLength| bytes long.
ScopedRandomFile() : size_(kDefaultLength) {
filename_ = MakeRandomFile(size_);
}

// Will create a file |length| bytes long.
explicit ScopedRandomFile(int length) : size_(length) {
filename_ = MakeRandomFile(size_);
}

// Will either create a file |length| bytes long, or will just generate a
// filename. |create| is whether to create the file or not.
ScopedRandomFile(int length, Create create) : size_(length) {
filename_ =
(create == kCreate ? MakeRandomFile(size_) : MakeRandomFilePath());
}

// Will either create a file of |kDefaultLength| bytes long, or will just
// generate a filename. |create| is whether to create the file or not.
explicit ScopedRandomFile(Create create) : size_(kDefaultLength) {
filename_ =
(create == kCreate ? MakeRandomFile(size_) : MakeRandomFilePath());
}

~ScopedRandomFile() { SbFileDelete(filename_.c_str()); }

// Creates and returns a random filename (no path), but does not create the
// file.
static std::string MakeRandomFilename();

// Returns the filename generated for this file.
const std::string& filename() const { return filename_; }

// Returns the SPECIFIED size of the file (not the size returned by the
// filesystem).
const int size() const { return size_; }

// Checks |buffer| of size |size| against this class's write pattern, offset
// by |pattern_offset|. Failures print the original line number |line|.
static void ExpectPattern(int pattern_offset,
void* buffer,
int size,
int line);

private:
// Creates a file with a random name and |length| bytes, returning the path to
// the new file.
static std::string MakeRandomFile(int length);

// Creates and returns a path to a random file, but does not create the file.
static std::string MakeRandomFilePath();

std::string filename_;
int size_;
};

} // namespace nplb
} // namespace starboard

#endif // STARBOARD_NPLB_POSIX_COMPLIANCE_POSIX_FILE_HELPERS_H_
131 changes: 131 additions & 0 deletions starboard/nplb/posix_compliance/posix_file_open_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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.

#if SB_API_VERSION >= 16

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

#include "starboard/file.h"
#include "starboard/nplb/posix_compliance/posix_file_helpers.h"
#include "starboard/shared/posix/file_internal.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

void BasicTest(bool existing,
int open_flags,
bool expected_created,
bool expected_success,
int original_line) {
ScopedRandomFile random_file(existing ? ScopedRandomFile::kCreate
: ScopedRandomFile::kDontCreate);
const std::string& filename = random_file.filename();
#define SB_FILE_OPEN_TEST_CONTEXT \
"existing=" << existing << ", flags=0x" << std::hex << open_flags \
<< std::dec << ", expected_created=" << expected_created \
<< ", expected_success=" << expected_success \
<< ", filename=" << filename \
<< ", original_line=" << original_line

if (!existing) {
EXPECT_FALSE(SbFileExists(filename.c_str())) << SB_FILE_OPEN_TEST_CONTEXT;
if (SbFileExists(filename.c_str())) {
return;
}
}

bool created = !expected_created;
int fd = open(filename.c_str(), open_flags);
if (!expected_success) {
EXPECT_FALSE(fd >= 0) << SB_FILE_OPEN_TEST_CONTEXT;
// EXPECT_EQ(expected_created, created) << SB_FILE_OPEN_TEST_CONTEXT;
// EXPECT_NE(kSbFileOk, error) << SB_FILE_OPEN_TEST_CONTEXT;

// Try to clean up in case test fails.
if (!(fd < 0)) {
close(fd);
}
} else {
EXPECT_TRUE(fd >= 0);
// EXPECT_EQ(expected_created, created) << SB_FILE_OPEN_TEST_CONTEXT;
// EXPECT_EQ(kSbFileOk, error) << SB_FILE_OPEN_TEST_CONTEXT;
if (fd >= 0) {
int result = close(fd);
EXPECT_TRUE(result == 0) << SB_FILE_OPEN_TEST_CONTEXT;
}
}
#undef SB_FILE_OPEN_TEST_CONTEXT
}

TEST(PosixFileOpenTest, OpenOnlyOpensExistingFile) {
// BasicTest(true, kSbFileOpenOnly | kSbFileRead, false, true, __LINE__);
BasicTest(true, O_RDONLY, false, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenOnlyDoesNotOpenNonExistingFile) {
// BasicTest(false, kSbFileOpenOnly | kSbFileRead, false, false, __LINE__);
BasicTest(false, O_RDONLY, false, false, __LINE__);
}

TEST(PosixFileOpenTest, CreateOnlyDoesNotCreateExistingFile) {
// BasicTest(true, kSbFileCreateOnly | kSbFileWrite, false, false, __LINE__);
BasicTest(true, O_CREAT | O_EXCL | O_WRONLY, false, false, __LINE__);
}

TEST(PosixFileOpenTest, CreateOnlyCreatesNonExistingFile) {
// BasicTest(false, kSbFileCreateOnly | kSbFileWrite, true, true, __LINE__);
BasicTest(false, O_CREAT | O_EXCL | O_WRONLY, true, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenAlwaysOpensExistingFile) {
// BasicTest(true, kSbFileOpenAlways | kSbFileWrite, false, true, __LINE__);
BasicTest(true, O_CREAT | O_WRONLY, false, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenAlwaysCreatesNonExistingFile) {
BasicTest(false, kSbFileOpenAlways | kSbFileWrite, true, true, __LINE__);
BasicTest(false, O_CREAT | O_WRONLY, true, true, __LINE__);
}

TEST(PosixFileOpenTest, CreateAlwaysTruncatesExistingFile) {
// BasicTest(true, kSbFileCreateAlways | kSbFileWrite, true, true, __LINE__);
BasicTest(true, O_CREAT | O_TRUNC | O_WRONLY, true, true, __LINE__);
}

TEST(PosixFileOpenTest, CreateAlwaysCreatesNonExistingFile) {
// BasicTest(false, kSbFileCreateAlways | kSbFileWrite, true, true, __LINE__);
BasicTest(false, O_CREAT | O_TRUNC | O_WRONLY, true, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenTruncatedTruncatesExistingFile) {
// BasicTest(true, kSbFileOpenTruncated | kSbFileWrite, false, true,
// __LINE__);
BasicTest(true, O_TRUNC | O_WRONLY, false, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenTruncatedDoesNotCreateNonExistingFile) {
// BasicTest(false, kSbFileOpenTruncated | kSbFileWrite, false, false,
// __LINE__);
BasicTest(false, O_TRUNC | O_WRONLY, false, false, __LINE__);
}

} // namespace
} // namespace nplb
} // namespace starboard

#endif // SB_API_VERSION >= 16
1 change: 1 addition & 0 deletions starboard/xb1/platform_configuration/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ config("target") {
"iso_stdio_wide_specifiers.lib",
"mfplat.lib",
"mfuuid.lib",
"oldnames.lib",
"windowsapp.lib",
]
ldflags += [
Expand Down

0 comments on commit da11c5a

Please sign in to comment.