2014-09-02 15:50:11 -04:00
|
|
|
|
// Copyright 2014 The Crashpad 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.
|
|
|
|
|
|
2014-10-17 13:41:45 -04:00
|
|
|
|
#include "snapshot/mac/mach_o_image_segment_reader.h"
|
2014-09-02 15:50:11 -04:00
|
|
|
|
|
|
|
|
|
#include <mach-o/loader.h>
|
|
|
|
|
|
2016-01-06 12:22:50 -05:00
|
|
|
|
#include "base/macros.h"
|
2014-09-02 15:50:11 -04:00
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
2014-10-07 17:28:50 -04:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace test {
|
2014-09-02 15:50:11 -04:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// Most of MachOImageSegmentReader is tested as part of MachOImageReader, which
|
|
|
|
|
// depends on MachOImageSegmentReader to provide major portions of its
|
|
|
|
|
// functionality. Because MachOImageSegmentReader is difficult to use except by
|
|
|
|
|
// a Mach-O load command reader such as MachOImageReader, these portions
|
|
|
|
|
// of MachOImageSegmentReader are not tested independently.
|
|
|
|
|
//
|
|
|
|
|
// The tests here exercise the portions of MachOImageSegmentReader that are
|
|
|
|
|
// exposed and independently useful.
|
|
|
|
|
|
|
|
|
|
TEST(MachOImageSegmentReader, SegmentNameString) {
|
|
|
|
|
// The output value should be a string of up to 16 characters, even if the
|
|
|
|
|
// input value is not NUL-terminated within 16 characters.
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("__TEXT"), "__TEXT");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("__OVER"), "__OVER");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString(""), "");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("p"), "p");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("NoUnderChar"),
|
|
|
|
|
"NoUnderChar");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("0123456789abcde"),
|
|
|
|
|
"0123456789abcde");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("0123456789abcdef"),
|
|
|
|
|
"0123456789abcdef");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("gfedcba9876543210"),
|
|
|
|
|
"gfedcba987654321");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("hgfedcba9876543210"),
|
|
|
|
|
"hgfedcba98765432");
|
2014-09-02 15:50:11 -04:00
|
|
|
|
|
|
|
|
|
// Segment names defined in <mach-o/loader.h>. All of these should come
|
|
|
|
|
// through SegmentNameString() cleanly and without truncation.
|
|
|
|
|
const char* kSegmentTestData[] = {
|
|
|
|
|
SEG_TEXT,
|
|
|
|
|
SEG_DATA,
|
|
|
|
|
SEG_OBJC,
|
|
|
|
|
SEG_ICON,
|
|
|
|
|
SEG_LINKEDIT,
|
|
|
|
|
SEG_UNIXSTACK,
|
|
|
|
|
SEG_IMPORT,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (size_t index = 0; index < arraysize(kSegmentTestData); ++index) {
|
|
|
|
|
EXPECT_EQ(
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
MachOImageSegmentReader::SegmentNameString(kSegmentTestData[index]),
|
|
|
|
|
kSegmentTestData[index])
|
2014-09-02 15:50:11 -04:00
|
|
|
|
<< base::StringPrintf("index %zu", index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(MachOImageSegmentReader, SectionNameString) {
|
|
|
|
|
// The output value should be a string of up to 16 characters, even if the
|
|
|
|
|
// input value is not NUL-terminated within 16 characters.
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("__text"), "__text");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("__over"), "__over");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SectionNameString(""), "");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("p"), "p");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("NoUnderChar"),
|
|
|
|
|
"NoUnderChar");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("0123456789abcde"),
|
|
|
|
|
"0123456789abcde");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("0123456789abcdef"),
|
|
|
|
|
"0123456789abcdef");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("gfedcba9876543210"),
|
|
|
|
|
"gfedcba987654321");
|
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("hgfedcba9876543210"),
|
|
|
|
|
"hgfedcba98765432");
|
2014-09-02 15:50:11 -04:00
|
|
|
|
|
|
|
|
|
// Section names defined in <mach-o/loader.h>. All of these should come
|
|
|
|
|
// through SectionNameString() cleanly and without truncation.
|
|
|
|
|
const char* kSectionTestData[] = {
|
|
|
|
|
SECT_TEXT,
|
|
|
|
|
SECT_FVMLIB_INIT0,
|
|
|
|
|
SECT_FVMLIB_INIT1,
|
|
|
|
|
SECT_DATA,
|
|
|
|
|
SECT_BSS,
|
|
|
|
|
SECT_COMMON,
|
|
|
|
|
SECT_OBJC_SYMBOLS,
|
|
|
|
|
SECT_OBJC_MODULES,
|
|
|
|
|
SECT_OBJC_STRINGS,
|
|
|
|
|
SECT_OBJC_REFS,
|
|
|
|
|
SECT_ICON_HEADER,
|
|
|
|
|
SECT_ICON_TIFF,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (size_t index = 0; index < arraysize(kSectionTestData); ++index) {
|
|
|
|
|
EXPECT_EQ(
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
MachOImageSegmentReader::SectionNameString(kSectionTestData[index]),
|
|
|
|
|
kSectionTestData[index])
|
2014-09-02 15:50:11 -04:00
|
|
|
|
<< base::StringPrintf("index %zu", index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(MachOImageSegmentReader, SegmentAndSectionNameString) {
|
|
|
|
|
struct SegmentAndSectionTestData {
|
|
|
|
|
const char* segment;
|
|
|
|
|
const char* section;
|
|
|
|
|
const char* output;
|
|
|
|
|
};
|
|
|
|
|
const SegmentAndSectionTestData kSegmentAndSectionTestData[] = {
|
|
|
|
|
{"segment", "section", "segment,section"},
|
|
|
|
|
{"Segment", "Section", "Segment,Section"},
|
|
|
|
|
{"SEGMENT", "SECTION", "SEGMENT,SECTION"},
|
|
|
|
|
{"__TEXT", "__plain", "__TEXT,__plain"},
|
|
|
|
|
{"__TEXT", "poetry", "__TEXT,poetry"},
|
|
|
|
|
{"__TEXT", "Prose", "__TEXT,Prose"},
|
|
|
|
|
{"__PLAIN", "__text", "__PLAIN,__text"},
|
|
|
|
|
{"rich", "__text", "rich,__text"},
|
|
|
|
|
{"segment", "", "segment,"},
|
|
|
|
|
{"", "section", ",section"},
|
|
|
|
|
{"", "", ","},
|
|
|
|
|
{"0123456789abcdef", "section", "0123456789abcdef,section"},
|
|
|
|
|
{"gfedcba9876543210", "section", "gfedcba987654321,section"},
|
|
|
|
|
{"0123456789abcdef", "", "0123456789abcdef,"},
|
|
|
|
|
{"gfedcba9876543210", "", "gfedcba987654321,"},
|
|
|
|
|
{"segment", "0123456789abcdef", "segment,0123456789abcdef"},
|
|
|
|
|
{"segment", "gfedcba9876543210", "segment,gfedcba987654321"},
|
|
|
|
|
{"", "0123456789abcdef", ",0123456789abcdef"},
|
|
|
|
|
{"", "gfedcba9876543210", ",gfedcba987654321"},
|
|
|
|
|
{"0123456789abcdef",
|
|
|
|
|
"0123456789abcdef",
|
|
|
|
|
"0123456789abcdef,0123456789abcdef"},
|
|
|
|
|
{"gfedcba9876543210",
|
|
|
|
|
"gfedcba9876543210",
|
|
|
|
|
"gfedcba987654321,gfedcba987654321"},
|
|
|
|
|
|
|
|
|
|
// Sections defined in <mach-o/loader.h>. All of these should come through
|
|
|
|
|
// SegmentAndSectionNameString() cleanly and without truncation.
|
|
|
|
|
{SEG_TEXT, SECT_TEXT, "__TEXT,__text"},
|
|
|
|
|
{SEG_TEXT, SECT_FVMLIB_INIT0, "__TEXT,__fvmlib_init0"},
|
|
|
|
|
{SEG_TEXT, SECT_FVMLIB_INIT1, "__TEXT,__fvmlib_init1"},
|
|
|
|
|
{SEG_DATA, SECT_DATA, "__DATA,__data"},
|
|
|
|
|
{SEG_DATA, SECT_BSS, "__DATA,__bss"},
|
|
|
|
|
{SEG_DATA, SECT_COMMON, "__DATA,__common"},
|
|
|
|
|
{SEG_OBJC, SECT_OBJC_SYMBOLS, "__OBJC,__symbol_table"},
|
|
|
|
|
{SEG_OBJC, SECT_OBJC_MODULES, "__OBJC,__module_info"},
|
|
|
|
|
{SEG_OBJC, SECT_OBJC_STRINGS, "__OBJC,__selector_strs"},
|
|
|
|
|
{SEG_OBJC, SECT_OBJC_REFS, "__OBJC,__selector_refs"},
|
|
|
|
|
{SEG_ICON, SECT_ICON_HEADER, "__ICON,__header"},
|
|
|
|
|
{SEG_ICON, SECT_ICON_TIFF, "__ICON,__tiff"},
|
|
|
|
|
|
|
|
|
|
// These segments don’t normally have sections, but the above group tested
|
|
|
|
|
// the known segment names for segments that do normally have sections.
|
|
|
|
|
// This group does the same for segments that normally don’t.
|
|
|
|
|
{SEG_LINKEDIT, "", "__LINKEDIT,"},
|
|
|
|
|
{SEG_UNIXSTACK, "", "__UNIXSTACK,"},
|
|
|
|
|
{SEG_IMPORT, "", "__IMPORT,"},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (size_t index = 0; index < arraysize(kSegmentAndSectionTestData);
|
|
|
|
|
++index) {
|
|
|
|
|
const SegmentAndSectionTestData& test = kSegmentAndSectionTestData[index];
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
EXPECT_EQ(MachOImageSegmentReader::SegmentAndSectionNameString(
|
|
|
|
|
test.segment, test.section),
|
|
|
|
|
test.output)
|
2014-09-02 15:50:11 -04:00
|
|
|
|
<< base::StringPrintf("index %zu, segment %s, section %s",
|
|
|
|
|
index,
|
|
|
|
|
test.segment,
|
|
|
|
|
test.section);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2014-10-07 17:28:50 -04:00
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace crashpad
|