public void CheckSkipEnum()
        {
            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){2}AssemblyWithTypes.dll'>" +
                @"<SkipType name='TestClasses.TestEnum' />" +
                @"</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));

            var enumType = inAssmDef.MainModule.GetType("TestClasses.TestEnum");
            var enum1    = map.GetClass(new TypeKey(enumType));

            Assert.True(enum1.Status == ObfuscationStatus.Skipped, "Internal enum is obfuscated");
        }
        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.True(classA.Status == ObfuscationStatus.Skipped, "Public class shouldn't have been obfuscated");
        }
Beispiel #3
0
        public void CheckGoodDependency()
        {
            string xml = String.Format(
                @"<?xml version='1.0'?>" +
                @"<Obfuscator>" +
                @"<Var name='InPath' value='{0}' />" +
                @"<Module file='$(InPath)\AssemblyB.dll' />" +
                @"</Obfuscator>", TestHelper.InputPath);

            Obfuscar.Obfuscator obfuscator = Obfuscar.Obfuscator.CreateFromXml(xml);
        }
Beispiel #4
0
        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' />" +
                @"<Module file='$(InPath)\AssemblyWithTypes.dll'>" +
                @"</Module>" +
                @"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);

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

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

            typesToFind.Add("TestClasses.ClassB");

            AssemblyHelper.CheckAssembly("AssemblyWithTypes", 2,
                                         delegate {
                return(true);
            },
                                         delegate(TypeDefinition typeDef) {
                if (typesToFind.Contains(typeDef.ToString()))
                {
                    typesToFind.Remove(typeDef.ToString());
                }
            });
            Assert.IsTrue(typesToFind.Count == 0, "could not find ClassB, 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.Renamed, "private method is not obfuscated.");
            Assert.IsTrue(classAMethod2.Status == ObfuscationStatus.Skipped, "pubilc method is obfuscated.");
        }
        Obfuscar.ObfuscationMap BuildAndObfuscateAssemblies()
        {
            string xml = String.Format(
                @"<?xml version='1.0'?>" +
                @"<Obfuscator>" +
                @"<Var name='InPath' value='{0}' />" +
                @"<Var name='OutPath' value='{1}' />" +
                @"<Var name='KeepPublicApi' value='false' />" +
                @"<Module file='$(InPath)\AssemblyWithOverrides.dll' />" +
                @"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);

            Obfuscar.Obfuscator obfuscator = TestHelper.BuildAndObfuscate("AssemblyWithOverrides", String.Empty, xml);

            return(obfuscator.Mapping);
        }
        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.");
        }