public void VerifyProcessorArchitectureDoesNotCrashFullFusionNameSpecificVersion()
        {
            AssemblyNameExtension fusionName = new AssemblyNameExtension("System, PublicKeyToken=b77a5c561934e089, ProcessorArchitecture=MSIL");
            string path = GlobalAssemblyCache.GetLocation(fusionName, SystemProcessorArchitecture.MSIL, getRuntimeVersion, new Version("2.0.50727"), true, new FileExists(MockFileExists), _getPathFromFusionName, null /* use the real gac enumerator*/, true);

            Assert.Null(path);
        }
        public void VerifyNullPublicKeyspecificVersion()
        {
            AssemblyNameExtension fusionName = new AssemblyNameExtension("System, PublicKeyToken=null");
            string path = GlobalAssemblyCache.GetLocation(fusionName, SystemProcessorArchitecture.None, getRuntimeVersion, new Version("2.0.50727"), false, new FileExists(MockFileExists), _getPathFromFusionName, _gacEnumerator, true);

            Assert.Null(path);
        }
Exemple #3
0
        public string Resolve(string assemblyName)
        {
            string path;

            GlobalAssemblyCache.ResolvePartialName(assemblyName, out path, Architectures, this.PreferredCulture);
            return(File.Exists(path) ? path : null);
        }
Exemple #4
0
        public string ResolveAssemblyName(string displayName)
        {
            Contract.ThrowIfNull(displayName);
            AssemblyIdentity identity = GlobalAssemblyCache.ResolvePartialName(displayName, architectureFilter, preferredCulture);

            return((identity != null) ? identity.Location : null);
        }
 public void VerifyEmptyPublicKeyspecificVersion()
 {
     Assert.Throws <FileLoadException>(() =>
     {
         AssemblyNameExtension fusionName = new AssemblyNameExtension("System, PublicKeyToken=");
         string path = GlobalAssemblyCache.GetLocation(fusionName, ProcessorArchitecture.None, getRuntimeVersion, new Version("2.0.50727"), false, new FileExists(MockFileExists), _getPathFromFusionName, _gacEnumerator, true);
     }
                                       );
 }
        public bool TryResolvePartialName(string name, out string assemblyLocation)
        {
#if DESKTOP
            return(GlobalAssemblyCache.ResolvePartialName(name, out assemblyLocation) != null);
#else
            assemblyLocation = null;
            return(false);
#endif
        }
        public void VerifyFusionNamev2057020SpecificVersion()
        {
            // We want to pass a very generic name to get the correct gac entries.
            AssemblyNameExtension fusionName = new AssemblyNameExtension("System, Version=2.0.0.0");


            string path = GlobalAssemblyCache.GetLocation(fusionName, SystemProcessorArchitecture.None, _runtimeVersion, new Version("2.0.0"), false, new FileExists(MockFileExists), _getPathFromFusionName, _gacEnumerator, true);

            Assert.NotNull(path);
            Assert.Equal(system2Path, path);
        }
 public static string GetGACAssemblyLocation(string assemblyDisplayName)
 {
     return(_assemblyLocations.GetOrAdd(
                assemblyDisplayName,
                (name) =>
     {
         string path;
         GlobalAssemblyCache.ResolvePartialName(assemblyDisplayName, out path);
         return path;
     }));
 }
Exemple #9
0
        public void VerifySimpleNamev2057020()
        {
            // We want to pass a very generic name to get the correct gac entries.
            AssemblyNameExtension fusionName = new AssemblyNameExtension("System");


            string path = GlobalAssemblyCache.GetLocation(fusionName, SystemProcessorArchitecture.None, _runtimeVersion, new Version("2.0.57027"), false, new FileExists(MockFileExists), _getPathFromFusionName, _gacEnumerator, false);

            Assert.NotNull(path);
            Assert.True(path.Equals(system2Path, StringComparison.OrdinalIgnoreCase));
        }
        public string ResolveReference(string reference)
        {
            if (PathUtilities.IsFilePath(reference))
            {
                return(null);
            }

            string path;

            GlobalAssemblyCache.ResolvePartialName(reference, out path, _architectures, this.PreferredCulture);
            return((path != null && PortableShim.File.Exists(path)) ? path : null);
        }
