-
Notifications
You must be signed in to change notification settings - Fork 133
/
CrashReportTests.swift
379 lines (314 loc) · 16.2 KB
/
CrashReportTests.swift
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-2020 Datadog, Inc.
*/
import XCTest
@testable import DatadogCrashReporting
import CrashReporter
class CrashReportTests: XCTestCase {
// MARK: - Consistency
func testGivenPLCrashReportWithConsistentValues_whenInitializing_itReturnsValue() throws {
// Given
let mockStackFrame = PLCrashReportMock.StackFrame()
let mockThread = PLCrashReportMock.ThreadInfo()
mockThread.mockStackFrames = [mockStackFrame]
let mockImage = PLCrashReportMock.BinaryImageInfo()
mockImage.mockHasImageUUID = true
mockImage.mockImageUUID = .mockRandom()
mockImage.mockImageName = .mockRandom()
let mock = PLCrashReportMock()
mock.mockUUIDRef = CFUUIDCreate(nil)
mock.mockSystemInfo = .init()
mock.mockProcessInfo = .init()
mock.mockHasProcessInfo = true
mock.mockSignalInfo = .init()
mock.mockExceptionInfo = .init()
mock.mockHasExceptionInfo = true
mock.mockThreads = [mockThread]
mock.mockImages = [mockImage]
// When
let crashReport = try CrashReport(from: mock)
// Then
XCTAssertNotNil(crashReport.incidentIdentifier)
XCTAssertNotNil(crashReport.systemInfo)
XCTAssertNotNil(crashReport.processInfo)
XCTAssertNotNil(crashReport.signalInfo)
XCTAssertNotNil(crashReport.exceptionInfo)
XCTAssertEqual(crashReport.threads.count, 1)
XCTAssertEqual(crashReport.threads[0].stackFrames.count, 1)
XCTAssertEqual(crashReport.binaryImages.count, 1)
}
func testGivenPLCrashReportWithNoThreadsAndImages_whenInitializing_itReturnsNil() throws {
// Given
let mock = PLCrashReportMock()
mock.mockThreads = nil
mock.mockImages = nil
// When
XCTAssertThrowsError(try CrashReport(from: mock)) { error in
// Then
let exception = error as! CrashReportException
XCTAssertEqual(exception.description, "Received inconsistent `PLCrashReport` # has threads = false, has images = false")
}
}
func testGivenPLCrashReportWithSomeInconsistentValues_whenInitializing_itReturnsValue() throws {
// Given
let mock = PLCrashReportMock()
mock.mockUUIDRef = Bool.random() ? CFUUIDCreate(nil) : nil
mock.mockSystemInfo = Bool.random() ? .init() : nil
mock.mockProcessInfo = Bool.random() ? .init() : nil
mock.mockHasProcessInfo = Bool.random()
mock.mockSignalInfo = Bool.random() ? .init() : nil
mock.mockExceptionInfo = Bool.random() ? .init() : nil
mock.mockHasExceptionInfo = Bool.random()
mock.mockThreads = [.init()]
mock.mockImages = [.init()]
// When
let crashReport = try CrashReport(from: mock)
// Then
XCTAssertNotNil(crashReport, "It should initialize as long as it has threads and images")
}
// MARK: - Values
func testItReadsIncidentIdentifier() throws {
// Given
let uuid = UUID().uuidString
let mock = PLCrashReportMock()
mock.mockUUIDRef = CFUUIDCreateFromString(nil, uuid as CFString)
mock.mockThreads = [.init()]
mock.mockImages = [.init()]
// When
let crashReport = try XCTUnwrap(CrashReport(from: mock))
// Then
XCTAssertEqual(crashReport.incidentIdentifier, uuid)
}
func testItReadsSystemInfo() throws {
// Given
let mock = PLCrashReportMock()
mock.mockSystemInfo = .init()
mock.mockSystemInfo.mockTimestamp = .mockRandomInThePast()
mock.mockThreads = [.init()]
mock.mockImages = [.init()]
// When
let crashReport = try XCTUnwrap(CrashReport(from: mock))
// Then
XCTAssertEqual(crashReport.systemInfo?.timestamp, mock.mockSystemInfo.mockTimestamp)
}
func testItReadsProcessInfo() throws {
// Given
let mock = PLCrashReportMock()
mock.mockHasProcessInfo = true
mock.mockProcessInfo = .init()
mock.mockProcessInfo.mockProcessName = .mockRandom()
mock.mockProcessInfo.mockProcessPath = .mockRandom()
mock.mockProcessInfo.mockParentProcessID = .mockRandom()
mock.mockProcessInfo.mockParentProcessName = .mockRandom()
mock.mockThreads = [.init()]
mock.mockImages = [.init()]
// When
let crashReport = try XCTUnwrap(CrashReport(from: mock))
// Then
XCTAssertEqual(crashReport.processInfo?.processName, mock.mockProcessInfo.mockProcessName)
XCTAssertEqual(crashReport.processInfo?.processPath, mock.mockProcessInfo.mockProcessPath)
XCTAssertEqual(crashReport.processInfo?.parentProcessID, mock.mockProcessInfo.mockParentProcessID)
XCTAssertEqual(crashReport.processInfo?.parentProcessName, mock.mockProcessInfo.mockParentProcessName)
}
func testItReadsSignalInfo() throws {
// Given
let mock = PLCrashReportMock()
mock.mockSignalInfo = .init()
mock.mockSignalInfo.mockName = .mockRandom()
mock.mockSignalInfo.mockCode = .mockRandom()
mock.mockSignalInfo.mockAddress = .mockRandom()
mock.mockThreads = [.init()]
mock.mockImages = [.init()]
// When
let crashReport = try XCTUnwrap(CrashReport(from: mock))
// Then
XCTAssertEqual(crashReport.signalInfo?.name, mock.mockSignalInfo.mockName)
XCTAssertEqual(crashReport.signalInfo?.code, mock.mockSignalInfo.mockCode)
XCTAssertEqual(crashReport.signalInfo?.address, mock.mockSignalInfo.mockAddress)
}
func testItReadsExceptionInfo() throws {
// Given
let mockStackFrames: [PLCrashReportMock.StackFrame] = (0x01..<0x10).map { value in
let mockStackFrame = PLCrashReportMock.StackFrame()
mockStackFrame.mockInstructionPointer = UInt64(value)
return mockStackFrame
}
let mock = PLCrashReportMock()
mock.mockHasExceptionInfo = true
mock.mockExceptionInfo = .init()
mock.mockExceptionInfo.mockExceptionName = .mockRandom()
mock.mockExceptionInfo.mockExceptionReason = .mockRandom()
mock.mockExceptionInfo.mockStackFrames = mockStackFrames
// When
let exceptionInfo = try XCTUnwrap(ExceptionInfo(from: mock))
// Then
XCTAssertEqual(exceptionInfo.name, mock.mockExceptionInfo.mockExceptionName)
XCTAssertEqual(exceptionInfo.reason, mock.mockExceptionInfo.mockExceptionReason)
XCTAssertEqual(exceptionInfo.stackFrames.count, mockStackFrames.count)
}
func testItReadsThreadInfo() throws {
// Given
let mockStackFrames: [PLCrashReportMock.StackFrame] = (0x01..<0x10).map { value in
let mockStackFrame = PLCrashReportMock.StackFrame()
mockStackFrame.mockInstructionPointer = UInt64(value)
return mockStackFrame
}
let mockThread = PLCrashReportMock.ThreadInfo()
mockThread.mockThreadNumber = .mockRandom()
mockThread.mockCrashed = .random()
mockThread.mockStackFrames = mockStackFrames
let mock = PLCrashReportMock()
mock.mockThreads = [mockThread]
// When
let threadInfo = try XCTUnwrap(ThreadInfo(from: mockThread, in: mock))
// Then
XCTAssertEqual(threadInfo.threadNumber, mockThread.mockThreadNumber)
XCTAssertEqual(threadInfo.crashed, mockThread.mockCrashed)
XCTAssertEqual(threadInfo.stackFrames.count, mockStackFrames.count)
}
private let systemImagePaths_device = [
"/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore",
"/usr/lib/system/libdyld.dylib"
]
private let systemImagePaths_simulator = [
"/Users/john.appleseed/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
]
private let userImagePaths_device = [
"/private/var/containers/Bundle/Application/0000/Example.app/Example",
"/private/var/containers/Bundle/Application/0000/Example.app/Frameworks/DatadogCrashReporting.framework/DatadogCrashReporting"
]
private let userImagePaths_simulator = [
"/Users/john.appleseed/Library/Developer/CoreSimulator/Devices/0000/data/Containers/Bundle/Application/0000/Example.app/Example",
"/Users/john.appleseed/Library/Developer/Xcode/DerivedData/Datadog-abcd/Build/Products/Release-iphonesimulator/DatadogCrashReporting.framework/DatadogCrashReporting"
]
func testItDetectsSystemImages() throws {
for systemImagePath in systemImagePaths_device {
XCTAssertTrue(BinaryImageInfo.isPathSystemImageInDevice(systemImagePath), "\(systemImagePath) is a system image")
}
for systemImagePath in systemImagePaths_simulator {
XCTAssertTrue(BinaryImageInfo.isPathSystemImageInSimulator(systemImagePath), "\(systemImagePath) is a system image")
}
for userImagePath in userImagePaths_device {
XCTAssertFalse(BinaryImageInfo.isPathSystemImageInDevice(userImagePath), "\(userImagePath) is an user image")
}
for userImagePath in userImagePaths_simulator {
XCTAssertFalse(BinaryImageInfo.isPathSystemImageInSimulator(userImagePath), "\(userImagePath) is an user image")
}
}
func testItReadsBinaryImageInfo() throws {
func mock(with imagePath: URL) -> PLCrashReportMock.BinaryImageInfo {
let mock = PLCrashReportMock.BinaryImageInfo()
mock.mockImageUUID = .mockRandom()
mock.mockHasImageUUID = true
mock.mockImageName = imagePath.path
mock.mockImageBaseAddress = .mockRandom()
mock.mockImageSize = .mockRandom()
mock.mockCodeType = .init()
mock.mockCodeType.mockTypeEncoding = PLCrashReportProcessorTypeEncodingMach
mock.mockCodeType.mockType = UInt64(CPU_TYPE_X86_64)
return mock
}
// Given
#if targetEnvironment(simulator)
let systemImagePath = URL(string: systemImagePaths_simulator.randomElement()!)!
let userImagePath = URL(string: userImagePaths_simulator.randomElement()!)!
#else
let systemImagePath = URL(string: systemImagePaths_device.randomElement()!)!
let userImagePath = URL(string: userImagePaths_device.randomElement()!)!
#endif
let mockSystemImage = mock(with: systemImagePath)
let mockUserImage = mock(with: userImagePath)
// When
let systemBinaryImageInfo = try XCTUnwrap(BinaryImageInfo(from: mockSystemImage))
let userBinaryImageInfo = try XCTUnwrap(BinaryImageInfo(from: mockUserImage))
// Then
XCTAssertEqual(systemBinaryImageInfo.uuid, mockSystemImage.mockImageUUID)
XCTAssertEqual(systemBinaryImageInfo.imageName, systemImagePath.lastPathComponent)
XCTAssertTrue(systemBinaryImageInfo.isSystemImage, "\(systemImagePath) is a system image")
XCTAssertEqual(systemBinaryImageInfo.imageBaseAddress, mockSystemImage.mockImageBaseAddress)
XCTAssertEqual(systemBinaryImageInfo.imageSize, mockSystemImage.mockImageSize)
XCTAssertEqual(systemBinaryImageInfo.codeType?.architectureName, "x86_64")
XCTAssertEqual(userBinaryImageInfo.uuid, mockUserImage.mockImageUUID)
XCTAssertEqual(userBinaryImageInfo.imageName, userImagePath.lastPathComponent)
XCTAssertFalse(userBinaryImageInfo.isSystemImage, "\(userImagePath) is a user image")
XCTAssertEqual(userBinaryImageInfo.imageBaseAddress, mockUserImage.mockImageBaseAddress)
XCTAssertEqual(userBinaryImageInfo.imageSize, mockUserImage.mockImageSize)
XCTAssertEqual(userBinaryImageInfo.codeType?.architectureName, "x86_64")
}
func testItReadsCodeType() {
typealias ProcessorInfo = PLCrashReportMock.BinaryImageInfo.ProcessorInfo
typealias CodeType = BinaryImageInfo.CodeType
func mockKnownProcessorInfo(type: UInt64, subtype: UInt64) -> ProcessorInfo {
let mock = ProcessorInfo()
mock.mockTypeEncoding = PLCrashReportProcessorTypeEncodingMach
mock.mockType = type
mock.mockSubtype = subtype
return mock
}
func mockUnknownProcessorInfo() -> ProcessorInfo {
let mock = ProcessorInfo()
mock.mockTypeEncoding = PLCrashReportProcessorTypeEncodingUnknown
mock.mockType = .mockRandom()
mock.mockSubtype = .mockRandom()
return mock
}
var mock = mockKnownProcessorInfo(type: UInt64(CPU_TYPE_X86), subtype: .mockRandom())
XCTAssertEqual(CodeType(from: mock)?.architectureName, "i386")
mock = mockKnownProcessorInfo(type: UInt64(CPU_TYPE_X86_64), subtype: .mockRandom())
XCTAssertEqual(CodeType(from: mock)?.architectureName, "x86_64")
mock = mockKnownProcessorInfo(type: UInt64(CPU_TYPE_ARM), subtype: .mockRandom())
XCTAssertEqual(CodeType(from: mock)?.architectureName, "arm")
// We use XOR to get a value different than any XOR component:
mock = mockKnownProcessorInfo(type: UInt64(CPU_TYPE_ARM ^ CPU_TYPE_X86_64 ^ CPU_TYPE_ARM ^ CPU_TYPE_ARM64), subtype: .mockRandom())
XCTAssertNil(CodeType(from: mock)?.architectureName)
mock = mockKnownProcessorInfo(type: UInt64(CPU_TYPE_ARM64), subtype: UInt64(CPU_SUBTYPE_ARM64_ALL))
XCTAssertEqual(CodeType(from: mock)?.architectureName, "arm64")
mock = mockKnownProcessorInfo(type: UInt64(CPU_TYPE_ARM64), subtype: UInt64(CPU_SUBTYPE_ARM64_V8))
XCTAssertEqual(CodeType(from: mock)?.architectureName, "armv8")
mock = mockKnownProcessorInfo(type: UInt64(CPU_TYPE_ARM64), subtype: UInt64(CPU_SUBTYPE_ARM64E))
XCTAssertEqual(CodeType(from: mock)?.architectureName, "arm64e")
// We use XOR to get a value different than any XOR component:
mock = mockKnownProcessorInfo(type: UInt64(CPU_TYPE_ARM64), subtype: UInt64(CPU_SUBTYPE_ARM64_ALL ^ CPU_SUBTYPE_ARM64_V8 ^ CPU_SUBTYPE_ARM64E))
XCTAssertEqual(CodeType(from: mock)?.architectureName, "arm64-unknown")
mock = mockUnknownProcessorInfo()
XCTAssertNil(CodeType(from: mock))
}
func testItReadsStackFrameWhenItsBinaryImageIsFound() {
// Given
let stackFrameNumber: Int = .mockRandom()
let imagePath: URL = .mockRandomPath()
let mockImage = PLCrashReportMock.BinaryImageInfo()
mockImage.mockHasImageUUID = true
mockImage.mockImageUUID = .mockRandom()
mockImage.mockImageName = imagePath.path
mockImage.mockImageBaseAddress = .mockRandom()
let mockStackFrame = PLCrashReportMock.StackFrame()
mockStackFrame.mockInstructionPointer = .mockRandom()
// When
let mock = PLCrashReportMock()
mock.mockImageForAddress[mockStackFrame.mockInstructionPointer] = mockImage // register mock image
let stackFrameInfo = StackFrame(from: mockStackFrame, number: stackFrameNumber, in: mock)
// Then
XCTAssertEqual(stackFrameInfo.number, stackFrameNumber)
XCTAssertEqual(stackFrameInfo.instructionPointer, mockStackFrame.mockInstructionPointer)
XCTAssertEqual(stackFrameInfo.libraryName, imagePath.lastPathComponent)
XCTAssertEqual(stackFrameInfo.libraryBaseAddress, mockImage.mockImageBaseAddress)
}
func testItReadsStackFrameWhenItsBinaryImageIsNotFound() {
// Given
let stackFrameNumber: Int = .mockRandom()
let mockStackFrame = PLCrashReportMock.StackFrame()
mockStackFrame.mockInstructionPointer = .mockRandom()
// When
let mock = PLCrashReportMock()
mock.mockImageForAddress = [:] // do not register any image
let stackFrameInfo = StackFrame(from: mockStackFrame, number: stackFrameNumber, in: mock)
// Then
XCTAssertEqual(stackFrameInfo.number, stackFrameNumber)
XCTAssertEqual(stackFrameInfo.instructionPointer, mockStackFrame.mockInstructionPointer)
XCTAssertNil(stackFrameInfo.libraryName)
XCTAssertNil(stackFrameInfo.libraryBaseAddress)
}
}