-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PiperOrigin-RevId: 623470785 Change-Id: I40c3bf7667ffcf888d241ca1d5f96b74e3e5ad68
- Loading branch information
1 parent
3191d1e
commit e2e1c3f
Showing
5 changed files
with
121 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include "sandboxed_api/embed_file.h" | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "sandboxed_api/file_toc.h" | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "absl/memory/memory.h" | ||
#include "absl/strings/string_view.h" | ||
|
||
namespace sapi { | ||
|
||
class EmbedFileTestPeer { | ||
public: | ||
static std::unique_ptr<EmbedFile> NewInstance() { | ||
return absl::WrapUnique(new EmbedFile()); | ||
} | ||
}; | ||
|
||
namespace { | ||
|
||
using ::testing::Eq; | ||
using ::testing::Ne; | ||
|
||
constexpr absl::string_view kRegularContents = "Hello world!"; | ||
constexpr FileToc kRegularToc = { | ||
.name = "regular", | ||
.data = kRegularContents.data(), | ||
.size = kRegularContents.size(), | ||
.md5digest = {}, // MD5 is unused in SAPI implementation | ||
}; | ||
|
||
constexpr FileToc kFaultyToc = { | ||
.name = "regular", | ||
.data = nullptr, | ||
.size = 100, | ||
.md5digest = {}, // MD5 is unused in SAPI implementation | ||
}; | ||
|
||
TEST(EmbedFileTest, GetRegularFd) { | ||
std::unique_ptr<EmbedFile> embed_file = EmbedFileTestPeer::NewInstance(); | ||
int fd = embed_file->GetFdForFileToc(&kRegularToc); | ||
EXPECT_THAT(fd, Ne(-1)); | ||
} | ||
|
||
TEST(EmbedFileTest, DuplicateGetFdIsSame) { | ||
std::unique_ptr<EmbedFile> embed_file = EmbedFileTestPeer::NewInstance(); | ||
int fd = embed_file->GetFdForFileToc(&kRegularToc); | ||
EXPECT_THAT(fd, Ne(-1)); | ||
int fd2 = embed_file->GetFdForFileToc(&kRegularToc); | ||
EXPECT_THAT(fd, Eq(fd2)); | ||
} | ||
|
||
TEST(EmbedFileTest, GetDupFdReturnsFreshFd) { | ||
std::unique_ptr<EmbedFile> embed_file = EmbedFileTestPeer::NewInstance(); | ||
int fd = embed_file->GetFdForFileToc(&kRegularToc); | ||
EXPECT_THAT(fd, Ne(-1)); | ||
int dup_fd = embed_file->GetDupFdForFileToc(&kRegularToc); | ||
EXPECT_THAT(fd, Ne(dup_fd)); | ||
close(dup_fd); | ||
} | ||
|
||
TEST(EmbedFileTest, FaultyTocFails) { | ||
std::unique_ptr<EmbedFile> embed_file = EmbedFileTestPeer::NewInstance(); | ||
int fd = embed_file->GetFdForFileToc(&kFaultyToc); | ||
EXPECT_THAT(fd, Eq(-1)); | ||
int dup_fd = embed_file->GetDupFdForFileToc(&kFaultyToc); | ||
EXPECT_THAT(dup_fd, Eq(-1)); | ||
} | ||
|
||
TEST(EmbedFileTest, OverlongNameTocFails) { | ||
std::string overlong_name(1000, 'a'); | ||
FileToc overlong_name_toc = { | ||
.name = overlong_name.c_str(), | ||
.data = kRegularContents.data(), | ||
.size = kRegularContents.size(), | ||
.md5digest = {}, // MD5 is unused in SAPI implementation | ||
}; | ||
std::unique_ptr<EmbedFile> embed_file = EmbedFileTestPeer::NewInstance(); | ||
int fd = embed_file->GetFdForFileToc(&overlong_name_toc); | ||
EXPECT_THAT(fd, Eq(-1)); | ||
} | ||
|
||
} // namespace | ||
} // namespace sapi |