OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include "util/mac/mach_o_image_segment_reader.h" | |
16 | |
17 #include <mach-o/loader.h> | |
18 | |
19 #include "base/basictypes.h" | |
20 #include "base/strings/stringprintf.h" | |
21 #include "gtest/gtest.h" | |
22 | |
23 namespace crashpad { | |
24 namespace test { | |
25 namespace { | |
26 | |
27 // Most of MachOImageSegmentReader is tested as part of MachOImageReader, which | |
28 // depends on MachOImageSegmentReader to provide major portions of its | |
29 // functionality. Because MachOImageSegmentReader is difficult to use except by | |
30 // a Mach-O load command reader such as MachOImageReader, these portions | |
31 // of MachOImageSegmentReader are not tested independently. | |
32 // | |
33 // The tests here exercise the portions of MachOImageSegmentReader that are | |
34 // exposed and independently useful. | |
35 | |
36 TEST(MachOImageSegmentReader, SegmentNameString) { | |
37 // The output value should be a string of up to 16 characters, even if the | |
38 // input value is not NUL-terminated within 16 characters. | |
39 EXPECT_EQ("__TEXT", MachOImageSegmentReader::SegmentNameString("__TEXT")); | |
40 EXPECT_EQ("__OVER", MachOImageSegmentReader::SegmentNameString("__OVER")); | |
41 EXPECT_EQ("", MachOImageSegmentReader::SegmentNameString("")); | |
42 EXPECT_EQ("p", MachOImageSegmentReader::SegmentNameString("p")); | |
43 EXPECT_EQ("NoUnderChar", | |
44 MachOImageSegmentReader::SegmentNameString("NoUnderChar")); | |
45 EXPECT_EQ("0123456789abcde", | |
46 MachOImageSegmentReader::SegmentNameString("0123456789abcde")); | |
47 EXPECT_EQ("0123456789abcdef", | |
48 MachOImageSegmentReader::SegmentNameString("0123456789abcdef")); | |
49 EXPECT_EQ("gfedcba987654321", | |
50 MachOImageSegmentReader::SegmentNameString("gfedcba9876543210")); | |
51 EXPECT_EQ("hgfedcba98765432", | |
52 MachOImageSegmentReader::SegmentNameString("hgfedcba9876543210")); | |
53 | |
54 // Segment names defined in <mach-o/loader.h>. All of these should come | |
55 // through SegmentNameString() cleanly and without truncation. | |
56 const char* kSegmentTestData[] = { | |
57 SEG_TEXT, | |
58 SEG_DATA, | |
59 SEG_OBJC, | |
60 SEG_ICON, | |
61 SEG_LINKEDIT, | |
62 SEG_UNIXSTACK, | |
63 SEG_IMPORT, | |
64 }; | |
65 | |
66 for (size_t index = 0; index < arraysize(kSegmentTestData); ++index) { | |
67 EXPECT_EQ( | |
68 kSegmentTestData[index], | |
69 MachOImageSegmentReader::SegmentNameString(kSegmentTestData[index])) | |
70 << base::StringPrintf("index %zu", index); | |
71 } | |
72 } | |
73 | |
74 TEST(MachOImageSegmentReader, SectionNameString) { | |
75 // The output value should be a string of up to 16 characters, even if the | |
76 // input value is not NUL-terminated within 16 characters. | |
77 EXPECT_EQ("__text", MachOImageSegmentReader::SectionNameString("__text")); | |
78 EXPECT_EQ("__over", MachOImageSegmentReader::SectionNameString("__over")); | |
79 EXPECT_EQ("", MachOImageSegmentReader::SectionNameString("")); | |
80 EXPECT_EQ("p", MachOImageSegmentReader::SectionNameString("p")); | |
81 EXPECT_EQ("NoUnderChar", | |
82 MachOImageSegmentReader::SectionNameString("NoUnderChar")); | |
83 EXPECT_EQ("0123456789abcde", | |
84 MachOImageSegmentReader::SectionNameString("0123456789abcde")); | |
85 EXPECT_EQ("0123456789abcdef", | |
86 MachOImageSegmentReader::SectionNameString("0123456789abcdef")); | |
87 EXPECT_EQ("gfedcba987654321", | |
88 MachOImageSegmentReader::SectionNameString("gfedcba9876543210")); | |
89 EXPECT_EQ("hgfedcba98765432", | |
90 MachOImageSegmentReader::SectionNameString("hgfedcba9876543210")); | |
91 | |
92 // Section names defined in <mach-o/loader.h>. All of these should come | |
93 // through SectionNameString() cleanly and without truncation. | |
94 const char* kSectionTestData[] = { | |
95 SECT_TEXT, | |
96 SECT_FVMLIB_INIT0, | |
97 SECT_FVMLIB_INIT1, | |
98 SECT_DATA, | |
99 SECT_BSS, | |
100 SECT_COMMON, | |
101 SECT_OBJC_SYMBOLS, | |
102 SECT_OBJC_MODULES, | |
103 SECT_OBJC_STRINGS, | |
104 SECT_OBJC_REFS, | |
105 SECT_ICON_HEADER, | |
106 SECT_ICON_TIFF, | |
107 }; | |
108 | |
109 for (size_t index = 0; index < arraysize(kSectionTestData); ++index) { | |
110 EXPECT_EQ( | |
111 kSectionTestData[index], | |
112 MachOImageSegmentReader::SectionNameString(kSectionTestData[index])) | |
113 << base::StringPrintf("index %zu", index); | |
114 } | |
115 } | |
116 | |
117 TEST(MachOImageSegmentReader, SegmentAndSectionNameString) { | |
118 struct SegmentAndSectionTestData { | |
119 const char* segment; | |
120 const char* section; | |
121 const char* output; | |
122 }; | |
123 const SegmentAndSectionTestData kSegmentAndSectionTestData[] = { | |
124 {"segment", "section", "segment,section"}, | |
125 {"Segment", "Section", "Segment,Section"}, | |
126 {"SEGMENT", "SECTION", "SEGMENT,SECTION"}, | |
127 {"__TEXT", "__plain", "__TEXT,__plain"}, | |
128 {"__TEXT", "poetry", "__TEXT,poetry"}, | |
129 {"__TEXT", "Prose", "__TEXT,Prose"}, | |
130 {"__PLAIN", "__text", "__PLAIN,__text"}, | |
131 {"rich", "__text", "rich,__text"}, | |
132 {"segment", "", "segment,"}, | |
133 {"", "section", ",section"}, | |
134 {"", "", ","}, | |
135 {"0123456789abcdef", "section", "0123456789abcdef,section"}, | |
136 {"gfedcba9876543210", "section", "gfedcba987654321,section"}, | |
137 {"0123456789abcdef", "", "0123456789abcdef,"}, | |
138 {"gfedcba9876543210", "", "gfedcba987654321,"}, | |
139 {"segment", "0123456789abcdef", "segment,0123456789abcdef"}, | |
140 {"segment", "gfedcba9876543210", "segment,gfedcba987654321"}, | |
141 {"", "0123456789abcdef", ",0123456789abcdef"}, | |
142 {"", "gfedcba9876543210", ",gfedcba987654321"}, | |
143 {"0123456789abcdef", | |
144 "0123456789abcdef", | |
145 "0123456789abcdef,0123456789abcdef"}, | |
146 {"gfedcba9876543210", | |
147 "gfedcba9876543210", | |
148 "gfedcba987654321,gfedcba987654321"}, | |
149 | |
150 // Sections defined in <mach-o/loader.h>. All of these should come through | |
151 // SegmentAndSectionNameString() cleanly and without truncation. | |
152 {SEG_TEXT, SECT_TEXT, "__TEXT,__text"}, | |
153 {SEG_TEXT, SECT_FVMLIB_INIT0, "__TEXT,__fvmlib_init0"}, | |
154 {SEG_TEXT, SECT_FVMLIB_INIT1, "__TEXT,__fvmlib_init1"}, | |
155 {SEG_DATA, SECT_DATA, "__DATA,__data"}, | |
156 {SEG_DATA, SECT_BSS, "__DATA,__bss"}, | |
157 {SEG_DATA, SECT_COMMON, "__DATA,__common"}, | |
158 {SEG_OBJC, SECT_OBJC_SYMBOLS, "__OBJC,__symbol_table"}, | |
159 {SEG_OBJC, SECT_OBJC_MODULES, "__OBJC,__module_info"}, | |
160 {SEG_OBJC, SECT_OBJC_STRINGS, "__OBJC,__selector_strs"}, | |
161 {SEG_OBJC, SECT_OBJC_REFS, "__OBJC,__selector_refs"}, | |
162 {SEG_ICON, SECT_ICON_HEADER, "__ICON,__header"}, | |
163 {SEG_ICON, SECT_ICON_TIFF, "__ICON,__tiff"}, | |
164 | |
165 // These segments don’t normally have sections, but the above group tested | |
166 // the known segment names for segments that do normally have sections. | |
167 // This group does the same for segments that normally don’t. | |
168 {SEG_LINKEDIT, "", "__LINKEDIT,"}, | |
169 {SEG_UNIXSTACK, "", "__UNIXSTACK,"}, | |
170 {SEG_IMPORT, "", "__IMPORT,"}, | |
171 }; | |
172 | |
173 for (size_t index = 0; index < arraysize(kSegmentAndSectionTestData); | |
174 ++index) { | |
175 const SegmentAndSectionTestData& test = kSegmentAndSectionTestData[index]; | |
176 EXPECT_EQ(test.output, | |
177 MachOImageSegmentReader::SegmentAndSectionNameString( | |
178 test.segment, test.section)) | |
179 << base::StringPrintf("index %zu, segment %s, section %s", | |
180 index, | |
181 test.segment, | |
182 test.section); | |
183 } | |
184 } | |
185 | |
186 } // namespace | |
187 } // namespace test | |
188 } // namespace crashpad | |
OLD | NEW |