private static Assembly ResolveAssembly(object sender, ResolveEventArgs args) { var requestedName = new AssemblyName(args.Name); if (knownAssemblies.ContainsKey(requestedName.Name)) { foreach (var item in knownAssemblies[requestedName.Name]) { if (requestedName.Name == item.Item1.Name && (requestedName.Version == null || requestedName.Version == item.Item1.Version) && (requestedName.CultureInfo == null || requestedName.CultureInfo == item.Item1.CultureInfo) && (requestedName.GetPublicKey() == null || requestedName.GetPublicKey() == item.Item1.GetPublicKey())) { var path = Path.Combine(item.Item2, requestedName.Name + ".dll"); if (File.Exists(path)) return Assembly.LoadFrom(path); } } } foreach (var directory in searchDirectories) { var path = Path.Combine(directory, requestedName.Name + ".dll"); if (File.Exists(path)) return Assembly.LoadFrom(path); } return null; }
internal static bool PublicKeyMatches(System.Reflection.AssemblyName a1, byte[] publicKeyOrToken) { if (publicKeyOrToken == null) { return(a1.GetPublicKey() == null); } byte[] publicKey = a1.GetPublicKey(); if (publicKey != null && publicKeyOrToken.Length == publicKey.Length) { for (int i = 0; i < publicKey.Length; i++) { if (publicKey[i] != publicKeyOrToken[i]) { return(false); } } return(true); } byte[] publicKeyToken = a1.GetPublicKeyToken(); if (publicKeyOrToken.Length == publicKeyToken.Length) { for (int i = 0; i < publicKeyToken.Length; i++) { if (publicKeyToken[i] != publicKeyOrToken[i]) { return(false); } } return(true); } return(false); }
// // Converts an AssemblyName to a RuntimeAssemblyName that is free from any future mutations on the AssemblyName. // public static RuntimeAssemblyName ToRuntimeAssemblyName(this AssemblyName assemblyName) { if (assemblyName.Name == null) { throw new ArgumentException(); } AssemblyNameFlags flags = assemblyName.Flags; AssemblyContentType contentType = assemblyName.ContentType; #pragma warning disable SYSLIB0037 // AssemblyName.ProcessorArchitecture is obsolete ProcessorArchitecture processorArchitecture = assemblyName.ProcessorArchitecture; #pragma warning restore SYSLIB0037 AssemblyNameFlags combinedFlags = CombineAssemblyNameFlags(flags, contentType, processorArchitecture); byte[]? pkOriginal; if (0 != (flags & AssemblyNameFlags.PublicKey)) { pkOriginal = assemblyName.GetPublicKey(); } else { pkOriginal = assemblyName.GetPublicKeyToken(); } // AssemblyName's PKT property getters do NOT copy the array before giving it out. Make our own copy // as the original is wide open to tampering by anyone. byte[]? pkCopy = null; if (pkOriginal != null) { pkCopy = new byte[pkOriginal.Length]; ((ICollection <byte>)pkOriginal).CopyTo(pkCopy, 0); } return(new RuntimeAssemblyName(assemblyName.Name, assemblyName.Version, assemblyName.CultureName, combinedFlags, pkCopy)); }
protected string LookupFullName(string name) { AssemblyName asm = new AssemblyName(name); if (asm.Version != null || asm.GetPublicKeyToken() != null || asm.GetPublicKey() != null) { return name; } return _assemblyFullNames.ContainsKey(name.ToLower()) ? _assemblyFullNames[name.ToLower()] : name; }
public void Case5_SetPublicKey() { AssemblyName n = new AssemblyName(); byte[] barray = new byte[16]; n.SetPublicKey(barray); byte[] barray_returned = n.GetPublicKey(); Assert.NotNull(barray_returned); Assert.True(isEqual(barray, barray_returned)); }
private static bool IsSimplyNamed(AssemblyName partialName) { byte[] publicKeyToken = partialName.GetPublicKeyToken(); if ((publicKeyToken != null) && (publicKeyToken.Length == 0)) { return(true); } publicKeyToken = partialName.GetPublicKey(); return((publicKeyToken != null) && (publicKeyToken.Length == 0)); }
public void Case6_SetPublicKey() { AssemblyName n = new AssemblyName(); byte[] barray = new byte[16]; for (int i = 0; i < barray.Length; i++) barray[i] = (byte)'\0'; n.SetPublicKey(barray); byte[] barray_returned = n.GetPublicKey(); Assert.NotNull(barray_returned); Assert.True(isEqual(barray, barray_returned)); }
/// <summary> /// Uses the AssemblyName's public key to generate a hash equivalent to what /// StrongName.Normalize() gives. /// </summary> internal static string GetNormalizedStrongNameHash(AssemblyName name) { byte[] publicKey = name.GetPublicKey(); // If we don't have a key, we're not strong named if (publicKey == null || publicKey.Length == 0) return null; // Emulate what we get from StrongName.Normalize(). using (MemoryStream ms = new MemoryStream()) using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write(publicKey); bw.Write(name.Version.Major); bw.Write(name.Name); ms.Position = 0; return GetStrongHashSuitableForObjectName(ms); } }
bool CheckInternalsVisibleAttribute (Attribute a) { string assembly_name = a.GetString (); if (assembly_name.Length == 0) return false; AssemblyName aname = null; try { aname = new AssemblyName (assembly_name); } catch (FileLoadException) { } catch (ArgumentException) { } // Bad assembly name format if (aname == null) Report.Warning (1700, 3, a.Location, "Assembly reference `" + assembly_name + "' is invalid and cannot be resolved"); // Report error if we have defined Version or Culture else if (aname.Version != null || aname.CultureInfo != null) throw new Exception ("Friend assembly `" + a.GetString () + "' is invalid. InternalsVisibleTo cannot have version or culture specified."); else if (aname.GetPublicKey () == null && Name.GetPublicKey () != null && Name.GetPublicKey ().Length != 0) { Report.Error (1726, a.Location, "Friend assembly reference `" + aname.FullName + "' is invalid." + " Strong named assemblies must specify a public key in their InternalsVisibleTo declarations"); return false; } return true; }
[Test] // ctor (String) public void Constructor1_Retargetable () { const string assemblyName = "TestAssembly"; try { new AssemblyName (assemblyName + ", Retargetable=Yes"); Assert.Fail ("#A1"); } catch (FileLoadException ex) { // The given assembly name or codebase was invalid Assert.AreEqual (typeof (FileLoadException), ex.GetType (), "#A2"); Assert.IsNull (ex.FileName, "#A3"); Assert.IsNull (ex.InnerException, "#A4"); Assert.IsNotNull (ex.Message, "#A5"); } try { new AssemblyName (assemblyName + ", Retargetable=No"); Assert.Fail ("#B1"); } catch (FileLoadException ex) { // The given assembly name or codebase was invalid Assert.AreEqual (typeof (FileLoadException), ex.GetType (), "#B2"); Assert.IsNull (ex.FileName, "#B3"); Assert.IsNull (ex.InnerException, "#B4"); Assert.IsNotNull (ex.Message, "#B5"); } try { new AssemblyName (assemblyName + ", Version=1.0.0.0, Retargetable=Yes"); Assert.Fail ("#C1"); } catch (FileLoadException ex) { // The given assembly name or codebase was invalid Assert.AreEqual (typeof (FileLoadException), ex.GetType (), "#C2"); Assert.IsNull (ex.FileName, "#C3"); Assert.IsNull (ex.InnerException, "#C4"); Assert.IsNotNull (ex.Message, "#C5"); } try { new AssemblyName (assemblyName + ", Version=1.0.0.0, Culture=neutral, Retargetable=Yes"); Assert.Fail ("#D1"); } catch (FileLoadException ex) { // The given assembly name or codebase was invalid Assert.AreEqual (typeof (FileLoadException), ex.GetType (), "#D2"); Assert.IsNull (ex.FileName, "#D3"); Assert.IsNull (ex.InnerException, "#D4"); Assert.IsNotNull (ex.Message, "#D5"); } try { new AssemblyName (assemblyName + ", Version=1.0.0.0, PublicKeyToken=null, Retargetable=Yes"); Assert.Fail ("#E1"); } catch (FileLoadException ex) { // The given assembly name or codebase was invalid Assert.AreEqual (typeof (FileLoadException), ex.GetType (), "#E2"); Assert.IsNull (ex.FileName, "#E3"); Assert.IsNull (ex.InnerException, "#E4"); Assert.IsNotNull (ex.Message, "#E5"); } an = new AssemblyName (assemblyName + ", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, Retargetable=yEs"); Assert.IsNull (an.CodeBase, "F:CodeBase"); Assert.AreEqual (CultureInfo.InvariantCulture, an.CultureInfo, "#F:CultureInfo"); Assert.IsNull (an.EscapedCodeBase, "#F:EscapedCodeBase"); Assert.AreEqual (AssemblyNameFlags.Retargetable, an.Flags, "#F:Flags"); Assert.AreEqual ("TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, Retargetable=Yes", an.FullName, "#F:FullName"); Assert.IsNull (an.GetPublicKey (), "#F:GetPublicKey"); Assert.AreEqual (new byte [0], an.GetPublicKeyToken (), "#F:GetPublicKeyToken"); Assert.AreEqual (AssemblyHashAlgorithm.None, an.HashAlgorithm, "#F:HashAlgorithm"); Assert.IsNull (an.KeyPair, "#F:KeyPair"); Assert.AreEqual (assemblyName, an.Name, "#F:Name"); Assert.AreEqual (ProcessorArchitecture.None, an.ProcessorArchitecture, "#F:PA"); Assert.AreEqual (an.FullName, an.ToString (), "#F:ToString"); Assert.AreEqual (new Version (1, 0, 0, 0), an.Version, "#F:Version"); Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "#F:VersionCompatibility"); an = new AssemblyName (assemblyName + ", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, Retargetable=nO"); Assert.IsNull (an.CodeBase, "G:CodeBase"); Assert.AreEqual (CultureInfo.InvariantCulture, an.CultureInfo, "#G:CultureInfo"); Assert.IsNull (an.EscapedCodeBase, "#G:EscapedCodeBase"); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#G:Flags"); Assert.AreEqual ("TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", an.FullName, "#G:FullName"); Assert.IsNull (an.GetPublicKey (), "#G:GetPublicKey"); Assert.AreEqual (new byte [0], an.GetPublicKeyToken (), "#G:GetPublicKeyToken"); Assert.AreEqual (AssemblyHashAlgorithm.None, an.HashAlgorithm, "#G:HashAlgorithm"); Assert.IsNull (an.KeyPair, "#G:KeyPair"); Assert.AreEqual (assemblyName, an.Name, "#G:Name"); Assert.AreEqual (ProcessorArchitecture.None, an.ProcessorArchitecture, "#G:PA"); Assert.AreEqual (an.FullName, an.ToString (), "#G:ToString"); Assert.AreEqual (new Version (1, 0, 0, 0), an.Version, "#G:Version"); Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "#G:VersionCompatibility"); an = new AssemblyName (assemblyName + ", Version=1.0.0.0, Culture=neutral, PublicKeyToken=" + GetTokenString (pk_token1) + ", Retargetable=yes"); Assert.IsNull (an.CodeBase, "H:CodeBase"); Assert.AreEqual (CultureInfo.InvariantCulture, an.CultureInfo, "#H:CultureInfo"); Assert.IsNull (an.EscapedCodeBase, "#H:EscapedCodeBase"); Assert.AreEqual (AssemblyNameFlags.Retargetable, an.Flags, "#H:Flags"); Assert.AreEqual ("TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=" + GetTokenString (pk_token1) + ", Retargetable=Yes", an.FullName, "#H:FullName"); Assert.IsNull (an.GetPublicKey (), "#H:GetPublicKey"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#H:GetPublicKeyToken"); Assert.AreEqual (AssemblyHashAlgorithm.None, an.HashAlgorithm, "#H:HashAlgorithm"); Assert.IsNull (an.KeyPair, "#H:KeyPair"); Assert.AreEqual (assemblyName, an.Name, "#H:Name"); Assert.AreEqual (ProcessorArchitecture.None, an.ProcessorArchitecture, "#H:PA"); Assert.AreEqual (an.FullName, an.ToString (), "#H:ToString"); Assert.AreEqual (new Version (1, 0, 0, 0), an.Version, "#H:Version"); Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "#H:VersionCompatibility"); }
/* internal static bool PublicKeyTokensEqual(String token1, byte[] token2) { if (token2 == null || token2.Length == 0) return token1 == "null"; if (token1 == "null") return false; Contract.Assert(token1.Length == 2 * token2.Length, "Lengths didn't match"); for (int i = 0; i < token2.Length; i++) { int firstPart = (token1[2 * i] <= '9') ? token1[2 * i] - '0' : token1[2 * i] - 'a' + 10; int secondPart = (token1[2 * i + 1] <= '9') ? token1[2 * i + 1] - '0' : token1[2 * i + 1] - 'a' + 10; byte b1 = (byte)((firstPart << 4) + secondPart); if (b1 != token2[i]) return false; } return true; } */ /* internal static bool PublicKeyTokensEqual(byte[] token1, byte[] token2) { if (token2 == null || token2.Length == 0) return token1 == null || token1.Length == 0; if (token1 == null) return false; System.Diagnostics.Contracts.Contract.Assert(token1.Length == token2.Length, "Lengths didn't match"); for (int i = 0; i < token1.Length; i++) if (token1[i] != token2[i]) return false; return true; } */ internal static bool PublicKeyMatches(AssemblyName a1, AssemblyName a2) { byte[] key = a2.GetPublicKey(); return PublicKeyMatches(a1, key); }
public String ApplyPolicy(String assemblyName) { AssemblyName asmName = new AssemblyName(assemblyName); byte[] pk = asmName.GetPublicKeyToken(); if (pk == null) pk = asmName.GetPublicKey(); // Simply-named assemblies cannot have policy, so for those, // we simply return the passed-in assembly name. if ((pk == null) || (pk.Length == 0)) return assemblyName; else return nApplyPolicy(asmName); }
internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access, bool corlib_internal) { /* This is obsolete now, as mcs doesn't use SRE any more */ if ((access & COMPILER_ACCESS) != 0) throw new NotImplementedException ("COMPILER_ACCESS is no longer supperted, use a newer mcs."); if (!Enum.IsDefined (typeof (AssemblyBuilderAccess), access)) throw new ArgumentException (string.Format (CultureInfo.InvariantCulture, "Argument value {0} is not valid.", (int) access), "access"); name = n.Name; this.access = (uint)access; flags = (uint) n.Flags; // don't call GetCurrentDirectory for Run-only builders (CAS may not like that) if (IsSave && (directory == null || directory.Length == 0)) { dir = Directory.GetCurrentDirectory (); } else { dir = directory; } /* Set defaults from n */ if (n.CultureInfo != null) { culture = n.CultureInfo.Name; versioninfo_culture = n.CultureInfo.Name; } Version v = n.Version; if (v != null) { version = v.ToString (); } if (n.KeyPair != null) { // full keypair is available (for signing) sn = n.KeyPair.StrongName (); } else { // public key is available (for delay-signing) byte[] pk = n.GetPublicKey (); if ((pk != null) && (pk.Length > 0)) { sn = new Mono.Security.StrongName (pk); } } if (sn != null) flags |= (uint) AssemblyNameFlags.PublicKey; this.corlib_internal = corlib_internal; if (sn != null) { this.pktoken = new byte[sn.PublicKeyToken.Length * 2]; int pkti = 0; foreach (byte pkb in sn.PublicKeyToken) { string part = pkb.ToString("x2"); this.pktoken[pkti++] = (byte)part[0]; this.pktoken[pkti++] = (byte)part[1]; } } basic_init (this); }
internal AssemblyLoader(TypeProvider typeProvider, string filePath) { this.isLocalAssembly = false; this.typeProvider = typeProvider; if (!File.Exists(filePath)) { throw new FileNotFoundException(); } System.Reflection.AssemblyName assemblyName = System.Reflection.AssemblyName.GetAssemblyName(filePath); if (assemblyName != null) { ITypeResolutionService service = (ITypeResolutionService)typeProvider.GetService(typeof(ITypeResolutionService)); if (service != null) { try { this.assembly = service.GetAssembly(assemblyName); if ((((this.assembly == null) && (assemblyName.GetPublicKeyToken() != null)) && ((assemblyName.GetPublicKeyToken().GetLength(0) == 0) && (assemblyName.GetPublicKey() != null))) && (assemblyName.GetPublicKey().GetLength(0) == 0)) { System.Reflection.AssemblyName name = (System.Reflection.AssemblyName)assemblyName.Clone(); name.SetPublicKey(null); name.SetPublicKeyToken(null); this.assembly = service.GetAssembly(name); } } catch { } } if (this.assembly == null) { try { if (MultiTargetingInfo.MultiTargetingUtilities.IsFrameworkReferenceAssembly(filePath)) { this.assembly = System.Reflection.Assembly.Load(assemblyName.FullName); } else { this.assembly = System.Reflection.Assembly.Load(assemblyName); } } catch { } } } if (this.assembly == null) { this.assembly = System.Reflection.Assembly.LoadFrom(filePath); } }
public void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa) { if (a.IsValidSecurityAttribute ()) { if (declarative_security == null) declarative_security = new Dictionary<SecurityAction, PermissionSet> (); a.ExtractSecurityPermissionSet (declarative_security); return; } if (a.Type == pa.AssemblyCulture) { string value = a.GetString (); if (value == null || value.Length == 0) return; if (RootContext.Target == Target.Exe) { a.Error_AttributeEmitError ("The executables cannot be satelite assemblies, remove the attribute or keep it empty"); return; } if (value == "neutral") value = ""; if (RootContext.Target == Target.Module) { SetCustomAttribute (ctor, cdata); } else { builder_extra.SetCulture (value, a.Location); } return; } if (a.Type == pa.AssemblyVersion) { string value = a.GetString (); if (value == null || value.Length == 0) return; var vinfo = IsValidAssemblyVersion (value.Replace ('*', '0')); if (vinfo == null) { a.Error_AttributeEmitError (string.Format ("Specified version `{0}' is not valid", value)); return; } if (RootContext.Target == Target.Module) { SetCustomAttribute (ctor, cdata); } else { builder_extra.SetVersion (vinfo, a.Location); } return; } if (a.Type == pa.AssemblyAlgorithmId) { const int pos = 2; // skip CA header uint alg = (uint) cdata [pos]; alg |= ((uint) cdata [pos + 1]) << 8; alg |= ((uint) cdata [pos + 2]) << 16; alg |= ((uint) cdata [pos + 3]) << 24; if (RootContext.Target == Target.Module) { SetCustomAttribute (ctor, cdata); } else { builder_extra.SetAlgorithmId (alg, a.Location); } return; } if (a.Type == pa.AssemblyFlags) { const int pos = 2; // skip CA header uint flags = (uint) cdata[pos]; flags |= ((uint) cdata [pos + 1]) << 8; flags |= ((uint) cdata [pos + 2]) << 16; flags |= ((uint) cdata [pos + 3]) << 24; // Ignore set PublicKey flag if assembly is not strongnamed if ((flags & (uint) AssemblyNameFlags.PublicKey) != 0 && public_key == null) flags &= ~(uint) AssemblyNameFlags.PublicKey; if (RootContext.Target == Target.Module) { SetCustomAttribute (ctor, cdata); } else { builder_extra.SetFlags (flags, a.Location); } return; } if (a.Type == pa.TypeForwarder) { TypeSpec t = a.GetArgumentType (); if (t == null || TypeManager.HasElementType (t)) { Report.Error (735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute"); return; } if (emitted_forwarders == null) { emitted_forwarders = new Dictionary<ITypeDefinition, Attribute> (); } else if (emitted_forwarders.ContainsKey (t.MemberDefinition)) { Report.SymbolRelatedToPreviousError (emitted_forwarders[t.MemberDefinition].Location, null); Report.Error (739, a.Location, "A duplicate type forward of type `{0}'", TypeManager.CSharpName (t)); return; } emitted_forwarders.Add (t.MemberDefinition, a); if (t.MemberDefinition.DeclaringAssembly == this) { Report.SymbolRelatedToPreviousError (t); Report.Error (729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly", TypeManager.CSharpName (t)); return; } if (t.IsNested) { Report.Error (730, a.Location, "Cannot forward type `{0}' because it is a nested type", TypeManager.CSharpName (t)); return; } builder_extra.AddTypeForwarder (t, a.Location); return; } if (a.Type == pa.Extension) { a.Error_MisusedExtensionAttribute (); return; } if (a.Type == pa.InternalsVisibleTo) { string assembly_name = a.GetString (); if (assembly_name.Length == 0) return; AssemblyName aname = null; try { aname = new AssemblyName (assembly_name); } catch (Exception) { Report.Warning (1700, 3, a.Location, "Assembly reference `{0}' is invalid and cannot be resolved", assembly_name); return; } if (aname.Version != null || aname.CultureInfo != null || aname.ProcessorArchitecture != ProcessorArchitecture.None) { Report.Error (1725, a.Location, "Friend assembly reference `{0}' is invalid. InternalsVisibleTo declarations cannot have a version, culture or processor architecture specified", assembly_name); return; } // TODO: GetPublicKey () does not work on .NET when AssemblyName is constructed from a string if (public_key != null && aname.GetPublicKey () == null) { Report.Error (1726, a.Location, "Friend assembly reference `{0}' is invalid. Strong named assemblies must specify a public key in their InternalsVisibleTo declarations", assembly_name); return; } } else if (a.Type == pa.RuntimeCompatibility) { wrap_non_exception_throws_custom = true; } SetCustomAttribute (ctor, cdata); }
public void Self () { Assembly a = Assembly.GetExecutingAssembly (); an = a.GetName (); Assert.AreEqual (CultureInfo.InvariantCulture, an.CultureInfo, "CultureInfo"); Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "Flags"); Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm"); Assert.IsNull (an.KeyPair, "KeyPair"); Assert.IsNotNull (an.Name, "Name"); #if NET_2_0 //Assert.AreEqual (ProcessorArchitecture.MSIL, an.ProcessorArchitecture, "PA"); #endif Assert.AreEqual (new Version (0, 0, 0, 0), an.Version, "Version"); Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "VersionCompatibility"); Assert.AreEqual (new byte [0], an.GetPublicKey (), "GetPublicKey"); Assert.AreEqual (an.FullName, an.ToString (), "ToString"); }
public void KeyPair () { an = new AssemblyName (); an.Name = "test"; an.KeyPair = new StrongNameKeyPair (keyPair); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#A1"); Assert.IsNotNull (an.KeyPair, "#A2"); Assert.IsNull (an.GetPublicKey (), "#A3"); Assert.IsNull (an.GetPublicKeyToken (), "#A4"); Assert.AreEqual ("test", an.FullName, "#A5"); an.KeyPair = null; Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#B1"); Assert.IsNull (an.KeyPair, "#B2"); Assert.IsNull (an.GetPublicKey (), "#B3"); Assert.IsNull (an.GetPublicKeyToken (), "#B4"); Assert.AreEqual ("test", an.FullName, "#B5"); }
public void SetPublicKeyToken () { an = new AssemblyName (); an.SetPublicKeyToken (pk_token1); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#A1"); Assert.IsNull (an.KeyPair, "#A2"); Assert.IsNull (an.GetPublicKey (), "#A3"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#A4"); an.SetPublicKeyToken ((byte []) null); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#B1"); Assert.IsNull (an.KeyPair, "#B2"); Assert.IsNull (an.GetPublicKey (), "#B3"); Assert.IsNull (an.GetPublicKeyToken (), "#B4"); an.SetPublicKeyToken (new byte [0]); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#C1"); Assert.IsNull (an.KeyPair, "#C2"); Assert.IsNull (an.GetPublicKey (), "#C3"); Assert.IsNotNull (an.GetPublicKeyToken (), "#C4"); Assert.AreEqual (0, an.GetPublicKeyToken ().Length, "#C5"); }
public void SetPublicKey_PublicKey_Invalid () { an = new AssemblyName (); an.SetPublicKey (new byte [] { 0x0b, 0x0a }); Assert.AreEqual (new byte [] { 0x0b, 0x0a }, an.GetPublicKey (), "#1"); }
public void SetPublicKey () { an = new AssemblyName (); Assert.IsNull (an.GetPublicKey (), "#A1"); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#A2"); Assert.IsNull (an.KeyPair, "#A3"); Assert.IsNull (an.GetPublicKeyToken (), "#A4"); an.SetPublicKey (publicKey1); Assert.AreEqual (publicKey1, an.GetPublicKey (), "#B1"); Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#B2"); Assert.IsNull (an.KeyPair, "#B3"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#B4"); an.SetPublicKey (keyPair); Assert.AreEqual (keyPair, an.GetPublicKey (), "#B5"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#B6"); an.SetPublicKey ((byte []) null); Assert.IsNull (an.GetPublicKey (), "#C1"); #if NET_2_0 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#C2"); #else Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#C2"); #endif Assert.IsNull (an.KeyPair, "#C3"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#C4"); an.SetPublicKey (publicKey1); an.SetPublicKeyToken (pk_token1); an.SetPublicKey ((byte []) null); Assert.IsNull (an.GetPublicKey (), "#D1"); #if NET_2_0 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#D2"); #else Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#D2"); #endif Assert.IsNull (an.KeyPair, "#D3"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#D4"); an.SetPublicKey ((byte []) null); an.SetPublicKeyToken (pk_token1); an.SetPublicKey ((byte []) null); Assert.IsNull (an.GetPublicKey (), "#E1"); #if NET_2_0 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#E2"); #else Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#E2"); #endif Assert.IsNull (an.KeyPair, "#E3"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#E4"); an = new AssemblyName (); an.SetPublicKey (publicKey1); an.SetPublicKey ((byte []) null); an.SetPublicKeyToken (pk_token1); an.SetPublicKey ((byte []) null); Assert.IsNull (an.GetPublicKey (), "#F1"); Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#F2"); Assert.IsNull (an.KeyPair, "#F3"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#F4"); an = new AssemblyName (); an.SetPublicKey (publicKey1); an.SetPublicKey ((byte []) null); an.SetPublicKeyToken (pk_token1); Assert.IsNull (an.GetPublicKey (), "#G1"); #if NET_2_0 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#G2"); #else Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#G2"); #endif Assert.IsNull (an.KeyPair, "#G3"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#G4"); an = new AssemblyName (); an.SetPublicKey (new byte [0]); Assert.IsNotNull (an.GetPublicKey (), "#H1"); Assert.AreEqual (0, an.GetPublicKey ().Length, "#H2"); Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#H3"); Assert.IsNull (an.KeyPair, "#H4"); #if NET_2_0 Assert.IsNotNull (an.GetPublicKeyToken (), "#H5"); Assert.AreEqual (0, an.GetPublicKeyToken ().Length, "#H6"); #else Assert.IsNull (an.GetPublicKeyToken (), "#H5"); #endif an = new AssemblyName (); an.SetPublicKey (publicKey1); Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#I1"); an.SetPublicKey (publicKey1); Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#I2"); an = new AssemblyName (); an.SetPublicKey ((byte []) null); Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#J1"); an.SetPublicKey ((byte []) null); #if NET_2_0 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#J2"); #else Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#J2"); #endif an.SetPublicKey ((byte []) null); Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#J3"); an.SetPublicKey (publicKey1); Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#J4"); Assert.AreEqual (publicKey1, an.GetPublicKey (), "#J5"); an.SetPublicKey (publicKey2); Assert.AreEqual (publicKey2, an.GetPublicKey (), "#J6"); }
[Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM. public void Constructor1_Version () { const string assemblyName = "TestAssembly"; const string assemblyVersion = "1.2.3.4"; an = new AssemblyName (assemblyName + ", Version=" + assemblyVersion); Assert.IsNull (an.CodeBase, "CodeBase"); Assert.IsNull (an.CultureInfo, "CultureInfo"); Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase"); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags"); Assert.AreEqual ("TestAssembly, Version=1.2.3.4", an.FullName, "FullName"); Assert.AreEqual (AssemblyHashAlgorithm.None, an.HashAlgorithm, "HashAlgorithm"); Assert.IsNull (an.KeyPair, "KeyPair"); Assert.AreEqual (assemblyName, an.Name, "Name"); Assert.AreEqual (ProcessorArchitecture.None, an.ProcessorArchitecture, "PA"); Assert.AreEqual (new Version (assemblyVersion), an.Version, "Version"); Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "VersionCompatibility"); Assert.IsNull (an.GetPublicKey (), "GetPublicKey"); Assert.IsNull (an.GetPublicKeyToken (), "GetPublicKeyToken"); Assert.AreEqual (an.FullName, an.ToString (), "ToString"); }
[Test] // ctor () public void Constructor0 () { an = new AssemblyName (); Assert.IsNull (an.CodeBase, "CodeBase"); Assert.IsNull (an.CultureInfo, "CultureInfo"); Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase"); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags"); #if NET_2_0 Assert.AreEqual (String.Empty, an.FullName, "FullName"); #else Assert.IsNull (an.FullName, "FullName"); #endif Assert.AreEqual (AssemblyHashAlgorithm.None, an.HashAlgorithm, "HashAlgorithm"); Assert.IsNull (an.KeyPair, "KeyPair"); Assert.IsNull (an.Name, "Name"); #if NET_2_0 Assert.AreEqual (ProcessorArchitecture.None, an.ProcessorArchitecture, "PA"); #endif Assert.IsNull (an.Version, "Version"); Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "VersionCompatibility"); Assert.IsNull (an.GetPublicKey (), "GetPublicKey"); Assert.IsNull (an.GetPublicKeyToken (), "GetPublicKeyToken"); #if NET_2_0 Assert.AreEqual (string.Empty, an.ToString (), "ToString"); #else Assert.AreEqual (typeof (AssemblyName).FullName, an.ToString (), "ToString"); #endif }
public void Serialization_WithoutStrongName () { an = new AssemblyName (); an.CodeBase = "http://www.test.com/test.dll"; an.CultureInfo = CultureInfo.InvariantCulture; an.Flags = AssemblyNameFlags.None; an.HashAlgorithm = AssemblyHashAlgorithm.SHA1; an.KeyPair = null; an.Name = "TestAssembly2"; an.Version = new Version (1, 5, 0, 0); an.VersionCompatibility = AssemblyVersionCompatibility.SameMachine; MemoryStream ms = new MemoryStream (); BinaryFormatter bf = new BinaryFormatter (); bf.Serialize (ms, an); // reset position of memorystream ms.Position = 0; // deserialze assembly name AssemblyName dsAssemblyName = (AssemblyName) bf.Deserialize (ms); // close the memorystream ms.Close (); // compare orginal and deserialized assembly name Assert.AreEqual (an.CodeBase, dsAssemblyName.CodeBase, "CodeBase"); Assert.AreEqual (an.CultureInfo, dsAssemblyName.CultureInfo, "CultureInfo"); Assert.AreEqual (an.Flags, dsAssemblyName.Flags, "Flags"); Assert.AreEqual (an.HashAlgorithm, dsAssemblyName.HashAlgorithm, "HashAlgorithm"); Assert.AreEqual (an.Name, dsAssemblyName.Name, "Name"); Assert.AreEqual (an.Version, dsAssemblyName.Version, "Version"); Assert.AreEqual (an.VersionCompatibility, dsAssemblyName.VersionCompatibility, "VersionCompatibility"); Assert.AreEqual (an.EscapedCodeBase, dsAssemblyName.EscapedCodeBase, "EscapedCodeBase"); Assert.AreEqual (an.FullName, dsAssemblyName.FullName, "FullName"); Assert.AreEqual (an.ToString (), dsAssemblyName.ToString (), "ToString"); Assert.AreEqual (an.GetPublicKey (), dsAssemblyName.GetPublicKey (), "PublicKey"); Assert.AreEqual (an.GetPublicKeyToken (), dsAssemblyName.GetPublicKeyToken (), "PublicToken"); }
public string ApplyPolicy(string assemblyName) { AssemblyName assemblyName2 = new AssemblyName(assemblyName); byte[] array = assemblyName2.GetPublicKeyToken(); if (array == null) { array = assemblyName2.GetPublicKey(); } if (array == null || array.Length == 0) { return assemblyName; } return this.nApplyPolicy(assemblyName2); }
[Category ("NotWorking")] // bug #351708 public void Constructor1_ProcessorArchitecture () { const string assemblyName = "TestAssembly"; an = new AssemblyName (assemblyName + ", ProcessorArchitecture=X86"); Assert.IsNull (an.CodeBase, "CodeBase"); Assert.IsNull (an.CultureInfo, "CultureInfo"); Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase"); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags"); Assert.AreEqual ("TestAssembly", an.FullName, "FullName"); Assert.AreEqual (AssemblyHashAlgorithm.None, an.HashAlgorithm, "HashAlgorithm"); Assert.IsNull (an.KeyPair, "KeyPair"); Assert.AreEqual (assemblyName, an.Name, "Name"); Assert.AreEqual (ProcessorArchitecture.X86, an.ProcessorArchitecture, "PA"); Assert.IsNull (an.Version, "Version"); Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "VersionCompatibility"); Assert.IsNull (an.GetPublicKey (), "GetPublicKey"); Assert.IsNull (an.GetPublicKeyToken (), "GetPublicKeyToken"); Assert.AreEqual ("TestAssembly", an.ToString (), "ToString"); an = new AssemblyName (assemblyName + ", ProcessorArchitecture=mSiL"); Assert.AreEqual (ProcessorArchitecture.MSIL, an.ProcessorArchitecture, "PA: MSIL"); an = new AssemblyName (assemblyName + ", ProcessorArchitecture=AmD64"); Assert.AreEqual (ProcessorArchitecture.Amd64, an.ProcessorArchitecture, "PA: Amd64"); an = new AssemblyName (assemblyName + ", ProcessorArchitecture=iA64"); Assert.AreEqual (ProcessorArchitecture.IA64, an.ProcessorArchitecture, "PA: IA64"); }
public string ApplyPolicy(string assemblyName) { AssemblyName an = new AssemblyName(assemblyName); byte[] publicKeyToken = an.GetPublicKeyToken(); if (publicKeyToken == null) { publicKeyToken = an.GetPublicKey(); } if ((publicKeyToken != null) && (publicKeyToken.Length != 0)) { return this.nApplyPolicy(an); } return assemblyName; }
internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access, bool corlib_internal) { is_compiler_context = (access & COMPILER_ACCESS) != 0; // remove Mono specific flag to allow enum check to pass access &= ~COMPILER_ACCESS; #if MOONLIGHT // only "Run" is supported by Silverlight // however SMCS requires more than this but runs outside the CoreCLR sandbox if (SecurityManager.SecurityEnabled && (access != AssemblyBuilderAccess.Run)) throw new ArgumentException ("access"); #endif if (!Enum.IsDefined (typeof (AssemblyBuilderAccess), access)) throw new ArgumentException (string.Format (CultureInfo.InvariantCulture, "Argument value {0} is not valid.", (int) access), "access"); name = n.Name; this.access = (uint)access; flags = (uint) n.Flags; // don't call GetCurrentDirectory for Run-only builders (CAS may not like that) if (IsSave && (directory == null || directory.Length == 0)) { dir = Directory.GetCurrentDirectory (); } else { dir = directory; } /* Set defaults from n */ if (n.CultureInfo != null) { culture = n.CultureInfo.Name; versioninfo_culture = n.CultureInfo.Name; } Version v = n.Version; if (v != null) { version = v.ToString (); } if (n.KeyPair != null) { // full keypair is available (for signing) sn = n.KeyPair.StrongName (); } else { // public key is available (for delay-signing) byte[] pk = n.GetPublicKey (); if ((pk != null) && (pk.Length > 0)) { sn = new Mono.Security.StrongName (pk); } } if (sn != null) flags |= (uint) AssemblyNameFlags.PublicKey; this.corlib_internal = corlib_internal; if (sn != null) { this.pktoken = new byte[sn.PublicKeyToken.Length * 2]; int pkti = 0; foreach (byte pkb in sn.PublicKeyToken) { string part = pkb.ToString("x2"); this.pktoken[pkti++] = (byte)part[0]; this.pktoken[pkti++] = (byte)part[1]; } } basic_init (this); }
private static bool IsSimplyNamed(AssemblyName partialName) { byte[] pk = partialName.GetPublicKeyToken(); if ((pk != null) && (pk.Length == 0)) return true; pk = partialName.GetPublicKey(); if ((pk != null) && (pk.Length == 0)) return true; return false; }
internal byte[] GetPublicKey() { AssemblyName aname = GetName(true); return(aname.GetPublicKey()); }
// modified copy from sn private static VerificationResult VerifyStrongName (AssemblyName an, string assemblyFile) { byte [] publicKey = StrongNameManager.GetMappedPublicKey (an.GetPublicKeyToken ()); if ((publicKey == null) || (publicKey.Length < 12)) { // no mapping publicKey = an.GetPublicKey (); if ((publicKey == null) || (publicKey.Length < 12)) return VerificationResult.WeakNamed; } // Note: MustVerify is based on the original token (by design). Public key // remapping won't affect if the assembly is verified or not. if (StrongNameManager.MustVerify (an)) { RSA rsa = CryptoConvert.FromCapiPublicKeyBlob (publicKey, 12); StrongName sn = new StrongName (rsa); if (sn.Verify (assemblyFile)) { return VerificationResult.StrongNamed; } else { return VerificationResult.DelaySigned; } } else { return VerificationResult.Skipped; } }
/// <summary> /// Constructor /// </summary> /// <param name="asmName">Assembly name info</param> public AssemblyNameInfo(AssemblyName asmName) { if (asmName == null) return; this.hashAlgId = (AssemblyHashAlgorithm)asmName.HashAlgorithm; this.version = asmName.Version ?? new Version(0, 0, 0, 0); this.flags = (AssemblyAttributes)asmName.Flags; this.publicKeyOrToken = (PublicKeyBase)PublicKeyBase.CreatePublicKey(asmName.GetPublicKey()) ?? PublicKeyBase.CreatePublicKeyToken(asmName.GetPublicKeyToken()); this.name = asmName.Name ?? string.Empty; this.culture = asmName.CultureInfo != null && asmName.CultureInfo.Name != null ? asmName.CultureInfo.Name : string.Empty; }
public void Case4_SetPublicKey() { AssemblyName n = new AssemblyName(); n.SetPublicKey(null); Assert.Null(n.GetPublicKey()); }
public void Clone_Self () { an = Assembly.GetExecutingAssembly ().GetName (); AssemblyName clone = (AssemblyName) an.Clone (); Assert.AreEqual (an.CodeBase, clone.CodeBase, "CodeBase"); Assert.AreEqual (an.CultureInfo, clone.CultureInfo, "CultureInfo"); Assert.AreEqual (an.EscapedCodeBase, clone.EscapedCodeBase, "EscapedCodeBase"); Assert.AreEqual (an.Flags, clone.Flags, "Flags"); Assert.AreEqual (an.FullName, clone.FullName, "FullName"); Assert.AreEqual (an.HashAlgorithm, clone.HashAlgorithm, "HashAlgorithm"); Assert.AreEqual (an.KeyPair, clone.KeyPair, "KeyPair"); Assert.AreEqual (an.Name, clone.Name, "Name"); #if NET_2_0 //Assert.AreEqual (ProcessorArchitecture.MSIL, clone.ProcessorArchitecture, "PA"); #endif Assert.AreEqual (an.Version, clone.Version, "Version"); Assert.AreEqual (an.VersionCompatibility, clone.VersionCompatibility, "VersionCompatibility"); Assert.AreEqual (an.GetPublicKey (), clone.GetPublicKey (), "GetPublicKey"); Assert.AreEqual (an.GetPublicKeyToken (), clone.GetPublicKeyToken (), "GetPublicKeyToken"); Assert.AreEqual (an.ToString (), clone.ToString (), "ToString"); }
private static bool IsSimplyNamed(AssemblyName partialName) { byte[] publicKeyToken = partialName.GetPublicKeyToken(); if ((publicKeyToken != null) && (publicKeyToken.Length == 0)) { return true; } publicKeyToken = partialName.GetPublicKey(); return ((publicKeyToken != null) && (publicKeyToken.Length == 0)); }
[Test] // ctor (String) public void Constructor1_PublicKeyToken () { const string assemblyName = "TestAssembly"; an = new AssemblyName (assemblyName + ", PublicKeyToken=" + GetTokenString (pk_token1)); Assert.IsNull (an.CodeBase, "#A:CodeBase"); Assert.IsNull (an.CultureInfo, "#A:CultureInfo"); Assert.IsNull (an.EscapedCodeBase, "#A:EscapedCodeBase"); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#A:Flags"); Assert.AreEqual ("TestAssembly, PublicKeyToken=" + GetTokenString (pk_token1), an.FullName, "#A:FullName"); Assert.AreEqual (AssemblyHashAlgorithm.None, an.HashAlgorithm, "#A:HashAlgorithm"); Assert.IsNull (an.KeyPair, "#A:KeyPair"); Assert.AreEqual (assemblyName, an.Name, "#A:Name"); Assert.AreEqual (ProcessorArchitecture.None, an.ProcessorArchitecture, "#A:PA"); Assert.IsNull (an.Version, "#A:Version"); Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "#A:VersionCompatibility"); Assert.IsNull (an.GetPublicKey (), "#A:GetPublicKey"); Assert.AreEqual (pk_token1, an.GetPublicKeyToken (), "#A:GetPublicKeyToken"); Assert.AreEqual (an.FullName, an.ToString (), "#A:ToString"); an = new AssemblyName (assemblyName + ", PublicKeyToken=null"); Assert.IsNull (an.CodeBase, "#B:CodeBase"); Assert.IsNull (an.CultureInfo, "#B:CultureInfo"); Assert.IsNull (an.EscapedCodeBase, "#B:EscapedCodeBase"); Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "#B:Flags"); //Assert.AreEqual ("TestAssembly, PublicKeyToken=null", an.FullName, "#B:FullName"); Assert.AreEqual (AssemblyHashAlgorithm.None, an.HashAlgorithm, "#B:HashAlgorithm"); Assert.IsNull (an.KeyPair, "#B:KeyPair"); Assert.AreEqual (assemblyName, an.Name, "#B:Name"); Assert.AreEqual (ProcessorArchitecture.None, an.ProcessorArchitecture, "#B:PA"); Assert.IsNull (an.Version, "#B:Version"); Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "#B:VersionCompatibility"); Assert.IsNull (an.GetPublicKey (), "#B:GetPublicKey"); Assert.AreEqual (new byte [0], an.GetPublicKeyToken (), "#B:GetPublicKeyToken"); Assert.AreEqual (an.FullName, an.ToString (), "#B:ToString"); }