Esempio n. 1
0
        protected void CheckEnums(string name, int expectedTypes, string[] expected, string[] notExpected)
        {
            C5.HashSet <string> valsToFind = new C5.HashSet <string>( );
            valsToFind.AddAll(expected);
            C5.HashSet <string> valsNotToFind = new C5.HashSet <string>( );
            valsNotToFind.AddAll(notExpected);

            AssemblyHelper.CheckAssembly(name, expectedTypes,
                                         delegate(TypeDefinition typeDef) { return(typeDef.BaseType.FullName == "System.Enum"); },
                                         delegate(TypeDefinition typeDef)
            {
                // num expected + num unexpected + field storage
                int totalValues = expected.Length + notExpected.Length + 1;
                Assert.AreEqual(totalValues, typeDef.Fields.Count,
                                String.Format("Type should have {0} values.", totalValues));

                foreach (FieldDefinition field in typeDef.Fields)
                {
                    Assert.IsFalse(valsNotToFind.Contains(field.Name), String.Format(
                                       "Did not expect to find event '{0}'.", field.Name));

                    valsToFind.Remove(field.Name);
                }

                Assert.IsFalse(valsToFind.Count > 0, "Failed to find all expected values.");
            });
        }
Esempio n. 2
0
        public void CheckNestedTypes()
        {
            string xml = String.Format(
                @"<?xml version='1.0'?>" +
                @"<Obfuscator>" +
                @"<Var name='InPath' value='{0}' />" +
                @"<Var name='OutPath' value='{1}' />" +
                @"<Module file='$(InPath)\AssemblyWithNestedTypes.dll'>" +
                @"<SkipType name='TestClasses.ClassA/NestedClassA' />" +
                @"</Module>" +
                @"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);

            TestHelper.BuildAndObfuscate("AssemblyWithNestedTypes", string.Empty, xml);

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

            typesToFind.Add("A.A");
            typesToFind.Add("A.A/a");
            typesToFind.Add("A.A/a/B");
            typesToFind.Add("A.A/NestedClassA");

            AssemblyHelper.CheckAssembly("AssemblyWithNestedTypes", 1,
                                         delegate {
                return(true);
            },
                                         delegate(TypeDefinition typeDef) {
                Assert.IsTrue(typesToFind.Contains(typeDef.ToString()), "Type {0} not expected.", typeDef.ToString());
                typesToFind.Remove(typeDef.ToString());
            });
            Assert.IsTrue(typesToFind.Count == 0, "Not all types found.");
        }
Esempio n. 3
0
        public void CheckModuleInclude()
        {
            string outputPath = TestHelper.OutputPath;
            string xml        = string.Format(
                @"<?xml version='1.0'?>" +
                @"<Obfuscator>" +
                @"  <Var name='InPath' value='{0}' />" +
                @"  <Var name='OutPath' value='{1}' />" +
                @"  <Module file='$(InPath){2}SkipVirtualMethodTest1.dll'>" +
                @"    <Include path='$(InPath){2}TestIncludeModule.xml' />" +
                @"  </Module>" +
                @"</Obfuscator>", TestHelper.InputPath, outputPath, Path.DirectorySeparatorChar);

            File.Copy(Path.Combine(TestHelper.InputPath, @"..", "TestIncludeModule.xml"),
                      Path.Combine(TestHelper.InputPath, "TestIncludeModule.xml"), true);

            TestHelper.BuildAndObfuscate("SkipVirtualMethodTest", "1", xml);

            string[] expected = new string[]
            {
                "Method1"
            };

            string[] notExpected = new string[]
            {
                "Method2"
            };

            AssemblyHelper.CheckAssembly(Path.Combine(outputPath, "SkipVirtualMethodTest1.dll"), 2, expected,
                                         notExpected,
                                         delegate(TypeDefinition typeDef) { return(!typeDef.IsInterface); },
                                         CheckType);
        }
Esempio n. 4
0
        public void CheckDefault()
        {
            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='HidePrivateApi' value='true' />" +
                @"<Var name='KeepPublicApi' value='true' />" +
                @"<Module file='$(InPath){2}AssemblyWithNestedTypes2.dll'>" +
                @"</Module>" +
                @"</Obfuscator>", TestHelper.InputPath, outputPath, Path.DirectorySeparatorChar);

            TestHelper.BuildAndObfuscate("AssemblyWithNestedTypes2", string.Empty, xml);

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

            typesToFind.Add("TestClasses.ClassA");
            typesToFind.Add("TestClasses.ClassA/A");
            typesToFind.Add("TestClasses.ClassA/NestedClassB");
            typesToFind.Add("TestClasses.ClassA/NestedClassB/NestedClassC");

            AssemblyHelper.CheckAssembly(Path.Combine(outputPath, "AssemblyWithNestedTypes2.dll"), 1,
                                         delegate {
                return(true);
            },
                                         delegate(TypeDefinition typeDef) {
                Assert.True(typesToFind.Contains(typeDef.ToString()), string.Format("Type {0} not expected.", typeDef.ToString()));
                typesToFind.Remove(typeDef.ToString());
            });
            Assert.True(typesToFind.Count == 0, "Not all types found.");
        }
Esempio n. 5
0
        public void CheckSkipsVirtualMethodFromClass()
        {
            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='true' />" +
                @"<Var name='HidePrivateApi' value='true' />" +
                @"<Module file='$(InPath){2}SkipVirtualMethodTest2.dll'>" +
                @"<SkipMethod type='SkipVirtualMethodTest.Class1' name='Method1' />" +
                @"</Module>" +
                @"</Obfuscator>", TestHelper.InputPath, outputPath, Path.DirectorySeparatorChar);

            TestHelper.BuildAndObfuscate("SkipVirtualMethodTest", "2", xml);

            string[] expected = new string[]
            {
                "Method1"
            };

            string[] notExpected = new string[]
            {
                "Method2"
            };

            AssemblyHelper.CheckAssembly(Path.Combine(outputPath, "SkipVirtualMethodTest2.dll"), 2, expected,
                                         notExpected,
                                         delegate(TypeDefinition typeDef) { return(!typeDef.IsInterface); },
                                         CheckType);
        }
Esempio n. 6
0
        public void CheckSkipsVirtualMethodFromClass()
        {
            string xml = String.Format(
                @"<?xml version='1.0'?>" +
                @"<Obfuscator>" +
                @"<Var name='InPath' value='{0}' />" +
                @"<Var name='OutPath' value='{1}' />" +
                @"<Module file='$(InPath)\SkipVirtualMethodTest2.dll'>" +
                @"<SkipMethod type='SkipVirtualMethodTest.Class1' name='Method1' />" +
                @"</Module>" +
                @"</Obfuscator>", TestHelper.InputPath, TestHelper.OutputPath);

            TestHelper.BuildAndObfuscate("SkipVirtualMethodTest", "2", xml);

            string[] expected = new string[] {
                "Method1"
            };

            string[] notExpected = new string[] {
                "Method2"
            };

            AssemblyHelper.CheckAssembly("SkipVirtualMethodTest2", 2, expected, notExpected,
                                         delegate(TypeDefinition typeDef) {
                return(!typeDef.IsInterface);
            },
                                         CheckType);
        }
Esempio n. 7
0
        public void CheckHidePrivateApiTrue()
        {
            string xml = String.Format(
                @"<?xml version='1.0'?>" +
                @"<Obfuscator>" +
                @"<Var name='InPath' value='{0}' />" +
                @"<Var name='OutPath' value='{1}' />" +
                @"<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 find ClassA, which should have been obfuscated.");
        }
Esempio n. 8
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.");
        }
