Thread.CurrentPrincipal Set In TestFixtureSetUp Not Maintained Between Tests

Bug #519912 reported by Amy Thorne
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit Framework
Fix Released
High
Charlie Poole
NUnit V2
Fix Released
High
Charlie Poole

Bug Description

If I run the tests below in the NUnit GUI 2.5.2, both tests pass. If I run them in the 2.5.3 GUI, the second test to run fails.

I think this is related to the code change here, http://bazaar.launchpad.net/~nunit-core/nunitv2/trunk/revision/3061.2.6#src/NUnitCore/core/TestContext.cs, to fix the issue "Bug #459219 Changes to thread princpal cause failures under .NET 4.0", https://bugs.launchpad.net/nunitv2/+bug/459219

I think the changed behaviour happens because in the TestContext class, the constructor uses the static field "current" as being the context that existed at the beginning of running the test. But the static field gets set before the identity changes in TFSU, so when it's used at the TestMethod level, even though Thread.CurrentPrincipal is "foo", the current principal in the "current" field is "". So then, when we restore the prior context after running the test method, we restore Thread.CurrentPrincipal to "" instead of "foo".

This report originated from a thread on the NUnit-discuss group here, http://groups.google.co.uk/group/nunit-discuss/browse_thread/thread/d9d47eb87587152a

using System.Security.Principal;
using System.Threading;
using NUnit.Framework;

namespace ThreadStateTest
{
    [TestFixture]
    public class ThreadIdentityTest2
    {
        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            var identity = new GenericIdentity("foo");
            Thread.CurrentPrincipal = new GenericPrincipal(identity, new string[0]);
        }

        [Test]
        public void Test1()
        {
            Assert.AreEqual("foo", Thread.CurrentPrincipal.Identity.Name);
        }

        [Test]
        public void Test2()
        {
            Assert.AreEqual("foo", Thread.CurrentPrincipal.Identity.Name);
        }
    }
}

Tags: v2port

Related branches

Revision history for this message
Charlie Poole (charlie.poole) wrote :

I think NUnit needs to set the CurrentPrincipal for each thread from the TestContext upon creating it. This affects several different locations in NUnit, so we should try to encapsulate it.

Changed in nunitv2:
importance: Undecided → High
status: New → Triaged
milestone: none → 2.5.4
Changed in nunitv2:
assignee: nobody → Charlie Poole (charlie.poole)
status: Triaged → In Progress
Revision history for this message
Charlie Poole (charlie.poole) wrote :

This turns out to also apply to changes to CurrentCulture or CurrentUICulture done in a TestFixtureSetUp method. The fix deals with these as well.

Changed in nunit-3.0:
assignee: nobody → Charlie Poole (charlie.poole)
importance: Undecided → High
status: New → Triaged
milestone: none → 2.9.4
Changed in nunitv2:
status: In Progress → Fix Committed
Changed in nunitv2:
status: Fix Committed → Fix Released
Changed in nunit-3.0:
milestone: 2.9.4 → none
tags: added: v2port
Changed in nunit-3.0:
status: Triaged → Fix Committed
milestone: none → 2.9.5
Changed in nunit-3.0:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.