Build/Test Issues

NCrunch error when impersonating Windows user in a test

Started by ebrousseau on 1,518 views

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
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.

Post a reply

Log in to reply.