That fuck shit the fascists are using
1/*
2 * Copyright 2023 Signal Messenger, LLC
3 * SPDX-License-Identifier: AGPL-3.0-only
4 */
5
6package org.tm.archive.database
7
8import org.junit.Test
9import org.signal.core.util.forEach
10import org.signal.core.util.requireLong
11import org.signal.core.util.requireNonNullString
12import org.signal.core.util.select
13import org.signal.core.util.updateAll
14import org.tm.archive.crash.CrashConfig
15import org.tm.archive.dependencies.ApplicationDependencies
16import org.tm.archive.testing.assertIs
17
18class LogDatabaseTest {
19
20 private val db: LogDatabase = LogDatabase.getInstance(ApplicationDependencies.getApplication())
21
22 @Test
23 fun crashTable_matchesNamePattern() {
24 val currentTime = System.currentTimeMillis()
25
26 db.crashes.saveCrash(
27 createdAt = currentTime,
28 name = "TestName",
29 message = "Test Message",
30 stackTrace = "test\nstack\ntrace"
31 )
32
33 val foundMatch = db.crashes.anyMatch(
34 listOf(
35 CrashConfig.CrashPattern(namePattern = "Test")
36 ),
37 promptThreshold = currentTime
38 )
39
40 foundMatch assertIs true
41 }
42
43 @Test
44 fun crashTable_matchesMessagePattern() {
45 val currentTime = System.currentTimeMillis()
46
47 db.crashes.saveCrash(
48 createdAt = currentTime,
49 name = "TestName",
50 message = "Test Message",
51 stackTrace = "test\nstack\ntrace"
52 )
53
54 val foundMatch = db.crashes.anyMatch(
55 listOf(
56 CrashConfig.CrashPattern(messagePattern = "Message")
57 ),
58 promptThreshold = currentTime
59 )
60
61 foundMatch assertIs true
62 }
63
64 @Test
65 fun crashTable_matchesStackTracePattern() {
66 val currentTime = System.currentTimeMillis()
67
68 db.crashes.saveCrash(
69 createdAt = currentTime,
70 name = "TestName",
71 message = "Test Message",
72 stackTrace = "test\nstack\ntrace"
73 )
74
75 val foundMatch = db.crashes.anyMatch(
76 listOf(
77 CrashConfig.CrashPattern(stackTracePattern = "stack")
78 ),
79 promptThreshold = currentTime
80 )
81
82 foundMatch assertIs true
83 }
84
85 @Test
86 fun crashTable_matchesNameAndMessagePattern() {
87 val currentTime = System.currentTimeMillis()
88
89 db.crashes.saveCrash(
90 createdAt = currentTime,
91 name = "TestName",
92 message = "Test Message",
93 stackTrace = "test\nstack\ntrace"
94 )
95
96 val foundMatch = db.crashes.anyMatch(
97 listOf(
98 CrashConfig.CrashPattern(namePattern = "Test", messagePattern = "Message")
99 ),
100 promptThreshold = currentTime
101 )
102
103 foundMatch assertIs true
104 }
105
106 @Test
107 fun crashTable_matchesNameAndStackTracePattern() {
108 val currentTime = System.currentTimeMillis()
109
110 db.crashes.saveCrash(
111 createdAt = currentTime,
112 name = "TestName",
113 message = "Test Message",
114 stackTrace = "test\nstack\ntrace"
115 )
116
117 val foundMatch = db.crashes.anyMatch(
118 listOf(
119 CrashConfig.CrashPattern(namePattern = "Test", stackTracePattern = "stack")
120 ),
121 promptThreshold = currentTime
122 )
123
124 foundMatch assertIs true
125 }
126
127 @Test
128 fun crashTable_matchesNameAndMessageAndStackTracePattern() {
129 val currentTime = System.currentTimeMillis()
130
131 db.crashes.saveCrash(
132 createdAt = currentTime,
133 name = "TestName",
134 message = "Test Message",
135 stackTrace = "test\nstack\ntrace"
136 )
137
138 val foundMatch = db.crashes.anyMatch(
139 listOf(
140 CrashConfig.CrashPattern(namePattern = "Test", messagePattern = "Message", stackTracePattern = "stack")
141 ),
142 promptThreshold = currentTime
143 )
144
145 foundMatch assertIs true
146 }
147
148 @Test
149 fun crashTable_doesNotMatchNamePattern() {
150 val currentTime = System.currentTimeMillis()
151
152 db.crashes.saveCrash(
153 createdAt = currentTime,
154 name = "TestName",
155 message = "Test Message",
156 stackTrace = "test\nstack\ntrace"
157 )
158
159 val foundMatch = db.crashes.anyMatch(
160 listOf(
161 CrashConfig.CrashPattern(namePattern = "Blah")
162 ),
163 promptThreshold = currentTime
164 )
165
166 foundMatch assertIs false
167 }
168
169 @Test
170 fun crashTable_matchesNameButNotMessagePattern() {
171 val currentTime = System.currentTimeMillis()
172
173 db.crashes.saveCrash(
174 createdAt = currentTime,
175 name = "TestName",
176 message = "Test Message",
177 stackTrace = "test\nstack\ntrace"
178 )
179
180 val foundMatch = db.crashes.anyMatch(
181 listOf(
182 CrashConfig.CrashPattern(namePattern = "Test", messagePattern = "Blah")
183 ),
184 promptThreshold = currentTime
185 )
186
187 foundMatch assertIs false
188 }
189
190 @Test
191 fun crashTable_matchesNameButNotStackTracePattern() {
192 val currentTime = System.currentTimeMillis()
193
194 db.crashes.saveCrash(
195 createdAt = currentTime,
196 name = "TestName",
197 message = "Test Message",
198 stackTrace = "test\nstack\ntrace"
199 )
200
201 val foundMatch = db.crashes.anyMatch(
202 listOf(
203 CrashConfig.CrashPattern(namePattern = "Test", stackTracePattern = "Blah")
204 ),
205 promptThreshold = currentTime
206 )
207
208 foundMatch assertIs false
209 }
210
211 @Test
212 fun crashTable_matchesNamePatternButPromptedTooRecently() {
213 val currentTime = System.currentTimeMillis()
214
215 db.crashes.saveCrash(
216 createdAt = currentTime,
217 name = "TestName",
218 message = "Test Message",
219 stackTrace = "test\nstack\ntrace"
220 )
221
222 db.writableDatabase
223 .updateAll(LogDatabase.CrashTable.TABLE_NAME)
224 .values(LogDatabase.CrashTable.LAST_PROMPTED_AT to currentTime)
225 .run()
226
227 val foundMatch = db.crashes.anyMatch(
228 listOf(
229 CrashConfig.CrashPattern(namePattern = "Test")
230 ),
231 promptThreshold = currentTime - 100
232 )
233
234 foundMatch assertIs false
235 }
236
237 @Test
238 fun crashTable_noMatches() {
239 val currentTime = System.currentTimeMillis()
240
241 val foundMatch = db.crashes.anyMatch(
242 listOf(
243 CrashConfig.CrashPattern(namePattern = "Test")
244 ),
245 promptThreshold = currentTime - 100
246 )
247
248 foundMatch assertIs false
249 }
250
251 @Test
252 fun crashTable_updatesLastPromptTime() {
253 val currentTime = System.currentTimeMillis()
254
255 db.crashes.saveCrash(
256 createdAt = currentTime,
257 name = "TestName",
258 message = "Test Message",
259 stackTrace = "test\nstack\ntrace"
260 )
261
262 db.crashes.saveCrash(
263 createdAt = currentTime,
264 name = "XXX",
265 message = "XXX",
266 stackTrace = "XXX"
267 )
268
269 db.crashes.markAsPrompted(
270 listOf(
271 CrashConfig.CrashPattern(namePattern = "Test")
272 ),
273 promptedAt = currentTime
274 )
275
276 db.writableDatabase
277 .select(LogDatabase.CrashTable.NAME, LogDatabase.CrashTable.LAST_PROMPTED_AT)
278 .from(LogDatabase.CrashTable.TABLE_NAME)
279 .run()
280 .forEach {
281 if (it.requireNonNullString(LogDatabase.CrashTable.NAME) == "TestName") {
282 it.requireLong(LogDatabase.CrashTable.LAST_PROMPTED_AT) assertIs currentTime
283 } else {
284 it.requireLong(LogDatabase.CrashTable.LAST_PROMPTED_AT) assertIs 0
285 }
286 }
287 }
288}