diff --git a/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt b/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt index 8c3c29860e..f3d8477a5e 100644 --- a/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt +++ b/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt @@ -3,6 +3,7 @@ package com.itsaky.androidide.repositories import android.util.Log import com.itsaky.androidide.plugins.PluginInfo import com.itsaky.androidide.plugins.PluginMetadata +import com.itsaky.androidide.plugins.manager.core.PluginBuildFiles import com.itsaky.androidide.plugins.manager.core.PluginManager import com.itsaky.androidide.plugins.manager.loaders.toPluginMetadata import kotlinx.coroutines.Dispatchers @@ -144,7 +145,9 @@ class PluginRepositoryImpl( val fileExtension = if (pluginFile.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true)) ".$PLUGIN_ARCHIVE_EXTENSION" else ".apk" - val finalFileName = "${pluginId}$fileExtension" + // A name of its own per build: Android caches a plugin's resources, assets and native + // libraries by path, so the new build would otherwise run against the old one's. + val finalFileName = "${PluginBuildFiles.fileStem(pluginId, pluginFile)}$fileExtension" if (!pluginsDir.exists()) { pluginsDir.mkdirs() diff --git a/app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt b/app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt new file mode 100644 index 0000000000..f7794cf82e --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt @@ -0,0 +1,55 @@ +package com.itsaky.androidide.repositories + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.plugins.manager.core.PluginBuildFiles +import com.itsaky.androidide.plugins.manager.core.PluginManager +import com.itsaky.androidide.plugins.manager.core.PluginValidation +import com.itsaky.androidide.plugins.manager.loaders.PluginManifest +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +/** + * Each plugin build is installed under a name of its own. Android caches a plugin's resources and + * assets by path, so a build installed at the previous build's path read the previous build's + * assets until the app restarted (ADFA-7160). + */ +@RunWith(RobolectricTestRunner::class) +class PluginRepositoryImplInstallTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun aBuildIsInstalledUnderItsContentHash() = + runTest { + val pluginsDir = temp.newFolder("plugins") + val build = temp.newFile("incoming.cgp").apply { writeText("build A") } + val expected = "${PluginBuildFiles.fileStem(ID, build)}.cgp" + val manager = + mockk(relaxed = true) { + every { getPluginValidation(any()) } returns + Result.success( + PluginValidation( + manifest = PluginManifest(ID, "Example", "1.0", "", "", "Main", "1.0"), + isDebug = false, + iconDayEntryExists = false, + iconNightEntryExists = false, + ), + ) + every { getPlugin(ID) } returns mockk() + } + + PluginRepositoryImpl({ manager }, pluginsDir).installPluginFromFile(build).getOrThrow() + + assertThat(pluginsDir.list()!!.toList()).containsExactly(expected) + } + + private companion object { + const val ID = "org.example.plugin" + } +} diff --git a/docs/PLUGIN_AUTHORING.md b/docs/PLUGIN_AUTHORING.md index eff39136fe..c51558dd79 100644 --- a/docs/PLUGIN_AUTHORING.md +++ b/docs/PLUGIN_AUTHORING.md @@ -112,7 +112,7 @@ these in `PluginLoader.getPluginMetadata()` | Key | Type | Required | Notes | |---------------------------|-------------------------|----------|-----------------------------------------------------------------------| -| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. | +| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. It names files, so it can't be empty, `.` or `..`, or contain `/`, `\` or NUL, and it can't be `oat` or end in `.cgp` (any case), which CoGo uses for its own files; CoGo refuses such a plugin. | | `plugin.name` | string | yes\* | Display name. | | `plugin.version` | string | yes\* | Defaults to `"1.0.0"`. Prefer `${pluginVersion}` from `pluginBuilder`. | | `plugin.description` | string | no | Shown in the plugin list. | diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/LinkSafeDelete.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/LinkSafeDelete.kt new file mode 100644 index 0000000000..aa0c7941c5 --- /dev/null +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/LinkSafeDelete.kt @@ -0,0 +1,59 @@ +package com.itsaky.androidide.plugins.manager.core + +import org.slf4j.LoggerFactory +import java.io.File +import java.io.IOException +import java.nio.file.FileVisitResult +import java.nio.file.Files +import java.nio.file.LinkOption +import java.nio.file.Path +import java.nio.file.SimpleFileVisitor +import java.nio.file.attribute.BasicFileAttributes + +private val log = LoggerFactory.getLogger("LinkSafeDelete") + +/** + * Deletes this file or folder tree without ever following a link: a link, the root included, is + * deleted as a link and its target is left alone. File.deleteRecursively() follows a link nested in + * the tree and empties its target, and plugin code can make a link anywhere in the app's storage. + * + * True when nothing is left, as with deleteRecursively(), so true for a path that doesn't exist. Keeps + * going past an entry it can't delete, as deleteRecursively() does. + */ +internal fun File.deleteWithoutFollowingLinks(): Boolean { + val root = toPath() + if (!Files.exists(root, LinkOption.NOFOLLOW_LINKS)) return true + // walkFileTree without FOLLOW_LINKS hands a link to visitFile, never into its target. + val visitor = + object : SimpleFileVisitor() { + override fun visitFile( + file: Path, + attrs: BasicFileAttributes, + ) = tryDelete(file) + + override fun visitFileFailed( + file: Path, + exc: IOException, + ) = tryDelete(file) + + override fun postVisitDirectory( + dir: Path, + exc: IOException?, + ) = tryDelete(dir) + + private fun tryDelete(path: Path): FileVisitResult { + try { + Files.deleteIfExists(path) + } catch (e: IOException) { + log.debug("Could not delete {}", path, e) + } + return FileVisitResult.CONTINUE + } + } + try { + Files.walkFileTree(root, visitor) + } catch (e: IOException) { + log.debug("Could not walk {}", root, e) + } + return !Files.exists(root, LinkOption.NOFOLLOW_LINKS) +} diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt new file mode 100644 index 0000000000..7f413b6e85 --- /dev/null +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -0,0 +1,300 @@ +package com.itsaky.androidide.plugins.manager.core + +import org.adfa.constants.PLUGIN_ARCHIVE_EXTENSION +import org.slf4j.LoggerFactory +import java.io.File +import java.io.IOException +import java.nio.file.Files +import java.nio.file.LinkOption +import java.security.MessageDigest +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicBoolean +import java.util.zip.ZipFile + +/** + * The files of installed plugin builds: the plugin files in [pluginsDir], and the native libraries + * extracted from them under [nativeLibsDir]. + * + * Every build gets paths of its own. Android caches a plugin's resources and assets by file path, + * and refuses to open a native library at a path another class loader already opened, so a build + * installed at the previous build's paths runs against the previous build's files until the app + * restarts. Plugin files are named `-.cgp`, after their content. A file installed + * before that, `.cgp`, keeps loading until its plugin is next updated. An older CoGo + * build updates that file in place, so its libraries' folder is named after its size and time. + * + * [legacyNativeLibsDir] is where CoGo builds before per-build folders extracted libraries. It is + * deleted, never read. + */ +class PluginBuildFiles internal constructor( + private val pluginsDir: File, + private val nativeLibsDir: File, + private val legacyNativeLibsDir: File? = null, + private val readPluginId: (File) -> String?, +) { + private val legacyDeleted = AtomicBoolean(legacyNativeLibsDir == null) + + // One extraction or deletion per plugin at a time: loads can overlap, and two extractions of a + // build would share its scratch folder. + private val locks = ConcurrentHashMap() + + // Native-library folders handed to a class loader in this process. A folder deleted by an + // uninstall is never handed out again under the same path: the runtime would refuse to open a + // library there for the reinstalled build's class loader while the old one still holds it. + private val handedOut: MutableSet = ConcurrentHashMap.newKeySet() + + /** Whether [file] is a plugin file, which only its manifest ties to a plugin. */ + private fun isPluginFile(file: File): Boolean = + file.isFile && file.parentFile == pluginsDir && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) + + /** + * Whether [file] is ART's compiled code for a file of [pluginId] that is gone. Call it after + * [delete], which removed all of [pluginId]'s plugin files. The name can't tell the plugin: an + * older CoGo build's `.cgp` may have an ID that looks like `-`. So a plugin + * file left under the stem belongs to another plugin, and its compiled code is kept. + */ + fun isLeftoverCompiledCodeOf( + pluginId: String, + file: File, + ): Boolean { + if (!isCompiledCodeOf(pluginId, file)) return false + val stem = file.name.replace(COMPILED_CODE_END, "") + // loadPlugins loads .CGP too, so any case of the extension keeps the code. + return pluginsDir.listFiles { f -> isPluginFile(f) && f.nameWithoutExtension == stem }.isNullOrEmpty() + } + + /** Every plugin file whose manifest has [pluginId]. Its name can't tell: it names the build. */ + fun filesOf(pluginId: String): List = + pluginsDir + .listFiles(::isPluginFile) + .orEmpty() + .filter { file -> + runCatching { readPluginId(file) } + .onFailure { log.warn("Could not read the plugin ID of {}", file.name, it) } + .getOrNull() == pluginId + } + + /** + * Deletes every file of [pluginId], not just the first, since two would both load. True when + * there was one and all were deleted: a file left behind loads again on the next start. The + * plugin's native libraries go even when it has no file left: an install whose load failed + * deletes its file but not the libraries it extracted. They don't count toward the result: with + * its files gone the plugin is gone, and its next install deletes the folders of other builds. + */ + fun delete(pluginId: String): Boolean = + synchronized(lockFor(pluginId)) { + deleteLegacyNativeLibs() + if (!pluginDirOf(pluginId).deleteWithoutFollowingLinks()) { + log.error("Could not delete all native libraries of {}", pluginId) + } + val files = filesOf(pluginId) + val undeleted = files.filterNot(File::delete) + undeleted.forEach { log.error("Could not delete plugin file {}", it.absolutePath) } + files.isNotEmpty() && undeleted.isEmpty() + } + + /** + * Extracts [build]'s native libraries for [abi] into a folder of that build's own, and deletes + * [pluginId]'s folders that belong to no installed build. Null when [build] has none for [abi]. + */ + fun extractNativeLibs( + pluginId: String, + build: File, + abi: String, + ): File? = + synchronized(lockFor(pluginId)) { + deleteLegacyNativeLibs() + val pluginDir = pluginDirOf(pluginId) + if (Files.isSymbolicLink(pluginDir.toPath())) { + log.warn("{} is a link; deleting the link and extracting into a folder", pluginDir) + pluginDir.deleteWithoutFollowingLinks() + // Else the steps below run inside the link's target and empty it. + if (Files.isSymbolicLink(pluginDir.toPath())) throw IOException("Could not delete the link ${pluginDir.absolutePath}") + } + val key = buildKey(pluginId, build) + // Before folderFor: a link left at the build's path would block the scratch folder's rename. + deleteLinksIn(pluginDir) + val buildDir = folderFor(pluginDir, key) + val extracted = isRealDirectory(buildDir) || extractNativeLibs(build, "lib/$abi/", buildDir) + // Another installed build of this plugin keeps its folder: it may be loading from it. + val otherBuilds = installedKeys(pluginId) - key + pluginDir + .listFiles { file -> file != buildDir && !(isRealDirectory(file) && file.name.substringBefore('~') in otherBuilds) } + ?.forEach(File::deleteWithoutFollowingLinks) + pluginDir.delete() // only when empty + if (extracted) buildDir.also { handedOut += it.absolutePath } else null + } + + private fun lockFor(pluginId: String): Any = locks.computeIfAbsent(pluginId) { Any() } + + private fun deleteLegacyNativeLibs() { + if (legacyDeleted.compareAndSet(false, true) && legacyNativeLibsDir?.deleteWithoutFollowingLinks() == false) { + log.warn("Could not delete {}", legacyNativeLibsDir) + } + } + + /** + * [pluginId]'s folder of native libraries. Throws unless that is a folder of [nativeLibsDir] + * named [pluginId]: an ID that is a path would have it delete a folder elsewhere. Compares + * normalized paths and resolves no link, so a link at that path is returned as it is, for the + * caller to delete as a link. + */ + private fun pluginDirOf(pluginId: String): File { + val root = nativeLibsDir.toPath().toAbsolutePath().normalize() + val dir = root.resolve(pluginId).normalize() + require("\\" !in pluginId && dir.parent == root && dir.fileName?.toString() == pluginId) { + "Plugin ID \"$pluginId\" names no folder of ${nativeLibsDir.absolutePath}" + } + return dir.toFile() + } + + // A link is never a folder of ours: plugin code can plant one, and it would be followed. + private fun isRealDirectory(file: File): Boolean = Files.isDirectory(file.toPath(), LinkOption.NOFOLLOW_LINKS) + + private fun deleteLinksIn(pluginDir: File) { + pluginDir.listFiles { file -> Files.isSymbolicLink(file.toPath()) }?.forEach { + log.warn("{} is a link; deleting the link", it) + it.deleteWithoutFollowingLinks() + } + } + + /** + * The build's folder: ``, or `~` when an uninstall in this process deleted the + * folders it handed out before. An existing one is reused. + */ + private fun folderFor( + pluginDir: File, + key: String, + ): File { + val existing = pluginDir.listFiles { file -> isRealDirectory(file) && (file.name == key || file.name.startsWith("$key~")) } + existing?.minByOrNull { it.name.length }?.let { return it } + return generateSequence(0) { it + 1 } + .map { n -> File(pluginDir, if (n == 0) key else "$key~$n") } + .first { it.absolutePath !in handedOut } + } + + /** + * The build keys of [pluginId]'s files, by name, which is cheap enough for every load. A file of + * another plugin whose ID starts with `-` can match too; that only keeps a folder. + */ + private fun installedKeys(pluginId: String): Set = + pluginsDir + .listFiles { file -> + file.isFile && + file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) && + (file.nameWithoutExtension == pluginId || file.nameWithoutExtension.startsWith("$pluginId-")) + }.orEmpty() + .mapTo(HashSet()) { buildKey(pluginId, it) } + + /** Extracts into a scratch folder renamed when complete, so an interrupted extraction is never reused. */ + private fun extractNativeLibs( + build: File, + libPrefix: String, + buildDir: File, + ): Boolean { + val scratch = File(buildDir.parentFile, ".${buildDir.name}.partial") + scratch.deleteWithoutFollowingLinks() + scratch.mkdirs() + val targetPath = scratch.toPath().toAbsolutePath().normalize() + + var found = false + ZipFile(build).use { zip -> + zip + .entries() + .asSequence() + .filter { !it.isDirectory && it.name.startsWith(libPrefix) && it.name.endsWith(".so") } + .forEach { entry -> + val outPath = targetPath.resolve(entry.name.substringAfterLast("/")).normalize() + if (outPath.startsWith(targetPath)) { + zip.getInputStream(entry).use { input -> + outPath.toFile().outputStream().use { output -> + input.copyTo(output) + } + } + outPath.toFile().setExecutable(true, false) + found = true + } + } + } + + if (!found) { + scratch.deleteWithoutFollowingLinks() + return false + } + if (!scratch.renameTo(buildDir)) { + scratch.deleteWithoutFollowingLinks() + throw IOException("Could not move native libraries into ${buildDir.absolutePath}") + } + return true + } + + companion object { + private val log = LoggerFactory.getLogger(PluginBuildFiles::class.java) + + private const val HASH_CHARS = 16 + + // ART's files for `.cgp`, or `.CGP`: it names profiles after the file. One list for matching + // and for finding the stem: a suffix missing from either would leave the stem unmatched and delete another plugin's code. + private const val COMPILED_CODE_SUFFIX = "(odex|vdex|art|(?i:$PLUGIN_ARCHIVE_EXTENSION)\\.(cur\\.)?prof)" + private val COMPILED_CODE_END = Regex("\\.$COMPILED_CODE_SUFFIX$") + + // Not app_plugin_native_libs: an older CoGo build reuses its plugin's folder there whatever it + // holds, so it would find these per-build folders and no libraries. Folder names as getDir() + // makes them. + private const val NATIVE_LIBS_DIR = "app_plugin_build_libs" + private const val LEGACY_NATIVE_LIBS_DIR = "app_plugin_native_libs" + + /** The plugin build files of the app whose data folder is [dataDir]. */ + internal fun inDataDir( + pluginsDir: File, + dataDir: File, + readPluginId: (File) -> String?, + ) = PluginBuildFiles(pluginsDir, File(dataDir, NATIVE_LIBS_DIR), File(dataDir, LEGACY_NATIVE_LIBS_DIR), readPluginId) + + /** The name, without extension, to install [build] under: `-`. */ + fun fileStem( + pluginId: String, + build: File, + ): String = "$pluginId-${sha256Hex(build).take(HASH_CHARS)}" + + /** + * The name of [installed]'s library folder: the hash in its name. A `.cgp` installed + * before hashes gets `legacy--`, since an older CoGo build rewrites it in + * place. Its name alone would keep the previous content's libraries. + */ + internal fun buildKey( + pluginId: String, + installed: File, + ): String = + if (installed.nameWithoutExtension == pluginId) { + "legacy-${installed.length()}-${installed.lastModified()}" + } else { + installed.nameWithoutExtension.removePrefix("$pluginId-") + } + + /** + * Whether [file] is ART's compiled code for a file of [pluginId]: `.odex`, `.vdex` or + * `.art`, or `.cgp.prof` or `.cgp.cur.prof`. Matched exactly, since another plugin's ID + * or content hash can contain this ID. By name only: see [isLeftoverCompiledCodeOf]. + */ + internal fun isCompiledCodeOf( + pluginId: String, + file: File, + ): Boolean = + file.isFile && + Regex("${Regex.escape(pluginId)}(-[0-9a-f]{$HASH_CHARS})?\\.$COMPILED_CODE_SUFFIX") + .matches(file.name) + + private fun sha256Hex(file: File): String { + val digest = MessageDigest.getInstance("SHA-256") + file.inputStream().use { input -> + val buffer = ByteArray(DEFAULT_BUFFER_SIZE) + while (true) { + val read = input.read(buffer) + if (read < 0) break + digest.update(buffer, 0, read) + } + } + return digest.digest().joinToString("") { "%02x".format(it) } + } + } +} diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt new file mode 100644 index 0000000000..cd0910fe70 --- /dev/null +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt @@ -0,0 +1,34 @@ +package com.itsaky.androidide.plugins.manager.core + +import org.adfa.constants.PLUGIN_ARCHIVE_EXTENSION + +/** + * Fails when [id] can't be a plugin ID. The ID names the plugin's files and folders, so one that + * is a path reaches outside them: installing `../files` would delete the app's whole files folder. + * So do two names CoGo uses itself in the plugins folder, where the data folder is ``: `oat` + * is ART's folder of every plugin's compiled code, and `x.cgp` is plugin `x`'s file from an older + * CoGo build. Uninstalling either would delete what it names. + * Nullable because a `plugin.json` manifest without an ID parses to a null one. + */ +internal fun checkPluginId(id: String?): Result = + when { + id.isNullOrEmpty() || id == "." || id == ".." || id.any { it == '/' || it == '\\' || it == '\u0000' } -> { + Result.failure(IllegalArgumentException("Invalid plugin ID \"$id\": it must be a name, not a path")) + } + + id.equals("oat", ignoreCase = true) -> { + Result.failure(IllegalArgumentException("Invalid plugin ID \"$id\": \"oat\" is the folder of compiled plugin code")) + } + + id.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) -> { + Result.failure( + IllegalArgumentException( + "Invalid plugin ID \"$id\": it must not end in .$PLUGIN_ARCHIVE_EXTENSION, which names a plugin file", + ), + ) + } + + else -> { + Result.success(id) + } + } diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index 03e42340e7..4f1dd0f8b7 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -4,6 +4,7 @@ package com.itsaky.androidide.plugins.manager.core import android.app.Activity import android.content.Context +import android.os.Build import com.itsaky.androidide.actions.SidebarSlotManager import com.itsaky.androidide.plugins.IPlugin import com.itsaky.androidide.plugins.PluginContext @@ -92,6 +93,7 @@ import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import org.adfa.constants.PLUGIN_ARCHIVE_EXTENSION import java.io.File +import java.nio.file.Files import java.util.concurrent.ConcurrentHashMap class PluginManager private constructor( @@ -331,6 +333,10 @@ class PluginManager private constructor( private val lifecycleDispatcher = PluginLifecycleDispatcher() private val pluginsDir = File(context.filesDir, "plugins") + private val buildFiles = + PluginBuildFiles.inDataDir(pluginsDir, context.dataDir) { file -> + PluginLoader(context, file).getPluginMetadata()?.id + } private val documentationManager = PluginDocumentationManager(context) private var templateReloadListener: (() -> Unit)? = null private var snippetRefreshListener: ((String) -> Unit)? = null @@ -479,17 +485,25 @@ class PluginManager private constructor( return Result.success(manifest to loader) } + /** + * The manifest, its ID unchecked: the Add-ons Manager reports this failing as an unreadable file, + * so a bad ID is left for [getPluginValidation] to report. Don't build a path from it. + */ fun getPluginMetadataOnly(pluginFile: File): Result = loadAndValidate(pluginFile).map { it.first } - fun getPluginValidation(pluginFile: File): Result = - loadAndValidate(pluginFile).map { (manifest, loader) -> + /** Fails for a plugin ID that is a path: install deletes and creates folders named after it. */ + fun getPluginValidation(pluginFile: File): Result { + val (manifest, loader) = loadAndValidate(pluginFile).getOrElse { return Result.failure(it) } + checkPluginId(manifest.id).onFailure { return Result.failure(it) } + return Result.success( PluginValidation( manifest = manifest, isDebug = loader.isDebuggable(), iconDayEntryExists = manifest.iconDay?.let(loader::hasEntry) ?: false, iconNightEntryExists = manifest.iconNight?.let(loader::hasEntry) ?: false, - ) - } + ), + ) + } /** * Load plugin and return both the plugin instance and its metadata @@ -562,6 +576,7 @@ class PluginManager private constructor( if (manifest == null) { return Result.failure(IllegalArgumentException("Plugin manifest not found in: ${file.name}")) } + checkPluginId(manifest.id).onFailure { return Result.failure(it) } logger.debug("Parsed manifest for plugin: ${manifest.name} (${manifest.id})") @@ -588,7 +603,7 @@ class PluginManager private constructor( var nativeLibPath: String? = try { - pluginLoader.extractNativeLibs(manifest.id)?.absolutePath + buildFiles.extractNativeLibs(manifest.id, file, Build.SUPPORTED_ABIS[0])?.absolutePath } catch (e: Exception) { logger.warn("Failed to extract native libs for plugin: ${manifest.id}", e) null @@ -603,7 +618,7 @@ class PluginManager private constructor( } if (nativeLibPath != null && !permissions.contains(PluginPermission.NATIVE_CODE)) { - File(nativeLibPath).deleteRecursively() + File(nativeLibPath).deleteWithoutFollowingLinks() if (manifest.sidebarItems > 0) { SidebarSlotManager.releasePluginSlots(manifest.id) } @@ -685,6 +700,7 @@ class PluginManager private constructor( isEnabled, iconDayPath = iconDayPath, iconNightPath = iconNightPath, + nativeLibraryDir = nativeLibPath?.let(::File), ) loadedPlugins[manifest.id] = loadedPlugin @@ -798,7 +814,7 @@ class PluginManager private constructor( } } - PluginProjectManager.getInstance().cleanupPluginTemplates(pluginId) + PluginProjectManager.getInstance().cleanupPluginTemplates(pluginId, loadedPlugins.keys - pluginId) PluginSnippetManager.getInstance().cleanupPlugin(pluginId) snippetRefreshListener?.invoke(pluginId) @@ -842,10 +858,6 @@ class PluginManager private constructor( // Drop lifecycle listeners this plugin registered; its classloader is going away. lifecycleDispatcher.removeAllFrom(pluginId) - File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId).let { dir -> - if (dir.exists()) dir.deleteRecursively() - } - notifyLanguageContributionsChanged(loadedPlugin.plugin) logger.info("Unloaded plugin: $pluginId") return true @@ -859,7 +871,13 @@ class PluginManager private constructor( incomingFile: File, existingPluginId: String, ): Boolean { - val existingFile = File(pluginsDir, "$existingPluginId.$PLUGIN_ARCHIVE_EXTENSION") + val existingFile = + loadedPlugins[existingPluginId]?.apkPath?.let(::File)?.takeIf(File::isFile) + ?: buildFiles.filesOf(existingPluginId).firstOrNull() + if (existingFile == null) { + logger.warn("No installed file found for $existingPluginId; treating as mismatch") + return false + } val incomingSig = PluginLoader(context, incomingFile).getSignatureHash() val existingSig = PluginLoader(context, existingFile).getSignatureHash() if (incomingSig == null || existingSig == null) { @@ -886,41 +904,7 @@ class PluginManager private constructor( logger.info("Successfully unloaded plugin from memory: $pluginId") } - // Find and delete the plugin file (CGP) - val pluginFiles = - pluginsDir.listFiles { file -> - file.isFile && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) - } - - if (pluginFiles == null || pluginFiles.isEmpty()) { - logger.error("No plugin files found in plugins directory") - return false - } - - logger.info("Found ${pluginFiles.size} plugin files to check") - var deleted = false - for (pluginFile in pluginFiles) { - try { - // Check if this contains the plugin we want to delete - val loader = PluginLoader(context, pluginFile) - val manifest = loader.getPluginMetadata() - - if (manifest != null) { - if (manifest.id == pluginId) { - if (pluginFile.delete()) { - deleted = true - break // Found and deleted the right file - } else { - logger.error("File exists: ${pluginFile.exists()}, Can write: ${pluginFile.canWrite()}") - } - } - } else { - logger.warn("Could not read manifest from ${pluginFile.name}") - } - } catch (e: Exception) { - logger.error("Error checking plugin file ${pluginFile.name}: ${e.message}", e) - } - } + val deleted = buildFiles.delete(pluginId) // Remove plugin state and cleanup contributions if (deleted) { @@ -1032,8 +1016,7 @@ class PluginManager private constructor( logger.error("No resource context registered for plugin $pluginId; ignoring its languages") return@flatMap emptyList() } - val nativeLibraryDir = - File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId).takeIf { it.isDirectory } + val nativeLibraryDir = loaded.nativeLibraryDir?.takeIf { it.isDirectory } definitions .filter { definition -> isLanguageAllowed(pluginId, loaded.manifest, definition, nativeLibraryDir) } .map { definition -> PluginLanguageContribution(pluginId, definition, assets, nativeLibraryDir) } @@ -1188,7 +1171,7 @@ class PluginManager private constructor( private fun cleanupPluginContributions(loadedPlugin: LoadedPlugin) { val pluginId = loadedPlugin.manifest.id - runCatching { PluginProjectManager.getInstance().cleanupPluginTemplates(pluginId) } + runCatching { PluginProjectManager.getInstance().cleanupPluginTemplates(pluginId, loadedPlugins.keys - pluginId) } .onFailure { logger.error("Failed to clean project templates for: $pluginId", it) } runCatching { PluginSnippetManager.getInstance().cleanupPlugin(pluginId) @@ -1587,6 +1570,7 @@ class PluginManager private constructor( pluginId = pluginId, permissions = permissions, onTemplatesChanged = { templateReloadListener?.invoke() }, + loadedPluginIds = { loadedPlugins.keys }, ) } @@ -1856,6 +1840,7 @@ class PluginManager private constructor( pluginId = pluginId, permissions = permissions, onTemplatesChanged = { templateReloadListener?.invoke() }, + loadedPluginIds = { loadedPlugins.keys }, ) } @@ -1897,25 +1882,22 @@ class PluginManager private constructor( logger.debug("Cleaning up ALL files and cache for plugin: $pluginId") val pluginDir = File(pluginsDir, pluginId) - if (pluginDir.exists()) { - val deleted = pluginDir.deleteRecursively() - logger.debug("Deleted plugin directory: ${pluginDir.absolutePath} (success: $deleted)") - } - - File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId).let { dir -> - if (dir.exists()) dir.deleteRecursively() - } + // No exists() guard: it follows links, so a dangling link would survive. + val deleted = pluginDir.deleteWithoutFollowingLinks() + logger.debug("Deleted plugin directory: ${pluginDir.absolutePath} (success: $deleted)") File(context.getDir("plugin_icons", Context.MODE_PRIVATE), pluginId).let { dir -> - if (dir.exists()) dir.deleteRecursively() + dir.deleteWithoutFollowingLinks() } // Clean up ART cache files in oat directory try { val oatDir = File(pluginsDir, "oat") if (oatDir.exists() && oatDir.isDirectory) { - oatDir.walkTopDown().forEach { file -> - if (file.name.contains(pluginId)) { + // Not into a link: a file named like this plugin's code could be anywhere it points. + oatDir.walkTopDown().onEnter { !Files.isSymbolicLink(it.toPath()) }.forEach { file -> + // Skips code whose plugin file remains: it is another plugin's. + if (buildFiles.isLeftoverCompiledCodeOf(pluginId, file)) { val deleted = file.deleteRecursively() logger.debug("Deleted ART cache: ${file.absolutePath} (success: $deleted)") } @@ -1925,18 +1907,8 @@ class PluginManager private constructor( logger.warn("Failed to cleanup ART cache files for: $pluginId", e) } - // Clean up any other directories or files that contain the plugin ID - try { - pluginsDir.walkTopDown().forEach { file -> - if (file != pluginsDir && file.name.contains(pluginId)) { - val deleted = file.deleteRecursively() - logger.debug("Deleted plugin-related item: ${file.absolutePath} (success: $deleted)") - } - } - } catch (e: Exception) { - logger.warn("Failed to cleanup plugin-related files for: $pluginId", e) - } - + // Nothing is swept by a name containing the ID: another plugin's ID, content hash or data + // can contain it. The plugin's files went by manifest ID in buildFiles.delete(). logger.debug("Complete plugin cleanup finished for: $pluginId") } } @@ -1985,6 +1957,7 @@ data class LoadedPlugin( var isEnabled: Boolean = true, val iconDayPath: String? = null, val iconNightPath: String? = null, + val nativeLibraryDir: File? = null, ) fun LoadedPlugin.toPluginMetadata(): PluginMetadata = diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt index 6fc6f51e2b..f106fae698 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt @@ -6,8 +6,8 @@ import android.content.pm.PackageInfo import android.content.pm.PackageManager import android.content.res.AssetManager import android.content.res.Resources -import android.os.Build import android.os.Bundle +import com.itsaky.androidide.plugins.manager.core.deleteWithoutFollowingLinks import dalvik.system.DexClassLoader import org.slf4j.LoggerFactory import java.io.File @@ -28,7 +28,6 @@ class PluginLoader( private var pluginResources: Resources? = null private var pluginPackageInfo: PackageInfo? = null private var pluginClassLoader: DexClassLoader? = null - private var nativeLibDir: File? = null /** * Load plugin resources from APK @@ -149,46 +148,6 @@ class PluginLoader( ) } - fun extractNativeLibs(pluginId: String): File? { - val pluginNativeDir = File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId) - if (pluginNativeDir.exists()) { - nativeLibDir = pluginNativeDir - return pluginNativeDir - } - - val libPrefix = "lib/${Build.SUPPORTED_ABIS[0]}/" - val targetPath = pluginNativeDir.toPath().toAbsolutePath().normalize() - pluginNativeDir.mkdirs() - - var found = false - ZipFile(pluginApk).use { zip -> - zip - .entries() - .asSequence() - .filter { !it.isDirectory && it.name.startsWith(libPrefix) && it.name.endsWith(".so") } - .forEach { entry -> - val outPath = targetPath.resolve(entry.name.substringAfterLast("/")).normalize() - if (outPath.startsWith(targetPath)) { - zip.getInputStream(entry).use { input -> - outPath.toFile().outputStream().use { output -> - input.copyTo(output) - } - } - outPath.toFile().setExecutable(true, false) - found = true - } - } - } - - if (!found) { - pluginNativeDir.deleteRecursively() - return null - } - - nativeLibDir = pluginNativeDir - return pluginNativeDir - } - fun isDebuggable(): Boolean { val packageInfo = pluginPackageInfo @@ -214,7 +173,7 @@ class PluginLoader( ): Pair { if (manifest.iconDay == null && manifest.iconNight == null) return null to null val iconDir = File(context.getDir("plugin_icons", Context.MODE_PRIVATE), pluginId) - iconDir.deleteRecursively() + iconDir.deleteWithoutFollowingLinks() iconDir.mkdirs() val targetPath = iconDir.toPath().toAbsolutePath().normalize() return try { diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt index 3176ac508c..615820cf1f 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt @@ -5,81 +5,104 @@ import org.slf4j.LoggerFactory import java.io.File class PluginProjectManager private constructor() { - - companion object { - private val log = LoggerFactory.getLogger(PluginProjectManager::class.java) - - @Volatile - private var instance: PluginProjectManager? = null - - fun getInstance(): PluginProjectManager { - return instance ?: synchronized(this) { - instance ?: PluginProjectManager().also { instance = it } - } - } - - private const val PLUGIN_CGT_PREFIX = "plugin_" - private const val CGT_EXTENSION = "cgt" - } - - private var templateReloadListener: (() -> Unit)? = null - - fun setTemplateReloadListener(listener: (() -> Unit)?) { - this.templateReloadListener = listener - } - - fun extractBundledCgtTemplates(pluginId: String, classLoader: ClassLoader) { - val searchPaths = listOf("templates/", "assets/templates/") - - val extracted = searchPaths.flatMap { basePath -> - extractCgtFromPath(pluginId, classLoader, basePath) - } - - if (extracted.isNotEmpty()) { - templateReloadListener?.invoke() - } - } - - private fun extractCgtFromPath( - pluginId: String, - classLoader: ClassLoader, - basePath: String - ): List { - val entries = classLoader.getResourceAsStream(basePath) - ?.bufferedReader() - ?.use { it.readLines() } - ?: return emptyList() - - return entries - .filter { it.endsWith(".$CGT_EXTENSION") } - .mapNotNull { entry -> - val inputStream = classLoader.getResourceAsStream("$basePath$entry") ?: return@mapNotNull null - val destFile = File(Environment.TEMPLATES_DIR, "${PLUGIN_CGT_PREFIX}${pluginId}_$entry") - - runCatching { - inputStream.use { input -> - destFile.outputStream().use { output -> input.copyTo(output) } - } - log.info("Extracted bundled template: {} from plugin {}", entry, pluginId) - destFile - }.onFailure { - log.error("Failed to extract bundled template $entry from plugin $pluginId", it) - }.getOrNull() - } - } - - fun cleanupPluginTemplates(pluginId: String) { - val prefix = "${PLUGIN_CGT_PREFIX}${pluginId}_" - val deleted = Environment.TEMPLATES_DIR - .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } - ?.count { file -> - file.delete().also { if (it) log.debug("Cleaned up plugin template: {}", file.name) } - } - ?: 0 - - if (deleted > 0) { - log.info("Cleaned up {} template files for plugin {}", deleted, pluginId) - templateReloadListener?.invoke() - } - } + companion object { + private val log = LoggerFactory.getLogger(PluginProjectManager::class.java) + + @Volatile + private var instance: PluginProjectManager? = null + + fun getInstance(): PluginProjectManager = + instance ?: synchronized(this) { + instance ?: PluginProjectManager().also { instance = it } + } + + private const val PLUGIN_CGT_PREFIX = "plugin_" + private const val CGT_EXTENSION = "cgt" + } + + private var templateReloadListener: (() -> Unit)? = null + + fun setTemplateReloadListener(listener: (() -> Unit)?) { + this.templateReloadListener = listener + } + + fun extractBundledCgtTemplates( + pluginId: String, + classLoader: ClassLoader, + ) { + val searchPaths = listOf("templates/", "assets/templates/") + + val extracted = + searchPaths.flatMap { basePath -> + extractCgtFromPath(pluginId, classLoader, basePath) + } + + if (extracted.isNotEmpty()) { + templateReloadListener?.invoke() + } + } + + private fun extractCgtFromPath( + pluginId: String, + classLoader: ClassLoader, + basePath: String, + ): List { + val entries = + classLoader + .getResourceAsStream(basePath) + ?.bufferedReader() + ?.use { it.readLines() } + ?: return emptyList() + + return entries + .filter { it.endsWith(".$CGT_EXTENSION") } + .mapNotNull { entry -> + val inputStream = classLoader.getResourceAsStream("$basePath$entry") ?: return@mapNotNull null + val destFile = File(Environment.TEMPLATES_DIR, "${PLUGIN_CGT_PREFIX}${pluginId}_$entry") + + runCatching { + inputStream.use { input -> + destFile.outputStream().use { output -> input.copyTo(output) } + } + log.info("Extracted bundled template: {} from plugin {}", entry, pluginId) + destFile + }.onFailure { + log.error("Failed to extract bundled template $entry from plugin $pluginId", it) + }.getOrNull() + } + } + + fun cleanupPluginTemplates( + pluginId: String, + otherPluginIds: Collection, + ) { + val deleted = + Environment.TEMPLATES_DIR + .listFiles { file -> isTemplateOf(pluginId, file.name, otherPluginIds) } + ?.count { file -> + file.delete().also { if (it) log.debug("Cleaned up plugin template: {}", file.name) } + } + ?: 0 + + if (deleted > 0) { + log.info("Cleaned up {} template files for plugin {}", deleted, pluginId) + templateReloadListener?.invoke() + } + } } + +/** + * Whether [fileName] in the templates folder is a template of [pluginId]: `plugin__.cgt`. + * Another plugin whose ID extends this one's (`_tools`) has names that start the same way, so + * its names are excluded. Templates exist only for loaded plugins, so [otherPluginIds] is the other + * loaded plugins. Undecidable: this plugin's own `tools_x.cgt` while `_tools` is loaded is + * kept, which leaves a file behind rather than deleting another plugin's. + */ +internal fun isTemplateOf( + pluginId: String, + fileName: String, + otherPluginIds: Collection, +): Boolean = + fileName.startsWith("plugin_${pluginId}_") && + fileName.substringAfterLast('.', "") == "cgt" && + otherPluginIds.none { it.startsWith("${pluginId}_") && fileName.startsWith("plugin_${it}_") } diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt index 202f8a6cea..b02a2a1ac7 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt @@ -1,6 +1,7 @@ package com.itsaky.androidide.plugins.manager.services import com.itsaky.androidide.plugins.PluginPermission +import com.itsaky.androidide.plugins.manager.project.isTemplateOf import com.itsaky.androidide.plugins.services.IdeTemplateService import com.itsaky.androidide.plugins.templates.CgtTemplateBuilder import com.itsaky.androidide.utils.Environment @@ -8,90 +9,89 @@ import org.slf4j.LoggerFactory import java.io.File class IdeTemplateServiceImpl( - private val pluginId: String, - private val permissions: Set, - private val onTemplatesChanged: () -> Unit + private val pluginId: String, + private val permissions: Set, + private val onTemplatesChanged: () -> Unit, + private val loadedPluginIds: () -> Collection, ) : IdeTemplateService { + companion object { + private val log = LoggerFactory.getLogger(IdeTemplateServiceImpl::class.java) + private const val PLUGIN_PREFIX = "plugin_" + private const val CGT_EXTENSION = "cgt" + } - companion object { - private val log = LoggerFactory.getLogger(IdeTemplateServiceImpl::class.java) - private const val PLUGIN_PREFIX = "plugin_" - private const val CGT_EXTENSION = "cgt" - } + private fun prefixedName(fileName: String): String = "${PLUGIN_PREFIX}${pluginId}_$fileName" - private fun prefixedName(fileName: String): String { - return "${PLUGIN_PREFIX}${pluginId}_$fileName" - } + override fun createTemplateBuilder(name: String): CgtTemplateBuilder = CgtTemplateBuilder(name) - override fun createTemplateBuilder(name: String): CgtTemplateBuilder { - return CgtTemplateBuilder(name) - } + override fun registerTemplate(cgtFile: File): Boolean { + if (!permissions.contains(PluginPermission.FILESYSTEM_WRITE)) { + log.warn("Plugin $pluginId lacks FILESYSTEM_WRITE permission for template registration") + return false + } - override fun registerTemplate(cgtFile: File): Boolean { - if (!permissions.contains(PluginPermission.FILESYSTEM_WRITE)) { - log.warn("Plugin $pluginId lacks FILESYSTEM_WRITE permission for template registration") - return false - } + if (!cgtFile.exists() || cgtFile.extension != CGT_EXTENSION) { + log.warn("Invalid template file for plugin $pluginId: ${cgtFile.absolutePath}") + return false + } - if (!cgtFile.exists() || cgtFile.extension != CGT_EXTENSION) { - log.warn("Invalid template file for plugin $pluginId: ${cgtFile.absolutePath}") - return false - } + val destFile = File(Environment.TEMPLATES_DIR, prefixedName(cgtFile.name)) + return try { + cgtFile.copyTo(destFile, overwrite = true) + log.info("Plugin $pluginId registered template: ${destFile.name}") + runCatching { onTemplatesChanged() } + .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } + true + } catch (e: Exception) { + log.error("Failed to register template for plugin $pluginId", e) + false + } + } - val destFile = File(Environment.TEMPLATES_DIR, prefixedName(cgtFile.name)) - return try { - cgtFile.copyTo(destFile, overwrite = true) - log.info("Plugin $pluginId registered template: ${destFile.name}") - runCatching { onTemplatesChanged() } - .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } - true - } catch (e: Exception) { - log.error("Failed to register template for plugin $pluginId", e) - false - } - } + override fun unregisterTemplate(templateFileName: String): Boolean { + val destFile = File(Environment.TEMPLATES_DIR, prefixedName(templateFileName)) + if (!destFile.exists()) return false - override fun unregisterTemplate(templateFileName: String): Boolean { - val destFile = File(Environment.TEMPLATES_DIR, prefixedName(templateFileName)) - if (!destFile.exists()) return false + return if (destFile.delete()) { + log.info("Plugin $pluginId unregistered template: ${destFile.name}") + runCatching { onTemplatesChanged() } + .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } + true + } else { + log.warn("Failed to delete template file: ${destFile.name}") + false + } + } - return if (destFile.delete()) { - log.info("Plugin $pluginId unregistered template: ${destFile.name}") - runCatching { onTemplatesChanged() } - .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } - true - } else { - log.warn("Failed to delete template file: ${destFile.name}") - false - } - } + override fun isTemplateRegistered(templateFileName: String): Boolean = + File(Environment.TEMPLATES_DIR, prefixedName(templateFileName)).exists() - override fun isTemplateRegistered(templateFileName: String): Boolean { - return File(Environment.TEMPLATES_DIR, prefixedName(templateFileName)).exists() - } + override fun getRegisteredTemplates(): List { + val prefix = prefixedName("") + val others = otherPluginIds() + return Environment.TEMPLATES_DIR + .listFiles { file -> isTemplateOf(pluginId, file.name, others) } + ?.map { it.name.removePrefix(prefix) } + ?: emptyList() + } - override fun getRegisteredTemplates(): List { - val prefix = prefixedName("") - return Environment.TEMPLATES_DIR - .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } - ?.map { it.name.removePrefix(prefix) } - ?: emptyList() - } + private fun otherPluginIds() = loadedPluginIds() - pluginId - override fun reloadTemplates() { - onTemplatesChanged() - } + override fun reloadTemplates() { + onTemplatesChanged() + } - fun cleanupAllTemplates() { - val prefix = prefixedName("") - val deleted = Environment.TEMPLATES_DIR - .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } - ?.count { it.delete() } - ?: 0 + fun cleanupAllTemplates() { + val others = otherPluginIds() + val deleted = + Environment.TEMPLATES_DIR + .listFiles { file -> isTemplateOf(pluginId, file.name, others) } + ?.count { it.delete() } + ?: 0 - if (deleted > 0) { - runCatching { onTemplatesChanged() } - .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } - } - } + if (deleted > 0) { + runCatching { onTemplatesChanged() } + .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } + } + } } diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt new file mode 100644 index 0000000000..f2a467644f --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -0,0 +1,348 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import org.junit.Assume +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File +import java.io.IOException +import java.nio.file.Files +import java.nio.file.LinkOption +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream +import kotlin.random.Random + +/** + * Each installed plugin build has paths of its own. Android caches resources, assets and native + * libraries by path, so a build at the previous build's paths ran against the previous build's + * files until the app restarted. + */ +class PluginBuildFilesTest { + @get:Rule + val temp = TemporaryFolder() + + private val pluginsDir by lazy { temp.newFolder("plugins") } + private val nativeLibsDir by lazy { temp.newFolder("plugin_native_libs") } + + // Uninstall tests write the plugin ID as the file's text; a real file has it in its manifest. + private val files by lazy { newFiles() } + + private fun newFiles() = PluginBuildFiles(pluginsDir, nativeLibsDir) { it.readText() } + + @Test + fun eachBuildIsNamedAfterItsContent() { + val a = build("a.cgp", "libfoo.so" to "A") + val b = build("b.cgp", "libfoo.so" to "B") + val aAgain = a.copyTo(File(temp.root, "a-again.cgp")) + + val stemA = PluginBuildFiles.fileStem(ID, a) + + assertThat(stemA).matches("${Regex.escape(ID)}-[0-9a-f]{16}") + assertWithMessage("two builds got one name").that(PluginBuildFiles.fileStem(ID, b)).isNotEqualTo(stemA) + assertThat(PluginBuildFiles.fileStem(ID, aAgain)).isEqualTo(stemA) + } + + @Test + fun aNewBuildLoadsItsLibrariesFromANewFolder() { + val a = installed(build("a.cgp", "libfoo.so" to "A")) + val dirA = files.extractNativeLibs(ID, a, ABI) + a.delete() + + val dirB = files.extractNativeLibs(ID, installed(build("b.cgp", "libfoo.so" to "B")), ABI) + + assertWithMessage("the new build reuses the old build's library path").that(dirB).isNotEqualTo(dirA) + assertThat(File(dirB, "libfoo.so").readText()).isEqualTo("B") + assertWithMessage("the replaced build's folder is left behind").that(dirA!!.exists()).isFalse() + } + + @Test + fun anotherInstalledBuildKeepsItsFolder() { + // Two files with one ID both load, in parallel: neither may delete the other's libraries. + val dirA = files.extractNativeLibs(ID, installed(build("a.cgp", "libfoo.so" to "A")), ABI) + files.extractNativeLibs(ID, installed(build("b.cgp", "libfoo.so" to "B")), ABI) + + assertThat(File(dirA, "libfoo.so").readText()).isEqualTo("A") + } + + @Test + fun anOlderCoGoBuildFindsNoFolderToReuse() { + val dataDir = temp.newFolder("data") + val build = installed(build("a.cgp", "libfoo.so" to "A")) + + val dir = PluginBuildFiles.inDataDir(pluginsDir, dataDir) { ID }.extractNativeLibs(ID, build, ABI) + + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("A") + // Before per-build folders, CoGo loaded a plugin's libraries from getDir("plugin_native_libs")/ + // whenever that folder existed, whatever it held. + assertWithMessage("an older CoGo build would find this build's folder and no libraries in it") + .that(File(dataDir, "app_plugin_native_libs/$ID").exists()) + .isFalse() + } + + @Test + fun librariesAnOlderCoGoBuildExtractedAreDeleted() { + val dataDir = temp.newFolder("data") + File(dataDir, "app_plugin_native_libs/$ID").mkdirs() + File(dataDir, "app_plugin_native_libs/$ID/libfoo.so").writeText("old") + val legacy = build("$ID.cgp", "libfoo.so" to "new").copyTo(File(pluginsDir, "$ID.cgp")) + + val dir = PluginBuildFiles.inDataDir(pluginsDir, dataDir) { ID }.extractNativeLibs(ID, legacy, ABI) + + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("new") + assertWithMessage("an older CoGo build's libraries are left behind") + .that(File(dataDir, "app_plugin_native_libs").exists()) + .isFalse() + } + + @Test + fun aFileUnderTheOldNameRewrittenInPlaceGetsItsOwnLibraries() { + val legacy = build("x.cgp", "libfoo.so" to "X").copyTo(File(pluginsDir, "$ID.cgp")) + legacy.setLastModified(1_000_000_000_000) + files.extractNativeLibs(ID, legacy, ABI) + // An older CoGo build updates the plugin under the same name. + build("y.cgp", "libfoo.so" to "Y").copyTo(legacy, overwrite = true) + legacy.setLastModified(1_000_000_001_000) + + val dir = newFiles().extractNativeLibs(ID, legacy, ABI) + + assertWithMessage("the rewritten file ran against the previous content's libraries") + .that(File(dir, "libfoo.so").readText()) + .isEqualTo("Y") + } + + @Test + fun anInstalledBuildUnderTheOldNameOfAnIdWithATildeKeepsItsFolder() { + val id = "org.example~plugin" + val legacy = build("old.cgp", "libfoo.so" to "old").copyTo(File(pluginsDir, "$id.cgp")) + val legacyDir = files.extractNativeLibs(id, legacy, ABI) + val hashed = build("new.cgp", "libfoo.so" to "new") + + files.extractNativeLibs(id, hashed.copyTo(File(pluginsDir, "${PluginBuildFiles.fileStem(id, hashed)}.cgp")), ABI) + + assertWithMessage("loading one build deleted the folder of another installed build") + .that(File(legacyDir, "libfoo.so").exists()) + .isTrue() + } + + @Test + fun aLeftoverScratchFolderIsNotReused() { + val build = installed(build("a.cgp", "libfoo.so" to "A")) + val scratch = File(nativeLibsDir, "$ID/.${PluginBuildFiles.buildKey(ID, build)}.partial") + scratch.mkdirs() + File(scratch, "libstale.so").writeText("from an interrupted extraction") + + val dir = files.extractNativeLibs(ID, build, ABI) + + assertThat(dir!!.list()!!.toList()).containsExactly("libfoo.so") + assertThat(scratch.exists()).isFalse() + } + + @Test + fun aBuildReinstalledInTheSameProcessGetsANewFolder() { + val build = build("a.cgp", "libfoo.so" to "A") + val first = files.extractNativeLibs(ID, installed(build), ABI) + files.delete(ID) + + val second = files.extractNativeLibs(ID, installed(build), ABI) + + // The old class loader may still hold the first path, and the runtime refuses to open it again. + assertWithMessage("the reinstalled build got the path the old class loader opened").that(second).isNotEqualTo(first) + assertThat(File(second, "libfoo.so").readText()).isEqualTo("A") + assertWithMessage("the next start extracts again instead of reusing the folder") + .that(newFiles().extractNativeLibs(ID, File(pluginsDir, "${PluginBuildFiles.fileStem(ID, build)}.cgp"), ABI)) + .isEqualTo(second) + } + + @Test + fun overlappingExtractionsOfABuildAllGetItsLibraries() { + val content = Random(7).nextBytes(4 shl 20) + repeat(ROUNDS) { round -> + val id = "$ID.r$round" + val build = build("r$round.cgp", "libbig.so" to content) + val installed = build.copyTo(File(pluginsDir, "${PluginBuildFiles.fileStem(id, build)}.cgp")) + val start = CountDownLatch(1) + val pool = Executors.newFixedThreadPool(THREADS) + val results = + (1..THREADS).map { + pool.submit { + start.await() + files.extractNativeLibs(id, installed, ABI) + } + } + start.countDown() + val dirs = results.map { it.get(60, TimeUnit.SECONDS) } + pool.shutdown() + + assertWithMessage("round $round: loads got different folders").that(dirs.toSet()).hasSize(1) + assertWithMessage("round $round: a library is incomplete") + .that(File(dirs.first(), "libbig.so").readBytes().contentEquals(content)) + .isTrue() + } + } + + @Test + fun aBuildWithoutLibrariesForTheAbiGetsNoFolder() { + File(nativeLibsDir, "$ID/0123456789abcdef").mkdirs() + val otherAbi = build("x.cgp", "libfoo.so" to "x", abi = "x86_64") + + assertThat(files.extractNativeLibs(ID, installed(otherAbi), ABI)).isNull() + assertThat(File(nativeLibsDir, ID).exists()).isFalse() + } + + @Test + fun uninstallDeletesTheLibrariesOfAPluginWithNoFileLeft() { + File(nativeLibsDir, "$ID/0123456789abcdef").mkdirs() + File(nativeLibsDir, "$ID/0123456789abcdef/libfoo.so").writeText("A") + + assertThat(files.delete(ID)).isFalse() + assertThat(File(nativeLibsDir, ID).exists()).isFalse() + } + + @Test + fun uninstallDeletesEveryFileOfThePluginAndNoOther() { + val legacy = File(pluginsDir, "$ID.cgp").apply { writeText(ID) } + val hashed = File(pluginsDir, "$ID-0123456789abcdef.cgp").apply { writeText(ID) } + // An ID that starts with this one, so only the manifest tells the files apart. + val other = File(pluginsDir, "$ID.extra-0123456789abcdef.cgp").apply { writeText("$ID.extra") } + + assertThat(files.filesOf(ID)).containsExactly(legacy, hashed) + assertThat(files.delete(ID)).isTrue() + assertWithMessage("a second file of the plugin survived").that(pluginsDir.listFiles()!!.toList()).containsExactly(other) + } + + @Test + fun aPluginIdThatIsAPathDeletesNothingOutsideTheLibrariesFolder() { + val victim = File(nativeLibsDir.parentFile, "victim").apply { mkdirs() } + File(victim, "keep.txt").writeText("not a plugin's") + val build = installed(build("a.cgp", "libfoo.so" to "A")) + + val deleted = runCatching { files.delete("../victim") } + val extracted = runCatching { files.extractNativeLibs("../victim", build, ABI) } + + assertWithMessage("a folder outside the libraries folder lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(deleted.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java) + assertThat(extracted.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun aPluginFolderThatLinksElsewhereIsNotFollowed() { + val victim = victim() + val link = File(nativeLibsDir, ID).also { Files.createSymbolicLink(it.toPath(), victim.toPath()) } + + val deleted = runCatching { files.delete(ID) } + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(deleted.exceptionOrNull()).isNull() + assertWithMessage("the link is still there").that(Files.exists(link.toPath(), LinkOption.NOFOLLOW_LINKS)).isFalse() + } + + @Test + fun aPluginFolderThatLinksElsewhereIsReplacedByAFolderOfLibraries() { + val victim = victim() + Files.createSymbolicLink(File(nativeLibsDir, ID).toPath(), victim.toPath()) + + val dir = files.extractNativeLibs(ID, installed(build("a.cgp", "libfoo.so" to "A")), ABI) + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("A") + assertWithMessage("the libraries went through a link").that(Files.isSymbolicLink(File(nativeLibsDir, ID).toPath())).isFalse() + } + + @Test + fun aPluginFolderLinkThatCannotBeDeletedFailsExtractionInsteadOfEmptyingItsTarget() { + val victim = victim() + val link = File(nativeLibsDir, ID).also { Files.createSymbolicLink(it.toPath(), victim.toPath()) } + val build = installed(build("a.cgp", "libfoo.so" to "A")) + + // The link can't be unlinked from a read-only folder. + nativeLibsDir.setWritable(false) + val extracted = + try { + // Root, and file systems without POSIX permissions, ignore the write bit. + Assume.assumeFalse("the folder stayed writable", nativeLibsDir.canWrite()) + runCatching { files.extractNativeLibs(ID, build, ABI) } + } finally { + nativeLibsDir.setWritable(true) + } + + assertThat(extracted.exceptionOrNull()).isInstanceOf(IOException::class.java) + assertThat(extracted.exceptionOrNull()).hasMessageThat().contains(link.absolutePath) + assertWithMessage("extraction went through the link").that(victim.list()?.toList()).containsExactly("keep.txt") + } + + @Test + fun aLinkInThePluginFolderIsNotFollowedByUninstall() { + val victim = victim() + File(nativeLibsDir, ID).mkdirs() + Files.createSymbolicLink(File(nativeLibsDir, "$ID/some-build").toPath(), victim.toPath()) + + files.delete(ID) + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(File(nativeLibsDir, ID).exists()).isFalse() + } + + @Test + fun aLinkInThePluginFolderIsNotFollowedByTheCleanupOfOldBuilds() { + val victim = victim() + File(nativeLibsDir, ID).mkdirs() + Files.createSymbolicLink(File(nativeLibsDir, "$ID/stale").toPath(), victim.toPath()) + + val dir = files.extractNativeLibs(ID, installed(build("a.cgp", "libfoo.so" to "A")), ABI) + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("A") + assertThat(File(nativeLibsDir, "$ID/stale").exists()).isFalse() + } + + @Test + fun aLinkAtTheBuildsFolderIsNotReused() { + val victim = victim() + val build = installed(build("a.cgp", "libfoo.so" to "A")) + File(nativeLibsDir, ID).mkdirs() + Files.createSymbolicLink(File(nativeLibsDir, "$ID/${PluginBuildFiles.buildKey(ID, build)}").toPath(), victim.toPath()) + + val dir = files.extractNativeLibs(ID, build, ABI) + + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("A") + assertWithMessage("the build's libraries load through a link").that(Files.isSymbolicLink(dir!!.toPath())).isFalse() + assertWithMessage("the link's target changed").that(victim.list()?.toList()).containsExactly("keep.txt") + } + + // Outside the folders the code under test deletes, with a file that must survive. + private fun victim(): File = + File(nativeLibsDir.parentFile, "victim").apply { mkdirs() }.also { File(it, "keep.txt").writeText("not a plugin's") } + + // As PluginRepositoryImpl.installPluginFromFile names and copies it. + private fun installed(build: File): File { + val name = "${PluginBuildFiles.fileStem(ID, build)}.cgp" + return build.copyTo(File(pluginsDir, name), overwrite = true) + } + + private fun build( + name: String, + vararg libs: Pair, + abi: String = ABI, + ): File = + File(temp.newFolder(), name).also { file -> + ZipOutputStream(file.outputStream()).use { zip -> + for ((lib, content) in libs) { + zip.putNextEntry(ZipEntry("lib/$abi/$lib")) + zip.write(if (content is ByteArray) content else content.toString().toByteArray()) + zip.closeEntry() + } + } + } + + private companion object { + const val ID = "org.example.plugin" + const val ABI = "arm64-v8a" + const val ROUNDS = 10 + const val THREADS = 6 + } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt new file mode 100644 index 0000000000..a38a2bea1d --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt @@ -0,0 +1,108 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import org.junit.After +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +/** + * A plugin ID names the plugin's files and folders, so the installer refuses one that is a path: + * installing a plugin whose ID was `../files` deleted the app's whole files folder. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginIdTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun theInstallerRefusesAPluginIdThatIsAPath() { + for (id in listOf("../files", "..", ".", "", "a/b", "/data", "a\\b", "a\u0000b")) { + val result = pluginManager().getPluginValidation(plugin(id)) + + assertWithMessage("the installer accepted \"$id\"").that(result.isFailure).isTrue() + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"$id\": it must be a name, not a path") + } + } + + @Test + fun theInstallerAcceptsAPluginIdThatIsAName() { + for (id in listOf("com.example.myplugin", "org.adfa.shell-hooks", "my_plugin", "a..b", "...")) { + assertThat( + pluginManager() + .getPluginValidation(plugin(id)) + .getOrThrow() + .manifest.id, + ).isEqualTo(id) + } + } + + @Test + fun loadingRefusesAPluginIdThatIsAPath() { + val files = RuntimeEnvironment.getApplication().filesDir + File(files, "keep.txt").writeText("the app's") + + val result = pluginManager().loadPlugin(plugin("../files")) + + assertWithMessage("loading deleted the app's files").that(files.list()?.toList()).contains("keep.txt") + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"../files\": it must be a name, not a path") + } + + @Test + fun theInstallerRefusesTheIdsOfCoGosOwnFiles() { + for ((id, message) in refusedCoGoIds) { + val result = pluginManager().getPluginValidation(plugin(id)) + + assertWithMessage("the installer accepted \"$id\"").that(result.isFailure).isTrue() + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"$id\": $message") + } + } + + @Test + fun loadingRefusesTheIdsOfCoGosOwnFiles() { + for ((id, message) in refusedCoGoIds) { + val result = pluginManager().loadPlugin(plugin(id)) + + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"$id\": $message") + } + } + + @Test + fun idsThatOnlyLookLikeCoGosOwnFilesAreAccepted() { + for (id in listOf("oat.tools", "cgp", "my.cgp.tools", "oats", "x.cgpx")) { + assertThat( + pluginManager() + .getPluginValidation(plugin(id)) + .getOrThrow() + .manifest.id, + ).isEqualTo(id) + } + } + + @Test + fun aManifestWithoutAnIdIsRefused() { + assertThat(checkPluginId(null).exceptionOrNull()) + .hasMessageThat() + .isEqualTo("Invalid plugin ID \"null\": it must be a name, not a path") + } + + @After + fun forgetManager() = forgetPluginManager() + + private val refusedCoGoIds = + listOf( + "oat" to "\"oat\" is the folder of compiled plugin code", + "OAT" to "\"oat\" is the folder of compiled plugin code", + "x.cgp" to "it must not end in .cgp, which names a plugin file", + "x.CGP" to "it must not end in .cgp, which names a plugin file", + ) + + private fun plugin(id: String): File = fakePlugin(File(temp.newFolder(), "plugin.cgp"), id) +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerTemplateWiringTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerTemplateWiringTest.kt new file mode 100644 index 0000000000..2d3cea44c1 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerTemplateWiringTest.kt @@ -0,0 +1,74 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.plugins.services.IdeTemplateService +import com.itsaky.androidide.utils.Environment +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +/** + * The plugin manager tells template cleanup which other plugins are loaded: unloading or disabling + * plugin "x" keeps the templates of the loaded plugin "x_tools", whose names start with the same prefix. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginManagerTemplateWiringTest { + private val previous = Environment.TEMPLATES_DIR + private lateinit var templates: File + private lateinit var own: File + private lateinit var other: File + + @Before + fun loadTwoPluginsWithTemplates() { + templates = File(RuntimeEnvironment.getApplication().filesDir, "templates").apply { mkdirs() } + Environment.TEMPLATES_DIR = templates + own = template("plugin_x_starter.cgt") + other = template("plugin_x_tools_starter.cgt") + val pluginsDir = File(RuntimeEnvironment.getApplication().filesDir, "plugins") + val main = NoopPlugin::class.java.name + listOf("x", "x_tools").forEach { + val result = pluginManager().loadPlugin(fakePlugin(File(pluginsDir, "$it-0123456789012345.cgp"), it, main)) + assertWithMessage("plugin $it did not load: ${result.exceptionOrNull()}").that(result.isSuccess).isTrue() + } + assertThat(pluginManager().getLoadedPluginIds()).containsExactly("x", "x_tools") + } + + @After + fun restoreTemplatesFolder() { + Environment.TEMPLATES_DIR = previous + forgetPluginManager() + } + + @Test + fun unloadingAPluginKeepsTheTemplatesOfAnotherLoadedPlugin() { + assertThat(pluginManager().unloadPlugin("x")).isTrue() + + assertThat(own.exists()).isFalse() + assertWithMessage("unloading \"x\" deleted a template of \"x_tools\"").that(other.exists()).isTrue() + } + + @Test + fun forceDisablingAPluginKeepsTheTemplatesOfAnotherLoadedPlugin() { + pluginManager().forceDisablePlugin("x") + + assertThat(own.exists()).isFalse() + assertWithMessage("disabling \"x\" deleted a template of \"x_tools\"").that(other.exists()).isTrue() + } + + @Test + fun aPluginsTemplateServiceDoesNotListTheTemplatesOfAnotherLoadedPlugin() { + val plugin = pluginManager().getPlugin("x") as NoopPlugin + val service = plugin.context.services.get(IdeTemplateService::class.java)!! + + assertThat(service.getRegisteredTemplates()).containsExactly("starter.cgt") + } + + private fun template(name: String): File = File(templates, name).apply { writeText(name) } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginTemplateCleanupTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginTemplateCleanupTest.kt new file mode 100644 index 0000000000..a1c5c442d7 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginTemplateCleanupTest.kt @@ -0,0 +1,77 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.plugins.PluginPermission +import com.itsaky.androidide.plugins.manager.project.PluginProjectManager +import com.itsaky.androidide.plugins.manager.services.IdeTemplateServiceImpl +import com.itsaky.androidide.utils.Environment +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +/** + * Updating or uninstalling plugin "com.example.my" deletes its own templates, not those of the + * loaded plugin "com.example.my_tools", whose names start with the same prefix. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginTemplateCleanupTest { + private val previous = Environment.TEMPLATES_DIR + private lateinit var templates: File + + @Before + fun pointTemplatesAtATempFolder() { + templates = File(RuntimeEnvironment.getApplication().filesDir, "templates").apply { mkdirs() } + Environment.TEMPLATES_DIR = templates + } + + @After + fun restoreTemplatesFolder() { + Environment.TEMPLATES_DIR = previous + } + + @Test + fun cleanupPluginTemplatesKeepsAnotherLoadedPluginsTemplates() { + val own = template("plugin_com.example.my_starter.cgt") + val other = template("plugin_com.example.my_tools_starter.cgt") + + PluginProjectManager.getInstance().cleanupPluginTemplates("com.example.my", listOf("com.example.my_tools")) + + assertThat(own.exists()).isFalse() + assertWithMessage("cleanup deleted another plugin's template").that(other.exists()).isTrue() + } + + @Test + fun cleanupAllTemplatesKeepsAnotherLoadedPluginsTemplates() { + val own = template("plugin_com.example.my_starter.cgt") + val other = template("plugin_com.example.my_tools_starter.cgt") + val service = service("com.example.my", loaded = listOf("com.example.my", "com.example.my_tools")) + + service.cleanupAllTemplates() + + assertThat(own.exists()).isFalse() + assertWithMessage("cleanup deleted another plugin's template").that(other.exists()).isTrue() + } + + @Test + fun getRegisteredTemplatesListsOnlyTheOwnTemplates() { + template("plugin_com.example.my_starter.cgt") + template("plugin_com.example.my_tools_starter.cgt") + val service = service("com.example.my", loaded = listOf("com.example.my", "com.example.my_tools")) + + assertThat(service.getRegisteredTemplates()).containsExactly("starter.cgt") + } + + private fun service( + id: String, + loaded: List, + ) = IdeTemplateServiceImpl(id, setOf(PluginPermission.FILESYSTEM_WRITE), {}, { loaded }) + + private fun template(name: String): File = File(templates, name).apply { writeText(name) } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt new file mode 100644 index 0000000000..f09ad228e1 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt @@ -0,0 +1,235 @@ +package com.itsaky.androidide.plugins.manager.core + +import android.content.Context +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File +import java.nio.file.Files +import java.nio.file.LinkOption + +/** + * Uninstalling a plugin deletes its own files only. Update uninstalls first, so the same goes for + * every update. The plugin uninstalled here is "a", which occurs in the other plugins' content + * hashes, IDs and file names. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginUninstallTest { + private val pluginsDir by lazy { File(RuntimeEnvironment.getApplication().filesDir, "plugins") } + private val oatDir by lazy { File(pluginsDir, "oat") } + + @After + fun forgetManager() = forgetPluginManager() + + @Test + fun anotherPluginsFileWhoseNameContainsTheIdIsKept() { + val uninstalled = fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val others = + listOf( + fakePlugin(File(pluginsDir, "org.demo.tool-0123456789abcdef.cgp"), "org.demo.tool"), + fakePlugin(File(pluginsDir, "a.extra-1111111111111111.cgp"), "a.extra"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertThat(uninstalled.exists()).isFalse() + assertWithMessage("uninstalling \"a\" deleted another plugin's file") + .that(pluginsDir.listFiles()!!.toList()) + .containsAtLeastElementsIn(others) + } + + @Test + fun anotherPluginsDataIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val others = listOf(file(pluginsDir, "a.extra/state.json"), file(pluginsDir, "org.demo.tool/cache.bin")) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted another plugin's data") + .that(others.filterNot(File::exists)) + .isEmpty() + } + + @Test + fun anotherPluginsCompiledCodeIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val others = + listOf( + file(oatDir, "arm64/org.demo.tool-0123456789abcdef.odex"), + file(oatDir, "arm64/a.extra-1111111111111111.vdex"), + file(oatDir, "org.demo.tool-0123456789abcdef.cgp.cur.prof"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted another plugin's compiled code") + .that(others.filterNot(File::exists)) + .isEmpty() + } + + @Test + fun thePluginsOwnDataAndCompiledCodeAreDeleted() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val own = + listOf( + file(pluginsDir, "a/state.json"), + file(oatDir, "arm64/a-0123456789012345.odex"), + file(oatDir, "arm64/a-0123456789012345.vdex"), + file(oatDir, "arm64/a-0123456789012345.art"), + file(oatDir, "a-0123456789012345.cgp.cur.prof"), + file(oatDir, "a-0123456789012345.cgp.prof"), + // From a file installed before hashes. + file(oatDir, "arm64/a.odex"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" left its own files").that(own.filter(File::exists)).isEmpty() + } + + @Test + fun theProfilesOfAPluginInstalledWithAnUpperCaseExtensionAreDeleted() { + fakePlugin(File(pluginsDir, "a-0123456789012345.CGP"), "a") + val own = listOf(file(oatDir, "a-0123456789012345.CGP.prof"), file(oatDir, "a-0123456789012345.CGP.cur.prof")) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" left the profiles of its .CGP file").that(own.filter(File::exists)).isEmpty() + } + + @Test + fun compiledCodeOfAnotherPluginWhoseIdLooksLikeThisPluginsFileNameIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + // Installed by an older CoGo build: named by its ID, which looks like "a" plus a hash. + fakePlugin(File(pluginsDir, "a-0123456789abcdef.cgp"), "a-0123456789abcdef") + val others = + listOf( + file(oatDir, "arm64/a-0123456789abcdef.odex"), + file(oatDir, "a-0123456789abcdef.cgp.cur.prof"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted the compiled code of \"a-0123456789abcdef\"") + .that(others.filterNot(File::exists)) + .isEmpty() + } + + @Test + fun compiledCodeOfAnotherPluginInstalledWithAnUpperCaseExtensionIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + fakePlugin(File(pluginsDir, "a-0123456789abcdef.CGP"), "a-0123456789abcdef") + val other = file(oatDir, "arm64/a-0123456789abcdef.odex") + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted the compiled code of a .CGP file").that(other.exists()).isTrue() + } + + @Test + fun aLinkAtThePluginsDataFolderIsDeletedNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim() + val link = File(pluginsDir, "a").toPath() + Files.createSymbolicLink(link, victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertWithMessage("the link is still there").that(Files.exists(link, LinkOption.NOFOLLOW_LINKS)).isFalse() + } + + @Test + fun aDanglingLinkAtThePluginsDataFolderIsDeleted() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val link = File(pluginsDir, "a").toPath() + Files.createSymbolicLink(link, File(RuntimeEnvironment.getApplication().filesDir.parentFile, "gone").toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("the dangling link survived").that(Files.exists(link, LinkOption.NOFOLLOW_LINKS)).isFalse() + } + + @Test + fun aLinkAtThePluginsIconsFolderIsDeletedNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim() + val link = iconsDir("a").toPath() + Files.createSymbolicLink(link, victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertWithMessage("the link is still there").that(Files.exists(link, LinkOption.NOFOLLOW_LINKS)).isFalse() + } + + @Test + fun aDanglingLinkAtThePluginsIconsFolderIsDeleted() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val link = iconsDir("a").toPath() + Files.createSymbolicLink(link, File(RuntimeEnvironment.getApplication().filesDir.parentFile, "gone").toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("the dangling link survived").that(Files.exists(link, LinkOption.NOFOLLOW_LINKS)).isFalse() + } + + @Test + fun aLinkInThePluginsDataIsNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim() + File(pluginsDir, "a").mkdirs() + Files.createSymbolicLink(File(pluginsDir, "a/link").toPath(), victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" emptied a link's target").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(File(pluginsDir, "a").exists()).isFalse() + } + + @Test + fun aLinkInThePluginsIconsIsNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim() + val icons = File(RuntimeEnvironment.getApplication().getDir("plugin_icons", Context.MODE_PRIVATE), "a").apply { mkdirs() } + Files.createSymbolicLink(File(icons, "link").toPath(), victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" emptied a link's target").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(icons.exists()).isFalse() + } + + @Test + fun aLinkInTheCompiledCodeFolderIsNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim().also { File(it, "a-0123456789012345.odex").writeText("not a plugin's") } + oatDir.mkdirs() + Files.createSymbolicLink(File(oatDir, "link").toPath(), victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted a file through a link") + .that(victim.list()?.toList()) + .containsExactly("keep.txt", "a-0123456789012345.odex") + } + + private fun iconsDir(id: String): File = File(RuntimeEnvironment.getApplication().getDir("plugin_icons", Context.MODE_PRIVATE), id) + + // Outside the folders an uninstall deletes, with a file that must survive. + private fun victim(): File = + File(RuntimeEnvironment.getApplication().filesDir.parentFile, "victim").apply { mkdirs() }.also { + File(it, "keep.txt").writeText("not a plugin's") + } + + private fun file( + dir: File, + path: String, + ): File = File(dir, path).apply { parentFile!!.mkdirs() }.apply { writeText(path) } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt new file mode 100644 index 0000000000..b7f7f88b5e --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt @@ -0,0 +1,98 @@ +package com.itsaky.androidide.plugins.manager.core + +import android.content.pm.ApplicationInfo +import android.content.pm.PackageInfo +import android.os.Bundle +import com.itsaky.androidide.plugins.IPlugin +import com.itsaky.androidide.plugins.PluginContext +import com.itsaky.androidide.plugins.PluginLogger +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** The plugin manager of the test's application. */ +internal fun pluginManager() = PluginManager.getInstance(RuntimeEnvironment.getApplication(), Any(), NoLogger) + +/** Forgets the singleton plugin manager: each test has an application of its own. */ +internal fun forgetPluginManager() { + PluginManager::class.java + .getDeclaredField("INSTANCE") + .apply { isAccessible = true } + .set(null, null) +} + +/** Writes [file] as a plugin whose AndroidManifest states [id] and [mainClass]: Robolectric can't read one from an APK. */ +internal fun fakePlugin( + file: File, + id: String, + mainClass: String = "org.example.plugin.Main", +): File { + file.parentFile?.mkdirs() + ZipOutputStream(file.outputStream()).use { it.putNextEntry(ZipEntry("classes.dex")) } + val info = + PackageInfo().apply { + packageName = "org.example.plugin" + applicationInfo = + ApplicationInfo().apply { + metaData = + Bundle().apply { + putString("plugin.id", id) + putString("plugin.main_class", mainClass) + // Loading deletes and recreates the plugin's icon folder, named after its ID. + putString("plugin.icon_day", "icon.png") + } + } + } + shadowOf(RuntimeEnvironment.getApplication().packageManager).setPackageArchiveInfo(file.absolutePath, info) + return file +} + +/** A plugin that keeps its context. Name it as a fakePlugin's main class: the class loader finds it through its parent. */ +class NoopPlugin : IPlugin { + lateinit var context: PluginContext + + override fun initialize(context: PluginContext): Boolean { + this.context = context + return true + } + + override fun activate() = true + + override fun deactivate() = true + + override fun dispose() = Unit +} + +internal object NoLogger : PluginLogger { + override val pluginId = "test" + + override fun debug(message: String) = Unit + + override fun debug( + message: String, + error: Throwable, + ) = Unit + + override fun info(message: String) = Unit + + override fun info( + message: String, + error: Throwable, + ) = Unit + + override fun warn(message: String) = Unit + + override fun warn( + message: String, + error: Throwable, + ) = Unit + + override fun error(message: String) = Unit + + override fun error( + message: String, + error: Throwable, + ) = Unit +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/project/IsTemplateOfTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/project/IsTemplateOfTest.kt new file mode 100644 index 0000000000..f8075f9575 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/project/IsTemplateOfTest.kt @@ -0,0 +1,42 @@ +package com.itsaky.androidide.plugins.manager.project + +import com.google.common.truth.Truth.assertThat +import org.junit.Test + +class IsTemplateOfTest { + @Test + fun aTemplateOfThePluginMatches() { + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my_starter.cgt", emptyList())).isTrue() + } + + @Test + fun aTemplateOfAnotherLoadedPluginWhoseIdExtendsThisOneIsNotMatched() { + val others = listOf("com.example.my_tools") + + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my_tools_starter.cgt", others)).isFalse() + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my_starter.cgt", others)).isTrue() + } + + @Test + fun anExtendingIdThatIsNotLoadedDoesNotProtectItsName() { + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my_tools_starter.cgt", emptyList())).isTrue() + } + + @Test + fun anotherPluginsNameWithoutTheUnderscoreIsNotMatched() { + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my2_starter.cgt", listOf("com.example.my2"))).isFalse() + } + + @Test + fun theOwnTemplateThatLooksLikeAnotherPluginsIsKept() { + assertThat(isTemplateOf("x", "plugin_x_tools_a.cgt", listOf("x_tools"))).isFalse() + } + + @Test + fun otherFilesAreNotMatched() { + assertThat(isTemplateOf("a", "plugin_a_starter.zip", emptyList())).isFalse() + assertThat(isTemplateOf("a", "plugin_a_starter", emptyList())).isFalse() + assertThat(isTemplateOf("a", "plugin_b_starter.cgt", emptyList())).isFalse() + assertThat(isTemplateOf("a", "core.cgt", emptyList())).isFalse() + } +}