[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2013 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Unittest for symbolize.py. |
| 8 | |
| 9 | This test uses test libraries generated by the Android g++ toolchain. |
| 10 | |
| 11 | Should things break you can recreate the libraries and get the updated |
| 12 | addresses and demangled names by running the following: |
| 13 | cd test/symbolize/ |
| 14 | make |
| 15 | nm -gC *.so |
| 16 | """ |
| 17 | |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 18 | import StringIO |
| 19 | import unittest |
| 20 | |
| 21 | import symbolize |
| 22 | |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 23 | LIB_A_PATH = '/build/android/tests/symbolize/liba.so' |
| 24 | LIB_B_PATH = '/build/android/tests/symbolize/libb.so' |
| 25 | |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 26 | def RunSymbolizer(text): |
| 27 | output = StringIO.StringIO() |
[email protected] | 74686ef | 2013-07-18 21:37:45 | [diff] [blame] | 28 | s = symbolize.Symbolizer(output) |
| 29 | s.write(text) |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 30 | return output.getvalue() |
| 31 | |
| 32 | |
| 33 | class SymbolizerUnittest(unittest.TestCase): |
| 34 | def testSingleLineNoMatch(self): |
| 35 | # Leading '#' is required. |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 36 | expected = '00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 37 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 38 | |
| 39 | # Whitespace should be exactly one space. |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 40 | expected = '#00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 41 | self.assertEqual(expected, RunSymbolizer(expected)) |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 42 | expected = '#00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 43 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 44 | |
| 45 | # Decimal stack frame numbers are required. |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 46 | expected = '#0a 0x00000000 ' + LIB_A_PATH + '+0x00000254\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 47 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 48 | |
| 49 | # Hexadecimal addresses are required. |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 50 | expected = '#00 0xghijklmn ' + LIB_A_PATH + '+0x00000254\n' |
| 51 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 52 | expected = '#00 0x00000000 ' + LIB_A_PATH + '+0xghijklmn\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 53 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 54 | |
| 55 | # Addresses must be exactly 8 characters. |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 56 | expected = '#00 0x0000000 ' + LIB_A_PATH + '+0x00000254\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 57 | self.assertEqual(expected, RunSymbolizer(expected)) |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 58 | expected = '#00 0x000000000 ' + LIB_A_PATH + '+0x00000254\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 59 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 60 | |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 61 | expected = '#00 0x0000000 ' + LIB_A_PATH + '+0x0000254\n' |
| 62 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 63 | expected = '#00 0x000000000 ' + LIB_A_PATH + '+0x000000254\n' |
| 64 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 65 | |
| 66 | # Addresses must be prefixed with '0x'. |
| 67 | expected = '#00 00000000 ' + LIB_A_PATH + '+0x00000254\n' |
| 68 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 69 | expected = '#00 0x00000000 ' + LIB_A_PATH + '+00000254\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 70 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 71 | |
| 72 | # Library name is required. |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 73 | expected = '#00 0x00000000\n' |
| 74 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 75 | expected = '#00 0x00000000 +0x00000254\n' |
| 76 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 77 | |
| 78 | # Library name must be followed by offset with no spaces around '+'. |
| 79 | expected = '#00 0x00000000 ' + LIB_A_PATH + ' +0x00000254\n' |
| 80 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 81 | expected = '#00 0x00000000 ' + LIB_A_PATH + '+ 0x00000254\n' |
| 82 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 83 | expected = '#00 0x00000000 ' + LIB_A_PATH + ' 0x00000254\n' |
| 84 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 85 | expected = '#00 0x00000000 ' + LIB_A_PATH + '+\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 86 | self.assertEqual(expected, RunSymbolizer(expected)) |
| 87 | |
| 88 | def testSingleLine(self): |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 89 | text = '#00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n' |
| 90 | expected = '#00 0x00000000 A::Bar(char const*)\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 91 | actual = RunSymbolizer(text) |
| 92 | self.assertEqual(expected, actual) |
| 93 | |
| 94 | def testSingleLineWithSurroundingText(self): |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 95 | text = 'LEFT #00 0x00000000 ' + LIB_A_PATH + '+0x00000254 RIGHT\n' |
| 96 | expected = 'LEFT #00 0x00000000 A::Bar(char const*) RIGHT\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 97 | actual = RunSymbolizer(text) |
| 98 | self.assertEqual(expected, actual) |
| 99 | |
| 100 | def testMultipleLinesSameLibrary(self): |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 101 | text = '#00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n' |
| 102 | text += '#01 0x00000000 ' + LIB_A_PATH + '+0x00000234\n' |
| 103 | expected = '#00 0x00000000 A::Bar(char const*)\n' |
| 104 | expected += '#01 0x00000000 A::Foo(int)\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 105 | actual = RunSymbolizer(text) |
| 106 | self.assertEqual(expected, actual) |
| 107 | |
| 108 | def testMultipleLinesDifferentLibrary(self): |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 109 | text = '#00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n' |
| 110 | text += '#01 0x00000000 ' + LIB_B_PATH + '+0x00000234\n' |
| 111 | expected = '#00 0x00000000 A::Bar(char const*)\n' |
| 112 | expected += '#01 0x00000000 B::Baz(float)\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 113 | actual = RunSymbolizer(text) |
| 114 | self.assertEqual(expected, actual) |
| 115 | |
| 116 | def testMultipleLinesWithSurroundingTextEverywhere(self): |
| 117 | text = 'TOP\n' |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 118 | text += 'LEFT #00 0x00000000 ' + LIB_A_PATH + '+0x00000254 RIGHT\n' |
| 119 | text += 'LEFT #01 0x00000000 ' + LIB_B_PATH + '+0x00000234 RIGHT\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 120 | text += 'BOTTOM\n' |
| 121 | expected = 'TOP\n' |
[email protected] | 2aca494f | 2013-07-12 20:17:54 | [diff] [blame] | 122 | expected += 'LEFT #00 0x00000000 A::Bar(char const*) RIGHT\n' |
| 123 | expected += 'LEFT #01 0x00000000 B::Baz(float) RIGHT\n' |
[email protected] | e804425 | 2013-07-11 16:17:56 | [diff] [blame] | 124 | expected += 'BOTTOM\n' |
| 125 | actual = RunSymbolizer(text) |
| 126 | self.assertEqual(expected, actual) |
| 127 | |
| 128 | |
| 129 | if __name__ == '__main__': |
| 130 | unittest.main() |