示例#1
0
        public void CheckMakedOnly()
        {
            string name = "AssemblyWithTypesAttrs3";
            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' />" +
                @"             <Var name='MarkedOnly' value='true' />" +
                @"				<Module file='$(InPath){2}{3}.dll'>"+
                @"				</Module>"+
                @"				</Obfuscator>",
                TestHelper.InputPath, TestHelper.OutputPath, Path.DirectorySeparatorChar, name);

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

            AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(obfuscator.Project.AssemblyList[0].FileName);

            TypeDefinition  classAType = inAssmDef.MainModule.GetType("TestClasses.TestEnum");
            ObfuscatedThing classA     = map.GetClass(new TypeKey(classAType));
            var             field      = classAType.Fields.FirstOrDefault(item => item.Name == "Default");
            var             f1         = map.GetField(new FieldKey(field));
            var             field2     = classAType.Fields.FirstOrDefault(item => item.Name == "Test");
            var             f2         = map.GetField(new FieldKey(field2));

            Assert.True(classA.Status == ObfuscationStatus.Skipped, "Public enum shouldn't have been obfuscated.");
            Assert.True(f1.Status == ObfuscationStatus.Skipped, "Public enum field should not be obfuscated");
            Assert.True(f2.Status == ObfuscationStatus.Skipped, "Public enum field should not be 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.");

            AssemblyDefinition outAssmDef = AssemblyDefinition.ReadAssembly(obfuscator.Project.AssemblyList[0].OutputFileName);

            Assert.False(outAssmDef.CustomAttributes.Count > 2, "obfuscation attribute on assembly should have been removed.");
            TypeDefinition classTypeRenamed = outAssmDef.MainModule.Types[2];

            Assert.False(classTypeRenamed.CustomAttributes.Count == 2, "obfuscation attribute on type should have been removed.");
            MethodDefinition testMethod = classTypeRenamed.Methods.First(_ => _.Name == "Test");

            Assert.False(testMethod.HasCustomAttributes, "obfuscation attribute on method should have been removed.");
            MethodDefinition test2Method = classTypeRenamed.Methods.First(_ => _.Name == "Test2");

            Assert.True(test2Method.HasCustomAttributes, "obfuscation attribute on method should not have been removed.");

            PropertyDefinition token = classTypeRenamed.Properties[0];

            Assert.Equal("access_token", token.CustomAttributes[0].Properties[0].Argument.Value);
            PropertyDefinition type = classTypeRenamed.Properties[1];

            Assert.Equal("token_type", type.CustomAttributes[0].Properties[0].Argument.Value);
        }
示例#2
0
        public void CheckInterfaces()
        {
            Obfuscator     item = BuildAndObfuscateAssemblies("AssemblyWithInterfaces");
            ObfuscationMap map  = item.Mapping;

            string assmName = "AssemblyWithInterfaces.dll";

            AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(TestHelper.InputPath, assmName));

            AssemblyDefinition outAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(item.Project.Settings.OutPath, assmName));
            {
                TypeDefinition     classCType = inAssmDef.MainModule.GetType("TestClasses.C");
                MethodDefinition   method     = FindMethodByName(classCType, "Method");
                PropertyDefinition property   = FindPropertyByName(classCType, "Property");

                ObfuscatedThing methodEntry   = map.GetMethod(new MethodKey(method));
                ObfuscatedThing propertyEntry = map.GetProperty(new PropertyKey(new TypeKey(classCType), property));

                Assert.True(methodEntry.Status == ObfuscationStatus.Skipped, "public interface method should not be obfuscated.");

                Assert.True(propertyEntry.Status == ObfuscationStatus.Skipped, "public interface property should not be obfuscated.");
            }
        }
