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); } } }
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 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)); }
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()); }