forked from zhongleiyang/obfuscar-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoSkipTypeTests.cs
192 lines (156 loc) · 8.42 KB
/
AutoSkipTypeTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using Mono.Cecil;
using NUnit.Framework;
using Obfuscar;
using System;
using System.Collections.Generic;
using System.IO;
namespace ObfuscarTests
{
[TestFixture]
public class AutoSkipTypeTests
{
MethodDefinition FindByName (TypeDefinition typeDef, string name)
{
foreach (MethodDefinition method in typeDef.Methods)
if (method.Name == name)
return method;
Assert.Fail (String.Format ("Expected to find method: {0}", name));
return null; // never here
}
[Test]
public void CheckHidePrivateApiFalse ()
{
string xml = String.Format (
@"<?xml version='1.0'?>" +
@"<Obfuscator>" +
@"<Var name='InPath' value='{0}' />" +
@"<Var name='OutPath' value='{1}' />" +
@"<Var name='HidePrivateApi' value='false' />" +
@"<Module file='$(InPath)\AssemblyWithTypes.dll'>" +
@"</Module>" +
@"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);
var obfuscator = TestHelper.BuildAndObfuscate ("AssemblyWithTypes", string.Empty, xml);
var map = obfuscator.Mapping;
string assmName = "AssemblyWithTypes.dll";
AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly (
Path.Combine (TestHelper.InputPath, assmName));
var classBType = inAssmDef.MainModule.GetType ("TestClasses.InternalClass");
var classB = map.GetClass (new TypeKey (classBType));
Assert.IsTrue (classB.Status == ObfuscationStatus.Skipped, "Internal class is obfuscated");
var classAType = inAssmDef.MainModule.GetType ("TestClasses.PublicClass");
var classA = map.GetClass (new TypeKey (classAType));
var classAmethod1 = FindByName (classAType, "PrivateMethod");
var classAmethod2 = FindByName (classAType, "PublicMethod");
var classAMethod1 = map.GetMethod (new MethodKey (classAmethod1));
var classAMethod2 = map.GetMethod (new MethodKey (classAmethod2));
Assert.IsTrue (classA.Status == ObfuscationStatus.Renamed, "Public class is not obfuscated");
Assert.IsTrue (classAMethod1.Status == ObfuscationStatus.Skipped, "private method is obfuscated.");
Assert.IsTrue (classAMethod2.Status == ObfuscationStatus.Renamed, "pubilc method is not obfuscated.");
}
[Test]
public void CheckHidePrivateApiTrue ()
{
string xml = String.Format (
@"<?xml version='1.0'?>" +
@"<Obfuscator>" +
@"<Var name='InPath' value='{0}' />" +
@"<Var name='OutPath' value='{1}' />" +
@"<Var name='HidePrivateApi' value='true' />" +
@"<Module file='$(InPath)\AssemblyWithTypes.dll'>" +
@"</Module>" +
@"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);
var obfuscator = TestHelper.BuildAndObfuscate ("AssemblyWithTypes", string.Empty, xml);
var map = obfuscator.Mapping;
string assmName = "AssemblyWithTypes.dll";
AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly (
Path.Combine (TestHelper.InputPath, assmName));
var classBType = inAssmDef.MainModule.GetType ("TestClasses.InternalClass");
var classB = map.GetClass (new TypeKey (classBType));
Assert.IsTrue (classB.Status == ObfuscationStatus.Renamed, "Internal class should have been obfuscated");
}
[Test]
public void CheckKeepPublicApiFalse ()
{
string xml = String.Format (
@"<?xml version='1.0'?>" +
@"<Obfuscator>" +
@"<Var name='InPath' value='{0}' />" +
@"<Var name='OutPath' value='{1}' />" +
@"<Var name='HidePrivateApi' value='true' />" +
@"<Module file='$(InPath)\AssemblyWithTypes.dll'>" +
@"</Module>" +
@"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);
var obfuscator = TestHelper.BuildAndObfuscate ("AssemblyWithTypes", string.Empty, xml);
var map = obfuscator.Mapping;
string assmName = "AssemblyWithTypes.dll";
AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly (
Path.Combine (TestHelper.InputPath, assmName));
TypeDefinition classAType = inAssmDef.MainModule.GetType ("TestClasses.PublicClass");
MethodDefinition classAmethod1 = FindByName (classAType, "PrivateMethod");
MethodDefinition classAmethod2 = FindByName (classAType, "PublicMethod");
ObfuscatedThing classAMethod1 = map.GetMethod (new MethodKey (classAmethod1));
ObfuscatedThing classAMethod2 = map.GetMethod (new MethodKey (classAmethod2));
var classA = map.GetClass (new TypeKey (classAType));
Assert.IsTrue (classA.Status == ObfuscationStatus.Renamed, "Public class should have been obfuscated");
Assert.IsTrue (classAMethod1.Status == ObfuscationStatus.Renamed, "private method is not obfuscated.");
Assert.IsTrue (classAMethod2.Status == ObfuscationStatus.Renamed, "pubilc method is not obfuscated.");
var protectedMethod = FindByName (classAType, "ProtectedMethod");
var protectedAfter = map.GetMethod (new MethodKey (protectedMethod));
Assert.IsTrue (protectedAfter.Status == ObfuscationStatus.Renamed, "protected method is not obfuscated.");
}
[Test]
public void CheckKeepPublicApiTrue ()
{
string xml = String.Format (
@"<?xml version='1.0'?>" +
@"<Obfuscator>" +
@"<Var name='InPath' value='{0}' />" +
@"<Var name='OutPath' value='{1}' />" +
@"<Var name='KeepPublicApi' value='true' />" +
@"<Var name='HidePrivateApi' value='true' />" +
@"<Module file='$(InPath)\AssemblyWithTypes.dll'>" +
@"</Module>" +
@"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);
Obfuscar.Obfuscator obfuscator = TestHelper.BuildAndObfuscate ("AssemblyWithTypes", string.Empty, xml);
var map = obfuscator.Mapping;
string assmName = "AssemblyWithTypes.dll";
AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly (
Path.Combine (TestHelper.InputPath, assmName));
TypeDefinition classAType = inAssmDef.MainModule.GetType ("TestClasses.PublicClass");
MethodDefinition classAmethod1 = FindByName (classAType, "PrivateMethod");
MethodDefinition classAmethod2 = FindByName (classAType, "PublicMethod");
ObfuscatedThing classAMethod1 = map.GetMethod (new MethodKey (classAmethod1));
ObfuscatedThing classAMethod2 = map.GetMethod (new MethodKey (classAmethod2));
var classA = map.GetClass (new TypeKey (classAType));
Assert.IsTrue (classA.Status == ObfuscationStatus.Skipped, "Public class shouldn't have been obfuscated");
Assert.IsTrue (classAMethod1.Status == ObfuscationStatus.Renamed, "private method is not obfuscated.");
Assert.IsTrue (classAMethod2.Status == ObfuscationStatus.Skipped, "pubilc method is obfuscated.");
var protectedMethod = FindByName (classAType, "ProtectedMethod");
var protectedAfter = map.GetMethod (new MethodKey (protectedMethod));
Assert.IsTrue (protectedAfter.Status == ObfuscationStatus.Skipped, "protected method is obfuscated.");
}
[Test]
public void CheckSkipNamespace ()
{
string xml = String.Format (
@"<?xml version='1.0'?>" +
@"<Obfuscator>" +
@"<Var name='InPath' value='{0}' />" +
@"<Var name='OutPath' value='{1}' />" +
@"<Var name='KeepPublicApi' value='false' />" +
@"<Var name='HidePrivateApi' value='true' />" +
@"<Module file='$(InPath)\AssemblyWithTypes.dll'>" +
@"<SkipNamespace name='TestClasses1' />" +
@"</Module>" +
@"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);
Obfuscar.Obfuscator obfuscator = TestHelper.BuildAndObfuscate ("AssemblyWithTypes", string.Empty, xml);
var map = obfuscator.Mapping;
string assmName = "AssemblyWithTypes.dll";
AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly (
Path.Combine (TestHelper.InputPath, assmName));
TypeDefinition classAType = inAssmDef.MainModule.GetType ("TestClasses1.PublicClass");
var classA = map.GetClass (new TypeKey (classAType));
Assert.IsTrue (classA.Status == ObfuscationStatus.Skipped, "Public class shouldn't have been obfuscated");
}
}
}