private void RoundTrip(AssemblyName name, bool testFullName = true)
        {
            AssemblyName rtName;

            FusionAssemblyIdentity.IAssemblyName obj;

            if (testFullName)
            {
                string fullName = name.FullName;

                obj    = FusionAssemblyIdentity.ToAssemblyNameObject(fullName);
                rtName = ToAssemblyName(obj);
                Assert.Equal(name.Name, rtName.Name);
                Assert.Equal(name.Version, rtName.Version);
                Assert.Equal(name.CultureInfo, rtName.CultureInfo);
                Assert.Equal(name.GetPublicKeyToken(), rtName.GetPublicKeyToken());
                Assert.Equal(name.Flags, rtName.Flags);
                Assert.Equal(name.ContentType, rtName.ContentType);

                string displayName = FusionAssemblyIdentity.GetDisplayName(obj, FusionAssemblyIdentity.ASM_DISPLAYF.FULL);
                Assert.Equal(fullName, displayName);
            }

            obj    = FusionAssemblyIdentity.ToAssemblyNameObject(name);
            rtName = ToAssemblyName(obj);
            Assert.Equal(name.Name, rtName.Name);
            Assert.Equal(name.Version, rtName.Version);
            Assert.Equal(name.CultureInfo, rtName.CultureInfo);
            Assert.Equal(name.GetPublicKeyToken(), rtName.GetPublicKeyToken());
            Assert.Equal(name.Flags, rtName.Flags);
            Assert.Equal(name.ContentType, rtName.ContentType);
        }
Ejemplo n.º 2
0
        private void TestParseVersion(string value, int major, int minor, int build, int revision, AssemblyIdentityParts expectedParts)
        {
            AssemblyIdentityParts actualParts;
            ulong actual;

            Assert.True(AssemblyIdentity.TryParseVersion(value, out actual, out actualParts));
            Assert.Equal(expectedParts, actualParts);

            Version actualVersion = AssemblyIdentity.ToVersion(actual);

            Assert.Equal(new Version(major, minor, build, revision), actualVersion);

            // compare with fusion
            var fusionName = FusionAssemblyIdentity.ToAssemblyNameObject("Name, Version=" + value);

            Assert.NotNull(fusionName);

            AssemblyIdentityParts fusionParts = 0;
            var fusionVersion = FusionAssemblyIdentity.GetVersion(fusionName, out fusionParts);

            Assert.Equal(fusionVersion, actualVersion);

            // Test limitation:
            // When constructing INameObject with CANOF.PARSE_DISPLAY_NAME option,
            // the Version=* is treated as unspecified version. That's also done by TryParseDisplayName,
            // but outside of TryParseVersion, which we are testing here.
            if (value == "*")
            {
                Assert.Equal((AssemblyIdentityParts)0, fusionParts);
            }
            else
            {
                Assert.Equal(expectedParts, fusionParts);
            }
        }
