StrongNameKeyPair problem when attempting to use MoQ
Asked Answered
C

1

6

I'm trying to create a mock HttpContextBase for unit test.

var fakePrinciple = new GenericPrincipal(
           new GenericIdentity(userId), 
           rolesList.ToArray());            
var mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Setup(t => t.User).Returns(fakePrinciple);
HttpContextBase mockedContext = mockHttpContext.Object;

The unit test fails at the last statement with

threw exception: System.ArgumentException: Unable to obtain public key for StrongNameKeyPair..

System.Reflection.StrongNameKeyPair.nGetPublicKey(Boolean exported, Byte[] array, String container) System.Reflection.StrongNameKeyPair.get_PublicKey() System.AppDomain.InternalDefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, String dir, Evidence evidence, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions, StackCrawlMark& stackMark, IEnumerable`1 unsafeAssemblyAttributes) System.AppDomain.DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) Castle.DynamicProxy.ModuleScope.CreateModule(Boolean signStrongName) Castle.DynamicProxy.ModuleScope.ObtainDynamicModuleWithStrongName() Castle.DynamicProxy.ModuleScope.ObtainDynamicModule(Boolean isStrongNamed) Castle.DynamicProxy.Generators.Emitters.ClassEmitter.CreateTypeBuilder(ModuleScope modulescope, String name, Type (blah blah snip)

I googled and the suggestions here don't seem to work (change RSA folder security setting etc) http://groups.google.com.br/group/castle-project-users/browse_thread/thread/85685cf32a795158

Am I correct to think that because HttpContextBase is part of System.Web.Abstraction, which is a signed assembly. Moq will actually attempt to sign the dynamic assembly, and fail?

Colenecoleopteran answered 11/3, 2010 at 14:26 Comment(3)
FWIW, I have used Moq to mock HttpContextBase lots of times and never had that issue. Whatever your issue is, it's not general for the combination of Moq and HttpContextBase.Hornpipe
The trick is that the permissions need to be set on MachineKeys folder not RSA. The post from Ayende does not make this completely obvious if you're not carefull reader.Cheekbone
So glad I found this, ugh, two days and finally fixed :D. Just wanted to add some more references that helped me, in case anyone else runs into this: ansaurus.com/question/3154345-strong-name-keys-on-windows-7 | msdn.microsoft.com/en-us/library/bb909654(v=vs.90).aspx really helped me zero in on the fix that worked for me.Stephenson
M
10

MoQ uses Castle DynamicProxy for generating mocks at runtime. Rhino Mocks uses the same library for the same purpose. If you check here:

http://ayende.com/Blog/archive/2006/06/09/UnableToObtainPublicKeyForStrongNameKeyPair.aspx

you'll see that the issue is one of permissions to the machine key store. Whatever user account is running the test must have permission to create and delete keys in the store.

You can find much more details about this issue here: http://groups.google.co.uk/group/RhinoMocks/browse_thread/thread/26df68ff01567509/5ddebf407228edc4

Musgrove answered 11/3, 2010 at 14:37 Comment(2)
I checked the permissions on RSA and Crypto folders but somehow it is not set for Machine Keys. Thanks Will.Colenecoleopteran
That's because it is the MachineKeys folder that matters and it does not inherit permissions!Cheekbone

© 2022 - 2024 — McMap. All rights reserved.