示例#3
0
        public void CheckClassHasAttribute()
        {
            Obfuscator     item = BuildAndObfuscateAssemblies();
            ObfuscationMap map  = item.Mapping;

            string assmName = "AssemblyWithSpecializedGenerics.dll";

            AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(TestHelper.InputPath, assmName));

            AssemblyDefinition outAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(item.Project.Settings.OutPath, assmName));

            {
                TypeDefinition   classAType    = inAssmDef.MainModule.GetType("TestClasses.ClassA`1");
                MethodDefinition classAmethod2 = FindByName(classAType, "Method2");

                TypeDefinition   classBType    = inAssmDef.MainModule.GetType("TestClasses.ClassB");
                MethodDefinition classBmethod2 = FindByName(classBType, "Method2");

                ObfuscatedThing classAEntry = map.GetMethod(new MethodKey(classAmethod2));
                ObfuscatedThing classBEntry = map.GetMethod(new MethodKey(classBmethod2));

                Assert.True(
                    classAEntry.Status == ObfuscationStatus.Renamed &&
                    classBEntry.Status == ObfuscationStatus.Renamed,
                    "Both methods should have been renamed.");

                Assert.True(
                    classAEntry.StatusText == classBEntry.StatusText,
                    "Both methods should have been renamed to the same thing.");
            }

            {
                TypeDefinition   classAType    = inAssmDef.MainModule.GetType("TestClasses.ClassA`1");
                MethodDefinition classAmethod2 = FindByName(classAType, "Method3");

                TypeDefinition   classBType    = inAssmDef.MainModule.GetType("TestClasses.ClassB");
                MethodDefinition classBmethod2 = FindByName(classBType, "Method3");

                ObfuscatedThing classAEntry = map.GetMethod(new MethodKey(classAmethod2));
                ObfuscatedThing classBEntry = map.GetMethod(new MethodKey(classBmethod2));

                Assert.True(
                    classAEntry.Status == ObfuscationStatus.Renamed &&
                    classBEntry.Status == ObfuscationStatus.Renamed,
                    "Both methods should have been renamed.");

                Assert.True(
                    classAEntry.StatusText == classBEntry.StatusText,
                    "Both methods should have been renamed to the same thing.");
            }
        }
        public void CheckClassHasAttribute()
        {
            Obfuscar.ObfuscationMap map = BuildAndObfuscateAssemblies();

            string assmName = "AssemblyWithOverrides.dll";

            AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(TestHelper.InputPath, assmName));

            AssemblyDefinition outAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(TestHelper.OutputPath, assmName));

            TypeDefinition   classAType    = inAssmDef.MainModule.GetType("TestClasses.ClassA");
            MethodDefinition classAmethod2 = FindByName(classAType, "Method2");
            MethodDefinition classAcompare = FindByName(classAType, "CompareTo");

            TypeDefinition   classBType    = inAssmDef.MainModule.GetType("TestClasses.ClassB");
            MethodDefinition classBmethod2 = FindByName(classBType, "Method2");
            MethodDefinition classBcompare = FindByName(classBType, "CompareTo");

            TypeDefinition   classCType    = inAssmDef.MainModule.GetType("TestClasses.ClassC");
            MethodDefinition classCmethod1 = FindByName(classCType, "Method1");

            TypeDefinition   classDType    = inAssmDef.MainModule.GetType("TestClasses.ClassD");
            MethodDefinition classDmethod1 = FindByName(classDType, "Method1");

            Obfuscar.ObfuscatedThing classAEntry  = map.GetMethod(new Obfuscar.MethodKey(classAmethod2));
            ObfuscatedThing          classAEntry1 = map.GetMethod(new MethodKey(classAcompare));

            Obfuscar.ObfuscatedThing classBEntry  = map.GetMethod(new Obfuscar.MethodKey(classBmethod2));
            Obfuscar.ObfuscatedThing classBEntry1 = map.GetMethod(new Obfuscar.MethodKey(classBcompare));
            ObfuscatedThing          classCEntry  = map.GetMethod(new MethodKey(classCmethod1));
            ObfuscatedThing          classDEntry  = map.GetMethod(new MethodKey(classDmethod1));

            Assert.IsTrue(
                classAEntry.Status == Obfuscar.ObfuscationStatus.Renamed &&
                classBEntry.Status == Obfuscar.ObfuscationStatus.Renamed,
                "Both methods should have been renamed.");

            Assert.IsTrue(
                classAEntry.StatusText == classBEntry.StatusText,
                "Both methods should have been renamed to the same thing.");

            Assert.IsTrue(classAEntry1.Status == ObfuscationStatus.Skipped);

            Assert.IsTrue(classBEntry1.Status == ObfuscationStatus.Skipped);

            Assert.IsTrue(classCEntry.Status == ObfuscationStatus.Renamed);

            Assert.IsTrue(classDEntry.Status == ObfuscationStatus.Renamed);
        }