Ejemplo n.º 3
0
        private void TestParseDisplayName(
            string displayName,
            AssemblyIdentity expected,
            AssemblyIdentityParts expectedParts,
            AssemblyIdentity expectedFusion
            )
        {
            var fusion = FusionAssemblyIdentity.ToAssemblyIdentity(
                FusionAssemblyIdentity.ToAssemblyNameObject(displayName)
                );

            Assert.Equal(expectedFusion, fusion);

            AssemblyIdentity      id = null;
            AssemblyIdentityParts actualParts;
            bool success = AssemblyIdentity.TryParseDisplayName(
                displayName,
                out id,
                out actualParts
                );

            Assert.Equal(expected, id);
            Assert.Equal(success, id != null);
            Assert.Equal(expectedParts, actualParts);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Looks up specified partial assembly name in the GAC and returns the best matching full assembly name/>.
        /// </summary>
        /// <param name="displayName">The display name of an assembly.</param>
        /// <param name="location">Full path name of the resolved assembly.</param>
        /// <param name="architectureFilter">The optional processor architecture.</param>
        /// <param name="preferredCulture">The optional preferred culture information.</param>
        /// <returns>An assembly identity or null, if <paramref name="displayName"/> can't be resolved.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="displayName"/> is null.</exception>
        public static unsafe string ResolvePartialName(
            string displayName,
            out string location,
            ProcessorArchitecture[] architectureFilter = null,
            CultureInfo preferredCulture = null)
        {
            if (displayName == null)
            {
                throw new ArgumentNullException("displayName");
            }

            location = null;
            FusionAssemblyIdentity.IAssemblyName nameObject = FusionAssemblyIdentity.ToAssemblyNameObject(displayName);
            if (nameObject == null)
            {
                return(null);
            }

            var    candidates  = GetAssemblyObjects(nameObject, architectureFilter);
            string cultureName = (preferredCulture != null && !preferredCulture.IsNeutralCulture) ? preferredCulture.Name : null;

            var bestMatch = FusionAssemblyIdentity.GetBestMatch(candidates, cultureName);

            if (bestMatch == null)
            {
                return(null);
            }

            string fullName = FusionAssemblyIdentity.GetDisplayName(bestMatch, FusionAssemblyIdentity.ASM_DISPLAYF.FULL);

            fixed(char *p = new char[MAX_PATH])
            {
                ASSEMBLY_INFO info = new ASSEMBLY_INFO
                {
                    cbAssemblyInfo            = (uint)Marshal.SizeOf(typeof(ASSEMBLY_INFO)),
                    pszCurrentAssemblyPathBuf = p,
                    cchBuf = (uint)MAX_PATH
                };

                IAssemblyCache assemblyCacheObject;

                CreateAssemblyCache(out assemblyCacheObject, 0);
                assemblyCacheObject.QueryAssemblyInfo(0, fullName, ref info);
                Debug.Assert(info.pszCurrentAssemblyPathBuf != null);
                Debug.Assert(info.pszCurrentAssemblyPathBuf[info.cchBuf - 1] == '\0');

                var result = Marshal.PtrToStringUni((IntPtr)info.pszCurrentAssemblyPathBuf, (int)info.cchBuf - 1);

                Debug.Assert(result.IndexOf('\0') == -1);
                location = result;
            }

            return(fullName);
        }
        private void TestParseSimpleName(string displayName, string expected, string expectedFusion)
        {
            var fusionName = FusionAssemblyIdentity.ToAssemblyNameObject(displayName);
            var actual     = (fusionName != null) ? FusionAssemblyIdentity.GetName(fusionName) : null;

            Assert.Equal(expectedFusion, actual);

            AssemblyIdentity id;

            actual = AssemblyIdentity.TryParseDisplayName(displayName, out id) ? id.Name : null;
            Assert.Equal(expected, actual);
        }
        private void TestParseVersion(string value)
        {
            string displayName = "Foo, Version=" + value;
            var    fusion      = FusionAssemblyIdentity.ToAssemblyIdentity(FusionAssemblyIdentity.ToAssemblyNameObject(displayName));

            AssemblyIdentity id      = null;
            bool             success = AssemblyIdentity.TryParseDisplayName(displayName, out id);

            Assert.Equal(fusion != null, success);

            if (success)
            {
                Assert.Equal(fusion.Version, id.Version);
            }
        }
        private void TestParseVersionInvalid(string value)
        {
            AssemblyIdentityParts actualParts;
            ulong actual;

            Assert.False(AssemblyIdentity.TryParseVersion(value, out actual, out actualParts));

            // compare with fusion
            var fusionName = FusionAssemblyIdentity.ToAssemblyNameObject("Name, Version=" + value);

            if (fusionName != null)
            {
                AssemblyIdentityParts fusionParts = 0;
                var fusionVersion = FusionAssemblyIdentity.GetVersion(fusionName, out fusionParts);

                // name parsing succeeds but there is no version:
                Assert.Equal((AssemblyIdentityParts)0, fusionParts);
            }
        }
Ejemplo n.º 8
0
        public void FusionGetBestMatch()
        {
            var foo       = FusionAssemblyIdentity.ToAssemblyNameObject("foo");
            var foo1      = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=1.0.0.0, Culture=neutral");
            var foo2      = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=2.0.0.0, Culture=neutral");
            var foo3      = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=3.0.0.0, Culture=neutral");
            var foo3_enUS = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=3.0.0.0, Culture=en-US");
            var foo3_deDE = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=3.0.0.0, Culture=de-DE");

            var m = FusionAssemblyIdentity.GetBestMatch(new[] { foo2, foo1, foo3 }, null);

            Assert.Equal(foo3, m);

            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo3, foo2, foo1 }, null);
            Assert.Equal(foo3, m);

            // only simple name is used
            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo2, foo3 }, null);
            Assert.Equal(foo3, m);

            // the first match if preferred cultures not specified
            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo1, foo3_deDE, foo3_enUS, foo2 }, null);
            Assert.Equal(foo3_deDE, m);

            // the first match if preferred cultures not specified
            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo1, foo3_deDE, foo3_enUS, foo2 }, null);
            Assert.Equal(foo3_deDE, m);

            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo1, foo3, foo3_deDE, foo3_enUS, foo2 }, "en-US");
            Assert.Equal(foo3_enUS, m);

            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo1, foo3_deDE, foo3, foo3_enUS, foo2 }, "cz-CZ");
            Assert.Equal(foo3, m);

            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo3_deDE, foo2 }, "en-US");
            Assert.Equal(foo3_deDE, m);

            // neutral culture wins over specific non-matching one:
            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo3_deDE, foo3, foo2 }, "en-US");
            Assert.Equal(foo3, m);
        }
