xref: /aosp_15_r20/external/google-breakpad/src/tools/python/tests/filter_syms_unittest.py (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1#!/usr/bin/env python3
2# Copyright 2012 Google LLC
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google LLC nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30"""Unit tests for filter_syms.py"""
31
32import io
33import ntpath
34import os
35import sys
36import unittest
37
38ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
39sys.path.insert(0, os.path.join(ROOT_DIR, '..'))
40
41# In root
42import filter_syms
43
44class FilterSysmsTest(unittest.TestCase):
45  def assertParsed(self, input_data, ignored_prefixes, expected):
46    input_io = io.StringIO(input_data)
47    output_io = io.StringIO()
48    parser = filter_syms.SymbolFileParser(input_io, output_io,
49                                          ignored_prefixes, ntpath)
50    parser.Process()
51    self.assertEqual(output_io.getvalue(), expected)
52
53  def testDuplicateFiles(self):
54    """Tests that duplicate files in FILE records are correctly removed and
55    that Line records are updated."""
56
57    INPUT = \
58"""MODULE windows x86 111111111111111111111111111111111 module1.pdb
59INFO CODE_ID FFFFFFFF module1.exe
60FILE 1 foo/../file1_1.cc
61FILE 2 bar/../file1_1.cc
62FILE 3 baz/../file1_1.cc
63FUNC 1000 c 0 Function1_1
641000 8 45 2
651008 4 46 3
66100c 4 44 1
67"""
68    EXPECTED_OUTPUT = \
69"""MODULE windows x86 111111111111111111111111111111111 module1.pdb
70INFO CODE_ID FFFFFFFF module1.exe
71FILE 1 file1_1.cc
72FUNC 1000 c 0 Function1_1
731000 8 45 1
741008 4 46 1
75100c 4 44 1
76"""
77    self.assertParsed(INPUT, [], EXPECTED_OUTPUT)
78
79  def testIgnoredPrefix(self):
80    """Tests that prefixes in FILE records are correctly removed."""
81
82    INPUT = \
83"""MODULE windows x86 111111111111111111111111111111111 module1.pdb
84INFO CODE_ID FFFFFFFF module1.exe
85FILE 1 /src/build/foo/../file1_1.cc
86FILE 2 /src/build/bar/../file1_2.cc
87FILE 3 /src/build/baz/../file1_2.cc
88FUNC 1000 c 0 Function1_1
891000 8 45 2
901008 4 46 3
91100c 4 44 1
92"""
93    EXPECTED_OUTPUT = \
94"""MODULE windows x86 111111111111111111111111111111111 module1.pdb
95INFO CODE_ID FFFFFFFF module1.exe
96FILE 1 file1_1.cc
97FILE 2 file1_2.cc
98FUNC 1000 c 0 Function1_1
991000 8 45 2
1001008 4 46 2
101100c 4 44 1
102"""
103    IGNORED_PREFIXES = ['\\src\\build\\']
104    self.assertParsed(INPUT, IGNORED_PREFIXES, EXPECTED_OUTPUT)
105
106  def testIgnoredPrefixesDuplicateFiles(self):
107    """Tests that de-duplication of FILE records happens BEFORE prefixes
108    are removed."""
109
110    INPUT = \
111"""MODULE windows x86 111111111111111111111111111111111 module1.pdb
112INFO CODE_ID FFFFFFFF module1.exe
113FILE 1 /src/build/foo/../file1_1.cc
114FILE 2 /src/build/bar/../file1_2.cc
115FILE 3 D:/src/build2/baz/../file1_2.cc
116FUNC 1000 c 0 Function1_1
1171000 8 45 2
1181008 4 46 3
119100c 4 44 1
120"""
121    EXPECTED_OUTPUT = \
122"""MODULE windows x86 111111111111111111111111111111111 module1.pdb
123INFO CODE_ID FFFFFFFF module1.exe
124FILE 1 file1_1.cc
125FILE 2 file1_2.cc
126FILE 3 file1_2.cc
127FUNC 1000 c 0 Function1_1
1281000 8 45 2
1291008 4 46 3
130100c 4 44 1
131"""
132    IGNORED_PREFIXES = ['\\src\\build\\', 'D:\\src\\build2\\']
133    self.assertParsed(INPUT, IGNORED_PREFIXES, EXPECTED_OUTPUT)
134
135if __name__ == '__main__':
136  unittest.main()
137