diff options
author | Samuel Huang <huangs@chromium.org> | 2018-03-13 18:19:34 +0000 |
---|---|---|
committer | Edward Lesmes <ehmaldonado@google.com> | 2021-07-23 21:50:59 +0000 |
commit | 06f1ae9aaca969ee95ef840f22b6b461c304542d (patch) | |
tree | f1e5c6624e70628e81fbf38d6cd14b974abe5d93 /rel32_utils_unittest.cc | |
download | zucchini-06f1ae9aaca969ee95ef840f22b6b461c304542d.tar.gz |
[Zucchini] Move Zucchini from /chrome/installer/ to /components/.
(Use "git log --follow" to see older revisions of files).
/components/ is the most logical place to put Zucchini, which only
depends on /base and /testing/gtest. This move also enables Zucchini to
be used by the Component Updater. Details:
- Move all files; run the following to change deps and guards:
sed 's/chrome\/installer/components/' *.cc *.h -i
sed 's/CHROME_INSTALLER/COMPONENTS/' *.cc *.h -i
- Sorting works out pretty well!
- Change all 'chrome/installer/zucchini' to 'components/zucchini'
throughout other parts of the repo; sort if necessary.
- Fix 6 'git cl lint' errors.
- Change 1 Bind() usage to BindRepeated().
- Update OWNER.
Bug: 729154
Change-Id: I50c5a7d411ea85f707b5994ab319dfb2a1acccf7
Reviewed-on: https://chromium-review.googlesource.com/954923
Reviewed-by: Greg Thompson <grt@chromium.org>
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
Reviewed-by: Samuel Huang <huangs@chromium.org>
Commit-Queue: Samuel Huang <huangs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542857}
NOKEYCHECK=True
GitOrigin-RevId: 577ef6c435e8d43be6e3e60ccbcbd1881780f4ec
Diffstat (limited to 'rel32_utils_unittest.cc')
-rw-r--r-- | rel32_utils_unittest.cc | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/rel32_utils_unittest.cc b/rel32_utils_unittest.cc new file mode 100644 index 0000000..80928de --- /dev/null +++ b/rel32_utils_unittest.cc @@ -0,0 +1,128 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/zucchini/rel32_utils.h" + +#include <stdint.h> + +#include <memory> +#include <vector> + +#include "base/memory/ptr_util.h" +#include "base/optional.h" +#include "base/test/gtest_util.h" +#include "components/zucchini/address_translator.h" +#include "components/zucchini/image_utils.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace zucchini { + +namespace { + +// A trivial AddressTranslator that applies constant shift. +class TestAddressTranslator : public AddressTranslator { + public: + TestAddressTranslator(offset_t image_size, rva_t rva_begin) { + DCHECK_GE(rva_begin, 0U); + CHECK_EQ(AddressTranslator::kSuccess, + Initialize({{0, image_size, rva_begin, image_size}})); + } +}; + +// Checks that |reader| emits and only emits |expected_refs|, in order. +void CheckReader(const std::vector<Reference>& expected_refs, + ReferenceReader* reader) { + for (Reference expected_ref : expected_refs) { + auto ref = reader->GetNext(); + EXPECT_TRUE(ref.has_value()); + EXPECT_EQ(expected_ref, ref.value()); + } + EXPECT_EQ(base::nullopt, reader->GetNext()); // Nothing should be left. +} + +} // namespace + +TEST(Rel32UtilsTest, Rel32ReaderX86) { + constexpr offset_t kTestImageSize = 0x00100000U; + constexpr rva_t kRvaBegin = 0x00030000U; + TestAddressTranslator translator(kTestImageSize, kRvaBegin); + + // For simplicity, test data is not real X86 machine code. We are only + // including rel32 targets, without the full instructions. + std::vector<uint8_t> bytes = { + 0xFF, 0xFF, 0xFF, 0xFF, // 00030000: (Filler) + 0x00, 0x00, 0x00, 0x80, // 00030004: 80030008 Marked, so invalid. + 0x04, 0x00, 0x00, 0x00, // 00030008: 00030010 + 0xFF, 0xFF, 0xFF, 0xFF, // 0003000C: (Filler) + 0x00, 0x00, 0x00, 0x00, // 00030010: 00030014 + 0xFF, 0xFF, 0xFF, 0xFF, // 00030014: (Filler) + 0xF4, 0xFF, 0xFF, 0xFF, // 00030018: 00030010 + 0xE4, 0xFF, 0xFF, 0xFF, // 0003001C: 00030004 + }; + ConstBufferView buffer(bytes.data(), bytes.size()); + // Specify rel32 locations directly, instead of parsing. + std::vector<offset_t> rel32_locations = {0x0008U, 0x0010U, 0x0018U, 0x001CU}; + + // Generate everything. + Rel32ReaderX86 reader1(buffer, 0x0000U, 0x0020U, &rel32_locations, + translator); + CheckReader({{0x0008U, 0x0010U}, + {0x0010U, 0x0014U}, + {0x0018U, 0x0010U}, + {0x001CU, 0x0004U}}, + &reader1); + + // Exclude last. + Rel32ReaderX86 reader2(buffer, 0x0000U, 0x001CU, &rel32_locations, + translator); + CheckReader({{0x0008U, 0x0010U}, {0x0010U, 0x0014U}, {0x0018U, 0x0010U}}, + &reader2); + + // Only find one. + Rel32ReaderX86 reader3(buffer, 0x000CU, 0x0018U, &rel32_locations, + translator); + CheckReader({{0x0010U, 0x0014U}}, &reader3); + + // Marked target encountered (error). + std::vector<offset_t> rel32_marked_locations = {0x00004U}; + Rel32ReaderX86 reader4(buffer, 0x0000U, 0x0020U, &rel32_marked_locations, + translator); + EXPECT_DCHECK_DEATH(reader4.GetNext()); +} + +TEST(Rel32UtilsTest, Rel32WriterX86) { + constexpr offset_t kTestImageSize = 0x00100000U; + constexpr rva_t kRvaBegin = 0x00030000U; + TestAddressTranslator translator(kTestImageSize, kRvaBegin); + + std::vector<uint8_t> bytes(32, 0xFF); + MutableBufferView buffer(bytes.data(), bytes.size()); + + Rel32WriterX86 writer(buffer, translator); + writer.PutNext({0x0008U, 0x0010U}); + EXPECT_EQ(0x00000004U, buffer.read<uint32_t>(0x08)); // 00030008: 00030010 + + writer.PutNext({0x0010U, 0x0014U}); + EXPECT_EQ(0x00000000U, buffer.read<uint32_t>(0x10)); // 00030010: 00030014 + + writer.PutNext({0x0018U, 0x0010U}); + EXPECT_EQ(0xFFFFFFF4U, buffer.read<uint32_t>(0x18)); // 00030018: 00030010 + + writer.PutNext({0x001CU, 0x0004U}); + EXPECT_EQ(0xFFFFFFE4U, buffer.read<uint32_t>(0x1C)); // 0003001C: 00030004 + + EXPECT_EQ(std::vector<uint8_t>({ + 0xFF, 0xFF, 0xFF, 0xFF, // 00030000: (Filler) + 0xFF, 0xFF, 0xFF, 0xFF, // 00030004: (Filler) + 0x04, 0x00, 0x00, 0x00, // 00030008: 00030010 + 0xFF, 0xFF, 0xFF, 0xFF, // 0003000C: (Filler) + 0x00, 0x00, 0x00, 0x00, // 00030010: 00030014 + 0xFF, 0xFF, 0xFF, 0xFF, // 00030014: (Filler) + 0xF4, 0xFF, 0xFF, 0xFF, // 00030018: 00030010 + 0xE4, 0xFF, 0xFF, 0xFF, // 0003001C: 00030004 + }), + bytes); +} + +} // namespace zucchini |