summaryrefslogtreecommitdiffstats
path: root/omaha_server/crash/tests/test_utils.py
blob: a7f824583c2bc98dbd4fbd9cef0e0e2f7d67890e (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
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
# coding: utf8

"""
This software is licensed under the Apache 2 license, quoted below.

Copyright 2014 Crystalnix Limited

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
"""

from builtins import bytes

import os

from mock import patch

from django import test
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse

from omaha_server.utils import is_private
from crash.utils import (
    get_stacktrace,
    add_signature_to_frame,
    parse_stacktrace,
    get_signature,
    send_stacktrace_sentry,
    parse_debug_meta_info,
)

from crash.models import Crash
from crash.tests.test_stacktrace_to_json import stacktrace


BASE_DIR = os.path.dirname(__file__)
TEST_DATA_DIR = os.path.join(BASE_DIR, 'testdata')
SYMBOLS_PATH = os.path.join(TEST_DATA_DIR, 'symbols')
CRASH_DUMP_PATH = os.path.join(TEST_DATA_DIR, '7b05e196-7e23-416b-bd13-99287924e214.dmp')
STACKTRACE_PATH = os.path.join(TEST_DATA_DIR, 'stacktrace.txt')


class ShTest(test.TestCase):
    def test_get_stacktrace(self):
        with open(STACKTRACE_PATH, 'rb') as f:
            stacktrace = f.read()

        rezult, stderr = get_stacktrace(CRASH_DUMP_PATH)

        self.assertEqual(bytes(rezult.stdout, 'utf8'), stacktrace)


class SignatureTest(test.TestCase):
    test_data = [
        ({'frame': 0,
          'module': 'bad.dll',
          'function': 'Func(A * a,B b)',
          'file': 'hg:hg.m.org/repo/name:dname/fname:rev',
          'line': 576},
         {'function': 'Func(A* a, B b)',
          'short_signature': 'Func',
          'line': 576,
          'file': 'hg:hg.m.org/repo/name:dname/fname:rev',
          'frame': 0,
          'signature': 'Func(A* a, B b)',
          'module': 'bad.dll'}),
        ({'file': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
          'frame': 0,
          'function': 'crashedFunc()',
          'line': 34,
          'module': 'BreakpadTestApp.exe'},
         {'file': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
          'frame': 0,
          'function': 'crashedFunc()',
          'line': 34,
          'module': 'BreakpadTestApp.exe',
          'short_signature': 'crashedFunc',
          'signature': 'crashedFunc()'})
    ]

    def test_add_signature_to_frame(self):
        for i in self.test_data:
            self.assertDictEqual(add_signature_to_frame(i[0]), i[1])

    def test_parse_stacktrace(self):
        stacktrace_dict = parse_stacktrace(stacktrace)
        self.assertDictEqual(stacktrace_dict['crashing_thread']['frames'][0],
                             {'abs_path': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
                              'frame': 0,
                              'function': 'crashedFunc()',
                              'lineno': 34,
                              'filename': 'BreakpadTestApp.exe',
                              'short_signature': 'crashedFunc',
                              'signature': 'crashedFunc()'})

    def test_get_signature(self):
        self.assertEqual(get_signature(parse_stacktrace(stacktrace)), 'crashedFunc()')


class SendStackTraceTest(test.TestCase):
    @patch('crash.utils.client')
    @test.override_settings(HOST_NAME='example.com',
                            CELERY_EAGER_PROPAGATES_EXCEPTIONS=False,)
    @is_private(False)
    def test_send_stacktrace_sentry(self, mock_client):
        meta = dict(
            lang='en',
            version='1.0.0.1',
        )
        crash = Crash(
            pk=123,
            upload_file_minidump=SimpleUploadedFile('./dump.dat', False),
            stacktrace=stacktrace,
            stacktrace_json=parse_stacktrace(stacktrace),
            appid='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
            userid='{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}',
            meta=meta,
            signature='signature',
        )

        send_stacktrace_sentry(crash)

        extra = {
            'crash_admin_panel_url': 'http://{}{}'.format(
                'example.com',
                '/admin/crash/crash/%s/' % crash.pk),
            'crashdump_url': crash.upload_file_minidump.url,
            'lang': 'en',
            'version': '1.0.0.1'}

        data = {
            'sentry.interfaces.User': {
                'id': '{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}'
            },
            'sentry.interfaces.Exception': {
                'values': [
                    {'stacktrace': {
                        'frames': [
                            {'function': 'crashedFunc()',
                             'abs_path': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
                             'short_signature': 'crashedFunc',
                             'frame': 0,
                             'filename': 'BreakpadTestApp.exe',
                             'lineno': 34,
                             'signature': 'crashedFunc()'},
                            {'function': 'deeperFunc()',
                             'abs_path': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
                             'short_signature': 'deeperFunc',
                             'frame': 1,
                             'filename': 'BreakpadTestApp.exe',
                             'lineno': 39,
                             'signature': 'deeperFunc()'},
                            {'function': 'deepFunc()',
                             'abs_path': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
                             'short_signature': 'deepFunc',
                             'frame': 2,
                             'filename': 'BreakpadTestApp.exe',
                             'lineno': 44,
                             'signature': 'deepFunc()'},
                            {'function': 'anotherFunc()',
                             'abs_path': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
                             'short_signature': 'anotherFunc',
                             'frame': 3,
                             'filename': 'BreakpadTestApp.exe',
                             'lineno': 49,
                             'signature': 'anotherFunc()'},
                            {'function': 'someFunc()',
                             'abs_path': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
                             'short_signature': 'someFunc',
                             'frame': 4,
                             'filename': 'BreakpadTestApp.exe',
                             'lineno': 54,
                             'signature': 'someFunc()'}, {
                                'function': 'func()',
                                'abs_path': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
                                'short_signature': 'func',
                                'frame': 5,
                                'filename': 'BreakpadTestApp.exe',
                                'lineno': 59,
                                'signature': 'func()'},
                            {'function': 'wmain',
                             'abs_path': r'c:\work\breakpadtestapp\breakpadtestapp\breakpadtestapp.cpp',
                             'short_signature': 'wmain',
                             'frame': 6,
                             'filename': 'BreakpadTestApp.exe',
                             'lineno': 84,
                             'signature': 'wmain'}, {
                                'function': '__tmainCRTStartup',
                                'abs_path': r'f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c',
                                'short_signature': '__tmainCRTStartup',
                                'frame': 7,
                                'filename': 'BreakpadTestApp.exe',
                                'lineno': 579,
                                'signature': '__tmainCRTStartup'},
                            {'frame': 8,
                             'module_offset': '0x13676',
                             'signature': 'kernel32.dll@0x13676',
                             'short_signature': 'kernel32.dll@0x13676',
                             'filename': 'kernel32.dll'},
                            {'frame': 9,
                             'module_offset': '0x39d41',
                             'signature': 'ntdll.dll@0x39d41',
                             'short_signature': 'ntdll.dll@0x39d41',
                             'filename': 'ntdll.dll'}],
                        'total_frames': 11, 'threads_index': 0},
                     'type': 'EXCEPTION_ACCESS_VIOLATION_WRITE',
                     'value': '0x0'}
                ]
            }
        }

        tags = {
            'cpu_arch': 'x86',
            'cpu_count': 4,
            'appid': '{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
            'cpu_info': 'GenuineIntel family 6 model 42 stepping 7',
            'os': 'Windows NT',
            'os_ver': '6.1.7600'
        }

        mock_client.capture.assert_called_once_with(
            'raven.events.Message', message='signature',
            extra=extra,
            data=data,
            tags=tags,
        )


class UtilsTest(test.TestCase):
    def test_parse_debug_meta_info(self):
        head = b'MODULE windows x86 C1C0FA629EAA4B4D9DD2ADE270A231CC1 BreakpadTestApp.pdb'
        self.assertDictEqual(parse_debug_meta_info(head),
                             dict(debug_id='C1C0FA629EAA4B4D9DD2ADE270A231CC1',
                                  debug_file='BreakpadTestApp.pdb'))

        head = b'MODULE mac x86_64 476350E1D4033CF180A2A7E388A7B6E40 Chromium Framework'
        self.assertDictEqual(parse_debug_meta_info(head),
                             dict(debug_id='476350E1D4033CF180A2A7E388A7B6E40',
                                  debug_file='Chromium Framework'))