Skip to content

Commit

Permalink
test: fix AssemblyReader tests on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Nov 25, 2022
1 parent ea49ff5 commit a664893
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions test/Sentry.Tests/Internals/AndroidAssemblyReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER
#if NET6_0_OR_GREATER && !__IOS__
using System.Reflection;
using Sentry.Testing;

Expand All @@ -15,25 +15,36 @@ public AndroidAssemblyReaderTests(ITestOutputHelper output)
_logger = new(output);
}

private IAndroidAssemblyReader GetSut(bool isAssemblyStore, bool compressed, List<string> supportedAbis)
private IAndroidAssemblyReader GetSut(bool isAssemblyStore, bool isCompressed)
{
#if ANDROID
// On Android, this tests the current app APK.
var apkPath = Environment.CommandLine;
// AndroidBuild not available here - We can't use the usual code
// var supportedAbis = AndroidBuild.SupportedAbis ?? new List<string> { AndroidBuild.CpuAbi ?? "" };
var supportedAbis = new List<string> { "x86_64" };
#else
var apkPath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"../../../Internals/",
$"android-Store={isAssemblyStore}-Compressed={compressed}.apk");

$"android-Store={isAssemblyStore}-Compressed={isCompressed}.apk");
var supportedAbis = new List<string> { "x86_64" };
#endif
_output.WriteLine($"Checking if APK exists: {apkPath}");
File.Exists(apkPath).Should().BeTrue();

return AndroidAssemblyReaderFactory.Open(apkPath, supportedAbis, _logger);
}

[Theory]
[SkippableTheory]
[InlineData(false)]
[InlineData(true)]
public void CreatesCorrectReader(bool isAssemblyStore)
{
using var sut = GetSut(isAssemblyStore, compressed: true, supportedAbis: new());
#if ANDROID
Skip.If(true, "It's unknown whether the current Android app APK is an assembly store or not.");
#endif
using var sut = GetSut(isAssemblyStore, isCompressed: true);
if (isAssemblyStore)
{
Assert.IsType<AndroidAssemblyStoreReader>(sut);
Expand All @@ -44,16 +55,16 @@ public void CreatesCorrectReader(bool isAssemblyStore)
}
}

[Theory]
[SkippableTheory]
[InlineData(false)]
[InlineData(true)]
public void ReturnsNullIfAssemblyDoesntExist(bool isAssemblyStore)
{
using var sut = GetSut(isAssemblyStore, compressed: true, supportedAbis: new() { "x86_64" });
using var sut = GetSut(isAssemblyStore, isCompressed: true);
Assert.Null(sut.TryReadAssembly("NonExistent.dll"));
}

[Theory]
[SkippableTheory]
[InlineData(false, true, "Mono.Android.dll")]
[InlineData(false, false, "Mono.Android.dll")]
[InlineData(false, true, "System.Threading.dll")]
Expand All @@ -64,7 +75,12 @@ public void ReturnsNullIfAssemblyDoesntExist(bool isAssemblyStore)
[InlineData(true, false, "System.Threading.dll")]
public void ReadsAssembly(bool isAssemblyStore, bool isCompressed, string assemblyName)
{
using var sut = GetSut(isAssemblyStore, isCompressed, supportedAbis: new() { "x86_64" });
#if ANDROID
// No need to run all combinations - we only test the current APK which is (likely) compressed assembly store.
Skip.If(!isAssemblyStore);
Skip.If(!isCompressed);
#endif
using var sut = GetSut(isAssemblyStore, isCompressed);

var peReader = sut.TryReadAssembly(assemblyName);
Assert.NotNull(peReader);
Expand Down

0 comments on commit a664893

Please sign in to comment.