Exemple #11
0
        /// <summary>
        /// This is used to try and resolve an assembly reference
        /// </summary>
        /// <param name="reference">The assembly reference</param>
        /// <param name="referrer">The module requiring the reference</param>
        /// <returns>The assembly node if resolved or null if not resolved</returns>
        public virtual AssemblyNode ResolveReference(AssemblyReference reference, Module referrer)
        {
            AssemblyNode assembly;

            if (reference == null)
            {
                throw new ArgumentNullException("reference");
            }

            // Try to get it from the cache
            string name = reference.StrongName;

            if (cache.ContainsKey(name))
            {
                return(cache[name]);
            }

            // Try to get it from the GAC if so indicated
            if (this.UseGac)
            {
                string location = GlobalAssemblyCache.GetLocation(reference);

                if (location != null)
                {
                    assembly = AssemblyNode.GetAssembly(location, null, false, false, false, false);

                    if (assembly != null)
                    {
                        this.Add(assembly);
                        return(assembly);
                    }
                }
            }

            // Try the redirects if not found
            foreach (BindingRedirectSettings brs in redirects)
            {
                if (brs.IsRedirectFor(name) && cache.ContainsKey(brs.StrongName))
                {
                    ConsoleApplication.WriteMessage(LogLevel.Info, "Using redirect '{0}' in place of '{1}'",
                                                    brs.StrongName, name);

                    assembly = cache[brs.StrongName];

                    // Add the same assembly under the current name
                    cache.Add(name, assembly);
                    return(assembly);
                }
            }

            // Couldn't find it; return null
            return(null);
        }
Exemple #12
0
        public override string ResolveReference(string reference, string baseFilePath)
        {
            if (PathUtilities.IsFilePath(reference))
            {
                return(base.ResolveReference(reference, baseFilePath));
            }

            string path;

            GlobalAssemblyCache.ResolvePartialName(reference, out path, _architectures, this.PreferredCulture);
            return(FileExists(path) ? path : null);
        }
        /// <param name="codeBaseUri">Uri pointing to the assembly</param>
        public static bool Contains(Uri codeBaseUri)
        {
            if (codeBaseUri == null)
            {
                Debug.Fail("codeBaseUri == null"); return(false);
            }
            lock (GlobalAssemblyCache.Lock)
            {
                if (!GlobalAssemblyCache.FusionLoaded)
                {
                    GlobalAssemblyCache.FusionLoaded = true;
                    System.Reflection.Assembly systemAssembly = typeof(object).Assembly;
                    //^ assume systemAssembly != null && systemAssembly.Location != null;
                    string dir = Path.GetDirectoryName(systemAssembly.Location);
                    //^ assume dir != null;
                    GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll"));
                }
                IAssemblyEnum assemblyEnum;
                int           rc = GlobalAssemblyCache.CreateAssemblyEnum(out assemblyEnum, null, null, ASM_CACHE.GAC, 0);
                if (rc < 0 || assemblyEnum == null)
                {
                    return(false);
                }
                IApplicationContext applicationContext;
                IAssemblyName       currentName;
                while (assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0)
                {
                    //^ assume currentName != null;
                    AssemblyName assemblyName = new AssemblyName(currentName);
                    string       scheme       = codeBaseUri.Scheme;
                    if (scheme != null && assemblyName.CodeBase.StartsWith(scheme))
                    {
                        try
                        {
                            Uri foundUri = new Uri(assemblyName.CodeBase);
                            if (codeBaseUri.Equals(foundUri))
                            {
                                return(true);
                            }
#if !FxCop
                        }
                        catch (Exception)
                        {
#else
                        }finally{
#endif
                        }
                    }
                }
                return(false);
            }
        }
        public void AssemblyAndGacLocation()
        {
            var names = GlobalAssemblyCache.GetAssemblyObjects(partialNameFilter: null, architectureFilter: default(ImmutableArray <ProcessorArchitecture>)).ToArray();

            Assert.True(names.Length > 100, "There are at least 100 assemblies in the GAC");

            var gacLocationsUpper = GlobalAssemblyCacheLocation.RootLocations.Select(location => location.ToUpper());

            foreach (var name in names)
            {
                string location = GlobalAssemblyCache.GetAssemblyLocation(name);
                Assert.NotNull(location);
                Assert.True(gacLocationsUpper.Any(gac => location.StartsWith(gac, StringComparison.OrdinalIgnoreCase)), "Path within some GAC root");
                Assert.Equal(Path.GetFullPath(location), location);
            }
        }
