summaryrefslogtreecommitdiffstats
path: root/build/android/symbolize_test.py
blob: 826d85228e6835aafe9c557193faafe9d36cf96d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
#
# Copyright 2013 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.

"""Unittest for symbolize.py.

This test uses test libraries generated by the Android g++ toolchain.

Should things break you can recreate the libraries and get the updated
addresses and demangled names by running the following:
  cd test/symbolize/
  make
  nm -gC *.so
"""

import StringIO
import unittest

import symbolize

LIB_A_PATH = '/build/android/tests/symbolize/liba.so'
LIB_B_PATH = '/build/android/tests/symbolize/libb.so'

def RunSymbolizer(text):
  output = StringIO.StringIO()
  s = symbolize.Symbolizer(output)
  s.write(text)
  return output.getvalue()


class SymbolizerUnittest(unittest.TestCase):
  def testSingleLineNoMatch(self):
    # Leading '#' is required.
    expected = '00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))

    # Whitespace should be exactly one space.
    expected = '#00  0x00000000 ' + LIB_A_PATH + '+0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))
    expected = '#00 0x00000000  ' + LIB_A_PATH + '+0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))

    # Decimal stack frame numbers are required.
    expected = '#0a 0x00000000 ' + LIB_A_PATH + '+0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))

    # Hexadecimal addresses are required.
    expected = '#00 0xghijklmn ' + LIB_A_PATH + '+0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))
    expected = '#00 0x00000000 ' + LIB_A_PATH + '+0xghijklmn\n'
    self.assertEqual(expected, RunSymbolizer(expected))

    # Addresses must be exactly 8 characters.
    expected = '#00 0x0000000 ' + LIB_A_PATH + '+0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))
    expected = '#00 0x000000000 ' + LIB_A_PATH + '+0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))

    expected = '#00 0x0000000 ' + LIB_A_PATH + '+0x0000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))
    expected = '#00 0x000000000 ' + LIB_A_PATH + '+0x000000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))

    # Addresses must be prefixed with '0x'.
    expected = '#00 00000000 ' + LIB_A_PATH + '+0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))
    expected = '#00 0x00000000 ' + LIB_A_PATH + '+00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))

    # Library name is required.
    expected = '#00 0x00000000\n'
    self.assertEqual(expected, RunSymbolizer(expected))
    expected = '#00 0x00000000 +0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))

    # Library name must be followed by offset with no spaces around '+'.
    expected = '#00 0x00000000 ' + LIB_A_PATH + ' +0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))
    expected = '#00 0x00000000 ' + LIB_A_PATH + '+ 0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))
    expected = '#00 0x00000000 ' + LIB_A_PATH + ' 0x00000254\n'
    self.assertEqual(expected, RunSymbolizer(expected))
    expected = '#00 0x00000000 ' + LIB_A_PATH + '+\n'
    self.assertEqual(expected, RunSymbolizer(expected))

  def testSingleLine(self):
    text = '#00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n'
    expected = '#00 0x00000000 A::Bar(char const*)\n'
    actual = RunSymbolizer(text)
    self.assertEqual(expected, actual)

  def testSingleLineWithSurroundingText(self):
    text = 'LEFT #00 0x00000000 ' + LIB_A_PATH + '+0x00000254 RIGHT\n'
    expected = 'LEFT #00 0x00000000 A::Bar(char const*) RIGHT\n'
    actual = RunSymbolizer(text)
    self.assertEqual(expected, actual)

  def testMultipleLinesSameLibrary(self):
    text = '#00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n'
    text += '#01 0x00000000 ' + LIB_A_PATH + '+0x00000234\n'
    expected = '#00 0x00000000 A::Bar(char const*)\n'
    expected += '#01 0x00000000 A::Foo(int)\n'
    actual = RunSymbolizer(text)
    self.assertEqual(expected, actual)

  def testMultipleLinesDifferentLibrary(self):
    text = '#00 0x00000000 ' + LIB_A_PATH + '+0x00000254\n'
    text += '#01 0x00000000 ' + LIB_B_PATH + '+0x00000234\n'
    expected = '#00 0x00000000 A::Bar(char const*)\n'
    expected += '#01 0x00000000 B::Baz(float)\n'
    actual = RunSymbolizer(text)
    self.assertEqual(expected, actual)

  def testMultipleLinesWithSurroundingTextEverywhere(self):
    text = 'TOP\n'
    text += 'LEFT #00 0x00000000 ' + LIB_A_PATH + '+0x00000254 RIGHT\n'
    text += 'LEFT #01 0x00000000 ' + LIB_B_PATH + '+0x00000234 RIGHT\n'
    text += 'BOTTOM\n'
    expected = 'TOP\n'
    expected += 'LEFT #00 0x00000000 A::Bar(char const*) RIGHT\n'
    expected += 'LEFT #01 0x00000000 B::Baz(float) RIGHT\n'
    expected += 'BOTTOM\n'
    actual = RunSymbolizer(text)
    self.assertEqual(expected, actual)


if __name__ == '__main__':
  unittest.main()