In a previous, we were looking at partial trust the lack of support for it. In this post, I'll show the key 'fixes' that we did to make Pex 'partial trust' aware.
Simulating Partial Trust
The easiest way to run under partial trust is to run your .net application from the network. However, in the context of a test framework, this would not work since many required permissions would not be granted (reflection, i/o, etc...). So we need a new AppDomain whose security policy considers the test framework assemblies as fully trusted.
string trust = "LocalIntranet";AppDomain domain = AppDomain.CreateAppDomain(trust);
PermissionSet permission = GetNamedPermissionSet(trust);
UnionCodeGroup code= new UnionCodeGroup( new AllMembershipCondition(), new PolicyStatement(permission, PolicyStatementAttribute.Nothing));
StrongName strongName = CreateStrongName(typeof(TestFixtureAttribute).Assembly);PermissionSet fullTrust = new PermissionSet(PermissionState.Unrestricted);UnionCodeGroup fullTrustCode = new UnionCodeGroup( new StrongNameMembershipCondition(strongName.PublicKey, strongName.Name, strongName.Version), new PolicyStatement(fullTrust, PolicyStatementAttribute.Nothing));code.AddChild(fullTrustCode);
PolicyLevel policy = PolicyLevel.CreateAppDomainLevel();policy.RootCodeGroup = code;domain.SetAppDomainPolicy(policy);
This is basically it (the rest of the details are left as an exercise :)).
Let them call you
Make sure to add the AllowPartiallyTrustedCallers to the test framework assembly otherwize users won't be allowed to call into it...
A twist...
Pex is bit invasive when it comes to partial trust. Pex rewrites the IL at runtime and turns all method bodies into... unsafe code (that is unverifiable). At this point, any will not run because of the SkipVerification permission.
No problemo, just add it to the permissionset:
permission.AddPermission( new SecurityPermission(SecurityPermissionFlag.SkipVerification) );
Page rendered at Thursday, September 09, 2010 5:09:35 AM (Pacific Daylight Time, UTC-07:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.