More strict detection for IsInstalledApp / CurrentlyInstalledVersion

This commit is contained in:
Caelan Sayler
2022-04-07 21:42:18 +01:00
parent dcbdbdb0cf
commit fc0772816a
2 changed files with 37 additions and 21 deletions

View File

@@ -27,7 +27,7 @@ namespace Squirrel
public string AppDirectory => Path.Combine(_localAppDataDirectoryOverride ?? GetLocalAppDataDirectory(), AppId);
/// <summary>True if the current executable is inside the target <see cref="AppDirectory"/>.</summary>
public bool IsInstalledApp => isUpdateExeAvailable() ? Utility.IsFileInDirectory(SquirrelRuntimeInfo.EntryExePath, AppDirectory) : false;
public bool IsInstalledApp => CurrentlyInstalledVersion() != null;
/// <summary>The directory packages and temp files are stored in.</summary>
protected string PackagesDirectory => Utility.PackageDirectoryForAppDir(AppDirectory);
@@ -184,21 +184,34 @@ namespace Squirrel
/// <inheritdoc/>
public SemanticVersion CurrentlyInstalledVersion(string executable = null)
{
string appDir;
try {
executable = executable ?? SquirrelRuntimeInfo.EntryExePath;
if (!Utility.IsFileInDirectory(executable, AppDirectory))
return null;
var appDirName = executable.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
.FirstOrDefault(x => x.StartsWith("app-", StringComparison.OrdinalIgnoreCase));
if (appDirName == null) return null;
return new SemanticVersion(appDirName.Substring(4));
appDir = AppDirectory;
} catch (InvalidOperationException) {
// app is not installed, see getUpdateExe()
return null;
}
executable = Path.GetFullPath(executable ?? SquirrelRuntimeInfo.EntryExePath);
// check if the application to check is in the correct application directory
if (!Utility.IsFileInDirectory(executable, appDir))
return null;
// check if Update.exe exists in the expected relative location
var baseDir = Path.GetDirectoryName(executable);
if (!File.Exists(Path.Combine(baseDir, "..\\Update.exe")))
return null;
var exePathWithoutAppDir = executable.Substring(appDir.Length);
var appDirName = exePathWithoutAppDir.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
.FirstOrDefault(x => x.StartsWith("app-", StringComparison.OrdinalIgnoreCase));
// check if we are inside an 'app-{ver}' directory and extract version
if (appDirName == null)
return null;
return new SemanticVersion(appDirName.Substring(4));
}
/// <inheritdoc/>

View File

@@ -314,17 +314,20 @@ namespace Squirrel.Tests
}
}
[Theory]
[InlineData("C:\\Foo\\Bar\\Test.exe", default(string))]
[InlineData("%LocalAppData%\\theApp\\app-1.0.0.1\\Test.exe", "1.0.0.1")]
[InlineData("%LocalAppData%\\aDifferentApp\\app-1.0.0.1\\Test.exe", default(string))]
public void CurrentlyInstalledVersionTests(string input, string expectedVersion)
[Fact]
public void IsInstalledHandlesInvalidDirectoryStructure()
{
input = Environment.ExpandEnvironmentVariables(input);
var expected = expectedVersion != null ? new SemanticVersion(expectedVersion) : default(SemanticVersion);
using (var fixture = new UpdateManager("http://lol", "theApp")) {
Assert.Equal(expected, fixture.CurrentlyInstalledVersion(input));
using (Utility.WithTempDirectory(out var tempDir)) {
Directory.CreateDirectory(Path.Combine(tempDir, "theApp"));
Directory.CreateDirectory(Path.Combine(tempDir, "theApp", "app-1.0.1"));
Directory.CreateDirectory(Path.Combine(tempDir, "theApp", "wrongDir"));
File.WriteAllText(Path.Combine(tempDir, "theApp", "Update.exe"), "1");
using (var fixture = new UpdateManager("http://lol", "theApp", tempDir)) {
Assert.Null(fixture.CurrentlyInstalledVersion(Path.Combine(tempDir, "app.exe")));
Assert.Null(fixture.CurrentlyInstalledVersion(Path.Combine(tempDir, "theApp", "app.exe")));
Assert.Null(fixture.CurrentlyInstalledVersion(Path.Combine(tempDir, "theApp", "wrongDir", "app.exe")));
Assert.Equal(new SemanticVersion(1, 0, 1, 9), fixture.CurrentlyInstalledVersion(Path.Combine(tempDir, "theApp", "app-1.0.1.9", "app.exe")));
}
}
}