示例#5
0
        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;

            HashSet <string> typesToFind = new HashSet <string> ();

            typesToFind.Add("TestClasses.ClassA");

            AssemblyHelper.CheckAssembly("AssemblyWithTypes", 2,
                                         delegate {
                return(true);
            },
                                         delegate(TypeDefinition typeDef) {
                if (typesToFind.Contains(typeDef.ToString()))
                {
                    typesToFind.Remove(typeDef.ToString());
                }
            });
            Assert.IsTrue(typesToFind.Count == 1, "could not find ClassA, which should not have been obfuscated.");

            string assmName = "AssemblyWithTypes.dll";

            AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(TestHelper.InputPath, assmName));

            AssemblyDefinition outAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(TestHelper.OutputPath, assmName));

            TypeDefinition   classAType    = inAssmDef.MainModule.GetType("TestClasses.ClassB");
            MethodDefinition classAmethod1 = FindByName(classAType, "TestA");
            MethodDefinition classAmethod2 = FindByName(classAType, "TestB");

            ObfuscatedThing classAMethod1 = map.GetMethod(new MethodKey(classAmethod1));
            ObfuscatedThing classAMethod2 = map.GetMethod(new MethodKey(classAmethod2));

            Assert.IsTrue(classAMethod1.Status == ObfuscationStatus.Skipped, "private method is obfuscated.");
            Assert.IsTrue(classAMethod2.Status == ObfuscationStatus.Renamed, "pubilc method is not obfuscated.");
        }
        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){2}AssemblyWithTypes.dll'>" +
                @"</Module>" +
                @"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath, Path.DirectorySeparatorChar);

            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");
            MethodDefinition classAmethod3 = FindByName(classAType, "InternalProtectedMethod");

            ObfuscatedThing classAMethod1 = map.GetMethod(new MethodKey(classAmethod1));
            ObfuscatedThing classAMethod2 = map.GetMethod(new MethodKey(classAmethod2));
            ObfuscatedThing classAMethod3 = map.GetMethod(new MethodKey(classAmethod3));
            var             classA        = map.GetClass(new TypeKey(classAType));

            Assert.True(classA.Status == ObfuscationStatus.Skipped, "Public class shouldn't have been obfuscated");
            Assert.True(classAMethod1.Status == ObfuscationStatus.Renamed, "private method is not obfuscated.");
            Assert.True(classAMethod2.Status == ObfuscationStatus.Skipped, "pubilc method is obfuscated.");
            Assert.True(classAMethod3.Status == ObfuscationStatus.Skipped, "internal protected method is obfuscated.");

            var protectedMethod = FindByName(classAType, "ProtectedMethod");
            var protectedAfter  = map.GetMethod(new MethodKey(protectedMethod));

            Assert.True(protectedAfter.Status == ObfuscationStatus.Skipped, "protected method is obfuscated.");
        }
        public void CheckMakedOnly()
        {
            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' />" +
                @"<Var name='MarkedOnly' value='true' />" +
                @"<Module file='$(InPath)\AssemblyWithTypesAttrs3.dll'>" +
                @"</Module>" +
                @"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);

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

            const string assmName = "AssemblyWithTypesAttrs3.dll";

            AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(TestHelper.InputPath, assmName));

            TypeDefinition  classAType = inAssmDef.MainModule.GetType("TestClasses.TestEnum");
            ObfuscatedThing classA     = map.GetClass(new TypeKey(classAType));
            var             field      = classAType.Fields.FirstOrDefault(item => item.Name == "Default");
            var             f1         = map.GetField(new FieldKey(field));
            var             field2     = classAType.Fields.FirstOrDefault(item => item.Name == "Test");
            var             f2         = map.GetField(new FieldKey(field2));

            Assert.True(classA.Status == ObfuscationStatus.Skipped, "Public enum shouldn't have been obfuscated.");
            Assert.True(f1.Status == ObfuscationStatus.Skipped, "Public enum field should not be obfuscated");
            Assert.True(f2.Status == ObfuscationStatus.Skipped, "Public enum field should not be 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.");
        }
        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)\AssemblyWithTypesAttrs.dll'>"+
                @"				</Module>"+
                @"				</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);

            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.");
        }
        public void CheckClassHasAttribute()
        {
            Obfuscator     item = BuildAndObfuscateAssemblies();
            ObfuscationMap map  = item.Mapping;

            string assmName = "AssemblyWithOverrides.dll";

            AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(TestHelper.InputPath, assmName));

            AssemblyDefinition outAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(item.Project.Settings.OutPath, assmName));
            {
                TypeDefinition   classAType    = inAssmDef.MainModule.GetType("TestClasses.ClassA");
                MethodDefinition classAmethod2 = FindByName(classAType, "Method2");
                MethodDefinition classAcompare = FindByName(classAType, "CompareTo");

                TypeDefinition   classBType    = inAssmDef.MainModule.GetType("TestClasses.ClassB");
                MethodDefinition classBmethod2 = FindByName(classBType, "Method2");
                MethodDefinition classBcompare = FindByName(classBType, "CompareTo");

                TypeDefinition   classCType    = inAssmDef.MainModule.GetType("TestClasses.ClassC");
                MethodDefinition classCmethod1 = FindByName(classCType, "Method1");

                TypeDefinition   classDType    = inAssmDef.MainModule.GetType("TestClasses.ClassD");
                MethodDefinition classDmethod1 = FindByName(classDType, "Method1");

                ObfuscatedThing classAEntry        = map.GetMethod(new MethodKey(classAmethod2));
                ObfuscatedThing classACompareEntry = map.GetMethod(new MethodKey(classAcompare));
                ObfuscatedThing classBEntry        = map.GetMethod(new MethodKey(classBmethod2));
                ObfuscatedThing classBCompareEntry = map.GetMethod(new MethodKey(classBcompare));
                ObfuscatedThing classCEntry        = map.GetMethod(new MethodKey(classCmethod1));
                ObfuscatedThing classDEntry        = map.GetMethod(new MethodKey(classDmethod1));

                var classFType   = inAssmDef.MainModule.GetType("TestClasses.ClassF");
                var classFmethod = FindByName(classFType, "Test");

                var classGType   = inAssmDef.MainModule.GetType("TestClasses.ClassG");
                var classGmethod = FindByName(classGType, "Test");

                var classFEntry = map.GetMethod(new MethodKey(classFmethod));
                var classGEntry = map.GetMethod(new MethodKey(classGmethod));

                Assert.True(
                    classAEntry.Status == ObfuscationStatus.Renamed &&
                    classBEntry.Status == ObfuscationStatus.Renamed,
                    "Both methods should have been renamed.");

                Assert.True(
                    classAEntry.StatusText == classBEntry.StatusText,
                    "Both methods should have been renamed to the same thing.");

                Assert.True(classACompareEntry.Status == ObfuscationStatus.Skipped);

                Assert.True(classBCompareEntry.Status == ObfuscationStatus.Skipped);

                Assert.True(classCEntry.Status == ObfuscationStatus.Renamed);

                Assert.True(classDEntry.Status == ObfuscationStatus.Renamed);

                Assert.True(
                    classFEntry.Status == ObfuscationStatus.Renamed && classGEntry.Status == ObfuscationStatus.Renamed,
                    "Both methods should have been renamed.");

                Assert.True(classFEntry.StatusText == classGEntry.StatusText,
                            "Both methods should have been renamed to the same thing.");
            }

            {
                TypeDefinition   classAType    = inAssmDef.MainModule.GetType("TestClasses.CA");
                MethodDefinition classAmethod2 = FindByName(classAType, "get_PropA");

                TypeDefinition   classBType    = inAssmDef.MainModule.GetType("TestClasses.CB");
                MethodDefinition classBmethod2 = FindByName(classBType, "get_PropB");

                TypeDefinition   classCType    = inAssmDef.MainModule.GetType("TestClasses.IA");
                MethodDefinition classCmethod1 = FindByName(classCType, "get_PropA");

                TypeDefinition   classDType    = inAssmDef.MainModule.GetType("TestClasses.IB");
                MethodDefinition classDmethod1 = FindByName(classDType, "get_PropB");

                ObfuscatedThing classAEntry = map.GetMethod(new MethodKey(classAmethod2));
                ObfuscatedThing classBEntry = map.GetMethod(new MethodKey(classBmethod2));
                ObfuscatedThing classCEntry = map.GetMethod(new MethodKey(classCmethod1));
                ObfuscatedThing classDEntry = map.GetMethod(new MethodKey(classDmethod1));

                Assert.True(
                    classAEntry.Status == ObfuscationStatus.Renamed &&
                    classCEntry.Status == ObfuscationStatus.Renamed,
                    "Both methods should have been renamed.");

                Assert.True(
                    classAEntry.StatusText == classCEntry.StatusText,
                    "Both methods should have been renamed to the same thing.");

                Assert.True(
                    classBEntry.Status == ObfuscationStatus.Renamed && classDEntry.Status == ObfuscationStatus.Renamed,
                    "Both methods should have been renamed.");

                Assert.True(classBEntry.StatusText == classDEntry.StatusText,
                            "Both methods should have been renamed to the same thing.");

                Assert.True(classAEntry.StatusText != classBEntry.StatusText,
                            "Both methods shouldn't have been renamed to the same thing.");
            }
        }