Exemple #15
0
        /// <summary>
        /// Returns the original location of the corresponding assembly if available, otherwise returns the location of the shadow copy.
        /// If the corresponding assembly is not in the GAC, null is returned.
        /// </summary>
        public static string GetLocation(AssemblyReference assemblyReference)
        {
#if CodeContracts
            if (!probeGAC)
            {
                return(null);
            }
#endif
            if (assemblyReference == null)
            {
                Debug.Fail("assemblyReference == null"); return(null);
            }
            lock (GlobalAssemblyCache.Lock){
                if (!GlobalAssemblyCache.FusionLoaded)
                {
                    GlobalAssemblyCache.FusionLoaded = true;
                    System.Reflection.Assembly systemAssembly = typeof(object).Assembly;
                    //^ assume systemAssembly != null && systemAssembly.Location != null;
                    string dir = Path.GetDirectoryName(systemAssembly.Location);
                    //^ assume dir != null;
                    GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll"));
                }
                IAssemblyEnum assemblyEnum;
                CreateAssemblyEnum(out assemblyEnum, null, null, ASM_CACHE.GAC, 0);
                if (assemblyEnum == null)
                {
                    return(null);
                }
                IApplicationContext applicationContext;
                IAssemblyName       currentName;
                while (assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0)
                {
                    //^ assume currentName != null;
                    AssemblyName aName = new AssemblyName(currentName);
                    if (assemblyReference.Matches(aName.Name, aName.Version, aName.Culture, aName.PublicKeyToken))
                    {
                        string codeBase = aName.CodeBase;
                        if (codeBase != null && codeBase.StartsWith("file:///"))
                        {
                            return(codeBase.Substring(8));
                        }
                        return(aName.GetLocation());
                    }
                }
                return(null);
            }
        }
        private IEnumerable <CompletionItem> GetCompletionsWorker(string pathSoFar)
        {
            var comma = pathSoFar.IndexOf(',');

            if (comma >= 0)
            {
                var path = pathSoFar.Substring(0, comma);
                return(from identity in GetAssemblyIdentities(path)
                       let text = identity.GetDisplayName()
                                  select new CompletionItem(_completionProvider, text, _textChangeSpan, glyph: Glyph.Assembly));
            }
            else
            {
                return(from displayName in s_lazyAssemblySimpleNames.Value
                       select new CompletionItem(
                           _completionProvider,
                           displayName, _textChangeSpan,
                           descriptionFactory: c => Task.FromResult(GlobalAssemblyCache.ResolvePartialName(displayName).GetDisplayName().ToSymbolDisplayParts()),
                           glyph: Glyph.Assembly));
            }
        }
        public virtual AssemblyNode ResolveReference(AssemblyReference reference, Module module)
        {
            if (reference == null)
            {
                throw new ArgumentNullException("reference");
            }

            //Console.WriteLine("resolving {0}", reference.StrongName);

            // try to get it from the cache
            string name = reference.StrongName;

            if (cache.ContainsKey(name))
            {
                return(cache[name]);
            }

            // try to get it from the gac
            if (useGac)
            {
                string location = GlobalAssemblyCache.GetLocation(reference);
                if (location != null)
                {
                    AssemblyNode assembly = AssemblyNode.GetAssembly(location, null, false, false, false, false);
                    if (assembly != null)
                    {
                        Add(assembly);
                        return(assembly);
                    }
                }
            }

            // couldn't find it; return null
            // Console.WriteLine("returning null on request for {0}", reference.StrongName);
            //OnUnresolvedAssemblyReference(reference, module);
            return(null);
        }
