Use file helpers in minielf_test

PiperOrigin-RevId: 346949861
Change-Id: Ib323a9ecd8fd8f268f09b028d13b220d3d8b60d1
This commit is contained in:
Wiktor Garbacz 2020-12-11 00:47:58 -08:00 committed by Copybara-Service
parent d4d58361e9
commit 81a68382d8
4 changed files with 27 additions and 28 deletions

View File

@ -157,10 +157,12 @@ cc_test(
], ],
features = ["-dynamic_link_test_srcs"], # see go/dynamic_link_test_srcs features = ["-dynamic_link_test_srcs"], # see go/dynamic_link_test_srcs
deps = [ deps = [
":file_helpers",
":maps_parser", ":maps_parser",
":minielf", ":minielf",
"//sandboxed_api/sandbox2:testing", "//sandboxed_api/sandbox2:testing",
"//sandboxed_api/util:status_matchers", "//sandboxed_api/util:status_matchers",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/strings", "@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],

View File

@ -186,7 +186,9 @@ if(SAPI_ENABLE_TESTS)
configure_file(testdata/chrome_grte_header configure_file(testdata/chrome_grte_header
testdata/chrome_grte_header COPYONLY) testdata/chrome_grte_header COPYONLY)
target_link_libraries(minielf_test PRIVATE target_link_libraries(minielf_test PRIVATE
absl::algorithm_container
absl::strings absl::strings
sandbox2::file_helpers
sandbox2::maps_parser sandbox2::maps_parser
sandbox2::minielf sandbox2::minielf
sandbox2::testing sandbox2::testing

View File

@ -38,8 +38,8 @@ class ElfFile {
int64_t file_size() const { return file_size_; } int64_t file_size() const { return file_size_; }
const std::string& interpreter() const { return interpreter_; } const std::string& interpreter() const { return interpreter_; }
const std::vector<Symbol> symbols() const { return symbols_; } const std::vector<Symbol>& symbols() const { return symbols_; }
const std::vector<std::string> imported_libraries() const { const std::vector<std::string>& imported_libraries() const {
return imported_libraries_; return imported_libraries_;
} }
bool position_independent() const { return position_independent_; } bool position_independent() const { return position_independent_; }

View File

@ -15,11 +15,14 @@
#include "sandboxed_api/sandbox2/util/minielf.h" #include "sandboxed_api/sandbox2/util/minielf.h"
#include <cstdint> #include <cstdint>
#include <string>
#include <vector> #include <vector>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "absl/algorithm/container.h"
#include "sandboxed_api/sandbox2/testing.h" #include "sandboxed_api/sandbox2/testing.h"
#include "sandboxed_api/sandbox2/util/file_helpers.h"
#include "sandboxed_api/sandbox2/util/maps_parser.h" #include "sandboxed_api/sandbox2/util/maps_parser.h"
#include "sandboxed_api/util/status_matchers.h" #include "sandboxed_api/util/status_matchers.h"
@ -30,9 +33,10 @@ extern "C" void ExportedFunctionName() {
namespace sandbox2 { namespace sandbox2 {
namespace { namespace {
using ::sapi::IsOk;
using ::testing::Eq; using ::testing::Eq;
using ::testing::IsTrue; using ::testing::IsTrue;
using ::testing::Not; using ::testing::Ne;
using ::testing::StrEq; using ::testing::StrEq;
TEST(MinielfTest, Chrome70) { TEST(MinielfTest, Chrome70) {
@ -51,36 +55,27 @@ TEST(MinielfTest, SymbolResolutionWorks) {
ASSERT_THAT(elf.position_independent(), IsTrue()); ASSERT_THAT(elf.position_independent(), IsTrue());
// Load /proc/self/maps to take ASLR into account. // Load /proc/self/maps to take ASLR into account.
char maps_buffer[1024 * 1024]{}; std::string maps_buffer;
FILE *f = fopen("/proc/self/maps", "r"); ASSERT_THAT(sandbox2::file::GetContents("/proc/self/maps", &maps_buffer,
ASSERT_THAT(f, Not(Eq(nullptr))); sandbox2::file::Defaults()),
fread(maps_buffer, 1, sizeof(maps_buffer), f); IsOk());
fclose(f);
SAPI_ASSERT_OK_AND_ASSIGN(std::vector<MapsEntry> maps, ParseProcMaps(maps_buffer)); SAPI_ASSERT_OK_AND_ASSIGN(std::vector<MapsEntry> maps, ParseProcMaps(maps_buffer));
// Find maps entry that covers this entry. // Find maps entry that covers this entry.
uint64_t function_address = reinterpret_cast<uint64_t>(ExportedFunctionName); uint64_t function_address = reinterpret_cast<uint64_t>(ExportedFunctionName);
bool entry_found = false; auto function_entry =
for (const auto &entry : maps) { absl::c_find_if(maps, [function_address](const MapsEntry& entry) {
if (entry.start <= function_address && entry.end > function_address) { return entry.start <= function_address && entry.end > function_address;
entry_found = true; });
function_address -= entry.start; ASSERT_THAT(function_entry, Ne(maps.end()));
break; function_address -= function_entry->start;
}
}
ASSERT_THAT(entry_found, IsTrue());
uint64_t exported_function_name__symbol_value = 0; auto function_symbol =
absl::c_find_if(elf.symbols(), [](const ElfFile::Symbol& symbol) {
for (const auto &s : elf.symbols()) { return symbol.name == "ExportedFunctionName";
if (s.name == "ExportedFunctionName") { });
exported_function_name__symbol_value = s.address; ASSERT_THAT(function_symbol, Ne(elf.symbols().end()));
break; EXPECT_THAT(function_symbol->address, Eq(function_address));
}
}
EXPECT_THAT(exported_function_name__symbol_value, function_address);
} }
TEST(MinielfTest, ImportedLibraries) { TEST(MinielfTest, ImportedLibraries) {