blob: 1ac6808bf52c35e1fd799a79d3915e109fd78839 [file]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests for the helpers in <lib/elfldltl/testing/...> used by fuzzers and
// other tests.
#include <lib/elfldltl/constants.h>
#include <lib/elfldltl/layout.h>
#include <lib/elfldltl/note.h>
#include <lib/elfldltl/testing/ehdr.h>
#include <lib/elfldltl/testing/note.h>
#include <lib/elfldltl/testing/typed-test.h>
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <span>
#include <string_view>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
namespace {
using namespace std::literals::string_view_literals;
FORMAT_TYPED_TEST_SUITE(ElfldltlTestingNoteTests);
TYPED_TEST(ElfldltlTestingNoteTests, RoundTrip) {
using Elf = typename TestFixture::Elf;
constexpr std::array kBuildId{
std::byte{1}, std::byte{2}, std::byte{3}, std::byte{4}, std::byte{5},
};
std::vector<std::byte> notes;
elfldltl::testing::AppendNote<Elf>(
notes, "GNU", std::to_underlying(elfldltl::ElfNoteType::kGnuBuildId), kBuildId);
elfldltl::testing::AppendNote<Elf>(notes, "Fuchsia", 7, {});
EXPECT_EQ(notes.size() % 4, 0u);
elfldltl::ElfNoteSegment<Elf::kData> segment(notes);
auto it = segment.begin();
ASSERT_NE(it, segment.end());
EXPECT_TRUE((*it).IsBuildId());
EXPECT_TRUE(std::ranges::equal((*it).desc, kBuildId));
++it;
ASSERT_NE(it, segment.end());
EXPECT_TRUE((*it).Is("Fuchsia", 7u));
EXPECT_TRUE((*it).desc.empty());
++it;
EXPECT_EQ(it, segment.end());
}
TYPED_TEST(ElfldltlTestingNoteTests, Align8) {
using Elf = typename TestFixture::Elf;
using Nhdr = typename Elf::Nhdr;
auto expect_zero = [](std::span<const std::byte> pad) {
EXPECT_TRUE(std::ranges::all_of(pad, [](std::byte b) { return b == std::byte{0}; }));
};
// "GNU\0" ends the name at offset 16, already 8-byte aligned, so the desc
// starts there; 3 desc bytes are padded out to 24.
constexpr std::array kDesc{std::byte{0xa}, std::byte{0xb}, std::byte{0xc}};
std::vector<std::byte> notes;
elfldltl::testing::AppendNote<Elf>(notes, "GNU", 5, kDesc, 8);
ASSERT_EQ(notes.size(), 24u);
Nhdr nhdr;
std::memcpy(&nhdr, notes.data(), sizeof(nhdr));
EXPECT_EQ(nhdr.namesz, 4u);
EXPECT_EQ(nhdr.descsz, 3u);
EXPECT_EQ(nhdr.type, 5u);
EXPECT_EQ(std::string_view(reinterpret_cast<const char*>(&notes[12]), 4), "GNU\0"sv);
EXPECT_TRUE(std::ranges::equal(std::span(notes).subspan(16, 3), kDesc));
expect_zero(std::span(notes).subspan(19, 5));
// "Fuchsia\0" ends the name at offset 20, so the desc is padded to start at
// 24 (with 4-byte alignment it would start at 20), and the note ends at 32.
// The second note starts at 24, where the first one ended.
elfldltl::testing::AppendNote<Elf>(notes, "Fuchsia", 6, kDesc, 8);
ASSERT_EQ(notes.size(), 24u + 32u);
expect_zero(std::span(notes).subspan(24 + 20, 4));
EXPECT_TRUE(std::ranges::equal(std::span(notes).subspan(24 + 24, 3), kDesc));
expect_zero(std::span(notes).subspan(24 + 27, 5));
}
FORMAT_TYPED_TEST_SUITE(ElfldltlTestingEhdrTests);
TYPED_TEST(ElfldltlTestingEhdrTests, Defaults) {
using Elf = TestFixture::Elf;
using Ehdr = Elf::Ehdr;
using Phdr = Elf::Phdr;
constexpr Ehdr kEhdr = elfldltl::testing::MakeEhdr<Elf>();
EXPECT_TRUE(kEhdr.Valid());
EXPECT_TRUE(kEhdr.Loadable());
EXPECT_EQ(kEhdr.type(), elfldltl::ElfType::kDyn);
EXPECT_EQ(kEhdr.machine(), elfldltl::ElfMachine::kNative);
EXPECT_EQ(kEhdr.phoff(), 0u);
EXPECT_EQ(kEhdr.ehsize(), sizeof(Ehdr));
EXPECT_EQ(kEhdr.phentsize(), sizeof(Phdr));
EXPECT_EQ(kEhdr.phnum(), 0u);
}
TYPED_TEST(ElfldltlTestingEhdrTests, CoreWithPhdrs) {
using Elf = TestFixture::Elf;
using Ehdr = Elf::Ehdr;
using Phdr = Elf::Phdr;
constexpr Ehdr kEhdr =
elfldltl::testing::MakeEhdr<Elf>(elfldltl::ElfType::kCore, elfldltl::ElfMachine::kRiscv, 3);
EXPECT_TRUE(kEhdr.Valid());
EXPECT_EQ(kEhdr.type(), elfldltl::ElfType::kCore);
EXPECT_EQ(kEhdr.machine(), elfldltl::ElfMachine::kRiscv);
EXPECT_EQ(kEhdr.phoff(), sizeof(Ehdr));
EXPECT_EQ(kEhdr.ehsize(), sizeof(Ehdr));
EXPECT_EQ(kEhdr.phentsize(), sizeof(Phdr));
EXPECT_EQ(kEhdr.phnum(), 3u);
}
} // namespace