← Back to team overview

nunit-core team mailing list archive

[Bug 519912] Re: Thread.CurrentPrincipal Set In TestFixtureSetUp Not Maintained Between Tests

 

** Branch linked: lp:nunit-3.0

-- 
Thread.CurrentPrincipal Set In TestFixtureSetUp Not Maintained Between Tests
https://bugs.launchpad.net/bugs/519912
You received this bug notification because you are a member of NUnit
Developers, which is subscribed to NUnit V2.

Status in NUnit Test Framework: Fix Committed
Status in NUnit V2 Test Framework: Fix Released

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);
        }
    } 
}





References