|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Reflection; |
| 4 | +using System.Security; |
| 5 | +using System.Security.Permissions; |
| 6 | +using LibGit2Sharp.Tests.TestHelpers; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace LibGit2Sharp.Tests |
| 10 | +{ |
| 11 | + public class ShadowCopyFixture : BaseFixture |
| 12 | + { |
| 13 | + [SkippableFact] |
| 14 | + public void CanProbeForNativeBinariesFromAShadowCopiedAssembly() |
| 15 | + { |
| 16 | + Type type = typeof(Wrapper); |
| 17 | + Assembly assembly = type.Assembly; |
| 18 | + |
| 19 | + // Build a new domain which will shadow copy assemblies |
| 20 | + string cachePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 21 | + Directory.CreateDirectory(cachePath); |
| 22 | + |
| 23 | + var setup = new AppDomainSetup |
| 24 | + { |
| 25 | + ApplicationBase = Path.GetDirectoryName(new Uri(assembly.CodeBase).LocalPath), |
| 26 | + ApplicationName = "ShadowWalker", |
| 27 | + ShadowCopyFiles = "true", |
| 28 | + CachePath = cachePath |
| 29 | + }; |
| 30 | + |
| 31 | + setup.ShadowCopyDirectories = setup.ApplicationBase; |
| 32 | + |
| 33 | + AppDomain domain = AppDomain.CreateDomain( |
| 34 | + setup.ApplicationName, |
| 35 | + null, |
| 36 | + setup, new PermissionSet(PermissionState.Unrestricted)); |
| 37 | + |
| 38 | + // Instantiate from the remote domain |
| 39 | + var wrapper = (Wrapper)domain.CreateInstanceAndUnwrap(assembly.FullName, type.FullName); |
| 40 | + |
| 41 | + // Ensure that LibGit2Sharp correctly probes for the native binaries |
| 42 | + // from the other domain |
| 43 | + string repoPath = BuildSelfCleaningDirectory().DirectoryPath; |
| 44 | + wrapper.CanInitANewRepositoryFromAShadowCopiedAssembly(repoPath); |
| 45 | + |
| 46 | + Assembly sourceAssembly = typeof(Repository).Assembly; |
| 47 | + |
| 48 | + // Ensure both assemblies share the same escaped code base... |
| 49 | + string cachedAssemblyEscapedCodeBase = wrapper.AssemblyEscapedCodeBase; |
| 50 | + Assert.Equal(sourceAssembly.EscapedCodeBase, cachedAssemblyEscapedCodeBase); |
| 51 | + |
| 52 | + // ...but are currently loaded from different locations... |
| 53 | + string cachedAssemblyLocation = wrapper.AssemblyLocation; |
| 54 | + Assert.NotEqual(sourceAssembly.Location, cachedAssemblyLocation); |
| 55 | + |
| 56 | + // ...that the assembly in the other domain is stored in the shadow copy cache... |
| 57 | + string cachedAssembliesPath = Path.Combine(setup.CachePath, setup.ApplicationName); |
| 58 | + Assert.True(cachedAssemblyLocation.StartsWith(cachedAssembliesPath)); |
| 59 | + |
| 60 | + // ...that this cache doesn't contain the `NativeBinaries` folder |
| 61 | + string cachedAssemblyParentPath = Path.GetDirectoryName(cachedAssemblyLocation); |
| 62 | + Assert.False(Directory.Exists(Path.Combine(cachedAssemblyParentPath, "NativeBinaries"))); |
| 63 | + |
| 64 | + // ...whereas `NativeBinaries` of course exists next to the source assembly |
| 65 | + string sourceAssemblyParentPath = Path.GetDirectoryName(new Uri(sourceAssembly.EscapedCodeBase).LocalPath); |
| 66 | + Assert.True(Directory.Exists(Path.Combine(sourceAssemblyParentPath, "NativeBinaries"))); |
| 67 | + |
| 68 | + AppDomain.Unload(domain); |
| 69 | + } |
| 70 | + |
| 71 | + public class Wrapper : MarshalByRefObject |
| 72 | + { |
| 73 | + private readonly Assembly assembly; |
| 74 | + public Wrapper() |
| 75 | + { |
| 76 | + assembly = typeof(Repository).Assembly; |
| 77 | + } |
| 78 | + |
| 79 | + public void CanInitANewRepositoryFromAShadowCopiedAssembly(string path) |
| 80 | + { |
| 81 | + var gitDirPath = Repository.Init(path); |
| 82 | + |
| 83 | + using (var repo = new Repository(gitDirPath)) |
| 84 | + { |
| 85 | + Assert.NotNull(repo); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public string AssemblyLocation |
| 90 | + { |
| 91 | + get |
| 92 | + { |
| 93 | + return assembly.Location; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public string AssemblyEscapedCodeBase |
| 98 | + { |
| 99 | + get |
| 100 | + { |
| 101 | + return assembly.EscapedCodeBase; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments