-
-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathImageFsInstallerTest.kt
More file actions
251 lines (206 loc) · 9.59 KB
/
Copy pathImageFsInstallerTest.kt
File metadata and controls
251 lines (206 loc) · 9.59 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
package com.winlator.xenvironment
import androidx.test.core.app.ApplicationProvider
import com.winlator.container.Container
import com.winlator.core.FileUtils
import java.io.File
import java.lang.reflect.Method
import java.nio.file.Files
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class ImageFsInstallerTest {
private val context = ApplicationProvider.getApplicationContext<android.content.Context>()
private val filesDir = context.filesDir
private val sharedDir = File(filesDir, "imagefs_shared")
private val imageFsLink = File(filesDir, "imagefs")
private val glibcDir = File(filesDir, "glibc")
private val bionicDir = File(filesDir, "bionic")
@After
fun tearDown() {
sharedDir.deleteRecursively()
if (Files.isSymbolicLink(imageFsLink.toPath()) || imageFsLink.exists()) {
imageFsLink.delete()
}
glibcDir.deleteRecursively()
bionicDir.deleteRecursively()
}
@Test
fun ensureSharedHomeRoot_callsSymlinkWhenHomeIsNotSymlink() {
val rootDir = File(filesDir, "imagefs-root-${System.nanoTime()}").apply { mkdirs() }
val sharedHome = File(sharedDir, "home")
val imageFsHome = File(rootDir, "home")
val expectedTarget = sharedHome.path
val expectedLink = imageFsHome.path
mockkStatic(FileUtils::class)
try {
every { FileUtils.isSymlink(any()) } returns false
every { FileUtils.symlink(any<String>(), any<String>()) } returns Unit
invokeEnsureSharedHomeRoot(context, rootDir)
assertTrue("Shared home should be created", sharedHome.exists())
verify(exactly = 1) { FileUtils.symlink(expectedTarget, expectedLink) }
} finally {
unmockkStatic(FileUtils::class)
}
rootDir.deleteRecursively()
}
@Test
fun ensureSharedHomeRoot_doesNotCallSymlinkWhenHomeAlreadySymlink() {
val rootDir = File(filesDir, "imagefs-root-symlink-${System.nanoTime()}").apply { mkdirs() }
val sharedHome = File(sharedDir, "home")
val imageFsHome = File(rootDir, "home")
mockkStatic(FileUtils::class)
try {
every { FileUtils.isSymlink(imageFsHome) } returns true
every { FileUtils.symlink(any<String>(), any<String>()) } returns Unit
invokeEnsureSharedHomeRoot(context, rootDir)
assertTrue("Shared home should still be created", sharedHome.exists())
verify(exactly = 0) { FileUtils.symlink(any<String>(), any<String>()) }
} finally {
unmockkStatic(FileUtils::class)
}
rootDir.deleteRecursively()
}
@Test
fun ensureSharedHomeRoot_alwaysCreatesSharedHomeDirectory() {
val rootDir = File(filesDir, "imagefs-root-shared-home-${System.nanoTime()}").apply { mkdirs() }
val sharedHome = File(sharedDir, "home")
invokeEnsureSharedHomeRoot(context, rootDir)
assertTrue("Shared home should always be created", sharedHome.exists())
assertTrue("Shared home should be a directory", sharedHome.isDirectory)
rootDir.deleteRecursively()
}
@Test
fun ensureSharedHomeRoot_usesExpectedLinkAndTargetPaths() {
val rootDir = File(filesDir, "imagefs-root-paths-${System.nanoTime()}").apply { mkdirs() }
val sharedHome = File(sharedDir, "home")
val imageFsHome = File(rootDir, "home")
mockkStatic(FileUtils::class)
try {
every { FileUtils.isSymlink(any()) } returns false
every { FileUtils.symlink(any<String>(), any<String>()) } returns Unit
invokeEnsureSharedHomeRoot(context, rootDir)
verify(exactly = 1) { FileUtils.symlink(sharedHome.path, imageFsHome.path) }
assertEquals(sharedHome.path, File(sharedDir, "home").path)
} finally {
unmockkStatic(FileUtils::class)
}
rootDir.deleteRecursively()
}
@Test
fun ensureProtonVersionSymlink_callsSymlinkForActiveProtonVersion() {
val rootDir = File(filesDir, "imagefs-root-proton-${System.nanoTime()}").apply { mkdirs() }
val activeProtonVersion = "proton-ge-9-2"
val sharedProtonTarget = File(sharedDir, "proton/$activeProtonVersion").apply { mkdirs() }
val expectedLink = File(rootDir, "opt/$activeProtonVersion")
mockkStatic(FileUtils::class)
try {
every { FileUtils.symlink(any<String>(), any<String>()) } returns Unit
every { FileUtils.delete(any<File>()) } returns true
every { FileUtils.isSymlink(any<File>()) } returns false
ImageFsInstaller.ensureProtonVersionSymlink(context, rootDir, activeProtonVersion)
verify(exactly = 1) {
FileUtils.symlink(sharedProtonTarget.absolutePath, expectedLink.absolutePath)
}
} finally {
unmockkStatic(FileUtils::class)
}
rootDir.deleteRecursively()
}
@Test
fun ensureProtonVersionSymlink_replacesDanglingSymlinkAtActivePath() {
val rootDir = File(filesDir, "imagefs-root-dangling-proton-${System.nanoTime()}").apply { mkdirs() }
val activeProtonVersion = "proton-ge-9-3"
val optDir = File(rootDir, "opt").apply { mkdirs() }
val optVersionLink = File(optDir, activeProtonVersion)
val missingTarget = File(rootDir, "missing-proton-target")
Files.createSymbolicLink(optVersionLink.toPath(), missingTarget.toPath())
val desiredTarget = File(sharedDir, "proton/$activeProtonVersion").apply { mkdirs() }
assertFalse("Dangling symlink should report exists=false", optVersionLink.exists())
assertTrue("Active path should still be a symlink", Files.isSymbolicLink(optVersionLink.toPath()))
mockkStatic(FileUtils::class)
try {
every { FileUtils.delete(optVersionLink) } answers { optVersionLink.delete() }
every { FileUtils.delete(any<File>()) } answers { firstArg<File>().delete() }
every { FileUtils.symlink(any<String>(), any<String>()) } answers {
val linkTarget = firstArg<String>()
val linkPath = secondArg<String>()
val linkFile = File(linkPath)
if (Files.exists(linkFile.toPath()) || Files.isSymbolicLink(linkFile.toPath())) {
linkFile.delete()
}
Files.createSymbolicLink(linkFile.toPath(), File(linkTarget).toPath())
}
ImageFsInstaller.ensureProtonVersionSymlink(context, rootDir, activeProtonVersion)
} finally {
unmockkStatic(FileUtils::class)
}
assertTrue("Active path should remain a symlink", Files.isSymbolicLink(optVersionLink.toPath()))
assertEquals(
desiredTarget.canonicalPath,
optVersionLink.canonicalFile.absolutePath,
)
rootDir.deleteRecursively()
}
@Test
fun removeCurrentProtonSymlink_removesOnlyNonActiveProtonSymlinks() {
val optDir = File(filesDir, "imagefs-opt-remove-proton-${System.nanoTime()}").apply { mkdirs() }
val active = File(optDir, "proton-ge-9-2").apply { mkdirs() }
val stale = File(optDir, "proton-ge-8-1").apply { mkdirs() }
val nonProton = File(optDir, "wine-9.0").apply { mkdirs() }
mockkStatic(FileUtils::class)
try {
every { FileUtils.isSymlink(active) } returns false
every { FileUtils.isSymlink(stale) } returns true
every { FileUtils.isSymlink(nonProton) } returns false
every { FileUtils.delete(any<File>()) } returns true
invokeRemoveCurrentProtonSymlink(optDir, "proton-ge-9-2")
verify(exactly = 1) { FileUtils.delete(stale) }
verify(exactly = 0) { FileUtils.delete(active) }
verify(exactly = 0) { FileUtils.delete(nonProton) }
} finally {
unmockkStatic(FileUtils::class)
}
optDir.deleteRecursively()
}
@Test
fun ensureImageFsSymlinks_returnsFalseWhenLegacyMigrationFails() {
val legacyRoot = File(filesDir, "legacy-migration-fail-${System.nanoTime()}").apply { mkdirs() }
val container = mockk<Container>(relaxed = true)
mockkStatic(ImageFSLegacyMigrator::class)
try {
every { ImageFSLegacyMigrator.migrateLegacyDirsIfNeeded(any(), any()) } returns false
assertFalse(ImageFsInstaller.ensureImageFsSymlinks(context, legacyRoot, container))
} finally {
unmockkStatic(ImageFSLegacyMigrator::class)
legacyRoot.deleteRecursively()
}
}
private fun invokeEnsureSharedHomeRoot(context: android.content.Context, rootDir: File) {
val method: Method = ImageFsInstaller::class.java.getDeclaredMethod(
"ensureSharedHomeRoot",
android.content.Context::class.java,
File::class.java,
)
method.isAccessible = true
method.invoke(null, context, rootDir)
}
private fun invokeRemoveCurrentProtonSymlink(optDir: File, activeProtonVersion: String) {
val method: Method = ImageFsInstaller::class.java.getDeclaredMethod(
"removeCurrentProtonSymlink",
File::class.java,
String::class.java,
)
method.isAccessible = true
method.invoke(null, optDir, activeProtonVersion)
}
}