public void Equals()
        {
            // For each pair of assembly strings...
            foreach (string assemblyString1 in s_assemblyStrings)
            {
                AssemblyName baseName1 = new AssemblyName(assemblyString1);

                foreach (string assemblyString2 in s_assemblyStrings)
                {
                    AssemblyName baseName2 = new AssemblyName(assemblyString2);

                    // ...and for each pair of production methods...
                    foreach (ProduceAssemblyNameEx produce1 in s_producers)
                    {
                        foreach (ProduceAssemblyNameEx produce2 in s_producers)
                        {
                            AssemblyNameExtension a1 = produce1(assemblyString1);
                            AssemblyNameExtension a2 = produce2(assemblyString2);

                            // Baseline is a mismatch which is known to exercise
                            // the full code path.
                            AssemblyNameExtension a3 = ProduceAsAssemblyName(assemblyString1);
                            AssemblyNameExtension a4 = ProduceAsString(assemblyString2);

                            bool result         = a1.Equals(a2);
                            bool resultBaseline = a3.Equals(a4);
                            if (result != resultBaseline)
                            {
                                Assert.Equal(resultBaseline, result);
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public void VerifyAssemblyNameExSerializationWithRemappedFromByTranslator()
        {
            AssemblyNameExtension assemblyNameOriginal = new AssemblyNameExtension("System.Xml, Version=10.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a");
            AssemblyNameExtension assemblyRemappedFrom = new AssemblyNameExtension("System.Xml, Version=9.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a");

            assemblyRemappedFrom.MarkImmutable();
            assemblyNameOriginal.AddRemappedAssemblyName(assemblyRemappedFrom);
            assemblyNameOriginal.RemappedFromEnumerator.Count().ShouldBe(1);

            AssemblyNameExtension assemblyNameDeserialized = null;

            MemoryStream serializationStream = new MemoryStream();
            ITranslator  writeTranslator     = BinaryTranslator.GetWriteTranslator(serializationStream);

            writeTranslator.Translate(ref assemblyNameOriginal, (ITranslator t) => new AssemblyNameExtension(t));

            serializationStream.Seek(0, SeekOrigin.Begin);
            ITranslator readTranslator = BinaryTranslator.GetReadTranslator(serializationStream, null);

            readTranslator.Translate(ref assemblyNameDeserialized, (ITranslator t) => new AssemblyNameExtension(t));

            assemblyNameDeserialized.Equals(assemblyNameOriginal).ShouldBeTrue();
            assemblyNameDeserialized.RemappedFromEnumerator.Count().ShouldBe(1);
            assemblyNameDeserialized.RemappedFromEnumerator.First().ShouldBe(assemblyRemappedFrom);
        }
Beispiel #3
0
        /// <summary>
        /// This is an assembly resolution handler necessary for fixing up types instantiated in different
        /// AppDomains and loaded with a Assembly.LoadFrom equivalent call. See comments in TaskEngine.ExecuteTask
        /// for more details.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        /// <owner>lukaszg</owner>
        internal Assembly ResolveAssembly(object sender, ResolveEventArgs args)
        {
            // Is this our task assembly?
            if (taskAssemblyFile != null)
            {
                if (File.Exists(taskAssemblyFile))
                {
                    try
                    {
                        AssemblyNameExtension taskAssemblyName = new AssemblyNameExtension(AssemblyName.GetAssemblyName(taskAssemblyFile));
                        AssemblyNameExtension argAssemblyName  = new AssemblyNameExtension(args.Name);

                        if (taskAssemblyName.Equals(argAssemblyName))
                        {
                            return(Assembly.UnsafeLoadFrom(taskAssemblyFile));
                        }
                    }
                    // any problems with the task assembly? return null.
                    catch (FileNotFoundException)
                    {
                        return(null);
                    }
                    catch (BadImageFormatException)
                    {
                        return(null);
                    }
                }
            }

            // otherwise, have a nice day.
            return(null);
        }
        public void CompareTo()
        {
            // For each pair of assembly strings...
            foreach (string assemblyString1 in s_assemblyStrings)
            {
                AssemblyName baseName1 = new AssemblyName(assemblyString1);

                foreach (string assemblyString2 in s_assemblyStrings)
                {
                    AssemblyName baseName2 = new AssemblyName(assemblyString2);

                    // ...and for each pair of production methods...
                    foreach (ProduceAssemblyNameEx produce1 in s_producers)
                    {
                        foreach (ProduceAssemblyNameEx produce2 in s_producers)
                        {
                            AssemblyNameExtension a1 = produce1(assemblyString1);
                            AssemblyNameExtension a2 = produce2(assemblyString2);

                            int result = a1.CompareTo(a2);

                            if (a1.Equals(a2))
                            {
                                Assert.Equal(0, result);
                            }

                            if (a1.CompareBaseNameTo(a2) != 0)
                            {
                                Assert.Equal(a1.CompareBaseNameTo(a2), result);
                            }

                            if
                            (
                                a1.CompareBaseNameTo(a2) == 0 &&    // Only check version if basenames match
                                a1.Version != a2.Version
                            )
                            {
                                if (a1.Version == null)
                                {
                                    // Expect -1 if a1.Version is null and the baseNames match
                                    Assert.Equal(-1, result);
                                }
                                else
                                {
                                    Assert.Equal(a1.Version.CompareTo(a2.Version), result);
                                }
                            }

                            int resultBaseline = String.Compare(a1.FullName, a2.FullName, StringComparison.OrdinalIgnoreCase);
                            // Only check to see if the result and the resultBaseline match when the result baseline is 0 and the result is not 0.
                            if (resultBaseline != result && resultBaseline == 0)
                            {
                                Assert.Equal(resultBaseline, result);
                            }
                        }
                    }
                }
            }
        }
Beispiel #5
0
        private Assembly ResolveAssembly(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
#endif
        {
            // Is this our task assembly?
            if (_taskAssemblyFile != null)
            {
                if (FileSystems.Default.FileExists(_taskAssemblyFile))
                {
                    try
                    {
#if FEATURE_APPDOMAIN
                        AssemblyNameExtension taskAssemblyName = new AssemblyNameExtension(AssemblyName.GetAssemblyName(_taskAssemblyFile));
                        AssemblyNameExtension argAssemblyName  = new AssemblyNameExtension(args.Name);

                        if (taskAssemblyName.Equals(argAssemblyName))
                        {
#if (!CLR2COMPATIBILITY)
                            return(Assembly.UnsafeLoadFrom(_taskAssemblyFile));
#else
                            return(Assembly.LoadFrom(_taskAssemblyFile));
#endif
                        }
#else // !FEATURE_APPDOMAIN
                        AssemblyNameExtension taskAssemblyName = new AssemblyNameExtension(AssemblyLoadContext.GetAssemblyName(_taskAssemblyFile));
                        AssemblyNameExtension argAssemblyName  = new AssemblyNameExtension(assemblyName);
                        if (taskAssemblyName.Equals(argAssemblyName))
                        {
                            return(AssemblyLoadContext.Default.LoadFromAssemblyPath(_taskAssemblyFile));
                        }
#endif
                    }
                    // any problems with the task assembly? return null.
                    catch (FileNotFoundException)
                    {
                        return(null);
                    }
                    catch (BadImageFormatException)
                    {
                        return(null);
                    }
                }
            }

            // otherwise, have a nice day.
            return(null);
        }
 /// <summary>
 /// We only compare the from because in terms of what is in the redist list unique from's are expected
 /// </summary>
 public bool Equals(AssemblyRemapping other)
 {
     return(_from.Equals(other._from));
 }
Beispiel #7
0
        /// <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);
        }