2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
9 import symbol_extractor
12 class TestPatchOrderFile(unittest
.TestCase
):
13 def testRemoveSuffixes(self
):
14 no_clone
= 'this.does.not.contain.clone'
15 self
.assertEquals(no_clone
, patch_orderfile
.RemoveSuffixes(no_clone
))
16 with_clone
= 'this.does.contain.clone.'
18 'this.does.contain', patch_orderfile
.RemoveSuffixes(with_clone
))
19 with_part
= 'this.is.a.part.42'
21 'this.is.a', patch_orderfile
.RemoveSuffixes(with_part
))
23 def testAliasClonedSymbols(self
):
25 symbol_extractor
.SymbolInfo(name
='aSymbol', offset
=0x42, size
=0x12,
27 symbol_extractor
.SymbolInfo(name
='aSymbol.clone.', offset
=8, size
=1,
29 (offset_to_symbol_infos
, name_to_symbol_infos
) = \
30 patch_orderfile
._GroupSymbolInfos
(symbol_infos
)
31 self
.assertEquals(len(offset_to_symbol_infos
), 2)
34 matching
= offset_to_symbol_infos
[s
.offset
][0]
35 self
.assertEquals(matching
.offset
, s
.offset
)
36 self
.assertEquals(matching
.size
, s
.size
)
37 self
.assertEquals(len(name_to_symbol_infos
), 1)
38 self
.assertEquals(len(name_to_symbol_infos
['aSymbol']), 2)
40 def testGroupSymbolsByOffset(self
):
42 symbol_extractor
.SymbolInfo(name
='aSymbol', offset
=0x42, size
=0x12,
44 symbol_extractor
.SymbolInfo(name
='anotherSymbol', offset
=0x42, size
=1,
46 (offset_to_symbol_infos
, _
) = \
47 patch_orderfile
._GroupSymbolInfos
(symbol_infos
)
48 self
.assertEquals(len(offset_to_symbol_infos
), 1)
49 self
.assertEquals(tuple(offset_to_symbol_infos
[0x42]), symbol_infos
)
51 def testSymbolsWithSameOffset(self
):
52 symbol_name
= "dummySymbol"
53 symbol_name2
= "other"
54 name_to_symbol_infos
= {symbol_name
: [
55 symbol_extractor
.SymbolInfo(symbol_name
, 0x42, 0x12,
57 offset_to_symbol_infos
= {
58 0x42: [symbol_extractor
.SymbolInfo(symbol_name
, 0x42, 0x12,
60 symbol_extractor
.SymbolInfo(symbol_name2
, 0x42, 0x12,
62 symbol_names
= patch_orderfile
._SymbolsWithSameOffset
(
63 symbol_name
, name_to_symbol_infos
, offset_to_symbol_infos
)
64 self
.assertEquals(len(symbol_names
), 2)
65 self
.assertEquals(symbol_names
[0], symbol_name
)
66 self
.assertEquals(symbol_names
[1], symbol_name2
)
67 self
.assertEquals([], patch_orderfile
._SymbolsWithSameOffset
(
68 "symbolThatShouldntMatch",
69 name_to_symbol_infos
, offset_to_symbol_infos
))
71 def testSectionNameToSymbols(self
):
72 mapping
= {'.text.foo': ['foo'],
73 '.text.startup.bar': ['bar', 'bar1']}
74 self
.assertEquals(list(patch_orderfile
._SectionNameToSymbols
(
75 '.text.foo', mapping
)),
77 self
.assertEquals(list(patch_orderfile
._SectionNameToSymbols
(
78 '.text.startup.bar', mapping
)),
80 self
.assertEquals(list(patch_orderfile
._SectionNameToSymbols
(
81 '.text.startup.bar', mapping
)),
83 self
.assertEquals(list(patch_orderfile
._SectionNameToSymbols
(
84 '.text.hot.foobar', mapping
)),
86 self
.assertEquals(list(patch_orderfile
._SectionNameToSymbols
(
87 '.text.startup.*', mapping
)),
90 def testSectionMatchingRules(self
):
91 symbol_name1
= 'symbol1'
92 symbol_name2
= 'symbol2'
93 symbol_name3
= 'symbol3'
94 section_name1
= '.text.' + symbol_name1
95 section_name3
= '.text.foo'
96 suffixed
= set([section_name3
])
97 name_to_symbol_infos
= {symbol_name1
: [
98 symbol_extractor
.SymbolInfo(symbol_name1
, 0x42, 0x12,
100 offset_to_symbol_infos
= {
101 0x42: [symbol_extractor
.SymbolInfo(symbol_name1
, 0x42, 0x12,
103 symbol_extractor
.SymbolInfo(symbol_name2
, 0x42, 0x12,
105 section_to_symbols_map
= {section_name1
: [symbol_name1
],
106 section_name3
: [symbol_name1
, symbol_name3
]}
107 symbol_to_sections_map
= {symbol_name1
:
108 [section_name1
, section_name3
],
109 symbol_name3
: [section_name3
]}
113 section_name3
+ '.*',
114 '.text.startup.' + symbol_name1
,
115 '.text.hot.' + symbol_name1
,
116 '.text.unlikely.' + symbol_name1
,
117 '.text.startup.symbol2',
119 '.text.unlikely.symbol2',
121 self
.assertEqual(expected
, list(patch_orderfile
._SectionMatchingRules
(
122 section_name1
, name_to_symbol_infos
, offset_to_symbol_infos
,
123 section_to_symbols_map
, symbol_to_sections_map
, suffixed
)))
125 def testUniqueGenerator(self
):
126 @patch_orderfile._UniqueGenerator
133 self
.assertEqual(list(TestIterator()), [1,2,3])
135 def testCombineSectionListsByPrimaryName(self
):
136 self
.assertEqual(patch_orderfile
._CombineSectionListsByPrimaryName
(
137 {'foo': ['.text.foo', '.text.bar.constprop.1'],
138 'foo.part.1': ['.text.baz'],
139 'foobar': ['.text.foobar']}),
140 {'foo': ['.text.foo', '.text.bar', '.text.baz'],
141 'foobar': ['.text.foobar']})
143 def testSectionsWithSuffixes(self
):
144 self
.assertEqual(patch_orderfile
._SectionsWithSuffixes
(
145 {'foo': ['.text.foo', '.text.bar.constprop.1'],
146 'foo.part.1': ['.text.baz'],
147 'foobar': ['.text.foobar']}),
151 if __name__
== "__main__":