-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce POSIX open and close on SB16. (#2460)
b/302715109
- Loading branch information
1 parent
230c7dd
commit fa976d1
Showing
9 changed files
with
437 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
135 changes: 135 additions & 0 deletions
135
starboard/nplb/posix_compliance/posix_file_open_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// 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 "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, | ||
mode_t mode = S_IRUSR | S_IWUSR) { | ||
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; | ||
#ifdef _WIN32 | ||
// File mode is set along with O_CREAT flag. | ||
// Windows only supports 1)_S_IREAD, which is mapped to S_IRUSR, 2) _S_IWRITE, | ||
// which is mapped to S_IWUSR, and 3) _S_IREAD | _S_IWRITE. | ||
if (open_flags & O_CREAT && (open_flags == S_IRUSR || open_flags == S_IWUSR || | ||
open_flags == (S_IRUSR | S_IWUSR))) { | ||
fd = open(filename.c_str(), open_flags, mode); | ||
} else { | ||
fd = open(filename.c_str(), open_flags); | ||
} | ||
#else | ||
fd = (open_flags & O_CREAT) ? open(filename.c_str(), open_flags, mode) | ||
: open(filename.c_str(), open_flags); | ||
#endif | ||
|
||
if (!expected_success) { | ||
EXPECT_FALSE(fd >= 0) << SB_FILE_OPEN_TEST_CONTEXT; | ||
|
||
// Try to clean up in case test fails. | ||
if (!(fd < 0)) { | ||
close(fd); | ||
} | ||
} else { | ||
EXPECT_TRUE(fd >= 0); | ||
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, O_RDONLY, false, true, __LINE__); | ||
} | ||
|
||
TEST(PosixFileOpenTest, OpenOnlyDoesNotOpenNonExistingFile) { | ||
BasicTest(false, O_RDONLY, false, false, __LINE__); | ||
} | ||
|
||
TEST(PosixFileOpenTest, CreateOnlyDoesNotCreateExistingFile) { | ||
BasicTest(true, O_CREAT | O_EXCL | O_WRONLY, false, false, __LINE__); | ||
} | ||
|
||
TEST(PosixFileOpenTest, CreateOnlyCreatesNonExistingFile) { | ||
BasicTest(false, O_CREAT | O_EXCL | O_WRONLY, true, true, __LINE__); | ||
} | ||
|
||
TEST(PosixFileOpenTest, OpenAlwaysOpensExistingFile) { | ||
BasicTest(true, O_CREAT | O_WRONLY, false, true, __LINE__); | ||
} | ||
|
||
TEST(PosixFileOpenTest, OpenAlwaysCreatesNonExistingFile) { | ||
BasicTest(false, O_CREAT | O_WRONLY, true, true, __LINE__); | ||
} | ||
|
||
TEST(PosixFileOpenTest, OpenAlwaysWithLinuxSpecificMode) { | ||
BasicTest(false, O_CREAT | O_TRUNC | O_WRONLY, true, true, __LINE__, | ||
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | ||
} | ||
|
||
TEST(PosixFileOpenTest, CreateAlwaysTruncatesExistingFile) { | ||
BasicTest(true, O_CREAT | O_TRUNC | O_WRONLY, true, true, __LINE__); | ||
} | ||
|
||
TEST(PosixFileOpenTest, CreateAlwaysCreatesNonExistingFile) { | ||
BasicTest(false, O_CREAT | O_TRUNC | O_WRONLY, true, true, __LINE__); | ||
} | ||
|
||
TEST(PosixFileOpenTest, OpenTruncatedTruncatesExistingFile) { | ||
BasicTest(true, O_TRUNC | O_WRONLY, false, true, __LINE__); | ||
} | ||
|
||
TEST(PosixFileOpenTest, OpenTruncatedDoesNotCreateNonExistingFile) { | ||
BasicTest(false, O_TRUNC | O_WRONLY, false, false, __LINE__); | ||
} | ||
|
||
} // namespace | ||
} // namespace nplb | ||
} // namespace starboard | ||
|
||
#endif // SB_API_VERSION >= 16 |
Oops, something went wrong.