public AssemblyKeyNameAttributeTest()
        {
            //create a dynamic assembly with the required attribute
            //and check for the validity

            dynAsmName.Name = "TestAssembly";

            dynAssembly = Thread.GetDomain().DefineDynamicAssembly(
                dynAsmName, AssemblyBuilderAccess.Run
                );

            // Set the required Attribute of the assembly.
            Type            attribute = typeof(AssemblyKeyNameAttribute);
            ConstructorInfo ctrInfo   = attribute.GetConstructor(
                new Type [] { typeof(string) }
                );
            CustomAttributeBuilder attrBuilder =
                new CustomAttributeBuilder(ctrInfo, new object [1] {
                "MyKey"
            });

            dynAssembly.SetCustomAttribute(attrBuilder);
            object [] attributes = dynAssembly.GetCustomAttributes(true);
            attr = attributes [0] as AssemblyKeyNameAttribute;
        }
        public static void AssemblyKeyNameAttributeTests()
        {
            var attr1 = new AssemblyKeyNameAttribute(null);

            Assert.Null(attr1.KeyName);

            var attr2 = new AssemblyKeyNameAttribute("My Key");

            Assert.Equal("My Key", attr2.KeyName);
        }
 public bool PosTest3()
 {
     bool retVal = true;
     TestLibrary.TestFramework.BeginScenario("PosTest3:Initialize the AssemblyKeyNameAttribute 3");
     try
     {
         string keyname = string.Empty;
         AssemblyKeyNameAttribute assemKeyNameAttr = new AssemblyKeyNameAttribute(keyname);
         if (assemKeyNameAttr == null || assemKeyNameAttr.KeyName != keyname)
         {
             TestLibrary.TestFramework.LogError("005", "the ExpectResult is not the ActualResult");
             retVal = false;
         }
     }
     catch (Exception e)
     {
         TestLibrary.TestFramework.LogError("006", "Unexpect exception:" + e);
         retVal = false;
     }
     return retVal;
 }
 public bool PosTest1()
 {
     bool retVal = true;
     TestLibrary.TestFramework.BeginScenario("PosTest1:Initialize the AssemblyKeyNameAttribute 1");
     try
     {
         string keyname = TestLibrary.Generator.GetString(-55, false, c_MIN_STR_LENGTH, c_MAX_STR_LENGTH);
         AssemblyKeyNameAttribute assemKeyNameAttr = new AssemblyKeyNameAttribute(keyname);
         if (assemKeyNameAttr == null || assemKeyNameAttr.KeyName != keyname)
         {
             TestLibrary.TestFramework.LogError("001", "the ExpectResult is not the ActualResult");
             retVal = false;
         }
     }
     catch (Exception e)
     {
         TestLibrary.TestFramework.LogError("002", "Unexpect exception:" + e);
         retVal = false;
     }
     return retVal;
 }
 public bool PosTest2()
 {
     bool retVal = true;
     TestLibrary.TestFramework.BeginScenario("PosTest2:Return the KeyName info from initialized the AssemblyKeyNameAttribute 2");
     try
     {
         string keyname = null;
         AssemblyKeyNameAttribute assemKeyNameAttr = new AssemblyKeyNameAttribute(keyname);
         if (assemKeyNameAttr.KeyName != null)
         {
             TestLibrary.TestFramework.LogError("003", "the ExpectResult is not the ActualResult");
             retVal = false;
         }
     }
     catch (Exception e)
     {
         TestLibrary.TestFramework.LogError("004", "Unexpect exception:" + e);
         retVal = false;
     }
     return retVal;
 }
Beispiel #6
0
    public bool PosTest3()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest3:Initialize the AssemblyKeyNameAttribute 3");
        try
        {
            string keyname = string.Empty;
            AssemblyKeyNameAttribute assemKeyNameAttr = new AssemblyKeyNameAttribute(keyname);
            if (assemKeyNameAttr == null || assemKeyNameAttr.KeyName != keyname)
            {
                TestLibrary.TestFramework.LogError("005", "the ExpectResult is not the ActualResult");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("006", "Unexpect exception:" + e);
            retVal = false;
        }
        return(retVal);
    }
Beispiel #7
0
    public bool PosTest1()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest1:Initialize the AssemblyKeyNameAttribute 1");
        try
        {
            string keyname = TestLibrary.Generator.GetString(-55, false, c_MIN_STR_LENGTH, c_MAX_STR_LENGTH);
            AssemblyKeyNameAttribute assemKeyNameAttr = new AssemblyKeyNameAttribute(keyname);
            if (assemKeyNameAttr == null || assemKeyNameAttr.KeyName != keyname)
            {
                TestLibrary.TestFramework.LogError("001", "the ExpectResult is not the ActualResult");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("002", "Unexpect exception:" + e);
            retVal = false;
        }
        return(retVal);
    }
Beispiel #8
0
    public bool PosTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest2:Return the KeyName info from initialized the AssemblyKeyNameAttribute 2");
        try
        {
            string keyname = null;
            AssemblyKeyNameAttribute assemKeyNameAttr = new AssemblyKeyNameAttribute(keyname);
            if (assemKeyNameAttr.KeyName != null)
            {
                TestLibrary.TestFramework.LogError("003", "the ExpectResult is not the ActualResult");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("004", "Unexpect exception:" + e);
            retVal = false;
        }
        return(retVal);
    }
Beispiel #9
0
        public void Ctor_String(string keyName)
        {
            var attribute = new AssemblyKeyNameAttribute(keyName);

            Assert.Equal(keyName, attribute.KeyName);
        }
        public void CtorTest()
        {
            var a = new AssemblyKeyNameAttribute("some text");

            Assert.AreEqual("some text", a.KeyName);
        }
        private void DoIt()
        {
            AssemblyName aname = new AssemblyName();

            aname.Name = Path.GetFileNameWithoutExtension(outFile);
            if (culture != null)
            {
                aname.CultureInfo = new CultureInfo(culture);
            }

            string fileName = Path.GetFileName(outFile);

            AssemblyBuilder ab;

            /*
             * Emit Manifest
             * */

            if (isTemplateFile)
            {
                // LAMESPEC: according to MSDN, the template assembly must have a
                // strong name but this is not enforced
                Assembly assembly = Assembly.LoadFrom(templateFile);

                // inherit signing related settings from template, but do not
                // override command-line options
                object [] attrs = assembly.GetCustomAttributes(true);
                foreach (object o in attrs)
                {
                    if (o is AssemblyKeyFileAttribute)
                    {
                        if (keyfile != null)
                        {
                            // ignore if specified on command line
                            continue;
                        }
                        AssemblyKeyFileAttribute keyFileAttr = (AssemblyKeyFileAttribute)o;
                        // ignore null or zero-length keyfile
                        if (keyFileAttr.KeyFile == null || keyFileAttr.KeyFile.Length == 0)
                        {
                            continue;
                        }
                        keyfile = Path.Combine(Path.GetDirectoryName(templateFile),
                                               keyFileAttr.KeyFile);
                    }
                    else if (o is AssemblyDelaySignAttribute)
                    {
                        if (delaysign != DelaySign.NotSet)
                        {
                            // ignore if specified on command line
                            continue;
                        }
                        AssemblyDelaySignAttribute delaySignAttr = (AssemblyDelaySignAttribute)o;
                        delaysign = delaySignAttr.DelaySign ? DelaySign.Yes :
                                    DelaySign.No;
                    }
                    else if (o is AssemblyKeyNameAttribute)
                    {
                        if (keyname != null)
                        {
                            // ignore if specified on command line
                            continue;
                        }
                        AssemblyKeyNameAttribute keynameAttr = (AssemblyKeyNameAttribute)o;
                        // ignore null or zero-length keyname
                        if (keynameAttr.KeyName == null || keynameAttr.KeyName.Length == 0)
                        {
                            continue;
                        }
                        keyname = keynameAttr.KeyName;
                    }
                }
                aname.Version       = assembly.GetName().Version;
                aname.HashAlgorithm = assembly.GetName().HashAlgorithm;
            }

            SetKeyPair(aname);

            if (fileName != outFile)
            {
                ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName(outFile));
            }
            else
            {
                ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.Save);
            }

            foreach (CustomAttributeBuilder cb in cattrs)
            {
                ab.SetCustomAttribute(cb);
            }

            /*
             * Emit modules
             */

            foreach (ModuleInfo mod in inputFiles)
            {
                MethodInfo mi = typeof(AssemblyBuilder).GetMethod("AddModule", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                if (mi == null)
                {
                    Report(0, "Cannot add modules on this runtime: try the Mono runtime instead.");
                }

                if (mod.target != null)
                {
                    File.Copy(mod.fileName, mod.target, true);
                    mod.fileName = mod.target;
                }

                bool isAssembly = false;
                try
                {
                    AssemblyName.GetAssemblyName(mod.fileName);
                    isAssembly = true;
                }
                catch (Exception)
                {
                }

                if (isAssembly)
                {
                    ReportWarning(1020, "Ignoring included assembly '" + mod.fileName + "'");
                }
                else
                {
                    mi.Invoke(ab, new object [] { mod.fileName });
                }
            }

            /*
             * Set entry point
             */

            if (entryPoint != null)
            {
                string mainClass  = entryPoint.Substring(0, entryPoint.LastIndexOf('.'));
                string mainMethod = entryPoint.Substring(entryPoint.LastIndexOf('.') + 1);

                MethodInfo mainMethodInfo = null;

                try
                {
                    Type mainType = ab.GetType(mainClass);
                    if (mainType != null)
                    {
                        mainMethodInfo = mainType.GetMethod(mainMethod);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                if (mainMethodInfo != null)
                {
                    ab.SetEntryPoint(mainMethodInfo);
                }
                else
                {
                    Report(1037, "Unable to find the entry point method '" + entryPoint + "'");
                }
            }

            /*
             * Emit resources
             */

            ab.DefineVersionInfoResource();

            if (win32IconFile != null)
            {
                try
                {
                    MethodInfo mi = typeof(AssemblyBuilder).GetMethod("DefineIconResource", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    if (mi == null)
                    {
                        Report(0, "Cannot embed win32 icons on this runtime: try the Mono runtime instead.");
                    }
                    mi.Invoke(ab, new object [] { win32IconFile });
                }
                catch (Exception ex)
                {
                    Report(1031, "Error reading icon '" + win32IconFile + "' --" + ex);
                }
            }

            if (win32ResFile != null)
            {
                try
                {
                    ab.DefineUnmanagedResource(win32ResFile);
                }
                catch (Exception ex)
                {
                    Report(1019, "Metadata failure creating assembly -- " + ex);
                }
            }

            foreach (ResourceInfo res in resources)
            {
                if (res.name == null)
                {
                    res.name = Path.GetFileName(res.fileName);
                }

                foreach (ResourceInfo res2 in resources)
                {
                    if ((res != res2) && (res.name == res2.name))
                    {
                        Report(1046, String.Format("Resource identifier '{0}' has already been used in this assembly", res.name));
                    }
                }

                if (res.isEmbedded)
                {
                    MethodInfo mi = typeof(AssemblyBuilder).GetMethod("EmbedResourceFile", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                                      null, CallingConventions.Any, new Type [] { typeof(string), typeof(string) }, null);
                    if (mi == null)
                    {
                        Report(0, "Cannot embed resources on this runtime: try the Mono runtime instead.");
                    }
                    mi.Invoke(ab, new object [] { res.name, res.fileName });
                }
                else
                {
                    if (res.target != null)
                    {
                        File.Copy(res.fileName, res.target, true);
                        res.fileName = res.target;
                    }

                    ab.AddResourceFile(res.name, res.fileName,
                                       res.isPrivate ? ResourceAttributes.Private : ResourceAttributes.Public);
                }
            }

            try
            {
                ab.Save(fileName);
            }
            catch (Exception ex)
            {
                Report(1019, "Metadata failure creating assembly -- " + ex);
            }
        }