public bool PosTest1()
    {
        bool retVal = true;

        // Add your scenario description here
        TestLibrary.TestFramework.BeginScenario("PosTest1: Verify Ctor.");

        try
        {
            string keyFile = "keyfile";
            AssemblyKeyFileAttribute akfa = new AssemblyKeyFileAttribute(keyFile);

            if (akfa.KeyFile != keyFile)
            {
                TestLibrary.TestFramework.LogError("001.1", "Ctor Err.");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return retVal;
    }
    public bool PosTest1()
    {
        bool retVal = true;

        // Add your scenario description here
        TestLibrary.TestFramework.BeginScenario("PosTest1: Verify property KeyFile .");

        try
        {
            string keyFile = "keyfile";
            AssemblyKeyFileAttribute akfa = new AssemblyKeyFileAttribute(keyFile);

            if (akfa.KeyFile != keyFile)
            {
                TestLibrary.TestFramework.LogError("001.1", "Property KeyFile Err.");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return(retVal);
    }
        public AssemblyKeyFileAttributeTest()
        {
            //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(AssemblyKeyFileAttribute);
            ConstructorInfo ctrInfo   = attribute.GetConstructor(
                new Type [] { typeof(string) }
                );
            CustomAttributeBuilder attrBuilder =
                new CustomAttributeBuilder(ctrInfo, new object [1] {
                "test.snk"
            });

            dynAssembly.SetCustomAttribute(attrBuilder);
            object [] attributes = dynAssembly.GetCustomAttributes(true);
            attr = attributes [0] as AssemblyKeyFileAttribute;
        }
示例#4
0
        public static void AssemblyKeyFileAttributeTests()
        {
            var attr1 = new AssemblyKeyFileAttribute(null);

            Assert.Null(attr1.KeyFile);

            var attr2 = new AssemblyKeyFileAttribute("KeyFile.snk");

            Assert.Equal("KeyFile.snk", attr2.KeyFile);
        }
示例#5
0
        public void Ctor_String(string keyFile)
        {
            var attribute = new AssemblyKeyFileAttribute(keyFile);

            Assert.Equal(keyFile, attribute.KeyFile);
        }
        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);
            }
        }
        public void CtorTest()
        {
            var a = new AssemblyKeyFileAttribute("some text");

            Assert.AreEqual("some text", a.KeyFile);
        }