1 // 2 // AssemblySignatureKeyAttributeTest.cs 3 // 4 // Authors: 5 // Alexander Köplinger (alkpli@microsoft.com) 6 // 7 // (c) 2017 Microsoft 8 // 9 // Permission is hereby granted, free of charge, to any person obtaining 10 // a copy of this software and associated documentation files (the 11 // "Software"), to deal in the Software without restriction, including 12 // without limitation the rights to use, copy, modify, merge, publish, 13 // distribute, sublicense, and/or sell copies of the Software, and to 14 // permit persons to whom the Software is furnished to do so, subject to 15 // the following conditions: 16 // 17 // The above copyright notice and this permission notice shall be 18 // included in all copies or substantial portions of the Software. 19 // 20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 // 28 29 using System; 30 using System.Threading; 31 using System.Reflection; 32 using System.Reflection.Emit; 33 using NUnit.Framework; 34 35 namespace MonoTests.System.Reflection { 36 37 /// <summary> 38 /// Summary description for AssemblySignatureKeyAttributeTest. 39 /// </summary> 40 [TestFixture] 41 public class AssemblySignatureKeyAttributeTest 42 { 43 #if !MOBILE 44 private AssemblyBuilder dynAssembly; 45 AssemblyName dynAsmName = new AssemblyName (); 46 AssemblySignatureKeyAttribute attr; 47 AssemblySignatureKeyAttributeTest()48 public AssemblySignatureKeyAttributeTest () 49 { 50 //create a dynamic assembly with the required attribute 51 //and check for the validity 52 53 dynAsmName.Name = "TestAssembly"; 54 55 dynAssembly = Thread.GetDomain ().DefineDynamicAssembly ( 56 dynAsmName,AssemblyBuilderAccess.Run 57 ); 58 59 // Set the required Attribute of the assembly. 60 Type attribute = typeof (AssemblySignatureKeyAttribute); 61 ConstructorInfo ctrInfo = attribute.GetConstructor ( 62 new Type [] { typeof (string), typeof (string) } 63 ); 64 CustomAttributeBuilder attrBuilder = 65 new CustomAttributeBuilder (ctrInfo, new object [2] { "PublicKey", "CounterSignature" }); 66 dynAssembly.SetCustomAttribute (attrBuilder); 67 object [] attributes = dynAssembly.GetCustomAttributes (true); 68 attr = attributes [0] as AssemblySignatureKeyAttribute; 69 } 70 71 [Test] SignatureKeyTest()72 public void SignatureKeyTest () 73 { 74 Assert.AreEqual ( 75 attr.PublicKey, 76 "PublicKey", "#1"); 77 Assert.AreEqual ( 78 attr.Countersignature, 79 "CounterSignature", "#2"); 80 } 81 82 [Test] TypeIdTest()83 public void TypeIdTest () 84 { 85 Assert.AreEqual ( 86 attr.TypeId, 87 typeof (AssemblySignatureKeyAttribute), "#1" 88 ); 89 } 90 91 [Test] MatchTestForTrue()92 public void MatchTestForTrue () 93 { 94 Assert.AreEqual ( 95 attr.Match (attr), 96 true, "#1"); 97 } 98 99 [Test] MatchTestForFalse()100 public void MatchTestForFalse () 101 { 102 Assert.AreEqual ( 103 attr.Match (new AssemblySignatureKeyAttribute ("OtherPublicKey", "OtherCounterSignature")), 104 false, "#1"); 105 } 106 #endif 107 [Test] CtorTest()108 public void CtorTest () 109 { 110 var a = new AssemblySignatureKeyAttribute ("some text", "some other text"); 111 Assert.AreEqual ("some text", a.PublicKey); 112 Assert.AreEqual ("some other text", a.Countersignature); 113 } 114 } 115 } 116 117