Skip to content

Commit 6f3c547

Browse files
committed
Support and test coverage for use in partial trust / Signing assembly
1 parent 0b930e6 commit 6f3c547

25 files changed

+380
-57
lines changed

AgileMapper.Net40/AgileMapper.Net40.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="..\VersionInfo.cs">
5555
<Link>Properties\VersionInfo.cs</Link>
5656
</Compile>
57+
<Compile Include="Properties\AssemblyInfo.Net40.cs" />
5758
</ItemGroup>
5859
<ItemGroup>
5960
<None Include="packages.config" />
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("AgileObjects.AgileMapper.UnitTests")]
4+
[assembly: InternalsVisibleTo("AgileObjects.AgileMapper.UnitTests.NonParallel")]

AgileMapper.UnitTests.Polyfills/project.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
22
"version": "0.5.0-*",
3+
34
"buildOptions": {
45
"warningsAsErrors": true,
56
"outputName": "AgileObjects.AgileMapper.UnitTests.Polyfills"
67
},
8+
79
"testRunner": "xunit",
10+
811
"frameworks": {
912
"netcoreapp1.0": {
1013
"dependencies": {

AgileMapper.UnitTests/AgileMapper.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
<Compile Include="WhenFlatteningObjects.cs" />
115115
<Compile Include="WhenMappingFromDictionaries.cs" />
116116
<Compile Include="WhenMappingToConstructors.cs" />
117+
<Compile Include="WhenUsingPartialTrust.cs" />
117118
<Compile Include="WhenViewingMappingPlans.cs" />
118119
<Compile Include="Reflection\WhenAccessingTypeInformation.cs" />
119120
<Compile Include="SimpleTypeConversion\WhenConvertingToDoubles.cs" />

AgileMapper.UnitTests/TestClasses/CustomerViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AgileObjects.AgileMapper.UnitTests.TestClasses
22
{
3-
internal class CustomerViewModel : PersonViewModel
3+
public class CustomerViewModel : PersonViewModel
44
{
55
public double Discount
66
{

AgileMapper.UnitTests/TestClasses/PublicField.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AgileObjects.AgileMapper.UnitTests.TestClasses
22
{
3-
internal class PublicField<T>
3+
public class PublicField<T>
44
{
55
public T Value;
66
}

AgileMapper.UnitTests/TestClasses/PublicProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AgileObjects.AgileMapper.UnitTests.TestClasses
22
{
3-
internal class PublicProperty<T>
3+
public class PublicProperty<T>
44
{
55
public T Value
66
{
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

AgileMapper.snk

596 Bytes
Binary file not shown.

AgileMapper/DerivedTypesCache.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
using Extensions;
99
using NetStandardPolyfills;
1010

11+
internal class TrustTester
12+
{
13+
// ReSharper disable once UnusedMember.Local
14+
private static void IsPartialTrust() { }
15+
}
16+
1117
internal class DerivedTypesCache
1218
{
19+
private static readonly bool _isPartialTrust;
1320
private static readonly Type[] _noTypes = { };
21+
1422
private readonly ICache<Assembly, IEnumerable<Type>> _typesByAssembly;
1523
private readonly ICache<Type, ICollection<Type>> _derivedTypesByType;
1624

@@ -20,6 +28,20 @@ public DerivedTypesCache()
2028
_derivedTypesByType = GlobalContext.Instance.Cache.CreateScoped<Type, ICollection<Type>>();
2129
}
2230

31+
static DerivedTypesCache()
32+
{
33+
try
34+
{
35+
typeof(TrustTester)
36+
.GetNonPublicStaticMethod("IsPartialTrust")
37+
.Invoke(null, null);
38+
}
39+
catch
40+
{
41+
_isPartialTrust = true;
42+
}
43+
}
44+
2345
public ICollection<Type> GetTypesDerivedFrom(Type type)
2446
{
2547
if (type.IsSealed() || type.IsFromBcl())
@@ -54,7 +76,14 @@ private static IEnumerable<Type> GetTypesFromAssembly(Assembly assembly)
5476
{
5577
try
5678
{
57-
return assembly.GetTypes();
79+
IEnumerable<Type> types = assembly.GetTypes();
80+
81+
if (_isPartialTrust)
82+
{
83+
types = types.Where(t => t.IsPublic());
84+
}
85+
86+
return types;
5887
}
5988
catch (ReflectionTypeLoadException ex)
6089
{

0 commit comments

Comments
 (0)