Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlatformNotSupportedException: 'CspParameters' issues when running tests on Linux #6

Closed
SidShetye opened this issue Apr 3, 2018 · 10 comments
Labels

Comments

@SidShetye
Copy link

Failure: Basic CLI commands like dotnet test or dotnet build don't seem to work.

Expectation:

  • dotnet build should build the project(s) based on the platform it's invoked on and folder it's invoked in
  • dotnet test should run the test project (nunit 3.x tests should be automatically discovered and executed via Microsoft.NET.Test.Sdk - this seems to be already in place).

Related cleanup suggestions

  • two obsolete formatted csproj files (crypto\crypto.csproj and crypto-test\crypto-test.csproj) that throw off the modern dotnet CLI
  • also appears to be a crypto\test\crypto.test.csproj and a crypto-test\crypto.test.csproj project adding to confusion.

Log of dotnet build failing

PS D:\projects\github\bc-csharp> dotnet build
Microsoft (R) Build Engine version 15.6.82.30579 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 56.18 ms for D:\projects\github\bc-csharp\crypto\src\crypto.csproj.
  Restore completed in 60.27 ms for D:\projects\github\bc-csharp\crypto\test\crypto.test.csproj.
C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\buildMultiTargeting\GitVersionTask.targets(43,5): error MSB4062: The "GitVersionTask.WriteVersionInfoToBuildLog" task could not be loaded from the assembly C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\buildMultiTargeting\..\build\GitVersionTask.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL'. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [D:\projects\github\bc-csharp\crypto\src\crypto.csproj]
C:\Program Files\dotnet\sdk\2.1.103\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets(41,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\2.1.103\15.0\Bin\Microsoft.CSharp.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. [D:\projects\github\bc-csharp\crypto\src\crypto.csproj]
C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\build\GitVersionTask.targets(45,5): error MSB4062: The "GitVersionTask.WriteVersionInfoToBuildLog" task could not be loaded from the assembly C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\build\GitVersionTask.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL'. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [D:\projects\github\bc-csharp\crypto\src\crypto.csproj]
C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\build\GitVersionTask.targets(45,5): error MSB4062: The "GitVersionTask.WriteVersionInfoToBuildLog" task could not be loaded from the assembly C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\build\GitVersionTask.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL'. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [D:\projects\github\bc-csharp\crypto\src\crypto.csproj]

Build FAILED.

C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\buildMultiTargeting\GitVersionTask.targets(43,5): error MSB4062: The "GitVersionTask.WriteVersionInfoToBuildLog" task could not be loaded from the assembly C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\buildMultiTargeting\..\build\GitVersionTask.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL'. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [D:\projects\github\bc-csharp\crypto\src\crypto.csproj]
C:\Program Files\dotnet\sdk\2.1.103\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets(41,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\2.1.103\15.0\Bin\Microsoft.CSharp.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. [D:\projects\github\bc-csharp\crypto\src\crypto.csproj]
C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\build\GitVersionTask.targets(45,5): error MSB4062: The "GitVersionTask.WriteVersionInfoToBuildLog" task could not be loaded from the assembly C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\build\GitVersionTask.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL'. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [D:\projects\github\bc-csharp\crypto\src\crypto.csproj]
C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\build\GitVersionTask.targets(45,5): error MSB4062: The "GitVersionTask.WriteVersionInfoToBuildLog" task could not be loaded from the assembly C:\Users\user1\.nuget\packages\gitversiontask\4.0.0-beta0011\build\GitVersionTask.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL'. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [D:\projects\github\bc-csharp\crypto\src\crypto.csproj]
    0 Warning(s)
    4 Error(s)

Time Elapsed 00:00:03.34
PS D:\projects\github\bc-csharp>
@clairernovotny
Copy link
Collaborator

Hi,

For today, dotnet cannot be used to build this project. There are a few blockers as you've seen. In addition, we still support the legacy PCL profile to ensure this is 100% drop-in compatible with the non-portable version's targets:
https://github.com/onovotny/bc-csharp/blob/netstandard/crypto/src/crypto.csproj#L3

For now, you need to use msbuild to build with msbuild /restore /t:build …

As to the legacy projects, I want to keep them for now until this PR is fully merged since it minimizes conflicts when updating from the source branch.

@SidShetye
Copy link
Author

SidShetye commented Apr 3, 2018

Thanks for the background, can appreciate those pain points.

Till then, how do we test this on linux? dotnet test is the typical go-to ...

@clairernovotny
Copy link
Collaborator

For now, the build and test needs to be on Windows, but the resulting library will work anywhere.

@SidShetye
Copy link
Author

SidShetye commented Apr 4, 2018

I needed to verify that assumption. Turns out there indeed are test issues on *nix but thankfully nothing too bad. I got dotnet build and dotnet test working by getting rid of legacy targets (they can just use older builds) in my repo clone. Changes are actually simple if you can look past the "diff" on deleted file. (diff here)

Here is the console log - since the tests have parameterized input, the exact failures aren't clear without diving a bit deeper

On Windows 784 of 788 test pass, on macOS 783 (1 failure) of 788. No idea about the 4 in between (ignored?)

$ dotnet test
Build started, please wait...
Build completed.

Test run for /Volumes/projects/github/bc-csharp/crypto/test/bin/Debug/netcoreapp2.0/crypto.test.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.5.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
NUnit Adapter 3.8.0.0: Test execution started
Running all tests in /Volumes/projects/github/bc-csharp/crypto/test/bin/Debug/netcoreapp2.0/crypto.test.dll
NUnit3TestExecutor converted 788 of 788 NUnit test cases
Not Run  TestFunction
Error Message:
 OneTimeSetUp: 

Not Run  TestSumOfMultipliesComplete
Not Run  TestSumOfTwoMultipliesComplete
Not Run  TestMultiply
Error Message:
 OneTimeSetUp: 

Failed   TestRsaInterop
Error Message:
 System.Security.Cryptography.CryptographicException : Specified key is not a valid size for this algorithm.
Stack Trace:
   at System.Security.Cryptography.AsymmetricAlgorithm.set_KeySize(Int32 value)
   at System.Security.Cryptography.RSAImplementation.RSASecurityTransforms.set_KeySize(Int32 value)
   at System.Security.Cryptography.RSA.Create(Int32 keySizeInBits)
   at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize)
   at Org.BouncyCastle.Security.Tests.TestDotNetUtilities.TestRsaInterop() in /Volumes/projects/github/bc-csharp/crypto/test/src/security/test/TestDotNetUtil.cs:line 27
NUnit Adapter 3.8.0.0: Test execution complete

Total tests: 788. Passed: 783. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 6.2869 Minutes

$ 

@SidShetye
Copy link
Author

FYI, tried this on Ubuntu 16.04 too. Test failure as follows - assumptions about CAPI being broken x-platform. You can try using Ubuntu on Windows 10 (via WSL)

Starting test execution, please wait...
NUnit Adapter 3.8.0.0: Test execution started
Running all tests in /mnt/d/projects/github/bc-csharp/crypto/test/bin/Debug/netcoreapp2.0/crypto.test.dll
NUnit3TestExecutor converted 788 of 788 NUnit test cases
Not Run  TestFunction
Error Message:
 OneTimeSetUp:

Not Run  TestSumOfMultipliesComplete
Not Run  TestSumOfTwoMultipliesComplete
Not Run  TestMultiply
Error Message:
 OneTimeSetUp:

Failed   TestRsaInterop
Error Message:
 System.PlatformNotSupportedException : 'CspParameters' requires Windows Cryptographic API (CAPI), which is not available on this platform.
Stack Trace:
   at System.Security.Cryptography.RSACryptoServiceProvider..ctor(CspParameters parameters)
   at Org.BouncyCastle.Security.DotNetUtilities.CreateRSAProvider(RSAParameters rp) in /mnt/d/projects/github/bc-csharp/crypto/src/security/DotNetUtilities.cs:line 230
   at Org.BouncyCastle.Security.DotNetUtilities.ToRSA(RsaKeyParameters rsaKey) in /mnt/d/projects/github/bc-csharp/crypto/src/security/DotNetUtilities.cs:line 158
   at Org.BouncyCastle.Security.Tests.TestDotNetUtilities.TestRsaInterop() in /mnt/d/projects/github/bc-csharp/crypto/test/src/security/test/TestDotNetUtil.cs:line 31
NUnit Adapter 3.8.0.0: Test execution complete

Total tests: 788. Passed: 783. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 7.1389 Minutes

@clairernovotny clairernovotny changed the title dotnet CLI doesn't work PlatformNotSupportedException: 'CspParameters' issues when running tests on Linux Aug 31, 2018
@clairernovotny
Copy link
Collaborator

clairernovotny commented Aug 31, 2018

Haven't had a chance to look at the interop issues, but dotnet build can build this directly now.

@jeremyVignelles
Copy link

Hi,
Just happened to me when calling DotNetUtilities.ToRSA(keyParameters) on a Linux on .net core 3.1 .

Do you know of any workaround I can use to create a RSA instance from my RSAParameters instance on linux?

@jeremyVignelles
Copy link

I found a workaround:

Instead of calling

var rsa = DotNetUtilities.ToRSA(keyParameters);

I'm using

var rsa = RSA.Create(DotNetUtilities.ToRSAParameters(keyParameters));

and it works.

I'd like to replace this method:

private static RSA CreateRSAProvider(RSAParameters rp)
{
CspParameters csp = new CspParameters();
csp.KeyContainerName = string.Format("BouncyCastle-{0}", Guid.NewGuid());
RSACryptoServiceProvider rsaCsp = new RSACryptoServiceProvider(csp);
rsaCsp.ImportParameters(rp);
return rsaCsp;
}

with

private static RSA CreateRSAProvider(RSAParameters rp) => RSA.Create(rp);

However, I don't know the implications of doing so and if that can break something else on some platform.

@clairernovotny
Copy link
Collaborator

This repo is mirror of the main repo with the only changes being to enable the portable build. For a functional change like you're proposing, I recommend creating an issue here: https://github.com/bcgit/bc-csharp/issues

@clairernovotny
Copy link
Collaborator

Closing due to age.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants