<p>I want to test the behavior of a certain piece of .NET code in partial trust environments.  What's the fastest way to set this up?  Feel free to assume that I (and other readers) are total CAS noobs.</p>

<p>@Nick: Thanks for the reply.  Alas, the tool in question is explicitly for unmanaged code.  I didn't say "managed" in my question, and should not have assumed that people would infer it from the ".NET" tag.</p>

[{'answer_id': 20726, 'author': 'Nick', 'author_id': 1490, 'author_profile': 'https://Stackoverflow.com/users/1490', 'pm_score': 1, 'selected': False, 'text': '<p>Use the <a href="http://www.microsoft.com/downloads/details.aspx?familyid=bd02c19c-1250-433c-8c1b-2619bd93b3a2&amp;displaylang=en" rel="nofollow noreferrer">Microsoft Application Verifier</a>.</p>\n\n<p>AppVerifier helps to determine:</p>\n\n<ul>\n<li>When the application is using APIs correctly:\n      (Unsafe TerminateThread APIs., Correct use of Thread Local Storage (TLS) APIs.,          o Correct use of virtual space manipulations (for example, VirtualAlloc, MapViewOfFile).</li>\n<li>Whether the application is hiding access violations using structured exception handling.</li>\n<li>Whether the application is attempting to use invalid handles.</li>\n<li>Whether there are memory corruptions or issues in the heap.</li>\n<li>Whether the application runs out of memory under low resources.</li>\n<li>Whether the correct usage of critical sections is occurring.</li>\n<li>Whether an application running in an administrative environment will run well in an environment with less privilege.</li>\n<li>Whether there are potential problems when the application is running as a limited user.</li>\n<li>Whether there are uninitialized variables in future function calls in a thread\'s context.</li>\n</ul>\n'}, {'answer_id': 20904, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 3, 'selected': True, 'text': "<p>This is an excellent question, especially from a TDD point of view and validating code under different trust scenarios. </p>\n\n<p>I think the way I'd approach this would be something along the lines of - </p>\n\n<ul>\n<li><p>Create an AppDomain in my TDD code using the AppDomain.CreateDomain() overload that allows you to pass in a PermissionSet. The PermissionSet would be constructed to match the different trust scenarios you'd want to test against.</p></li>\n<li><p>Load the assembly containing logic under test into the app domain</p></li>\n<li><p>Create instances of types/call methods etc in app domain, trap security exceptions</p></li>\n</ul>\n\n<p>Something kinda like that. I've not had time to knock up a proof of concept yet.</p>\n"}, {'answer_id': 22660, 'author': 'Martin', 'author_id': 1529, 'author_profile': 'https://Stackoverflow.com/users/1529', 'pm_score': 1, 'selected': False, 'text': '<p>You should look at the .NET Framework Configuration Tool.  It\'s in the .NET SDK, and you can find instructions on running it here...\n<a href="http://msdn.microsoft.com/en-us/library/2bc0cxhc.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/2bc0cxhc.aspx</a></p>\n\n<p>In the <em>Runtime Security Policy</em> section you\'ll find 3 policy levels: Enterprise, Machine and User.  If you drill into Machine or User you\'ll find definitions of <em>Code Groups</em> and <em>Permission Sets</em> .  When you say that you want to test some .NET code in partial trust environments, I guess you\'ll want to test against one of the standard permission sets already defined, such as <em>Internet</em> .  You need to define a <em>Code Group</em> that matches your app (or specific assemblies) and assign your chosen permission set to that <em>Code Group</em> .</p>\n\n<p>You can define your own custom <em>Permission Sets</em> too, but let\'s keep it simple for now.</p>\n\n<p>Choose whether you want your new code group to exist at machine-wide scope, or just for your user account, and drill into the Machine or User policy level accordingly.  You\'ll see a code group called _All _ Code_ .  Create a child code group inside that one, by right-clicking and selecting <em>New...</em></p>\n\n<p>Give it a name, say <em>PartialTrustGroup</em> , then click <em>Next</em> .</p>\n\n<p>You have to specify a membership condition for this group, and there are various types.  I like to create a specific folder called <em>PartialTrust</em> on my machine, and then create a URL membership condition that matches.  So, my URL looks like this...\nfile://c:/users/martin/documents/partialtrust/*</p>\n\n<p>The * is a wildcard to catch any assembly beneath that path.  Click <em>Next</em> .</p>\n\n<p>Now you can pick a permission set for your new code group.  For now, pick <em>Internet</em> .  It\'s quite a restrictive set, similar to a Java applet sandbox.  Click <em>Next</em> and <em>Finish</em> .</p>\n\n<p>Now right-click on your new code-group and select Properties.  In the General tab, ensure the topmost checkbox is selected, then click OK.</p>\n\n<p>Now, any .NET assemblies that are loaded from a location beneath the URL you specified will have the <em>Internet</em> permission set applied to them.  Expect to get some SecurityExceptions if you haven\'t written your code to carefully observe the reduced permission set.</p>\n\n<p>Sorry this is a long description.  It really is a lot more simple than it sounds.</p>\n'}, {'answer_id': 560883, 'author': 'Brann', 'author_id': 47341, 'author_profile': 'https://Stackoverflow.com/users/47341', 'pm_score': 2, 'selected': False, 'text': '<p>The functionality you\'re looking for is built-in into visual studio :</p>\n\n<p>On the security tab of your project, there\'s an "Advanced ..." button which let you configure whether you want to debug in full trust, or on a specified trust level.</p>\n'}, {'answer_id': 12189044, 'author': 'bricelam', 'author_id': 475031, 'author_profile': 'https://Stackoverflow.com/users/475031', 'pm_score': 1, 'selected': False, 'text': '<p>I just posted an article titled <a href="http://brice-lambson.blogspot.com/2012/08/partial-trust-testing-with-xunitnet.html" rel="nofollow">Partial Trust Testing with xUnit.net</a> on my blog. It details the xUnit.net-based framework that we use on the Entity Framework team to exercise code under medium trust.</p>\n\n<p>Here is an example of its usage.</p>\n\n<pre><code>public class SomeTests : MarshalByRefObject\n{\n    [PartialTrustFact]\n    public void Partial_trust_test1()\n    {\n        // Runs in medium trust\n    }\n}\n\n// Or...\n\n[PartialTrustFixture]\npublic class MoreTests : MarshalByRefObject\n{\n    [Fact]\n    public void Another_partial_trust_test()\n    {\n        // Runs in medium trust\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/20718', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/533/']