7

I have (only) Visual Studio 2010 installed. I'm trying to run my unit tests from a command-line build-system. All my development work is in my home directory on a Linux server that's shared out using Samba. When I try to run NUnit I get an error like this:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'NUnit.ConsoleRunner.Runner' threw an exception. ---> System.Security.Security
Exception: That assembly does not allow partially trusted callers.
   at NUnit.ConsoleRunner.Runner..cctor()
The action that failed was:
LinkDemand
The assembly or AppDomain that failed was:
nunit-console-runner, Version=2.5.9.10305, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77
The method that caused the failure was:
NUnit.Core.Logger GetLogger(System.Type)
The Zone of the assembly that failed was:
Intranet
The Url of the assembly that failed was:
file:///Z:/gitrepos/smarties/dependencies/Windows-x86/NUnit-2.5.9.10305/bin/net-2.0/lib/nunit-console-runner.DLL
   --- End of inner exception stack trace ---
   at NUnit.ConsoleRunner.Runner.Main(String[] args)
   at NUnit.ConsoleRunner.Class1.Main(String[] args)

I have tried using caspol.exe as described here: Edit and run .NET projects from network shares

>caspol -addgroup 1.2 -url file:///Z:/* FullTrust -name MyZShare
Microsoft (R) .NET Framework CasPol 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

WARNING: The .NET Framework does not apply CAS policy by default. Any settings shown or modified by CasPol will only affect applications that opt into using
CAS policy.

Please see http://go.microsoft.com/fwlink/?LinkId=131738 for more information.


The operation you are performing will alter security policy.
Are you sure you want to perform this operation? (yes/no)
yes
Added union code group with "-url" membership condition to the Machine level.
Success

It has no obvious effect.

Other instructions tell me to find some Microsoft .NET Security Policy tool in Administrative Tools in the Control Panel, but I have no such tool available. (Is this because I only have Visual Studio 2010, not earlier versions? I'm running Windows 7 Pro 64-bit, if it makes a difference.)

I really just want .NET to believe that this directory is on my own machine and not part of the intranet. I don't want any of this code to run from a network share when deployed, only as part of development. I also don't want to blindly trust all network shares, although even that would be a more palatable workaround than copying everything onto a local drive every time I want to run a test.

I would rather not have to sign everything with strong names at this point in development. (And I'm not even sure if it would help, since NUnit is apparently failing before it has even loaded my assembly.)


EDIT - In the end it turned out I was trying to solve the wrong problem. While figuring out how to get NUnit to run .NET 4.0 tests without a subprocess, I found this:

http://frater.wordpress.com/2010/05/04/debugging-nunit-tests-under-visual-studio-2010/

It says:

According to this page you should also add this line under the runtime element [in nunit-console.exe.config]:

<loadFromRemoteSources enabled="true" />

This allows libraries downloaded from a remote site (such as a website) to be loaded with full trust. I’m not sure why this would be necessary and indeed, my application debugged NUnit just fine without it. However, if you have problems, you might want to give it a try.

This fixed my problem for me without having to muck about with caspol.

1
  • I think what you actually want to do is enable the 'Local Intranet' zone in the Internet Settings->Security, and add the UNC path to that zone. You might also try adjusting the trust settings. I'm not 100% sure about this, hence I'm making it a comment rather than an answer. Nov 12, 2010 at 15:04

3 Answers 3

5

This is because in .NET 3.5 and below, all libraries located on network shares are treated as partial trust. It is possible to work around this by using a later version of the framework to run NUnit.

The NUnit runners target the v3.5 framework, presumably for legacy support. You can check this with the corflags tool that comes with Visual Studio

> corflags nunit-console.exe
Version   : v2.0.50727

This means if you run nunit-console.exe from the from command line and you have .NET 3.5 and .NET 4 installed, the v3.5 runtime will be used to run the executable. You can see this in the console output.

> nunit-console.exe
CLR Version: 2.0.50727.7905 ( Net 3.5 )

The config contains the <loadFromRemoteSources enabled="true"/> element, which causes .NET v4 to ignore the partially trusted caller error, but this setting is ignored in the older versions of the framework.

To force the application to run under .NET 4 or above, add the following line to the startup section of the application config file.

<supportedRuntime version="v4.0"/>

In my case, I had to add a step to the build server automation to make this config change before running the tests in a continuous integration environment.

desc 'run unit tests'
task :tests => :compile do
  runner_dir = FileList["packages/NUnit.Runners*/tools"].first
  runner_config = File.join(runner_dir, "nunit-console.exe.config")

  # running the tests from a network share fails due to code access policy if the 
  # runner is on framework v3.5 or lower. force the runner to use v4.0 or above 
  comment = %<!-- Comment out the next line to force use of .NET 4.0 -->
  runtime_version = %<supportedRuntime version="v4.0.30319"/>
  File.write(runner_config, File.read(runner_config).sub(comment, runtime_version))

  nunit! :actually_run_tests do |nunit|
    nunit.command = File.join(runner_dir, "nunit-console.exe")
    nunit.assemblies = FileList["out/**/*.Tests.dll"]
    nunit.log_level = :verbose
  end
end
1
  • You totally rock dude. After a couple of hours of looking at this problem your fix did it for me!! Thanks!!!
    – Denis
    Feb 14, 2014 at 21:35
2

I don't know know enough about security in .net 4.0 to give you a solution but I can provide some insight on the "missing" Microsoft .net security policy tool.

It has been removed in .net 4.0 thus any examples referring to it are prior to .net 4.0. Even if you installed .net framework 2.0 to gain the "SPT" it would only affect .net 2.0, 3.0, and 3.5 applications.

http://msdn.microsoft.com/en-us/library/2bc0cxhc.aspx

There have been extensive changes to security model in .Net 4.0 thus any documents prior to 4.0 shouldn't be trusted.

http://msdn.microsoft.com/en-us/library/dd233103.aspx

1

Are you sure that NUnit is running under .NET 4.0 instead of an earlier .NET Framework version? (You might have earlier framework versions installed even if you don't have earlier Visual Studio versions installed.) Also, what is the full command line (including all switch values) that you are using to launch the NUnit console application?

3
  • I believe NUnit is running under .NET 2.0. There are indeed lots of different versions of .NET installed with Visual Studio 2010. But I still seem to be missing the configuration tools for earlier versions. I run this command to invoke NUnit: dependencies\Windows-x86\NUnit-2.5.9.10305\bin\net-2.0\nunit-console-x86.exe -labels build\UpnpWidgetEmulator.Tests.dll This is invoked on a directory on Z:, the mapped network drive.
    – Weeble
    Nov 12, 2010 at 15:58
  • If it's running under 2.0, you'll need to change the 2.0 CAS policy to fix the problem. If you want a UI for 2.0 x86 CAS policy management, you can download the .NET 2.0 x86 SDK from microsoft.com/downloads/en/…. Nov 12, 2010 at 16:13
  • I think this answer and the comment are indeed what I asked for, so I'll mark them accepted, but I haven't actually tried out the 2.0 CAS tool since I didn't need it in the end. I've edited the question to explain how I solved my problem.
    – Weeble
    Nov 17, 2010 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.