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