-
Notifications
You must be signed in to change notification settings - Fork 7
/
EmbraceInternalInterfaceImplTest.kt
279 lines (254 loc) · 9.11 KB
/
EmbraceInternalInterfaceImplTest.kt
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
package io.embrace.android.embracesdk
import android.net.Uri
import android.webkit.URLUtil
import io.embrace.android.embracesdk.config.local.LocalConfig
import io.embrace.android.embracesdk.config.local.SdkLocalConfig
import io.embrace.android.embracesdk.config.remote.AnrRemoteConfig
import io.embrace.android.embracesdk.config.remote.NetworkSpanForwardingRemoteConfig
import io.embrace.android.embracesdk.fakes.FakeClock
import io.embrace.android.embracesdk.fakes.FakeConfigService
import io.embrace.android.embracesdk.fakes.fakeAnrBehavior
import io.embrace.android.embracesdk.fakes.fakeAutoDataCaptureBehavior
import io.embrace.android.embracesdk.fakes.fakeNetworkSpanForwardingBehavior
import io.embrace.android.embracesdk.fakes.injection.FakeInitModule
import io.embrace.android.embracesdk.internal.ApkToolsConfig
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest
import io.embrace.android.embracesdk.network.http.HttpMethod
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.slot
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.net.SocketException
internal class EmbraceInternalInterfaceImplTest {
private lateinit var internalImpl: EmbraceInternalInterfaceImpl
private lateinit var embraceImpl: EmbraceImpl
private lateinit var fakeClock: FakeClock
private lateinit var initModule: FakeInitModule
private lateinit var fakeConfigService: FakeConfigService
@Before
fun setUp() {
embraceImpl = mockk(relaxed = true)
fakeClock = FakeClock(currentTime = beforeObjectInitTime)
initModule = FakeInitModule(clock = fakeClock)
fakeConfigService = FakeConfigService()
internalImpl = EmbraceInternalInterfaceImpl(
embraceImpl,
initModule,
fakeConfigService,
initModule.openTelemetryModule.internalTracer
)
ApkToolsConfig.IS_NETWORK_CAPTURE_DISABLED = false
}
@Test
fun testLogInfo() {
internalImpl.logInfo("", emptyMap())
verify(exactly = 1) {
embraceImpl.logMessage(
EventType.INFO_LOG,
"",
emptyMap<String, String>(),
null,
null,
LogExceptionType.NONE,
null,
null
)
}
}
@Test
fun testLogWarning() {
internalImpl.logWarning("", emptyMap(), null)
verify(exactly = 1) {
embraceImpl.logMessage(
EventType.WARNING_LOG,
"",
emptyMap<String, String>(),
null,
null,
LogExceptionType.NONE,
null,
null
)
}
}
@Test
fun testLogError() {
internalImpl.logError("", emptyMap(), null, false)
verify(exactly = 1) {
embraceImpl.logMessage(
EventType.ERROR_LOG,
"",
emptyMap<String, String>(),
null,
null,
LogExceptionType.NONE,
null,
null
)
}
}
@Suppress("DEPRECATION")
@Test
fun testLogHandledException() {
val exception = Throwable("handled exception")
internalImpl.logHandledException(exception, LogType.ERROR, emptyMap(), null)
verify(exactly = 1) {
embraceImpl.logMessage(
EventType.ERROR_LOG,
"handled exception",
emptyMap<String, String>(),
exception.stackTrace,
null,
LogExceptionType.NONE,
null,
null
)
}
}
@Test
fun testCompletedNetworkRequest() {
mockkStatic(Uri::class)
mockkStatic(URLUtil::class)
every { Uri.parse("https://google.com") } returns mockk(relaxed = true)
every { URLUtil.isHttpsUrl("https://google.com") } returns true
internalImpl.recordCompletedNetworkRequest(
"https://google.com",
"get",
15092342340,
15092342799,
140,
2509,
200,
null,
null
)
val captor = slot<EmbraceNetworkRequest>()
verify(exactly = 1) {
embraceImpl.recordNetworkRequest(capture(captor))
}
val request = captor.captured
assertEquals("https://google.com", request.url)
assertEquals(HttpMethod.GET.name, request.httpMethod)
assertEquals(15092342340L, request.startTime)
assertEquals(15092342799L, request.endTime)
assertEquals(140L, request.bytesSent)
assertEquals(2509L, request.bytesReceived)
assertEquals(200, request.responseCode)
assertNull(request.error)
assertNull(request.networkCaptureData)
}
@Test
fun testIncompleteNetworkRequest() {
mockkStatic(Uri::class)
mockkStatic(URLUtil::class)
every { Uri.parse("https://google.com") } returns mockk(relaxed = true)
every { URLUtil.isHttpsUrl("https://google.com") } returns true
val exc = RuntimeException("Whoops")
internalImpl.recordIncompleteNetworkRequest(
"https://google.com",
"get",
15092342340L,
15092342799L,
exc,
"id-123",
null
)
val captor = slot<EmbraceNetworkRequest>()
verify(exactly = 1) {
embraceImpl.recordNetworkRequest(capture(captor))
}
val request = captor.captured
assertEquals("https://google.com", request.url)
assertEquals(HttpMethod.GET.name, request.httpMethod)
assertEquals(15092342340L, request.startTime)
assertEquals(15092342799L, request.endTime)
assertNull(request.error)
assertEquals("id-123", request.traceId)
assertNull(request.networkCaptureData)
}
@Test
fun testRecordAndDeduplicateNetworkRequest() {
val url = "https://embrace.io"
val callId = "testID"
val captor = slot<EmbraceNetworkRequest>()
val networkRequest: EmbraceNetworkRequest = EmbraceNetworkRequest.fromCompletedRequest(
url,
HttpMethod.GET,
15092342340L,
15092342799L,
140L,
2509L,
200
)
internalImpl.recordAndDeduplicateNetworkRequest(callId, networkRequest)
verify(exactly = 1) {
embraceImpl.recordAndDeduplicateNetworkRequest(callId, capture(captor))
}
assertEquals(url, captor.captured.url)
}
@Test
fun `check usage of SDK time`() {
assertEquals(beforeObjectInitTime, internalImpl.getSdkCurrentTime())
assertTrue(internalImpl.getSdkCurrentTime() < System.currentTimeMillis())
fakeClock.tick(10L)
assertEquals(fakeClock.now(), internalImpl.getSdkCurrentTime())
}
@Test
fun `test isInternalNetworkCaptureDisabled`() {
assertFalse(internalImpl.isInternalNetworkCaptureDisabled())
ApkToolsConfig.IS_NETWORK_CAPTURE_DISABLED = true
assertTrue(internalImpl.isInternalNetworkCaptureDisabled())
}
@Test
fun `check isNetworkSpanForwardingEnabled`() {
assertFalse(internalImpl.isNetworkSpanForwardingEnabled())
fakeConfigService.networkSpanForwardingBehavior =
fakeNetworkSpanForwardingBehavior(remoteConfig = { NetworkSpanForwardingRemoteConfig(pctEnabled = 100.0f) })
assertTrue(internalImpl.isNetworkSpanForwardingEnabled())
}
@Test
fun `check isAnrCaptureEnabled`() {
assertTrue(internalImpl.isAnrCaptureEnabled())
fakeConfigService.anrBehavior = fakeAnrBehavior(remoteCfg = { AnrRemoteConfig(pctEnabled = 0) })
assertFalse(internalImpl.isAnrCaptureEnabled())
fakeConfigService.anrBehavior = fakeAnrBehavior(remoteCfg = { AnrRemoteConfig(pctEnabled = 100) })
assertTrue(internalImpl.isAnrCaptureEnabled())
}
@Test
fun `check isNdkEnabled`() {
assertFalse(internalImpl.isNdkEnabled())
fakeConfigService.autoDataCaptureBehavior =
fakeAutoDataCaptureBehavior(localCfg = { LocalConfig("abcde", true, SdkLocalConfig()) })
assertTrue(internalImpl.isNdkEnabled())
}
@Test
fun `check logInternalError with exception`() {
val expectedException = SocketException()
internalImpl.logInternalError(expectedException)
verify(exactly = 1) {
embraceImpl.logInternalError(expectedException)
}
}
@Test
fun `check logInternalError with error type and message`() {
internalImpl.logInternalError("err", "message")
verify(exactly = 1) {
embraceImpl.logInternalError("err", "message")
}
}
@Test
fun `check stopping SDK`() {
internalImpl.stopSdk()
assertFalse(embraceImpl.isStarted)
}
companion object {
private val beforeObjectInitTime = System.currentTimeMillis() - 1
}
}