Skip to content

Commit

Permalink
[Android] Re-enable System.Security.Cryptography tests (#67057)
Browse files Browse the repository at this point in the history
The crypto test suites were originally disabled as they would run out of memory on both emulators and devices somewhat frequently.

This change enables them again, with the difference being that xunit will run tests on a single thread.

Addresses #62547
  • Loading branch information
steveisok authored Mar 24, 2022
1 parent 8c7120e commit f1dbe2e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 25 deletions.
14 changes: 14 additions & 0 deletions eng/testing/tests.mobile.targets
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@

<MainLibraryFileName Condition="'$(MainLibraryFileName)' == ''">AndroidTestRunner.dll</MainLibraryFileName>
</PropertyGroup>

<ItemGroup>
<_AndroidEnv Condition="'$(XUnitSingleThreadedMode)' == 'true'" Include="XUNIT_SINGLE_THREADED">
<Value>1</Value>
</_AndroidEnv>
<_AndroidEnv Condition="'$(XUnitUseRandomizedTestOrderer)' == 'true'" Include="XUNIT_RANDOM_ORDER_SEED">
<Value>1883302047</Value>
</_AndroidEnv>
<_AndroidEnv Condition="'$(XUnitSingleThreadedMode)' == 'true'" Include="XUNIT_VERBOSE">
<Value>1</Value>
</_AndroidEnv>
</ItemGroup>

<ItemGroup Condition="'$(RunAOTCompilation)' == 'true'">
<AotInputAssemblies Include="$(PublishDir)\*.dll">
<AotArguments>@(MonoAOTCompilerDefaultAotArguments, ';')</AotArguments>
Expand Down Expand Up @@ -140,6 +153,7 @@
MonoRuntimeHeaders="$(MicrosoftNetCoreAppRuntimePackNativeDir)include\mono-2.0"
Assemblies="@(BundleAssemblies)"
MainLibraryFileName="$(MainLibraryFileName)"
EnvironmentVariables="@(_AndroidEnv)"
ForceAOT="$(RunAOTCompilation)"
ForceInterpreter="$(MonoForceInterpreter)"
StripDebugSymbols="False"
Expand Down
18 changes: 8 additions & 10 deletions src/libraries/Common/tests/AndroidTestRunner/AndroidTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public static async Task<int> Main(string[] args)
int exitCode = 0;
s_MainTestName = Path.GetFileNameWithoutExtension(s_testLibs[0]);
string? verbose = Environment.GetEnvironmentVariable("XUNIT_VERBOSE")?.ToLower();
var simpleTestRunner = new SimpleAndroidTestRunner(verbose == "true" || verbose == "1");
bool enableMaxThreads = (Environment.GetEnvironmentVariable("XUNIT_SINGLE_THREADED") != "1");
var simpleTestRunner = new SimpleAndroidTestRunner(verbose == "true" || verbose == "1", enableMaxThreads);
simpleTestRunner.TestsCompleted += (e, result) =>
{
if (result.FailedTests > 0)
Expand All @@ -42,17 +43,14 @@ public static async Task<int> Main(string[] args)
return exitCode;
}

public SimpleAndroidTestRunner(bool verbose)
public SimpleAndroidTestRunner(bool verbose, bool enableMaxThreads)
{
if (verbose)
{
MinimumLogLevel = MinimumLogLevel.Verbose;
_maxParallelThreads = 1;
}
else
MinimumLogLevel = (verbose) ? MinimumLogLevel.Verbose : MinimumLogLevel.Info;
_maxParallelThreads = (enableMaxThreads) ? Environment.ProcessorCount : 1;

if (!enableMaxThreads)
{
MinimumLogLevel = MinimumLogLevel.Info;
_maxParallelThreads = Environment.ProcessorCount;
Console.WriteLine("XUNIT: SINGLE THREADED MODE ENABLED");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
<PropertyGroup>
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
<DefineConstants Condition="'$(TargetPlatformIdentifier)' == 'Unix' or '$(TargetPlatformIdentifier)' == 'Android' or '$(TargetPlatformIdentifier)' == 'OSX' or '$(TargetPlatformIdentifier)' == 'iOS' or '$(TargetPlatformIdentifier)' == 'tvOS'">$(DefineConstants);Unix</DefineConstants>
<UseAndroidCrypto Condition="'$(TargetPlatformIdentifier)' == 'Android'">true</UseAndroidCrypto>
<UseAppleCrypto Condition="'$(TargetPlatformIdentifier)' == 'OSX' or '$(TargetPlatformIdentifier)' == 'iOS' or '$(TargetPlatformIdentifier)' == 'tvOS'">true</UseAppleCrypto>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetPlatformIdentifier)' == 'Android'">
<UseAndroidCrypto>true</UseAndroidCrypto>
<XUnitSingleThreadedMode>true</XUnitSingleThreadedMode>
<XUnitUseRandomizedTestOrderer>true</XUnitUseRandomizedTestOrderer>
</PropertyGroup>
<Import Project="$(CommonPath)System\Security\Cryptography\Asn1Reader\System.Security.Cryptography.Asn1Reader.Shared.projitems" />
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
<PropertyGroup>
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
<UseAndroidCrypto Condition="'$(TargetPlatformIdentifier)' == 'Android'">true</UseAndroidCrypto>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetPlatformIdentifier)' == 'Android'">
<UseAndroidCrypto>true</UseAndroidCrypto>
<XUnitSingleThreadedMode>true</XUnitSingleThreadedMode>
<XUnitUseRandomizedTestOrderer>true</XUnitUseRandomizedTestOrderer>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs"
Expand Down
12 changes: 0 additions & 12 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,11 @@
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Private.Xml/tests/XPath/XPathDocument/System.Xml.XPath.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Private.Uri/tests/ExtendedFunctionalTests/System.Private.Uri.ExtendedFunctional.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Reflection.DispatchProxy/tests/System.Reflection.DispatchProxy.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Security.Cryptography.X509Certificates\tests\System.Security.Cryptography.X509Certificates.Tests.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetOS)' == 'Android' and '$(TargetArchitecture)' == 'x64' and '$(RunDisabledAndroidTests)' != 'true'">
<!-- Test flakiness on x64 https://github.com/dotnet/runtime/issues/49937 -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Threading\tests\System.Threading.Tests.csproj" />

