Repository navigation
Expand file tree
/
Copy pathSharedSettingsViewModelTest.kt
More file actions
485 lines (405 loc) · 17.4 KB
/
Copy pathSharedSettingsViewModelTest.kt
File metadata and controls
485 lines (405 loc) · 17.4 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Copyright 2017 - 2026 Riigi Infosüsteemi Amet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
@file:Suppress("PackageName")
package ee.ria.DigiDoc.viewmodel.shared
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.documentfile.provider.DocumentFile
import androidx.test.platform.app.InstrumentationRegistry
import com.google.gson.Gson
import ee.ria.DigiDoc.R
import ee.ria.DigiDoc.common.Constant.DIR_TSA_CERT
import ee.ria.DigiDoc.common.Constant.Defaults.DEFAULT_UUID_VALUE
import ee.ria.DigiDoc.common.testfiles.asset.AssetFile
import ee.ria.DigiDoc.configuration.ConfigurationProperty
import ee.ria.DigiDoc.configuration.ConfigurationSignatureVerifierImpl
import ee.ria.DigiDoc.configuration.loader.ConfigurationLoader
import ee.ria.DigiDoc.configuration.loader.ConfigurationLoaderImpl
import ee.ria.DigiDoc.configuration.properties.ConfigurationPropertiesImpl
import ee.ria.DigiDoc.configuration.repository.CentralConfigurationRepositoryImpl
import ee.ria.DigiDoc.configuration.repository.ConfigurationRepository
import ee.ria.DigiDoc.configuration.repository.ConfigurationRepositoryImpl
import ee.ria.DigiDoc.configuration.service.CentralConfigurationServiceImpl
import ee.ria.DigiDoc.domain.preferences.DataStore
import ee.ria.DigiDoc.libdigidoclib.init.Initialization
import ee.ria.DigiDoc.libdigidoclib.init.LibdigidocLibraryLoader
import ee.ria.DigiDoc.manager.ActivityManager
import ee.ria.DigiDoc.network.proxy.ManualProxy
import ee.ria.DigiDoc.network.proxy.ProxySetting
import ee.ria.DigiDoc.network.siva.SivaSetting
import ee.ria.libdigidocpp.DigiDocConf
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.apache.commons.io.FileUtils
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.mock
import java.io.File
import java.io.InputStream
import java.nio.charset.Charset
import java.nio.file.Files
import java.util.TimeZone
@RunWith(MockitoJUnitRunner::class)
class SharedSettingsViewModelTest {
@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
@get:Rule
val mockWebServer = MockWebServer()
@Mock
lateinit var contentResolver: ContentResolver
@Mock
lateinit var configurationRepository: ConfigurationRepository
@Mock
private lateinit var activityManager: ActivityManager
companion object {
private const val AWAIT_ERROR_TIMEOUT = 10_000L
private lateinit var configurationLoader: ConfigurationLoader
private lateinit var configurationRepository: ConfigurationRepository
@JvmStatic
@BeforeClass
fun setupOnce() {
runBlocking {
try {
val context = InstrumentationRegistry.getInstrumentation().targetContext
configurationLoader =
ConfigurationLoaderImpl(
Gson(),
CentralConfigurationRepositoryImpl(
CentralConfigurationServiceImpl(context, ConfigurationProperty()),
),
ConfigurationProperty(),
ConfigurationPropertiesImpl(),
ConfigurationSignatureVerifierImpl(),
)
configurationRepository = ConfigurationRepositoryImpl(configurationLoader)
LibdigidocLibraryLoader().init(context)
Initialization(configurationRepository).init(context)
} catch (_: Exception) {
}
}
}
}
private lateinit var context: Context
private lateinit var dataStore: DataStore
private lateinit var initialization: Initialization
private lateinit var viewModel: SharedSettingsViewModel
@Before
fun setup() {
MockitoAnnotations.openMocks(this)
context = InstrumentationRegistry.getInstrumentation().targetContext
dataStore = DataStore(context)
LibdigidocLibraryLoader().init(context)
initialization = Initialization(configurationRepository)
initialization.overrideProxy("", 80, "", "")
dataStore.setProxySetting(ProxySetting.NO_PROXY)
dataStore.setProxyHost("")
dataStore.setProxyPort(80)
dataStore.setProxyUsername("")
dataStore.setProxyPassword("")
viewModel =
SharedSettingsViewModel(
context = context,
contentResolver = contentResolver,
dataStore = dataStore,
configurationRepository = configurationRepository,
initialization = initialization,
activityManager = activityManager,
)
}
@Test
fun sharedSettingsViewModel_init_success() {
val result = viewModel.dataStore.getCountry()
assertEquals(0, result)
}
@Test
fun sharedSettingsViewModel_resetToDefaultSettings_success() {
val file =
AssetFile.getResourceFileAsFile(
context,
"siva.cer",
ee.ria.DigiDoc.common.R.raw.siva,
)
val uri = Uri.fromFile(file)
saveTsaCert(uri)
viewModel.resetToDefaultSettings()
// resetSigningSettings
assertEquals(DEFAULT_UUID_VALUE, dataStore.getSettingsUUID())
assertEquals("", dataStore.getSettingsTSAUrl())
assertFalse(dataStore.getSettingsAskRoleAndAddress())
assertFalse(dataStore.getSettingsDefaultLTA())
assertFalse(dataStore.getIsTsaCertificateViewVisible())
assertEquals("", dataStore.getTSACertName())
// resetRightsSettings
assertTrue(dataStore.getSettingsOpenAllFileTypes())
assertFalse(dataStore.getSettingsAllowScreenshots())
// resetSivaSettings
assertEquals(SivaSetting.DEFAULT, dataStore.getSivaSetting())
assertEquals("", dataStore.getSettingsSivaUrl())
assertEquals("", dataStore.getSettingsSivaCertName())
// resetProxySettings
assertEquals(ProxySetting.NO_PROXY, dataStore.getProxySetting())
assertEquals("", dataStore.getProxyHost())
assertEquals(80, dataStore.getProxyPort())
assertEquals("", dataStore.getProxyUsername())
assertEquals("", dataStore.getProxyPassword())
}
@Test
fun sharedSettingsViewModel_resetToDefaultSettings_resetsDefaultLTAToFalse() {
dataStore.setSettingsDefaultLTA(true)
viewModel.resetToDefaultSettings()
assertFalse(dataStore.getSettingsDefaultLTA())
}
@Test
fun sharedSettingsViewModel_saveProxySettings_savesManualProxySettings() {
dataStore.setProxySetting(ProxySetting.MANUAL_PROXY)
val manualProxySettings = ManualProxy("proxyHost", 8080, "proxyUser", "proxyPass")
viewModel.saveProxySettings(manualProxySettings)
assertEquals("proxyHost", dataStore.getProxyHost())
assertEquals(8080, dataStore.getProxyPort())
assertEquals("proxyUser", dataStore.getProxyUsername())
assertEquals("proxyPass", dataStore.getProxyPassword())
}
@Test
fun sharedSettingsViewModel_saveProxySettings_keepsManualProxySettingsWhenSystemProxyIsChosen() {
dataStore.setProxySetting(ProxySetting.MANUAL_PROXY)
val manualProxySettings = ManualProxy("proxyHost", 8080, "proxyUser", "proxyPass")
viewModel.saveProxySettings(manualProxySettings)
System.setProperty("http.proxyHost", "systemProxyHost")
dataStore.setProxySetting(ProxySetting.SYSTEM_PROXY)
try {
viewModel.saveProxySettings(ManualProxy("", 80, "", ""))
} finally {
System.clearProperty("http.proxyHost")
}
assertEquals("proxyHost", dataStore.getProxyHost())
assertEquals(8080, dataStore.getProxyPort())
assertEquals("proxyUser", dataStore.getProxyUsername())
assertEquals("proxyPass", dataStore.getProxyPassword())
}
@Test
fun sharedSettingsViewModel_saveProxySettings_keepsManualProxySettingsWhenNoProxyIsChosen() {
dataStore.setProxySetting(ProxySetting.MANUAL_PROXY)
val manualProxySettings = ManualProxy("proxyHost", 8080, "proxyUser", "proxyPass")
viewModel.saveProxySettings(manualProxySettings)
dataStore.setProxySetting(ProxySetting.NO_PROXY)
viewModel.saveProxySettings(ManualProxy("", 80, "", ""))
assertEquals("proxyHost", dataStore.getProxyHost())
assertEquals(8080, dataStore.getProxyPort())
assertEquals("proxyUser", dataStore.getProxyUsername())
assertEquals("proxyPass", dataStore.getProxyPassword())
}
@Test
fun sharedSettingsViewModel_saveProxySettings_clearsLibdigidocppProxyWhenNoProxyIsChosen() {
dataStore.setProxySetting(ProxySetting.MANUAL_PROXY)
viewModel.saveProxySettings(ManualProxy("proxyHost", 8080, "proxyUser", "proxyPass"))
dataStore.setProxySetting(ProxySetting.NO_PROXY)
viewModel.saveProxySettings(ManualProxy("proxyHost", 8080, "proxyUser", "proxyPass"))
assertEquals("", DigiDocConf.instance().proxyHost())
}
@Test
fun sharedSettingsViewModel_updateData_successWithValidCertFile() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
val file =
AssetFile.getResourceFileAsFile(
context,
"siva.cer",
ee.ria.DigiDoc.common.R.raw.siva,
)
val uri = Uri.fromFile(file)
viewModel.handleSivaFile(uri)
val validUrl = "https://valid-siva-url.com"
viewModel.updateSivaData(validUrl, context)
assertEquals(validUrl, viewModel.previousSivaUrl.value)
assertNotNull(viewModel.sivaCertificate.value)
assertEquals("*.eesti.ee", viewModel.sivaIssuedTo.value)
assertEquals("30.09.2024 (Expired)", viewModel.sivaValidTo.value)
}
@Test(expected = Test.None::class)
fun sharedSettingsViewModel_updateSivaData_withInvalidCertFile() {
val file = createTempFileWithStringContent("invalid_cert", "invalid_cert")
val uri = Uri.fromFile(file)
viewModel.handleSivaFile(uri)
val validUrl = "https://valid-siva-url.com"
viewModel.updateSivaData(validUrl, context)
assertEquals(validUrl, viewModel.previousSivaUrl.value)
}
@Test
fun sharedSettingsViewModel_updateTsaData_successWithValidCertFile() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
val file =
AssetFile.getResourceFileAsFile(
context,
"siva.cer",
ee.ria.DigiDoc.common.R.raw.siva,
)
val uri = Uri.fromFile(file)
viewModel.handleTsaFile(uri)
val validUrl = "https://valid-tsa-url.com"
viewModel.updateTsaData(validUrl, context)
assertEquals(validUrl, viewModel.previousTsaUrl.value)
assertNotNull(viewModel.tsaCertificate.value)
assertEquals("*.eesti.ee", viewModel.tsaIssuedTo.value)
assertEquals("30.09.2024 (Expired)", viewModel.tsaValidTo.value)
}
@Test(expected = Test.None::class)
fun sharedSettingsViewModel_updateTsaData_withInvalidCertFile() {
val file = createTempFileWithStringContent("invalid_cert", "invalid_cert")
val uri = Uri.fromFile(file)
viewModel.handleTsaFile(uri)
val validUrl = "https://valid-tsa-url.com"
viewModel.updateTsaData(validUrl, context)
assertEquals(validUrl, viewModel.previousTsaUrl.value)
}
@Test
fun sharedSettingsViewModel_handleSivaFile_success() {
val file =
AssetFile.getResourceFileAsFile(
context,
"siva.cer",
ee.ria.DigiDoc.common.R.raw.siva,
)
val uri = Uri.fromFile(file)
viewModel.handleSivaFile(uri)
assertEquals("sivaCert", dataStore.getSettingsSivaCertName())
}
@Test(expected = Test.None::class)
fun sharedSettingsViewModel_handleFile_handleSivaError() {
val uri: Uri = mock()
viewModel.handleSivaFile(uri)
}
@Test
fun sharedSettingsViewModel_handleTsaFile_success() {
val file =
AssetFile.getResourceFileAsFile(
context,
"siva.cer",
ee.ria.DigiDoc.common.R.raw.siva,
)
val uri = Uri.fromFile(file)
viewModel.handleTsaFile(uri)
assertEquals("tsaCert", dataStore.getTSACertName())
}
@Test(expected = Test.None::class)
fun sharedSettingsViewModel_handleFile_handleTsaError() {
val uri: Uri = mock()
viewModel.handleTsaFile(uri)
}
@Test
fun sharedSettingsViewModel_checkConnection_savesManualProxySettings() {
mockWebServer.enqueue(MockResponse().setResponseCode(407))
dataStore.setProxySetting(ProxySetting.MANUAL_PROXY)
viewModel.checkConnection(localProxy("proxyUser", "proxyPass"))
assertEquals("127.0.0.1", dataStore.getProxyHost())
assertEquals(mockWebServer.port, dataStore.getProxyPort())
assertEquals("proxyUser", dataStore.getProxyUsername())
assertEquals("proxyPass", dataStore.getProxyPassword())
}
@Test
fun sharedSettingsViewModel_checkConnection_reportsWrongCredentialsWhenProxyDemandsAuthentication() {
mockWebServer.enqueue(MockResponse().setResponseCode(407))
mockWebServer.enqueue(MockResponse().setResponseCode(407))
dataStore.setProxySetting(ProxySetting.MANUAL_PROXY)
viewModel.checkConnection(localProxy("proxyUser", "wrongPass"))
assertEquals(
R.string.main_settings_proxy_check_username_and_password,
awaitErrorMessage(),
)
}
@Test
fun sharedSettingsViewModel_checkConnection_reportsWrongCredentialsWhenProxyForbidsConnect() {
mockWebServer.enqueue(MockResponse().setResponseCode(403))
dataStore.setProxySetting(ProxySetting.MANUAL_PROXY)
viewModel.checkConnection(localProxy("proxyUser", "proxyPass"))
assertEquals(
R.string.main_settings_proxy_check_username_and_password,
awaitErrorMessage(),
)
}
@Test
fun sharedSettingsViewModel_checkConnection_reportsUnsuccessfulWhenProxyFailsForAnotherReason() {
mockWebServer.enqueue(MockResponse().setResponseCode(500))
dataStore.setProxySetting(ProxySetting.MANUAL_PROXY)
viewModel.checkConnection(localProxy("proxyUser", "proxyPass"))
assertEquals(
R.string.main_settings_proxy_check_connection_unsuccessful,
awaitErrorMessage(),
)
}
private fun localProxy(
username: String,
password: String,
) = ManualProxy("127.0.0.1", mockWebServer.port, username, password)
private fun awaitErrorMessage(): Int? {
val deadline = System.currentTimeMillis() + AWAIT_ERROR_TIMEOUT
while (System.currentTimeMillis() < deadline) {
viewModel.errorState.value?.let { return it }
Thread.sleep(50)
}
return null
}
@Test
fun sharedSettingsViewModel_recreateActivity_successChangingRecreateActivityValue() {
viewModel.recreateActivity()
verify(activityManager).setShouldRecreateActivity(true)
}
@Suppress("SameParameterValue")
private fun createTempFileWithStringContent(
filename: String,
content: String,
): File {
val tempFile = File.createTempFile(filename, ".txt", context.cacheDir)
Files.write(tempFile.toPath(), content.toByteArray(Charset.defaultCharset()))
return tempFile
}
private fun saveTsaCert(uri: Uri) {
try {
val initialStream: InputStream? = contentResolver.openInputStream(uri)
val documentFile = DocumentFile.fromSingleUri(context, uri)
if (documentFile != null) {
val tsaCertFolder = File(context.filesDir, DIR_TSA_CERT)
if (!tsaCertFolder.exists()) {
tsaCertFolder.mkdirs()
}
var fileName = documentFile.name
if (fileName.isNullOrEmpty()) {
fileName = "tsaCert"
}
val tsaFile = File(tsaCertFolder, fileName)
FileUtils.copyInputStreamToFile(initialStream, tsaFile)
dataStore.setTSACertName(tsaFile.name)
}
} catch (_: Exception) {
// Do nothing
}
}
}