File: rename_breakpad_unittest.py

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (321 lines) | stat: -rwxr-xr-x 13,370 bytes parent folder | download | duplicates (6)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env vpython3
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from logging import exception
import os
import sys
import unittest
import tempfile
import shutil

sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir, 'perf'))

from core import path_util
path_util.AddPyUtilsToPath()
path_util.AddTracingToPath()
import py_utils

import rename_breakpad
import mock


class RenameBreakpadFilesTestCase(unittest.TestCase):
  def setUp(self):
    self.breakpad_dir = tempfile.mkdtemp()
    self.breakpad_output_dir = tempfile.mkdtemp()

    # Directory to unzipped breakpad files.
    self.breakpad_unzip_dir = tempfile.mkdtemp(dir=self.breakpad_dir)

  def tearDown(self):
    shutil.rmtree(self.breakpad_dir)
    shutil.rmtree(self.breakpad_output_dir)

  def _listSubtree(self, root):
    """Returns absolute paths of files and dirs in root subtree.
    """
    files = set()
    dirs = set()
    for topdir, subdirs, filenames in os.walk(root):
      for filename in filenames:
        files.add(os.path.join(topdir, filename))
      for subdir in subdirs:
        dirs.add(os.path.join(topdir, subdir))
    return dirs, files

  def _assertFilesInInputDir(self,
                             expected_unmoved_files=frozenset(),
                             expected_unmoved_dirs=frozenset()):
    """Ensures that |RenameBreakpadFiles| doesn't move files/dirs.

    Automatically adds |self.breakpad_unzip_dir| to
    |expected_unmoved_dirs| since this file should never be moved.
    """
    breakpad_unzip_dir = {self.breakpad_unzip_dir}
    expected_unmoved_dirs = expected_unmoved_dirs.union(breakpad_unzip_dir)

    unmoved_dirs, unmoved_files = self._listSubtree(self.breakpad_dir)
    self.assertEqual(expected_unmoved_files, unmoved_files)
    self.assertEqual(expected_unmoved_dirs, unmoved_dirs)

  def _assertFilesInOutputDir(self, expected_moved_files=frozenset()):
    """Ensures that |RenameBreakpadFiles| correctly moves files.
    """
    moved_files = set(os.listdir(self.breakpad_output_dir))
    self.assertEqual(expected_moved_files, moved_files)

  def testRenameBreakpadFiles(self):
    breakpad_file1 = os.path.join(self.breakpad_unzip_dir, 'file1.breakpad')
    breakpad_file2 = os.path.join(self.breakpad_unzip_dir, 'file2.breakpad')

    with open(breakpad_file1, 'w') as file1:
      file1.write('MODULE Linux x86_64 34984AB4EF948C name1.so')
    with open(breakpad_file2, 'w') as file2:
      file2.write('MODULE mac x86_64 29e6f9a7ce00f name2.so')

    rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                        self.breakpad_output_dir)

    self._assertFilesInInputDir()

    expected_moved_files = {'34984AB4EF948C.breakpad', '29E6F9A7CE00F.breakpad'}
    self._assertFilesInOutputDir(expected_moved_files)

  def testRenameBreakpadFilesUpperCaseName(self):
    breakpad_file1 = os.path.join(self.breakpad_unzip_dir, 'file1.breakpad')

    with open(breakpad_file1, 'w') as file1:
      file1.write('MODULE Linux x86_64 12a345b6c7def890 name1.so')

    rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                        self.breakpad_output_dir)

    self._assertFilesInInputDir()

    expected_moved_files = {'12A345B6C7DEF890.breakpad'}
    self._assertFilesInOutputDir(expected_moved_files)

  def testRenameBreakpadFilesLinuxx86_64(self):
    breakpad_file1 = os.path.join(self.breakpad_unzip_dir, 'file1.breakpad.x64')

    with open(breakpad_file1, 'w') as file1:
      file1.write('MODULE Linux x86_64 34984AB4EF948C name1.so')

    rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                        self.breakpad_output_dir)

    self._assertFilesInInputDir()

    expected_moved_files = {'34984AB4EF948C.breakpad'}
    self._assertFilesInOutputDir(expected_moved_files)

  def testRenameBreakpadFilesMultipleSubdirs(self):
    subdir1 = tempfile.mkdtemp(dir=self.breakpad_unzip_dir)
    subdir2 = tempfile.mkdtemp(dir=self.breakpad_unzip_dir)
    breakpad_file1 = os.path.join(subdir1, 'file1.breakpad')
    breakpad_file2 = os.path.join(subdir2, 'file2.breakpad')
    breakpad_file3 = os.path.join(self.breakpad_unzip_dir, 'file3.breakpad')

    with open(breakpad_file1, 'w') as file1:
      file1.write('MODULE Linux x86_64 48537ABD name1.so')
    with open(breakpad_file2, 'w') as file2:
      file2.write('MODULE mac x86_64 38ABC9F name2.so')
    with open(breakpad_file3, 'w') as file3:
      file3.write('MODULE mac x86_64 45DFE name3.so')

    rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                        self.breakpad_output_dir)

    expected_unmoved_dirs = {subdir1, subdir2}
    self._assertFilesInInputDir(expected_unmoved_dirs=expected_unmoved_dirs)

    expected_moved_files = {
        '48537ABD.breakpad', '38ABC9F.breakpad', '45DFE.breakpad'
    }
    self._assertFilesInOutputDir(expected_moved_files)

  def testRenameBreakpadFilesNonBreakpad(self):
    valid_file = os.path.join(self.breakpad_unzip_dir, 'valid.breakpad')
    fake_file = os.path.join(self.breakpad_unzip_dir, 'fake.gz')
    empty_file = os.path.join(self.breakpad_unzip_dir, 'empty.json')
    random_dir = tempfile.mkdtemp(dir=self.breakpad_dir)
    non_breakpad_file = os.path.join(random_dir, 'random.txt')

    with open(valid_file, 'w') as file1:
      file1.write('MODULE mac x86_64 329FDEA987BC name.so')
    with open(fake_file, 'w') as file2:
      file2.write('random text blah blah blah')
    with open(empty_file, 'w'):
      pass
    with open(non_breakpad_file, 'w') as file3:
      file3.write('MODULE mac x86_64 329FDEA987BC name.so')

    rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                        self.breakpad_output_dir)

    expected_unmoved_files = {fake_file, empty_file, non_breakpad_file}
    expected_unmoved_dirs = {random_dir}
    self._assertFilesInInputDir(expected_unmoved_files, expected_unmoved_dirs)

    expected_moved_files = {'329FDEA987BC.breakpad'}
    self._assertFilesInOutputDir(expected_moved_files)

  def testRenameBreakpadFilesInvalidBreakpad(self):
    valid_file = os.path.join(self.breakpad_unzip_dir, 'valid.breakpad')
    empty_file = os.path.join(self.breakpad_unzip_dir, 'empty.breakpad')
    no_module_file = os.path.join(self.breakpad_unzip_dir, 'no-module.breakpad')
    short_file = os.path.join(self.breakpad_unzip_dir, 'short.breakpad')

    with open(valid_file, 'w') as file1:
      file1.write('MODULE mac x86_64 1240DF90E9AC39038EF400 Chrome Name')
    with open(empty_file, 'w'):
      pass
    with open(no_module_file, 'w') as file2:
      file2.write('NOTMODULE mac x86_64 1240DF90E9AC39038EF400 name')
    with open(short_file, 'w') as file3:
      file3.write('MODULE mac 1240DF90E9AC39038EF400 name')

    rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                        self.breakpad_output_dir)

    expected_unmoved_files = {empty_file, no_module_file, short_file}
    self._assertFilesInInputDir(expected_unmoved_files)

    expected_moved_files = {'1240DF90E9AC39038EF400.breakpad'}
    self._assertFilesInOutputDir(expected_moved_files)

  def testRenameBreakpadFilesOnlyNonBreakpadAndMisformat(self):
    fake_file = os.path.join(self.breakpad_unzip_dir, 'fake.breakpad')
    empty_file = os.path.join(self.breakpad_unzip_dir, 'empty.breakpad')
    no_module_file = os.path.join(self.breakpad_unzip_dir, 'no-module.breakpad')
    short_file = os.path.join(self.breakpad_unzip_dir, 'short.breakpad')
    random_dir = tempfile.mkdtemp(dir=self.breakpad_dir)
    non_breakpad_file = os.path.join(random_dir, 'random.txt')

    with open(fake_file, 'w') as file1:
      file1.write('random text blah blah blah')
    with open(empty_file, 'w'):
      pass
    with open(no_module_file, 'w') as file2:
      file2.write('NOTMODULE mac x86_64 1240DF90E9AC39038EF400 name')
    with open(short_file, 'w') as file3:
      file3.write('MODULE mac 1240DF90E9AC39038EF400 name')
    with open(non_breakpad_file, 'w') as file4:
      file4.write('MODULE mac x86_64 1240DF90E9AC39038EF400 Chrome Name')

    rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                        self.breakpad_output_dir)

    expected_unmoved_files = {
        fake_file, non_breakpad_file, empty_file, no_module_file, short_file
    }
    expected_unmoved_dirs = {random_dir}
    self._assertFilesInInputDir(expected_unmoved_files, expected_unmoved_dirs)

    self._assertFilesInOutputDir()

  def testRenameBreakpadFilesRepeatedModuleID(self):
    breakpad_file1 = os.path.join(self.breakpad_unzip_dir, 'file1.breakpad')
    breakpad_file2 = os.path.join(self.breakpad_unzip_dir, 'file2.breakpad')

    with open(breakpad_file1, 'w') as file1:
      file1.write('MODULE mac x86_64 12ABC8987DE name.so')
    with open(breakpad_file2, 'w') as file2:
      file2.write('MODULE mac x86_64 12ABC8987DE name.so')

    # Check that the right exception is raised.
    exception_msg = ('Symbol file modules ids are not unique')

    with self.assertRaises(AssertionError) as e:
      rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                          self.breakpad_output_dir)
    self.assertIn(exception_msg, str(e.exception))

    # Check breakpad file with repeated module id is not moved. More
    # complicated because either of the breakpad files could be moved.
    self.assertTrue(
        os.path.isfile(breakpad_file1) ^ os.path.isfile(breakpad_file2))

    # Ensure one of the breakpad module files got moved. No matter which
    # breakpad file got moved, it will have the same new module-based filename.
    expected_moved_files = {'12ABC8987DE.breakpad'}
    self._assertFilesInOutputDir(expected_moved_files)

  def testRenameBreakpadFilesRepeatedModuleIDMultipleSubdirs(self):
    subdir1 = tempfile.mkdtemp(dir=self.breakpad_unzip_dir)
    subdir2 = tempfile.mkdtemp(dir=self.breakpad_unzip_dir)
    breakpad_file1 = os.path.join(subdir1, 'file1.breakpad')
    breakpad_file2 = os.path.join(subdir2, 'file2.breakpad')

    with open(breakpad_file1, 'w') as file1:
      file1.write('MODULE mac x86_64 ABCE4853004895 name.so')
    with open(breakpad_file2, 'w') as file2:
      file2.write('MODULE mac x86_64 ABCE4853004895 name.so')

    # Check that the right exception is raised.
    exception_msg = ('Symbol file modules ids are not unique')
    with self.assertRaises(AssertionError) as e:
      rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                          self.breakpad_output_dir)
    self.assertIn(exception_msg, str(e.exception))

    # Check breakpad file with repeated module id is not moved. More
    # complicated because either of the breakpad files could be moved.
    self.assertTrue(
        os.path.isfile(breakpad_file1) ^ os.path.isfile(breakpad_file2))

    # Ensure one of the breakpad module files got moved. No matter which
    # breakpad file got moved, it will have the same new module-based filename.
    expected_moved_files = {'ABCE4853004895.breakpad'}
    self._assertFilesInOutputDir(expected_moved_files)

  def testRenameBreakpadFilesInputDirEqualsOutputDir(self):
    subdir = tempfile.mkdtemp(dir=self.breakpad_unzip_dir)
    breakpad_file1 = os.path.join(self.breakpad_unzip_dir, 'file1.breakpad')
    breakpad_file2 = os.path.join(subdir, 'file2.breakpad')

    with open(breakpad_file1, 'w') as file1:
      file1.write('MODULE Linux x86_64 34984AB4EF948C name1.so')
    with open(breakpad_file2, 'w') as file2:
      file2.write('MODULE mac x86_64 29e6f9a7ce00f name2.so')

    rename_breakpad.RenameBreakpadFiles(self.breakpad_dir, self.breakpad_dir)

    # All files should be moved into |self.breakpad_dir|.
    moved_breakpad1 = os.path.join(self.breakpad_dir, '34984AB4EF948C.breakpad')
    moved_breakpad2 = os.path.join(self.breakpad_dir, '29E6F9A7CE00F.breakpad')
    expected_files = {moved_breakpad1, moved_breakpad2}
    expected_unmoved_dirs = {subdir}
    self._assertFilesInInputDir(expected_files, expected_unmoved_dirs)

    # No files should be moved to |self.breakpad_output_dir|.
    self._assertFilesInOutputDir()

  def testRenameBreakpadFilesAllUnmoved(self):
    breakpad_file1 = os.path.join(self.breakpad_unzip_dir, 'file1.breakpad')
    breakpad_file2 = os.path.join(self.breakpad_unzip_dir, 'file2.breakpad')

    with open(breakpad_file1, 'w') as file1:
      file1.write('MODULE Linux x86_64 34984AB4EF948C name1.so')
    with open(breakpad_file2, 'w') as file2:
      file2.write('MODULE mac x86_64 29e6f9a7ce00f name2.so')

    rename_breakpad.RenameBreakpadFiles(self.breakpad_dir,
                                        self.breakpad_unzip_dir)

    # All files should be renamed but not moved.
    unmoved_breakpad1 = os.path.join(self.breakpad_unzip_dir,
                                     '34984AB4EF948C.breakpad')
    unmoved_breakpad2 = os.path.join(self.breakpad_unzip_dir,
                                     '29E6F9A7CE00F.breakpad')
    expected_renamed_files = {unmoved_breakpad1, unmoved_breakpad2}
    self._assertFilesInInputDir(expected_renamed_files)

    # No files should be moved to |self.breakpad_output_dir|.
    self._assertFilesInOutputDir()


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