Пример #1
0
        public void CheckExclusion()
        {
            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){2}AssemblyWithTypesAttrs.dll'>"+
                @"				</Module>"+
                @"				</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath, Path.DirectorySeparatorChar);

            var obfuscator = TestHelper.BuildAndObfuscate("AssemblyWithTypesAttrs", string.Empty, xml);
            var map        = obfuscator.Mapping;

            const string assmName = "AssemblyWithTypesAttrs.dll";

            AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(TestHelper.InputPath, assmName));
            {
                TypeDefinition  classAType    = inAssmDef.MainModule.GetType("TestClasses.InternalClass");
                ObfuscatedThing classA        = map.GetClass(new TypeKey(classAType));
                var             classAmethod1 = FindByName(classAType, "PublicMethod");
                var             method        = map.GetMethod(new MethodKey(classAmethod1));

                TypeDefinition  nestedClassAType  = classAType.NestedTypes[0];
                ObfuscatedThing nestedClassA      = map.GetClass(new TypeKey(nestedClassAType));
                TypeDefinition  nestedClassAType2 = nestedClassAType.NestedTypes[0];
                ObfuscatedThing nestedClassA2     = map.GetClass(new TypeKey(nestedClassAType2));

                Assert.True(classA.Status == ObfuscationStatus.Skipped,
                            "InternalClass shouldn't have been obfuscated.");
                Assert.True(method.Status == ObfuscationStatus.Skipped, "PublicMethod shouldn't have been obfuscated");
                Assert.True(nestedClassA.Status == ObfuscationStatus.Skipped,
                            "Nested class shouldn't have been obfuscated");
                Assert.True(nestedClassA2.Status == ObfuscationStatus.Skipped,
                            "Nested class shouldn't have been obfuscated");
            }

            {
                TypeDefinition  classAType    = inAssmDef.MainModule.GetType("TestClasses.InternalClass3");
                ObfuscatedThing classA        = map.GetClass(new TypeKey(classAType));
                var             classAmethod1 = FindByName(classAType, "PublicMethod");
                var             method        = map.GetMethod(new MethodKey(classAmethod1));

                TypeDefinition  nestedClassAType  = classAType.NestedTypes[0];
                ObfuscatedThing nestedClassA      = map.GetClass(new TypeKey(nestedClassAType));
                TypeDefinition  nestedClassAType2 = nestedClassAType.NestedTypes[0];
                ObfuscatedThing nestedClassA2     = map.GetClass(new TypeKey(nestedClassAType2));

                Assert.True(classA.Status == ObfuscationStatus.Skipped,
                            "InternalClass shouldn't have been obfuscated.");
                Assert.True(method.Status == ObfuscationStatus.Renamed, "PublicMethod should have been obfuscated");
                Assert.True(nestedClassA.Status == ObfuscationStatus.Renamed,
                            "Nested class should have been obfuscated");
                Assert.True(nestedClassA2.Status == ObfuscationStatus.Renamed,
                            "Nested class should have been obfuscated");
            }

            TypeDefinition  classBType    = inAssmDef.MainModule.GetType("TestClasses.PublicClass");
            ObfuscatedThing classB        = map.GetClass(new TypeKey(classBType));
            var             classBmethod1 = FindByName(classBType, "PublicMethod");
            var             method2       = map.GetMethod(new MethodKey(classBmethod1));

            Assert.True(classB.Status == ObfuscationStatus.Renamed, "PublicClass should have been obfuscated.");
            Assert.True(method2.Status == ObfuscationStatus.Renamed, "PublicMethod should have been obfuscated.");

            TypeDefinition  classCType    = inAssmDef.MainModule.GetType("TestClasses.InternalClass2");
            ObfuscatedThing classC        = map.GetClass(new TypeKey(classCType));
            var             classCmethod1 = FindByName(classCType, "PublicMethod");
            var             method1       = map.GetMethod(new MethodKey(classCmethod1));

            TypeDefinition  nestedClassBType = classCType.NestedTypes[0];
            ObfuscatedThing nestedClassB     = map.GetClass(new TypeKey(nestedClassBType));

            TypeDefinition  nestedClassBType2 = nestedClassBType.NestedTypes[0];
            ObfuscatedThing nestedClassB2     = map.GetClass(new TypeKey(nestedClassBType2));

            Assert.True(classC.Status == ObfuscationStatus.Renamed, "InternalClass2 should have been obfuscated.");
            Assert.True(method1.Status == ObfuscationStatus.Skipped, "PublicMethod shouldn't have been obfuscated.");
            Assert.True(nestedClassB.Status == ObfuscationStatus.Renamed, "Nested class should have been obfuscated");
            Assert.True(nestedClassB2.Status == ObfuscationStatus.Renamed, "Nested class should have been obfuscated");

            TypeDefinition  classDType    = inAssmDef.MainModule.GetType("TestClasses.PublicClass2");
            ObfuscatedThing classD        = map.GetClass(new TypeKey(classDType));
            var             classDmethod1 = FindByName(classDType, "PublicMethod");
            var             method3       = map.GetMethod(new MethodKey(classDmethod1));

            Assert.True(classD.Status == ObfuscationStatus.Skipped, "PublicClass2 shouldn't have been obfuscated.");
            Assert.True(method3.Status == ObfuscationStatus.Renamed, "PublicMethod should have been obfuscated.");
        }