示例#10
0
        public void CheckMarkedOnly2()
        {
            string outputPath = TestHelper.OutputPath;
            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' />" +
                @"                <Var name='MarkedOnly' value='true' />" +
                @"				<Module file='$(InPath){2}AssemblyWithTypesAttrs.dll'>"+
                @"				</Module>"+
                @"				</Obfuscator>", TestHelper.InputPath, 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 classBType = inAssmDef.MainModule.GetType("TestClasses.PublicClass");

            Assert.True(classBType.HasCustomAttributes, "Obfuscation attribute must exist");
            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.");

            AssemblyDefinition outAssmDef = AssemblyDefinition.ReadAssembly(
                Path.Combine(outputPath, assmName));
            TypeDefinition renamed = outAssmDef.MainModule.GetType(classB.StatusText.Substring(assmName.Length - 4 + 2));

            Assert.False(renamed.HasCustomAttributes, "Obfuscation attribute must not exist");

            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.Skipped, "InternalClass2 shouldn't have been obfuscated.");
            Assert.True(method1.Status == ObfuscationStatus.Skipped, "PublicMethod shouldn't have been obfuscated.");
            Assert.True(nestedClassB.Status == ObfuscationStatus.Skipped,
                        "Nested class shouldn't have been obfuscated");
            Assert.True(nestedClassB2.Status == ObfuscationStatus.Skipped,
                        "Nested class shouldn't 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.");
        }
