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); } } }
public void LoadFromFile_LeadingWhitespace() { var appConfig = Temp.CreateFile(); // whitespace in front of header: appConfig.WriteAllText( @" <?xml version=""1.0"" encoding=""utf-8"" ?> <configuration> <runtime> <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1""> <supportPortability PKT=""7cec85d7bea7798e"" enable=""false""/> <!-- platform --> </assemblyBinding> </runtime> </configuration> " ); var stream = new FileStream( appConfig.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ); AssertIsEnabled(appConfig.Path, platform: false, nonPlatform: true, fusionOnly: true); Assert.Throws <XmlException>(() => AssemblyPortabilityPolicy.LoadFromXml(stream)); }
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 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)); }
/// <param name="policy">Assembly portability policy, usually provided through an app.config file.</param> internal DesktopAssemblyIdentityComparer(AssemblyPortabilityPolicy policy) { this.policy = policy; }