public void TestAssemblyPatialMatchSimpleName() { AssemblyNameExtension assemblyNameToMatch = new AssemblyNameExtension("System.Xml"); AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xmla"); foreach (string assembly in s_assembliesForPartialMatch) { AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly); Assert.True(assemblyNameToMatch.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyNameToMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName)); } }
public bool FrameworkAssemblyEntryInRedist(AssemblyNameExtension assemblyName) { Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(assemblyName, "assemblyName"); NGen <bool> gen = 0; if (!this.assemblyNameInRedist.TryGetValue(assemblyName, out gen)) { string simpleName = GetSimpleName(assemblyName.Name); if (this.simpleNameMap.ContainsKey(simpleName)) { int num = (int)this.simpleNameMap[simpleName]; for (int i = num; i < this.assemblyList.Count; i++) { AssemblyEntry entry = this.assemblyList[i]; if (!string.Equals(simpleName, entry.SimpleName, StringComparison.OrdinalIgnoreCase)) { break; } if (entry.RedistName.StartsWith("Microsoft-Windows-CLRCoreComp", StringComparison.OrdinalIgnoreCase)) { AssemblyNameExtension extension = assemblyName; AssemblyNameExtension assemblyNameExtension = entry.AssemblyNameExtension; if (extension.PartialNameCompare(assemblyNameExtension, PartialComparisonFlags.PublicKeyToken | PartialComparisonFlags.Culture | PartialComparisonFlags.SimpleName)) { gen = 1; break; } } } } this.assemblyNameInRedist.Add(assemblyName, gen); } return((bool)gen); }
/// <summary> /// If the version is not set for an assembly reference, go through the redist list and find the highest version for that assembly. /// Make sure when matching the assembly in the redist that we take into account the publicKeyToken and the Culture. /// </summary> internal static AssemblyNameExtension GetHighestVersionInRedist(InstalledAssemblies installedAssemblies, AssemblyNameExtension assemblyName) { AssemblyNameExtension assemblyNameToUse = assemblyName; if ((assemblyNameToUse.Version == null && installedAssemblies != null)) { // If there are multiple entries in the redist list for this assembly, let's // pick the one with the highest version and resolve it. foreach (AssemblyEntry a in installedAssemblies.FindAssemblyNameFromSimpleName(assemblyName.Name)) { var current = new AssemblyNameExtension(a.FullName); // If the current version is higher than the previously looked at. if (current.Version != null && current.Version.CompareTo(assemblyNameToUse.Version) > 0) { // Only compare the Culture and the public key token, the simple names will ALWAYS be the same and the version we do not care about. if (assemblyName.PartialNameCompare(current, PartialComparisonFlags.Culture | PartialComparisonFlags.PublicKeyToken)) { assemblyNameToUse = current; } } } } return(assemblyNameToUse); }
public void CreateAssemblyNameExtensionWithNoSimpleName2() { AssemblyNameExtension extension = new AssemblyNameExtension("Version=2.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a"); AssemblyNameExtension extension2 = new AssemblyNameExtension("A, Version=2.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a"); extension2.PartialNameCompare(extension); }
public void TestAssemblyPartialMatchSimpleNameRetargetable() { AssemblyNameExtension assemblyNameToMatchRetargetable = new AssemblyNameExtension("System.Xml, Version=10.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a, Retargetable=Yes"); AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xml, Version=10.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a, Retargetable=No"); AssemblyNameExtension assemblyMatchNoRetargetable = new AssemblyNameExtension("System.Xml"); foreach (string assembly in s_assembliesForPartialMatch) { AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly); if (assemblyToCompare.FullName.IndexOf("Retargetable=Yes", StringComparison.OrdinalIgnoreCase) >= 0) { Assert.True(assemblyNameToMatchRetargetable.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyNameToMatchRetargetable.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true)); Assert.True(assemblyToCompare.PartialNameCompare(assemblyNameToNotMatch)); Assert.False(assemblyToCompare.PartialNameCompare(assemblyNameToNotMatch, PartialComparisonFlags.SimpleName, true)); Assert.False(assemblyToCompare.PartialNameCompare(assemblyMatchNoRetargetable)); Assert.False(assemblyToCompare.PartialNameCompare(assemblyMatchNoRetargetable, PartialComparisonFlags.SimpleName, true)); Assert.True(assemblyMatchNoRetargetable.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyMatchNoRetargetable.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true)); } else { Assert.False(assemblyNameToMatchRetargetable.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true)); // Match because retargetable false is the same as no retargetable bit bool match = assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare); if (assemblyToCompare.FullName.IndexOf("System.Xml, Version=10.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a", StringComparison.OrdinalIgnoreCase) >= 0) { Assert.True(match); } else { Assert.False(match); } Assert.True(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true)); Assert.True(assemblyMatchNoRetargetable.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyMatchNoRetargetable.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true)); } } }
public void TestAssemblyPatialMatchSimpleNamePublicKeyToken() { AssemblyNameExtension assemblyNameToMatchPublicToken = new AssemblyNameExtension("System.Xml, PublicKeyToken=b03f5f7f11d50a3a"); AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xml, PublicKeyToken=b03f5f7f11d50a3b"); AssemblyNameExtension assemblyMatchNoVersion = new AssemblyNameExtension("System.Xml"); foreach (string assembly in s_assembliesForPartialMatch) { AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly); // If there is a version make sure the assembly name with the correct publicKeyToken matches // Make sure the assembly with the wrong publicKeyToken does not match if (assemblyToCompare.GetPublicKeyToken() != null) { Assert.True(assemblyNameToMatchPublicToken.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyNameToMatchPublicToken.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken)); // Matches because publicKeyToken is not specified Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken)); } else { // If there is no version make names with a publicKeyToken specified do not match Assert.False(assemblyNameToMatchPublicToken.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToMatchPublicToken.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken)); // Matches because publicKeyToken is not specified Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken)); } } }
public void TestAssemblyPatialMatchSimpleNameCulture() { AssemblyNameExtension assemblyNameToMatchCulture = new AssemblyNameExtension("System.Xml, Culture=en"); AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xml, Culture=de-DE"); AssemblyNameExtension assemblyMatchNoVersion = new AssemblyNameExtension("System.Xml"); foreach (string assembly in s_assembliesForPartialMatch) { AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly); // If there is a version make sure the assembly name with the correct culture matches // Make sure the assembly with the wrong culture does not match if (assemblyToCompare.CultureInfo != null) { Assert.True(assemblyNameToMatchCulture.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyNameToMatchCulture.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture)); // Matches because culture is not specified Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture)); } else { // If there is no version make names with a culture specified do not match Assert.False(assemblyNameToMatchCulture.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToMatchCulture.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture)); // Matches because culture is not specified Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture)); } } }
public void TestAssemblyPatialMatchSimpleNameVersion() { AssemblyNameExtension assemblyNameToMatchVersion = new AssemblyNameExtension("System.Xml, Version=10.0.0.0"); AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xml, Version=5.0.0.0"); AssemblyNameExtension assemblyMatchNoVersion = new AssemblyNameExtension("System.Xml"); foreach (string assembly in s_assembliesForPartialMatch) { AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly); // If there is a version make sure the assembly name with the correct version matches // Make sure the assembly with the wrong version does not match if (assemblyToCompare.Version != null) { Assert.True(assemblyNameToMatchVersion.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyNameToMatchVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version)); // Matches because version is not specified Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version)); } else { // If there is no version make names with a version specified do not match Assert.False(assemblyNameToMatchVersion.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToMatchVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare)); Assert.False(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version)); // Matches because version is not specified Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare)); Assert.True(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version)); } } }
public void CreateAssemblyNameExtensionWithNoSimpleName2() { // Mono does not throw on this string if (!NativeMethodsShared.IsMono) { Assert.Throws <FileLoadException>(() => { AssemblyNameExtension extension = new AssemblyNameExtension("Version=2.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a"); AssemblyNameExtension extension2 = new AssemblyNameExtension("A, Version=2.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a"); extension2.PartialNameCompare(extension); } ); } }
/// <summary> /// Determines whether an assembly name matches the assembly pointed to by pathToCandidateAssembly /// </summary> /// <param name="assemblyName">The assembly name to look up.</param> /// <param name="isPrimaryProjectReference">True if this is a primary reference directly from the project file.</param> /// <param name="wantSpecificVersion">Whether the version needs to match exactly or loosely.</param> /// <param name="pathToCandidateAssembly">Path to a possible file.</param> /// <param name="searchLocation">Information about why the candidate file didn't match</param> /// <param name="fileExists">Delegate for File.Exists.</param> /// <param name="getAssemblyName">Delegate for AssemblyName.GetAssemblyName</param> /// <returns></returns> protected bool FileMatchesAssemblyName ( AssemblyNameExtension assemblyName, bool isPrimaryProjectReference, bool wantSpecificVersion, bool allowMismatchBetweenFusionNameAndFileName, string pathToCandidateAssembly, ResolutionSearchLocation searchLocation ) { searchLocation.FileNameAttempted = pathToCandidateAssembly; // Base name of the target file has to match the Name from the assemblyName if (!allowMismatchBetweenFusionNameAndFileName) { string candidateBaseName = Path.GetFileNameWithoutExtension(pathToCandidateAssembly); if (String.Compare(assemblyName.Name, candidateBaseName, StringComparison.CurrentCultureIgnoreCase) != 0) { if (searchLocation != null) { if (candidateBaseName.Length > 0) { searchLocation.AssemblyName = new AssemblyNameExtension(candidateBaseName); searchLocation.Reason = NoMatchReason.FusionNamesDidNotMatch; } else { searchLocation.Reason = NoMatchReason.TargetHadNoFusionName; } } return(false); } } bool isSimpleAssemblyName = assemblyName == null ? false : assemblyName.IsSimpleName; if (fileExists(pathToCandidateAssembly)) { // If the resolver we are using is targeting a given processor architecture then we must crack open the assembly and make sure the architecture is compatible // We cannot do these simple name matches. if (!compareProcessorArchitecture) { // If the file existed and the reference is a simple primary reference which does not contain an assembly name (say a raw file name) // then consider this a match. if (assemblyName == null && isPrimaryProjectReference && !wantSpecificVersion) { return(true); } if (isPrimaryProjectReference && !wantSpecificVersion && isSimpleAssemblyName) { return(true); } } // We have strong name information, so do some added verification here. AssemblyNameExtension targetAssemblyName = null; try { targetAssemblyName = getAssemblyName(pathToCandidateAssembly); } catch (System.IO.FileLoadException) { // Its pretty hard to get here, you need an assembly that contains a valid reference // to a dependent assembly that, in turn, throws a FileLoadException during GetAssemblyName. // Still it happened once, with an older version of the CLR. // ...falling through and relying on the targetAssemblyName==null behavior below... } if (searchLocation != null) { searchLocation.AssemblyName = targetAssemblyName; } // targetAssemblyName may be null if there was no metadata for this assembly. // In this case, there's no match. if (targetAssemblyName != null) { // If we are targeting a given processor architecture check to see if they match, if we are targeting MSIL then any architecture will do. if (compareProcessorArchitecture) { // Only reject the assembly if the target processor architecture does not match the assemby processor architecture and the assembly processor architecture is not NONE or MSIL. if ( targetAssemblyName.AssemblyName.ProcessorArchitecture != targetProcessorArchitecture && /* The target and assembly architectures do not match*/ (targetProcessorArchitecture != ProcessorArchitecture.None && targetAssemblyName.AssemblyName.ProcessorArchitecture != ProcessorArchitecture.None) && /*The assembly is not none*/ (targetProcessorArchitecture != ProcessorArchitecture.MSIL && targetAssemblyName.AssemblyName.ProcessorArchitecture != ProcessorArchitecture.MSIL) /*The assembly is not MSIL*/ ) { searchLocation.Reason = NoMatchReason.ProcessorArchitectureDoesNotMatch; return(false); } } bool matchedSpecificVersion = (wantSpecificVersion && assemblyName != null && assemblyName.Equals(targetAssemblyName)); bool matchPartialName = !wantSpecificVersion && assemblyName != null && assemblyName.PartialNameCompare(targetAssemblyName); if (matchedSpecificVersion || matchPartialName) { return(true); } else { // Reason was: FusionNames did not match. if (searchLocation != null) { searchLocation.Reason = NoMatchReason.FusionNamesDidNotMatch; } } } else { // Reason was: Target had no fusion name. if (searchLocation != null) { searchLocation.Reason = NoMatchReason.TargetHadNoFusionName; } } } else { // Reason was: No file found at that location. if (searchLocation != null) { searchLocation.Reason = NoMatchReason.FileNotFound; } } return(false); }
protected bool FileMatchesAssemblyName(AssemblyNameExtension assemblyName, bool isPrimaryProjectReference, bool wantSpecificVersion, bool allowMismatchBetweenFusionNameAndFileName, string pathToCandidateAssembly, ResolutionSearchLocation searchLocation) { searchLocation.FileNameAttempted = pathToCandidateAssembly; if (!allowMismatchBetweenFusionNameAndFileName) { string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathToCandidateAssembly); if (string.Compare(assemblyName.Name, fileNameWithoutExtension, StringComparison.CurrentCultureIgnoreCase) != 0) { if (searchLocation != null) { if (fileNameWithoutExtension.Length > 0) { searchLocation.AssemblyName = new AssemblyNameExtension(fileNameWithoutExtension); searchLocation.Reason = NoMatchReason.FusionNamesDidNotMatch; } else { searchLocation.Reason = NoMatchReason.TargetHadNoFusionName; } } return(false); } } bool flag = (assemblyName != null) && assemblyName.IsSimpleName; if (this.fileExists(pathToCandidateAssembly)) { if (!this.compareProcessorArchitecture) { if (((assemblyName == null) && isPrimaryProjectReference) && !wantSpecificVersion) { return(true); } if ((isPrimaryProjectReference && !wantSpecificVersion) && flag) { return(true); } } AssemblyNameExtension that = null; try { that = this.getAssemblyName(pathToCandidateAssembly); } catch (FileLoadException) { } if (searchLocation != null) { searchLocation.AssemblyName = that; } if (that != null) { if (((this.compareProcessorArchitecture && (that.AssemblyName.ProcessorArchitecture != this.targetProcessorArchitecture)) && ((this.targetProcessorArchitecture != ProcessorArchitecture.None) && (that.AssemblyName.ProcessorArchitecture != ProcessorArchitecture.None))) && ((this.targetProcessorArchitecture != ProcessorArchitecture.MSIL) && (that.AssemblyName.ProcessorArchitecture != ProcessorArchitecture.MSIL))) { searchLocation.Reason = NoMatchReason.ProcessorArchitectureDoesNotMatch; return(false); } bool flag2 = (wantSpecificVersion && (assemblyName != null)) && assemblyName.Equals(that); bool flag3 = (!wantSpecificVersion && (assemblyName != null)) && assemblyName.PartialNameCompare(that); if (flag2 || flag3) { return(true); } if (searchLocation != null) { searchLocation.Reason = NoMatchReason.FusionNamesDidNotMatch; } } else if (searchLocation != null) { searchLocation.Reason = NoMatchReason.TargetHadNoFusionName; } } else if (searchLocation != null) { searchLocation.Reason = NoMatchReason.FileNotFound; } return(false); }
internal static AssemblyNameExtension GetHighestVersionInRedist(InstalledAssemblies installedAssemblies, AssemblyNameExtension assemblyName) { AssemblyNameExtension extension = assemblyName; if ((extension.Version == null) && (installedAssemblies != null)) { AssemblyEntry[] entryArray = installedAssemblies.FindAssemblyNameFromSimpleName(assemblyName.Name); if (entryArray.Length <= 1) { return(extension); } for (int i = 0; i < entryArray.Length; i++) { AssemblyNameExtension that = new AssemblyNameExtension(entryArray[i].FullName); if (((that.Version != null) && (that.Version.CompareTo(extension.Version) > 0)) && assemblyName.PartialNameCompare(that, PartialComparisonFlags.PublicKeyToken | PartialComparisonFlags.Culture)) { extension = that; } } } return(extension); }