Esempio n. 9
0
        protected void CheckProperties(string name, int expectedTypes, string[] expected, string[] notExpected)
        {
            C5.HashSet <string> propsToFind = new C5.HashSet <string> ();
            propsToFind.AddAll(expected);
            C5.HashSet <string> propsNotToFind = new C5.HashSet <string> ();
            propsNotToFind.AddAll(notExpected);

            string[] expectedMethods = new string[expected.Length * 2];
            for (int i = 0; i < expected.Length; i++)
            {
                expectedMethods [i * 2 + 0] = "get_" + expected [i];
                expectedMethods [i * 2 + 1] = "set_" + expected [i];
            }

            string[] notExpectedMethods = new string[notExpected.Length * 2];
            for (int i = 0; i < notExpected.Length; i++)
            {
                notExpectedMethods [i * 2 + 0] = "get_" + notExpected [i];
                notExpectedMethods [i * 2 + 1] = "set_" + notExpected [i];
            }

            AssemblyHelper.CheckAssembly(name, expectedTypes, expectedMethods, notExpectedMethods,
                                         delegate(TypeDefinition typeDef) {
                return(true);
            },
                                         delegate(TypeDefinition typeDef) {
                Assert.AreEqual(expected.Length, typeDef.Properties.Count,
                                expected.Length == 1 ? "Type should have 1 property (others dropped by default)." :
                                String.Format("Type should have {0} properties (others dropped by default).", expected.Length));

                foreach (PropertyDefinition prop in typeDef.Properties)
                {
                    Assert.IsFalse(propsNotToFind.Contains(prop.Name), String.Format(
                                       "Did not expect to find property '{0}'.", prop.Name));

                    propsToFind.Remove(prop.Name);
                }

                Assert.IsFalse(propsToFind.Count > 0, "Failed to find all expected properties.");
            });
        }
Esempio n. 10
0
        protected void CheckEvents(string name, int expectedTypes, string[] expected, string[] notExpected)
        {
            C5.HashSet <string> eventsToFind = new C5.HashSet <string>( );
            eventsToFind.AddAll(expected);
            C5.HashSet <string> eventsNotToFind = new C5.HashSet <string>( );
            eventsNotToFind.AddAll(notExpected);

            C5.HashSet <string> methodsToFind = new C5.HashSet <string>( );
            for (int i = 0; i < expected.Length; i++)
            {
                methodsToFind.Add("add_" + expected[i]);
                methodsToFind.Add("remove_" + expected[i]);
            }

            C5.HashSet <string> methodsNotToFind = new C5.HashSet <string>( );
            for (int i = 0; i < notExpected.Length; i++)
            {
                methodsNotToFind.Add("add_" + notExpected[i]);
                methodsNotToFind.Add("remove_" + notExpected[i]);
            }

            bool foundDelType = false;

            AssemblyHelper.CheckAssembly(name, expectedTypes,
                                         delegate(TypeDefinition typeDef)
            {
                if (typeDef.BaseType.FullName == "System.MulticastDelegate")
                {
                    foundDelType = true;
                    return(false);
                }
                else
                {
                    return(true);
                }
            },
                                         delegate(TypeDefinition typeDef)
            {
                // make sure we have enough methods...
                // 2 methods / event + a method to fire them
                Assert.AreEqual(methodsToFind.Count + methodsNotToFind.Count + 1, typeDef.Methods.Count,
                                "Some of the methods for the type are missing.");

                foreach (MethodDefinition method in typeDef.Methods)
                {
                    Assert.IsFalse(methodsNotToFind.Contains(method.Name), String.Format(
                                       "Did not expect to find method '{0}'.", method.Name));

                    methodsToFind.Remove(method.Name);
                }

                Assert.AreEqual(expected.Length, typeDef.Events.Count,
                                expected.Length == 1 ? "Type should have 1 event (others dropped by default)." :
                                String.Format("Type should have {0} events (others dropped by default).", expected.Length));

                foreach (EventDefinition evt in typeDef.Events)
                {
                    Assert.IsFalse(eventsNotToFind.Contains(evt.Name), String.Format(
                                       "Did not expect to find event '{0}'.", evt.Name));

                    eventsToFind.Remove(evt.Name);
                }

                Assert.IsFalse(methodsToFind.Count > 0, "Failed to find all expected methods.");
                Assert.IsFalse(eventsToFind.Count > 0, "Failed to find all expected events.");
            });

            Assert.IsTrue(foundDelType, "Should have found the delegate type.");
        }