<!-- Out of memory https://github.com/dotnet/runtime/issues/66831 -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Security.Cryptography\tests\System.Security.Cryptography.Tests.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetOS)' == 'Android' and '$(TargetArchitecture)' == 'x86' and '$(RunDisabledAndroidTests)' != 'true'">
Expand All @@ -191,14 +187,6 @@

<!-- https://github.com/dotnet/runtime/issues/50493 -->
<ProjectExclusions Include="$(RepoRoot)\src\tests\FunctionalTests\Android\Device_Emulator\AOT\Android.Device_Emulator.Aot.Test.csproj" />

<!-- Out of memory https://github.com/dotnet/runtime/issues/66831 -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Security.Cryptography\tests\System.Security.Cryptography.Tests.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetOS)' == 'Android' and '$(TargetArchitecture)' == 'arm' and '$(RunDisabledAndroidTests)' != 'true'">
<!-- Out of memory https://github.com/dotnet/runtime/issues/66831 -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Security.Cryptography\tests\System.Security.Cryptography.Tests.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetOS)' == 'iOS' and '$(RunDisablediOSTests)' != 'true'">
Expand Down
6 changes: 6 additions & 0 deletions src/tasks/AndroidAppBuilder/AndroidAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class AndroidAppBuilderTask : Task
/// </summary>
public ITaskItem[] Assemblies { get; set; } = Array.Empty<ITaskItem>();

/// <summary>
/// The set of environment variables to provide to the native embedded application
/// </summary>
public ITaskItem[] EnvironmentVariables { get; set; } = Array.Empty<ITaskItem>();

/// <summary>
/// Prefer FullAOT mode for Emulator over JIT
/// </summary>
Expand Down Expand Up @@ -103,6 +108,7 @@ public override bool Execute()
apkBuilder.KeyStorePath = KeyStorePath;
apkBuilder.ForceInterpreter = ForceInterpreter;
apkBuilder.ForceAOT = ForceAOT;
apkBuilder.EnvironmentVariables = EnvironmentVariables;
apkBuilder.StaticLinkedRuntime = StaticLinkedRuntime;
apkBuilder.RuntimeComponents = RuntimeComponents;
apkBuilder.DiagnosticPorts = DiagnosticPorts;
Expand Down
12 changes: 11 additions & 1 deletion src/tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ApkBuilder
public string? KeyStorePath { get; set; }
public bool ForceInterpreter { get; set; }
public bool ForceAOT { get; set; }
public ITaskItem[] EnvironmentVariables { get; set; } = Array.Empty<ITaskItem>();
public bool InvariantGlobalization { get; set; }
public bool EnableRuntimeLogging { get; set; }
public bool StaticLinkedRuntime { get; set; }
Expand Down Expand Up @@ -371,8 +372,17 @@ public ApkBuilder(TaskLoggingHelper logger)
if (!string.IsNullOrEmpty(NativeMainSource))
File.Copy(NativeMainSource, javaActivityPath, true);

string envVariables = "";
foreach (ITaskItem item in EnvironmentVariables)
{
string name = item.ItemSpec;
string value = item.GetMetadata("Value");
envVariables += $"\t\tsetEnv(\"{name}\", \"{value}\");\n";
}

string monoRunner = Utils.GetEmbeddedResource("MonoRunner.java")
.Replace("%EntryPointLibName%", Path.GetFileName(mainLibraryFileName));
.Replace("%EntryPointLibName%", Path.GetFileName(mainLibraryFileName))
.Replace("%EnvVariables%", envVariables);

File.WriteAllText(monoRunnerPath, monoRunner);

Expand Down
2 changes: 2 additions & 0 deletions src/tasks/AndroidAppBuilder/Templates/MonoRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public void onCreate(Bundle arguments) {
argsToForward = argsList.toArray(new String[argsList.size()]);
}

%EnvVariables%

super.onCreate(arguments);
start();
}
Expand Down

0 comments on commit f1dbe2e

Please sign in to comment.