Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

SecurityException thrown when NCrunch tries to runs a ASP.NET Core 2.0 integration test
distilled
#1 Posted : Friday, May 18, 2018 7:39:26 PM(UTC)
Rank: Member

Groups: Registered
Joined: 8/3/2012(UTC)
Posts: 22

Thanks: 2 times
Was thanked: 6 time(s) in 6 post(s)
I have an Xunit test that uses the classes from the Microsoft.AspNetCore.TestHost package to integration test an ASP.NET Core 2.0 project. The project uses a custom implementation of Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository that uses Dapper to retrieve data protection keys from a SQL Server database. Both the ASP.NET Core 2.0 project and its test project target .NET 4.6.1.

If NCrunch tries to run it automatically when it is impacted by my changes it fails with the following message: "NCrunch was unable to retrieve a meaningful result from this test due to an unexpected error - was the execution process terminated?".

If I then right-click on the test and select "Run selected test in a new task runner process", the test passes.

If I right-click on the test and select "Debug selected test in a new task runner process", a SecurityException is thrown within seconds of Visual Studio attaching to the process. Intellitrace shows ADO.NET is attempting to query SQL Server as part of our custom IXmlRepository implementation. These are the exception details as copied to my clipboard by the unhandled exception modal:

Code:

System.Security.SecurityException
  HResult=0x8013150A
  Message=Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
  Source=mscorlib
  StackTrace:
   at System.StubHelpers.StubHelpers.DemandPermission(IntPtr pNMD)
   at nCrunch.TestExecution.FileSystemAccessMonitor.(String , Int32 , WIN32_FILE_ATTRIBUTE_DATA& )
   at nCrunch.TestExecution.FileSystemAccessMonitor.(String , Int32 , WIN32_FILE_ATTRIBUTE_DATA& )
   at Microsoft.Win32.Win32Native.GetFileAttributesEx(String name, Int32 fileInfoLevel, WIN32_FILE_ATTRIBUTE_DATA& lpFileInformation)


And this is what is written to Visual Studio's Output window:

Code:

System.Transactions Critical: 0 : <TraceRecord xmlns="(- BROKEN LINK -)" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>nCrunch.TestRunner.AppDomain_574b0f40679448ad9b02ded459716602</AppDomain><Exception><ExceptionType>System.Security.SecurityException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.</Message><StackTrace>   at System.StubHelpers.StubHelpers.DemandPermission(IntPtr pNMD)
   at nCrunch.TestExecution.FileSystemAccessMonitor.&#x3;(String &#x2;, Int32 &#x3;, WIN32_FILE_ATTRIBUTE_DATA&amp;amp; &#x5;)
   at nCrunch.TestExecution.FileSystemAccessMonitor.&#x2;(String &#x2;, Int32 &#x3;, WIN32_FILE_ATTRIBUTE_DATA&amp;amp; &#x5;)
   at Microsoft.Win32.Win32Native.GetFileAttributesEx(String name, Int32 fileInfoLevel, WIN32_FILE_ATTRIBUTE_DATA&amp;amp; lpFileInformation)</StackTrace><ExceptionString>System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.StubHelpers.StubHelpers.DemandPermission(IntPtr pNMD)
   at nCrunch.TestExecution.FileSystemAccessMonitor.&#x3;(String &#x2;, Int32 &#x3;, WIN32_FILE_ATTRIBUTE_DATA&amp;amp; &#x5;)
   at nCrunch.TestExecution.FileSystemAccessMonitor.&#x2;(String &#x2;, Int32 &#x3;, WIN32_FILE_ATTRIBUTE_DATA&amp;amp; &#x5;)
   at Microsoft.Win32.Win32Native.GetFileAttributesEx(String name, Int32 fileInfoLevel, WIN32_FILE_ATTRIBUTE_DATA&amp;amp; lpFileInformation)
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.SecurityPermission</ExceptionString></Exception></TraceRecord>
An unhandled exception of type 'System.Security.SecurityException' occurred in nCrunch.TestExecution.dll
Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Remco
#2 Posted : Friday, May 18, 2018 11:58:43 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,975

Thanks: 929 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi, thanks for sharing this issue.

Something in your environment is clashing with NCrunch's file access monitoring.

You can turn off this feature by setting the Track file dependencies setting to False. This will mean a slight loss in impact detection accuracy around resource files, which in most cases probably won't be noticeable.
1 user thanked Remco for this useful post.
mdenny on 9/30/2019(UTC)
distilled
#3 Posted : Monday, May 21, 2018 3:04:19 PM(UTC)
Rank: Member

Groups: Registered
Joined: 8/3/2012(UTC)
Posts: 22

Thanks: 2 times
Was thanked: 6 time(s) in 6 post(s)
Thanks! However, I still receive the message "NCrunch was unable to retrieve a meaningful result from this test due to an unexpected error - was the execution process terminated?" when NCrunch tries to automatically run the test. The test still passes when run manually, and I was able to debug it this time.
Remco
#4 Posted : Monday, May 21, 2018 9:13:00 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,975

Thanks: 929 times
Was thanked: 1257 time(s) in 1170 post(s)
This message is a blanket error that appears when the task runner process suddenly terminates without providing any information to the engine. There are several things that can often cause this:

- OutOfMemoryExceptions
- StackOverflowExceptions (usually in referenced code, as your own code is somewhat protected by NCrunch's instrumentation)
- Unmanaged exceptions
- Interference from externals (such as virus scanners)

It's strange that you're seeing this when the test is run automatically. It makes me think that the problem may be dependent on the execution sequence of your tests. If you run the test by itself without a debugger attached, does the problem appear?

Something else that springs to mind (based on your previous error) is the CLR's Code Access Security or Partially Trusted Code. NCrunch doesn't support the use of either of these features inside its test environment. If you're using these, they may be the cause of this problem and probably the previous one too.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.046 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download