private static void AssertIsEnabled(string appConfigPath, bool platform, bool nonPlatform, bool fusionOnly = false)
        {
            using (var policy = FusionAssemblyPortabilityPolicy.LoadFromFile(appConfigPath))
            {
                // portability is suppressed if the identities are not equivalent

                Assert.Equal(platform, IsEquivalent(policy,
                                                    "System, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
                                                    "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));

                Assert.Equal(nonPlatform, IsEquivalent(policy,
                                                       "System.ComponentModel.Composition, Version=5.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
                                                       "System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
            }

            if (!fusionOnly)
            {
                using (var stream = new FileStream(appConfigPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var actual = AssemblyPortabilityPolicy.LoadFromXml(stream);
                    Assert.Equal(platform, !actual.SuppressSilverlightPlatformAssembliesPortability);
                    Assert.Equal(nonPlatform, !actual.SuppressSilverlightLibraryAssembliesPortability);
                }
            }
        }
        private void TestMatch(
            string displayName1,
            string displayName2,
            AssemblyIdentityComparer.ComparisonResult match,
            AssemblyIdentityComparer.ComparisonResult?fusionMatch = null,
            bool ignoreVersion            = false,
            bool partial                  = false,
            bool unificationApplied       = false,
            bool?fusionUnificationApplied = null,
            string policyPath             = null)
        {
            if (fusionMatch == null)
            {
                fusionMatch = match;
            }

            using (var fusionPolicy = policyPath != null ? FusionAssemblyPortabilityPolicy.LoadFromFile(policyPath) : null)
            {
                var comparer = DesktopAssemblyIdentityComparer.Default;

                var policy = default(AssemblyPortabilityPolicy);
                if (policyPath != null)
                {
                    using (var policyStream = new FileStream(policyPath, FileMode.Open, FileAccess.Read))
                    {
                        policy   = AssemblyPortabilityPolicy.LoadFromXml(policyStream);
                        comparer = new DesktopAssemblyIdentityComparer(policy);
                    }
                }

                bool fusionUnificationApplied1;
                var  fusionResult1 = FusionAssemblyIdentityComparer.CompareAssemblyIdentity(displayName1, displayName2, ignoreVersion, policy: fusionPolicy, unificationApplied: out fusionUnificationApplied1);
                Assert.Equal(fusionMatch, fusionResult1);
                Assert.Equal(fusionUnificationApplied ?? unificationApplied, fusionUnificationApplied1);

                AssemblyIdentity      id1, id2;
                AssemblyIdentityParts parts1, parts2;

                Assert.True(AssemblyIdentity.TryParseDisplayName(displayName1, out id1, out parts1));
                Assert.Equal(partial, !AssemblyIdentity.IsFullName(parts1));

                Assert.True(AssemblyIdentity.TryParseDisplayName(displayName2, out id2, out parts2));
                Assert.True(AssemblyIdentity.IsFullName(parts2), "Expected full name");

                bool unificationApplied1;
                var  actual1 = comparer.Compare(null, displayName1, id2, out unificationApplied1, ignoreVersion);
                Assert.Equal(match, actual1);
                Assert.Equal(unificationApplied, unificationApplied1);

                if (!partial && id1 != null)
                {
                    bool unificationApplied2;
                    var  actual2 = comparer.Compare(id1, null, id2, out unificationApplied2, ignoreVersion);
                    Assert.Equal(match, actual2);
                    Assert.Equal(unificationApplied, unificationApplied2);
                }
            }
        }
        internal static AssemblyIdentityComparer.ComparisonResult CompareAssemblyIdentity(
            string fullName1,
            string fullName2,
            bool ignoreVersion,
            FusionAssemblyPortabilityPolicy policy,
            out bool unificationApplied
            )
        {
            unificationApplied = false;
            bool equivalent;
            AssemblyComparisonResult result;
            IntPtr asmConfigCookie = policy == null ? IntPtr.Zero : policy.ConfigCookie;
            int    hr = DefaultModelCompareAssemblyIdentity(
                fullName1,
                ignoreVersion,
                fullName2,
                ignoreVersion,
                out equivalent,
                out result,
                asmConfigCookie
                );

            if (hr != 0 || !equivalent)
            {
                return(AssemblyIdentityComparer.ComparisonResult.NotEquivalent);
            }

            switch (result)
            {
            case AssemblyComparisonResult.EquivalentFullMatch:
                // all properties match
                return(AssemblyIdentityComparer.ComparisonResult.Equivalent);

            case AssemblyComparisonResult.EquivalentWeakNamed:
                // both names are weak (have no public key token) and their simple names match:
                return(AssemblyIdentityComparer.ComparisonResult.Equivalent);

            case AssemblyComparisonResult.EquivalentFxUnified:
                // Framework assembly with unified version.
                unificationApplied = true;
                return(AssemblyIdentityComparer.ComparisonResult.Equivalent);

            case AssemblyComparisonResult.EquivalentUnified:
                // Strong named, all properties but version match.
                Debug.Assert(ignoreVersion);
                return(AssemblyIdentityComparer.ComparisonResult.EquivalentIgnoringVersion);

            default:
                // Partial name was specified:
                return(equivalent
                      ? AssemblyIdentityComparer.ComparisonResult.Equivalent
                      : AssemblyIdentityComparer.ComparisonResult.NotEquivalent);
            }
        }
        public void Fusion_Dispose()
        {
            var appConfig = Temp.CreateFile().WriteAllText(correctAppConfigText);

            var policy = FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig.Path);

            Assert.NotEqual(IntPtr.Zero, policy.ConfigCookie);
            policy.Dispose();
            Assert.Equal(IntPtr.Zero, policy.ConfigCookie);
            policy.Dispose();
            Assert.Equal(IntPtr.Zero, policy.ConfigCookie);
        }
        public bool Equals(FusionAssemblyPortabilityPolicy other)
        {
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            // This is not foolproof, but we just assume that if both assembly
            // policies have the same path and the same timestamp that they are the same.
            // We can't do any better because we don't have access to the config cookie internals.
            return((object)other != null && Enumerable.SequenceEqual(_fileHash, other._fileHash));
        }
        public bool Equals(FusionAssemblyPortabilityPolicy other)
        {
            if (ReferenceEquals(this, other))
            {
                return true;
            }

            // This is not foolproof, but we just assume that if both assembly
            // policies have the same path and the same timestamp that they are the same.
            // We can't do any better because we don't have access to the config cookie internals.
            return (object)other != null &&
                   Enumerable.SequenceEqual(this.fileHash, other.fileHash);
        }
예제 #7
0
        public void LoadFromFile_Errors()
        {
            var appConfig = Temp.CreateFile();
            var stream    = new FileStream(
                appConfig.Path,
                FileMode.Open,
                FileAccess.Read,
                FileShare.ReadWrite
                );

            // not XML:
            appConfig.WriteAllText("garbage");
            stream.Position = 0;
            Assert.Throws <COMException>(
                () => FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig.Path)
                );
            Assert.Throws <XmlException>(() => AssemblyPortabilityPolicy.LoadFromXml(stream));

            // missing root element:
            appConfig.WriteAllText(
                @"<?xml version=""1.0"" encoding=""utf-8"" ?>
"
                );
            stream.Position = 0;

            Assert.Throws <COMException>(
                () => FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig.Path)
                );
            Assert.Throws <XmlException>(() => AssemblyPortabilityPolicy.LoadFromXml(stream));

            // duplicate attribute:
            appConfig.WriteAllText(
                @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
       <supportPortability PKT=""7cec85d7bea7798e"" PKT=""31bf3856ad364e35"" enable=""false""/>
    </assemblyBinding>
  </runtime>
</configuration>
"
                );
            stream.Position = 0;

            Assert.Throws <COMException>(
                () => FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig.Path)
                );
            Assert.Throws <XmlException>(() => AssemblyPortabilityPolicy.LoadFromXml(stream));
        }
        private static bool IsEquivalent(FusionAssemblyPortabilityPolicy policy, string reference, string ported)
        {
            bool equivalent;
            FusionAssemblyIdentityComparer.AssemblyComparisonResult result;

            int hr = FusionAssemblyIdentityComparer.DefaultModelCompareAssemblyIdentity(
                reference,
                false,
                ported,
                false,
                out equivalent,
                out result,
                policy.ConfigCookie);

            return result == FusionAssemblyIdentityComparer.AssemblyComparisonResult.EquivalentFullMatch;
        }
        private static bool IsEquivalent(FusionAssemblyPortabilityPolicy policy, string reference, string ported)
        {
            bool equivalent;

            FusionAssemblyIdentityComparer.AssemblyComparisonResult result;

            int hr = FusionAssemblyIdentityComparer.DefaultModelCompareAssemblyIdentity(
                reference,
                false,
                ported,
                false,
                out equivalent,
                out result,
                policy.ConfigCookie);

            return(result == FusionAssemblyIdentityComparer.AssemblyComparisonResult.EquivalentFullMatch);
        }
예제 #10
0
        public void Fusion_TestEquals()
        {
            var appConfig = Temp.CreateFile().WriteAllText(correctAppConfigText);

            var policy1 = FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig.Path);
            var policy2 = FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig.Path);

            Assert.Equal(policy1, policy2);
            Assert.Equal(policy1.GetHashCode(), policy2.GetHashCode());

            appConfig.WriteAllText(correctAppConfigText);
            policy2 = FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig.Path);
            Assert.Equal(policy1, policy2);
            Assert.Equal(policy1.GetHashCode(), policy2.GetHashCode());

            appConfig = Temp.CreateFile().WriteAllText(correctAppConfigText);
            policy2   = FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig.Path);
            Assert.Equal(policy1, policy2);
            Assert.Equal(policy1.GetHashCode(), policy2.GetHashCode());

            appConfig.WriteAllText(
                @"
<?xml version=""1.0"" encoding=""utf-8"" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
       <supportPortability PKT=""01234567890abcdef"" enable=""false""/>
    </assemblyBinding>
  </runtime>
</configuration>"
                );

            policy2 = FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig.Path);
            Assert.NotEqual(policy1, policy2);

            var appConfig2 = Temp.CreateFile().WriteAllText(correctAppConfigText);

            policy2 = FusionAssemblyPortabilityPolicy.LoadFromFile(appConfig2.Path);
            Assert.Equal(policy1, policy2);
            Assert.Equal(policy1.GetHashCode(), policy2.GetHashCode());
        }
        internal static AssemblyIdentityComparer.ComparisonResult CompareAssemblyIdentity(string fullName1, string fullName2, bool ignoreVersion, FusionAssemblyPortabilityPolicy policy, out bool unificationApplied)
        {
            unificationApplied = false;
            bool equivalent;
            AssemblyComparisonResult result;
            IntPtr asmConfigCookie = policy == null ? IntPtr.Zero : policy.ConfigCookie;
            int hr = DefaultModelCompareAssemblyIdentity(fullName1, ignoreVersion, fullName2, ignoreVersion, out equivalent, out result, asmConfigCookie);
            if (hr != 0 || !equivalent)
            {
                return AssemblyIdentityComparer.ComparisonResult.NotEquivalent;
            }

            switch (result)
            {
                case AssemblyComparisonResult.EquivalentFullMatch:
                    // all properties match
                    return AssemblyIdentityComparer.ComparisonResult.Equivalent;

                case AssemblyComparisonResult.EquivalentWeakNamed:
                    // both names are weak (have no public key token) and their simple names match:
                    return AssemblyIdentityComparer.ComparisonResult.Equivalent;

                case AssemblyComparisonResult.EquivalentFxUnified:
                    // Framework assembly with unified version.
                    unificationApplied = true;
                    return AssemblyIdentityComparer.ComparisonResult.Equivalent;

                case AssemblyComparisonResult.EquivalentUnified:
                    // Strong named, all properties but version match.
                    Debug.Assert(ignoreVersion);
                    return AssemblyIdentityComparer.ComparisonResult.EquivalentIgnoringVersion;

                default:
                    // Partial name was specified:
                    return equivalent ? AssemblyIdentityComparer.ComparisonResult.Equivalent : AssemblyIdentityComparer.ComparisonResult.NotEquivalent;
            }
        }