Exemple #18
0
        private static string GetReferenceString(Reference reference)
        {
            if (!reference.StrongName)
            {
                return(reference.Path);
            }

            string name = reference.Name;

            if (name == "mscorlib")
            {
                // mscorlib is always loaded
                return(null);
            }

            // TODO: This shouldn't directly depend on GAC, rather we should have some kind of "reference simplifier".
            var possibleGacNames = GlobalAssemblyCache.GetAssemblyIdentities(name).ToArray();

            if (possibleGacNames.Length == 0)
            {
                // no assembly with simple "name" found in GAC, use path to identify the reference:
                return(reference.Path);
            }

            string version        = reference.Version;
            string culture        = reference.Culture;
            string publicKeyToken = reference.PublicKeyToken;

            var fullName = string.Concat(
                name,
                ", Version=",
                version,
                ", Culture=",
                (culture == "") ? "neutral" : culture,
                ", PublicKeyToken=",
                publicKeyToken.ToLowerInvariant());

            AssemblyIdentity identity;

            if (!AssemblyIdentity.TryParseDisplayName(fullName, out identity))
            {
                // ignore invalid names:
                return(null);
            }

            var foundEquivalent    = false;
            var foundNonEquivalent = false;

            foreach (var possibleGacName in possibleGacNames)
            {
                if (DesktopAssemblyIdentityComparer.Default.ReferenceMatchesDefinition(identity, possibleGacName))
                {
                    foundEquivalent = true;
                }
                else
                {
                    foundNonEquivalent = true;
                }

                if (foundEquivalent && foundNonEquivalent)
                {
                    break;
                }
            }

            if (!foundEquivalent)
            {
                // The reference name isn't equivalent to any GAC name.
                // The assembly is strong named but not GAC'd, so we need to load it from path:
                return(reference.Path);
            }

            if (foundNonEquivalent)
            {
                // We found some equivalent assemblies but also some non-equivalent.
                // So simple name doesn't identify the reference uniquely.
                return(fullName);
            }

            // We found a single simple name match that is equivalent to the given reference.
            // We can use the simple name to load the GAC'd assembly.
            return(name);
        }
        public void GetAssemblyIdentities()
        {
            AssemblyIdentity[] names;

            names = GlobalAssemblyCache.GetAssemblyIdentities(new AssemblyName("mscorlib")).ToArray();
            Assert.True(names.Length >= 1, "At least 1 mscorlib");
            foreach (var name in names)
            {
                Assert.Equal("mscorlib", name.Name);
            }

            names = GlobalAssemblyCache.GetAssemblyIdentities(new AssemblyName("mscorlib"), ImmutableArray.Create(ProcessorArchitecture.MSIL, ProcessorArchitecture.X86)).ToArray();
            Assert.True(names.Length >= 1, "At least one 32bit mscorlib");
            foreach (var name in names)
            {
                Assert.Equal("mscorlib", name.Name);
            }

            names = GlobalAssemblyCache.GetAssemblyIdentities("mscorlib").ToArray();
            Assert.True(names.Length >= 1, "At least 1 mscorlib");
            foreach (var name in names)
            {
                Assert.Equal("mscorlib", name.Name);
            }

            names = GlobalAssemblyCache.GetAssemblyIdentities("System.Core, Version=4.0.0.0").ToArray();
            Assert.True(names.Length >= 1, "At least System.Core");
            foreach (var name in names)
            {
                Assert.Equal("System.Core", name.Name);
                Assert.Equal(new Version(4, 0, 0, 0), name.Version);
                Assert.True(name.GetDisplayName().Contains("PublicKeyToken=b77a5c561934e089"), "PublicKeyToken matches");
            }

            names = GlobalAssemblyCache.GetAssemblyIdentities("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089").ToArray();
            Assert.True(names.Length >= 1, "At least System.Core");
            foreach (var name in names)
            {
                Assert.Equal("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", name.GetDisplayName());
            }

            var n = new AssemblyName("System.Core");

            n.Version = new Version(4, 0, 0, 0);
            n.SetPublicKeyToken(new byte[] { 0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89 });
            names = GlobalAssemblyCache.GetAssemblyIdentities(n).ToArray();

            Assert.True(names.Length >= 1, "At least System.Core");
            foreach (var name in names)
            {
                Assert.Equal("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", name.GetDisplayName());
            }

            names = GlobalAssemblyCache.GetAssemblyIdentities("x\u0002").ToArray();
            Assert.Equal(0, names.Length);

            names = GlobalAssemblyCache.GetAssemblyIdentities("\0").ToArray();
            Assert.Equal(0, names.Length);

            names = GlobalAssemblyCache.GetAssemblyIdentities("xxxx\0xxxxx").ToArray();
            Assert.Equal(0, names.Length);

            // fusion API CreateAssemblyEnum returns S_FALSE for this name
            names = GlobalAssemblyCache.GetAssemblyIdentities("nonexistingassemblyname" + Guid.NewGuid().ToString()).ToArray();
            Assert.Equal(0, names.Length);
        }
 private IEnumerable <AssemblyIdentity> GetAssemblyIdentities(string pathSoFar)
 {
     return(IOUtilities.PerformIO(() => GlobalAssemblyCache.GetAssemblyIdentities(pathSoFar),
                                  SpecializedCollections.EmptyEnumerable <AssemblyIdentity>()));
 }