Ejemplo n.º 9
0
        public void FusionAssemblyNameRoundTrip()
        {
            RoundTrip(new AssemblyName("foo"));
            RoundTrip(new AssemblyName {
                Name = "~!@#$%^&*()_+={}:\"<>?[];',./"
            });
            RoundTrip(new AssemblyName("\\,"));
            RoundTrip(new AssemblyName("\\\""));

            RoundTrip(new AssemblyIdentity("foo").ToAssemblyName());

            // 0xffff version is not included in AssemblyName.FullName for some reason:
            var name = new AssemblyIdentity("foo", version: new Version(0xffff, 0xffff, 0xffff, 0xffff)).ToAssemblyName();

            RoundTrip(name, testFullName: false);
            var obj     = FusionAssemblyIdentity.ToAssemblyNameObject(name);
            var display = FusionAssemblyIdentity.GetDisplayName(obj, FusionAssemblyIdentity.ASM_DISPLAYF.FULL);

            Assert.Equal("foo, Version=65535.65535.65535.65535, Culture=neutral, PublicKeyToken=null", display);

            RoundTrip(new AssemblyIdentity("foo", version: new Version(1, 2, 3, 4)).ToAssemblyName());
            RoundTrip(new AssemblyName("foo")
            {
                Version = new Version(1, 2, 3, 4)
            });

            RoundTrip(new AssemblyIdentity("foo", cultureName: CultureInfo.CurrentCulture.Name).ToAssemblyName());
            RoundTrip(new AssemblyIdentity("foo", cultureName: "").ToAssemblyName());
            RoundTrip(new AssemblyName("foo")
            {
                CultureInfo = CultureInfo.InvariantCulture
            });

            RoundTrip(new AssemblyIdentity("foo", version: new Version(1, 2, 3, 4), cultureName: "en-US").ToAssemblyName());
            RoundTrip(new AssemblyIdentity("foo", publicKeyOrToken: new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }.AsImmutableOrNull()).ToAssemblyName());
            RoundTrip(new AssemblyIdentity("foo", version: new Version(1, 2, 3, 4), cultureName: CultureInfo.CurrentCulture.Name, publicKeyOrToken: new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }.AsImmutableOrNull()).ToAssemblyName());

            RoundTrip(new AssemblyIdentity("foo", isRetargetable: true).ToAssemblyName());
            RoundTrip(new AssemblyIdentity("foo", contentType: AssemblyContentType.WindowsRuntime).ToAssemblyName());
        }
