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

Notification

Icon
Error

NCrunch error when impersonating Windows user in a test
ebrousseau
#1 Posted : Monday, January 6, 2020 10:25:07 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/6/2020(UTC)
Posts: 1
Location: Canada

Hi,

I have a few special tests that I run where I need to impersonate a specific Windows user during the test, to validate some rules related to this user's access rights.

Now, I got some tests that fail when run by NCrunch but that work when run by Visual Studio built-in Text Explorer. I've been able to pinpoint the issue to a specific case:

TestProject --> Lvl1ClassProject --> Lvl2ClassProject

In TestProject, I have a test method that call a method from a class in Lvl1ClassProject that calls another method from a class in Lvl2ClassProject.

The error I get is a FileNotFoundException of the Lvl2ClassProject's assembly when I run the test in NCrunch.

As I said, before calling the Lvl1ClassProject's method in the test method, I use some custom class to do the impersonation that mostly rely on the WindowsIdentity.Impersonate() method of the .Net Framework.

If I try to call the method with the interactive user (i.e. my user) running Visual Studio, without impersonating, the test will pass in NCrunch. Also, if I try to run the code from some "restricted" folder (like the "Documents" folder of my user) that's not accessible to the user being impersonated, I will get the FileNotFoundException both in NCrunch and in Visual Studio Test Explorer. It doesn't seems to be related to the impersonated user's access level as I still get the error in NCrunch even if I make the user a member of the "Administrators" group or if I explicitly gives "full control" rights to the user on the project's folders.

I have a small test solution that can reproduces the error. I could make it accessible if needed.

Some version info:
  • NCrunch 4.2.0.7
  • Visual Studio Community 2017 15.9.15
  • All libraries targeting .NET Framework 4.5.2
  • Windows 10 1809 64-bit


Regards,

Eric
Remco
#2 Posted : Tuesday, January 7, 2020 12:05:52 AM(UTC)
Rank: NCrunch Developer

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

Thanks: 794 times
Was thanked: 1043 time(s) in 993 post(s)
Hi Eric,

Thanks for posting.

I'm not 100% sure how your impersonation code is working, but I'd be fairly certain that the act of switching to a different user profile does create a new process to execute your code. This process won't be wired with all NCrunch's magic used to find assemblies between its workspaces, so the FileNotFoundException is probably an expected outcome.

As such, this would technically be a cross-process test. This use case explains the issue you're experiencing and gives some guidance on ways you can fix it.
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.025 seconds.