示例#11
0
        public void CheckSkipSerializableTypes()
        {
            string name = "AssemblyWithTypesAttrs3";
            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' />" +
                @"  <Var name='SkipSerializableTypes' value='true' />" +
                @"  <Module file='$(InPath){2}{3}.dll' />" +
                @"</Obfuscator>",
                TestHelper.InputPath, TestHelper.OutputPath, Path.DirectorySeparatorChar, name);

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

            AssemblyDefinition inAssmDef = AssemblyDefinition.ReadAssembly(obfuscator.Project.AssemblyList[0].FileName);

            TypeDefinition  classAType = inAssmDef.MainModule.GetType("TestClasses.TestEnum");
            ObfuscatedThing classA     = map.GetClass(new TypeKey(classAType));

            Assert.Equal(ObfuscationStatus.Renamed, classA.Status);

            var enumField = classAType.Fields.FirstOrDefault(item => item.Name == "Default");
            var f1        = map.GetField(new FieldKey(enumField));

            Assert.Equal(ObfuscationStatus.Renamed, f1.Status);

            var enumField2 = classAType.Fields.FirstOrDefault(item => item.Name == "Test");
            var f2         = map.GetField(new FieldKey(enumField2));

            Assert.Equal(ObfuscationStatus.Renamed, f2.Status);

            TypeDefinition  classBType    = inAssmDef.MainModule.GetType("TestClasses.PublicClass");
            var             classBTypeKey = new TypeKey(classBType);
            ObfuscatedThing classB        = map.GetClass(classBTypeKey);

            Assert.Equal(ObfuscationStatus.Skipped, classB.Status);

            var classBmethod1 = FindMethod(classBType, "PublicMethod");
            var method1       = map.GetMethod(new MethodKey(classBmethod1));

            Assert.Equal(ObfuscationStatus.Renamed, method1.Status);

            var classBproperty1 = FindProperty(classBType, "TestInt");
            var property1       = map.GetProperty(new PropertyKey(classBTypeKey, classBproperty1));

            Assert.Equal(ObfuscationStatus.Skipped, property1.Status);

            var classBfield1 = FindField(classBType, "TestSerializedField");
            var field1       = map.GetField(new FieldKey(classBfield1));

            Assert.Equal(ObfuscationStatus.Skipped, field1.Status);

            var classBfield2 = FindField(classBType, "TestNonSerializedField");
            var field2       = map.GetField(new FieldKey(classBfield2));

            Assert.Equal(ObfuscationStatus.Renamed, field2.Status);
        }