Ejemplo n.º 10
0
        public void FusionToAssemblyName()
        {
            var nameObject = FusionAssemblyIdentity.ToAssemblyNameObject("mscorlib");
            var name       = FusionAssemblyIdentity.ToAssemblyName(nameObject);

            Assert.Equal("mscorlib", name.Name);
            Assert.Null(name.Version);
            Assert.Null(name.CultureInfo);
            Assert.Null(name.GetPublicKey());
            Assert.Null(name.GetPublicKeyToken());
            Assert.Equal(AssemblyContentType.Default, name.ContentType);

            nameObject = FusionAssemblyIdentity.ToAssemblyNameObject("mscorlib, Version=2.0.0.0");
            name       = FusionAssemblyIdentity.ToAssemblyName(nameObject);
            Assert.Equal("mscorlib", name.Name);
            Assert.Equal(new Version(2, 0, 0, 0), name.Version);
            Assert.Null(name.CultureInfo);
            Assert.Null(name.GetPublicKey());
            Assert.Null(name.GetPublicKeyToken());
            Assert.Equal(AssemblyContentType.Default, name.ContentType);

            nameObject = FusionAssemblyIdentity.ToAssemblyNameObject("mscorlib, Version=2.0.0.0, Culture=neutral");
            name       = FusionAssemblyIdentity.ToAssemblyName(nameObject);
            Assert.Equal("mscorlib", name.Name);
            Assert.Equal(new Version(2, 0, 0, 0), name.Version);
            Assert.Equal(name.CultureInfo, CultureInfo.InvariantCulture);
            Assert.Null(name.GetPublicKey());
            Assert.Null(name.GetPublicKeyToken());
            Assert.Equal(AssemblyContentType.Default, name.ContentType);

            nameObject = FusionAssemblyIdentity.ToAssemblyNameObject("mscorlib, Version=2.0.0.0, Culture=en-US");
            name       = FusionAssemblyIdentity.ToAssemblyName(nameObject);
            Assert.Equal("mscorlib", name.Name);
            Assert.Equal(new Version(2, 0, 0, 0), name.Version);
            Assert.NotNull(name.CultureInfo);
            Assert.Equal("en-US", name.CultureInfo.Name);
            Assert.Null(name.GetPublicKey());
            Assert.Null(name.GetPublicKeyToken());
            Assert.Equal(AssemblyContentType.Default, name.ContentType);

            nameObject = FusionAssemblyIdentity.ToAssemblyNameObject("Windows, Version=255.255.255.255, ContentType=WindowsRuntime");
            name       = FusionAssemblyIdentity.ToAssemblyName(nameObject);
            Assert.Equal("Windows", name.Name);
            Assert.Equal(new Version(255, 255, 255, 255), name.Version);
            Assert.Null(name.CultureInfo);
            Assert.Null(name.GetPublicKey());
            Assert.Null(name.GetPublicKeyToken());
            Assert.Equal(AssemblyContentType.WindowsRuntime, name.ContentType);

            nameObject = FusionAssemblyIdentity.ToAssemblyNameObject("mscorlib, Version=2.0.0.0, Culture=nonsense");
            Assert.NotNull(nameObject);
            Assert.Throws <CultureNotFoundException>(() => FusionAssemblyIdentity.ToAssemblyName(nameObject));

            nameObject = FusionAssemblyIdentity.ToAssemblyNameObject("mscorlib, Version=2.0.0.0, Culture=null");
            Assert.NotNull(nameObject);
            Assert.Throws <CultureNotFoundException>(() => FusionAssemblyIdentity.ToAssemblyName(nameObject));

            Assert.Throws <CultureNotFoundException>(() => new AssemblyName("mscorlib, Version=2.0.0.0, Culture=nonsense"));
            Assert.Throws <CultureNotFoundException>(() => new AssemblyName("mscorlib, Version=2.0.0.0, Culture=null"));

            Assert.Throws <ArgumentException>(() => FusionAssemblyIdentity.ToAssemblyNameObject(new AssemblyName {
                Name = "x\0x"
            }));

            // invalid characters are ok in the name, the FullName can't be built though:
            foreach (char c in AssemblyIdentityTests.ClrInvalidCharacters)
            {
                nameObject = FusionAssemblyIdentity.ToAssemblyNameObject(new AssemblyName {
                    Name = c.ToString()
                });
                name = FusionAssemblyIdentity.ToAssemblyName(nameObject);
                Assert.Equal(c.ToString(), name.Name);
                Assert.Throws <FileLoadException>(() => name.FullName);
            }
        }