|
| 1 | +namespace AgileObjects.AgileMapper.UnitTests |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Security; |
| 5 | + using System.Security.Policy; |
| 6 | + using TestClasses; |
| 7 | + using Xunit; |
| 8 | + |
| 9 | + public class WhenUsingPartialTrust |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public void ShouldPerformASimpleMapping() |
| 13 | + { |
| 14 | + ExecuteInPartialTrust(helper => |
| 15 | + { |
| 16 | + helper.TestSimpleMapping(); |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void ShouldPerformAComplexMapping() |
| 22 | + { |
| 23 | + ExecuteInPartialTrust(helper => |
| 24 | + { |
| 25 | + helper.TestComplexMapping(); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void ShouldPerformADerivedMapping() |
| 31 | + { |
| 32 | + ExecuteInPartialTrust(helper => |
| 33 | + { |
| 34 | + helper.TestDerivedMapping(); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + private static void ExecuteInPartialTrust(Action<MappingHelper> testAction) |
| 39 | + { |
| 40 | + AppDomain partialTrustDomain = null; |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + var evidence = new Evidence(); |
| 45 | + evidence.AddHostEvidence(new Zone(SecurityZone.Internet)); |
| 46 | + |
| 47 | + var permissions = new NamedPermissionSet( |
| 48 | + "PartialTrust", |
| 49 | + SecurityManager.GetStandardSandbox(evidence)); |
| 50 | + |
| 51 | + partialTrustDomain = AppDomain.CreateDomain( |
| 52 | + "PartialTrust", |
| 53 | + evidence, |
| 54 | + new AppDomainSetup { ApplicationBase = "." }, |
| 55 | + permissions); |
| 56 | + |
| 57 | + var helperType = typeof(MappingHelper); |
| 58 | + |
| 59 | + var helper = (MappingHelper)partialTrustDomain |
| 60 | + .CreateInstanceAndUnwrap(helperType.Assembly.FullName, helperType.FullName); |
| 61 | + |
| 62 | + testAction.Invoke(helper); |
| 63 | + } |
| 64 | + finally |
| 65 | + { |
| 66 | + if (partialTrustDomain != null) |
| 67 | + { |
| 68 | + AppDomain.Unload(partialTrustDomain); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public class MappingHelper : MarshalByRefObject |
| 75 | + { |
| 76 | + public void TestSimpleMapping() |
| 77 | + { |
| 78 | + var source = new PublicProperty<string> { Value = "I don't trust you..." }; |
| 79 | + var result = Mapper.Map(source).ToANew<PublicField<string>>(); |
| 80 | + |
| 81 | + Assert.Equal("I don't trust you...", result.Value); |
| 82 | + } |
| 83 | + |
| 84 | + public void TestComplexMapping() |
| 85 | + { |
| 86 | + var source = new Customer { Name = "Untrusted!", Discount = 0.2m }; |
| 87 | + var result = Mapper.Map(source).ToANew<CustomerViewModel>(); |
| 88 | + |
| 89 | + Assert.Equal("Untrusted!", result.Name); |
| 90 | + Assert.Equal(0.2, result.Discount); |
| 91 | + } |
| 92 | + |
| 93 | + public void TestDerivedMapping() |
| 94 | + { |
| 95 | + Person source = new Customer { Name = "Untrusted Person :(", Discount = 0.1m }; |
| 96 | + var result = Mapper.Map(source).ToANew<CustomerViewModel>(); |
| 97 | + |
| 98 | + Assert.Equal("Untrusted Person :(", result.Name); |
| 99 | + Assert.Equal(0.1, result.Discount); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments