Second Stanza

February 26, 2009

SetPrincipalPolicy in Unit Tests

Filed under: .NET Development, Testing — Tags: — dfbaskin @ 9:42 pm

A while back, I noticed that this test always fails in Visual Studio 2008 w/SP1:


[TestClass]
public static class TestInfo
{
    [AssemblyInitialize]
    public static void InitializeTesting( TestContext testContext )
    {
        AppDomain.CurrentDomain.SetPrincipalPolicy(
             System.Security.Principal.PrincipalPolicy.WindowsPrincipal );
        Assert.IsInstanceOfType(
             System.Threading.Thread.CurrentPrincipal,
             typeof(System.Security.Principal.WindowsPrincipal) );
    }
}

According to the documentation:

Setting this value will only be effective if you set it before using the Thread.CurrentPrincipal property. For example, if you set Thread.CurrentPrincipal to a given principal (for example, a generic principal) and then use the SetPrincipalPolicy method to set the PrincipalPolicy to WindowsPrincipal, the current principal will remain the generic principal.

So it’s apparent that MS Test is doing just that and causing the test to fail. This was confirmed in the Microsoft Connect item I posted about it:

This is by design. We noticed this when doing testing of the fix for SP1 and made a trade off. The tradeoff is a performance hit that we would take each time we invoke a method in the test assembly verses using a simple workaround. The workaround is to explicitly set the “System.Threading.Thread.CurrentPrincipal”.

We use the Thread.CurrentPrincipal policy prior to invoking any test method to make a backup of its value. We do this so we can restore it to the original policy after the test finishes executing. This was done to solve the issues with custom principals being serialized back into the main AppDomain and failing to deseralize.

Thanks
Visual Studio Product Team

At least the workaround is simple enough.

February 23, 2009

Conflict Between TypeMock and Reporting

Filed under: .NET Development — dfbaskin @ 1:47 pm

When using a LocalReport object within a unit test where TypeMock Isolator was running, I got the error:

Microsoft.Reporting.WinForms.LocalProcessingException : An error occurred during local report processing.
  ----> Microsoft.ReportingServices.ReportProcessing.ReportProcessingException :
Failed to load expression host assembly.
Details: Operation could destabilize the runtime.

If TypeMock Isolator was disabled, the test ran correctly. This particular test was not using TypeMock calls at all. It was just the presence of the TypeMock Isolator engine that caused the error. I assumed that there was some conflict between TypeMock Isolator and the report processing engine.

This post, by Mohamed Sharaf, provided some clues as to what was causing the error. In the end, using the ExecuteReportInCurrentAppDomain method allowed the test to run correctly.

Create a free website or blog at WordPress.com.