Exemple #21
0
        /// <summary>
        /// This is used to try and resolve an assembly reference
        /// </summary>
        /// <param name="reference">The assembly reference</param>
        /// <param name="referrer">The module requiring the reference</param>
        /// <returns>The assembly node if resolved or null if not resolved</returns>
        public virtual AssemblyNode ResolveReference(AssemblyReference reference, Module referrer)
        {
            AssemblyNode assembly;

            if (reference == null)
            {
                throw new ArgumentNullException("reference");
            }

            // Try to get it from the cache
            string name = reference.StrongName;

            if (cache.ContainsKey(name))
            {
                return(cache[name]);
            }

            // Try to get it from the GAC if so indicated
            if (this.UseGac)
            {
                string location = GlobalAssemblyCache.GetLocation(reference);

                if (location != null)
                {
                    assembly = AssemblyNode.GetAssembly(location, null, false, false, false, false);

                    if (assembly != null)
                    {
                        this.Add(assembly);
                        return(assembly);
                    }
                }
            }

            // Try the redirects if not found
            foreach (BindingRedirectSettings brs in redirects)
            {
                if (brs.IsRedirectFor(name) && cache.ContainsKey(brs.StrongName))
                {
                    ConsoleApplication.WriteMessage(LogLevel.Info, "Using redirect '{0}' in place of '{1}'",
                                                    brs.StrongName, name);

                    assembly = cache[brs.StrongName];

                    // Add the same assembly under the current name
                    cache.Add(name, assembly);
                    return(assembly);
                }
            }

            // For mscorlib v255.255.255.255, redirect to System.Runtime.  This is typically one like a .NETCore
            // framework which redirects all of the system types there.
            if (reference.Name == "mscorlib" && reference.Version.Major == 255)
            {
                // The system assembly should be set.  If so, it'll point to the one we need.
                if (SystemTypes.SystemAssembly != null)
                {
                    assembly = SystemTypes.SystemAssembly;
                    cache.Add(name, assembly);
                    return(assembly);
                }

                // If not, look for it in the cache
                string key = cache.Keys.FirstOrDefault(k => k.StartsWith("System.Runtime,", StringComparison.Ordinal));

                if (key != null)
                {
                    assembly = cache[key];
                    cache.Add(name, assembly);
                    return(assembly);
                }
            }

            if (reference.Name != "mscorlib")
            {
                // Try for a framework assembly in the target platform
                var assemblyRef = (AssemblyReference)TargetPlatform.AssemblyReferenceFor[Identifier.For(reference.Name).UniqueIdKey];

                if (assemblyRef != null && System.IO.File.Exists(assemblyRef.Location))
                {
                    assembly = AssemblyNode.GetAssembly(assemblyRef.Location, null, false, false, false, false);

                    ConsoleApplication.WriteMessage(LogLevel.Info, "Using framework redirect '{0}' in place of '{1}'",
                                                    assembly.StrongName, name);

                    cache.Add(name, assembly);
                    return(assembly);
                }
            }

            // Couldn't find it; return null
            return(null);
        }
Exemple #22
0
 public static string GetGacAssemblyPath(AssemblyName assemblyName)
 {
     return(GlobalAssemblyCache.FindAssemblyInNetGac(assemblyName));
 }