1 # Copyright (C) 2011 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 from webkitpy.layout_tests.port.leakdetector import LeakDetector
32 from webkitpy.common.system.filesystem_mock import MockFileSystem
33 from webkitpy.common.system.outputcapture import OutputCapture
34 from webkitpy.tool.mocktool import MockExecutive
37 class LeakDetectorTest(unittest.TestCase):
39 class MockPort(object):
41 self._filesystem = MockFileSystem()
42 self._executive = MockExecutive()
46 def _make_detector(self):
47 return LeakDetector(self._mock_port())
49 def test_leaks_args(self):
50 detector = self._make_detector()
51 detector._callstacks_to_exclude_from_leaks = lambda: ['foo bar', 'BAZ']
52 detector._types_to_exlude_from_leaks = lambda: ['abcdefg', 'hi jklmno']
53 expected_args = ['--exclude-callstack="foo bar"', '--exclude-callstack="BAZ"', '--exclude-type="abcdefg"', '--exclude-type="hi jklmno"', 1234]
54 self.assertEquals(detector._leaks_args(1234), expected_args)
56 example_leaks_output = """Process 5122: 663744 nodes malloced for 78683 KB
57 Process 5122: 337301 leaks for 6525216 total leaked bytes.
58 Leak: 0x38cb600 size=3072 zone: DefaultMallocZone_0x1d94000 instance of 'NSCFData', type ObjC, implemented in Foundation
59 0xa033f0b8 0x01001384 0x00000b3a 0x00000b3a ..3.....:...:...
60 0x00000000 0x038cb620 0x00000000 0x00000000 .... ...........
61 0x00000000 0x21000000 0x726c6468 0x00000000 .......!hdlr....
62 0x00000000 0x7269646d 0x6c707061 0x00000000 ....mdirappl....
63 0x00000000 0x04000000 0x736c69c1 0x00000074 .........ilst...
64 0x6f74a923 0x0000006f 0x7461641b 0x00000061 #.too....data...
65 0x00000001 0x76614c00 0x2e323566 0x302e3236 .....Lavf52.62.0
66 0x37000000 0x6d616ea9 0x2f000000 0x61746164 ...7.nam.../data
68 Leak: 0x2a9c960 size=288 zone: DefaultMallocZone_0x1d94000
69 0x09a1cc47 0x1bda8560 0x3d472cd1 0xfbe9bccd G...`....,G=....
70 0x8bcda008 0x9e972a91 0xa892cf63 0x2448bdb0 .....*..c.....H$
71 0x4736fc34 0xdbe2d94e 0x25f56688 0x839402a4 4.6GN....f.%....
72 0xd12496b3 0x59c40c12 0x8cfcab2a 0xd20ef9c4 ..$....Y*.......
73 0xe7c56b1b 0x5835af45 0xc69115de 0x6923e4bb .k..E.5X......#i
74 0x86f15553 0x15d40fa9 0x681288a4 0xc33298a9 SU.........h..2.
75 0x439bb535 0xc4fc743d 0x7dfaaff8 0x2cc49a4a 5..C=t.....}J..,
76 0xdd119df8 0x7e086821 0x3d7d129e 0x2e1b1547 ....!h.~..}=G...
78 Leak: 0x25102fe0 size=176 zone: DefaultMallocZone_0x1d94000 string 'NSException Data'
81 def test_parse_leaks_output(self):
82 self.assertEquals(self._make_detector()._parse_leaks_output(self.example_leaks_output, 5122), (337301, 0, 6525216))
84 def test_leaks_files_in_directory(self):
85 detector = self._make_detector()
86 self.assertEquals(detector.leaks_files_in_directory('/bogus-directory'), [])
87 detector._filesystem = MockFileSystem({
88 '/mock-results/DumpRenderTree-1234-leaks.txt': '',
89 '/mock-results/DumpRenderTree-23423-leaks.txt': '',
90 '/mock-results/DumpRenderTree-823-leaks.txt': '',
92 self.assertEquals(len(detector.leaks_files_in_directory('/mock-results')), 3)
94 def test_parse_leak_files(self):
95 detector = self._make_detector()
97 def mock_run_script(name, args, include_configuration_arguments=False):
98 print "MOCK _run_script: %s %s" % (name, args)
99 return """1 calls for 16 bytes: -[NSURLRequest mutableCopyWithZone:] | +[NSObject(NSObject) allocWithZone:] | _internal_class_createInstanceFromZone | calloc | malloc_zone_calloc
101 total: 5,888 bytes (0 bytes excluded)."""
102 detector._port._run_script = mock_run_script
104 leak_files = ['/mock-results/DumpRenderTree-1234-leaks.txt', '/mock-results/DumpRenderTree-1235-leaks.txt']
105 expected_stdout = "MOCK _run_script: parse-malloc-history ['--merge-depth', 5, '/mock-results/DumpRenderTree-1234-leaks.txt', '/mock-results/DumpRenderTree-1235-leaks.txt']\n"
106 results_tuple = OutputCapture().assert_outputs(self, detector.parse_leak_files, [leak_files], expected_stdout=expected_stdout)
107 self.assertEquals(results_tuple, ("5,888 bytes", 1))