forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for POSIX fstat, lseek, and read. (youtube#2602)
b/302715109 Test-On-Device: true Change-Id: I4db736278c54cdf1af85ba03836361f68796aa8f
- Loading branch information
1 parent
aa2188e
commit ca46052
Showing
30 changed files
with
978 additions
and
268 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
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
97 changes: 97 additions & 0 deletions
97
starboard/nplb/posix_compliance/posix_file_get_info_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,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 |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.