Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ShadowCopyFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 546b767044b65a22bcb831f7b097406291dc32ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.IO;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
{
    public class ShadowCopyFixture : BaseFixture
    {
        [Fact]
        public void CanProbeForNativeBinariesFromAShadowCopiedAssembly()
        {
            Type type = typeof(Wrapper);
            Assembly assembly = type.Assembly;

            // Build a new domain which will shadow copy assemblies
            string cachePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            Directory.CreateDirectory(cachePath);

            var setup = new AppDomainSetup
            {
                ApplicationBase = Path.GetDirectoryName(new Uri(assembly.CodeBase).LocalPath),
                ApplicationName = "ShadowWalker",
                ShadowCopyFiles = "true",
                CachePath = cachePath
            };

            setup.ShadowCopyDirectories = setup.ApplicationBase;

            AppDomain domain = AppDomain.CreateDomain(
                setup.ApplicationName,
                null,
                setup, new PermissionSet(PermissionState.Unrestricted));

            // Instantiate from the remote domain
            var wrapper = (Wrapper)domain.CreateInstanceAndUnwrap(assembly.FullName, type.FullName);

            // Ensure that LibGit2Sharp correctly probes for the native binaries
            // from the other domain
            string repoPath = BuildSelfCleaningDirectory().DirectoryPath;
            wrapper.CanInitANewRepositoryFromAShadowCopiedAssembly(repoPath);

            Assembly sourceAssembly = typeof(IRepository).Assembly;

            // Ensure both assemblies share the same escaped code base...
            string cachedAssemblyEscapedCodeBase = wrapper.AssemblyEscapedCodeBase;
            Assert.Equal(sourceAssembly.EscapedCodeBase, cachedAssemblyEscapedCodeBase);

            // ...but are currently loaded from different locations...
            string cachedAssemblyLocation = wrapper.AssemblyLocation;
            if (cachedAssemblyLocation.StartsWith("/private"))
            {
                // On OS X, sometimes you get /private/var/… instead of /var/…, but they map to the same place.
                cachedAssemblyLocation = cachedAssemblyLocation.Substring("/private".Length);
            }
            Assert.NotEqual(sourceAssembly.Location, cachedAssemblyLocation);

            // ...that the assembly in the other domain is stored in the shadow copy cache...
            string cachedAssembliesPath = Path.Combine(setup.CachePath, setup.ApplicationName);
            Assert.True(cachedAssemblyLocation.StartsWith(cachedAssembliesPath));

            if (!IsRunningOnUnix())
            {
                // ...that this cache doesn't contain the `NativeBinaries` folder
                string cachedAssemblyParentPath = Path.GetDirectoryName(cachedAssemblyLocation);
                Assert.False(Directory.Exists(Path.Combine(cachedAssemblyParentPath, "NativeBinaries")));

                // ...whereas `NativeBinaries` of course exists next to the source assembly
                string sourceAssemblyParentPath =
                    Path.GetDirectoryName(new Uri(sourceAssembly.EscapedCodeBase).LocalPath);
                Assert.True(Directory.Exists(Path.Combine(sourceAssemblyParentPath, "NativeBinaries")));
            }

            AppDomain.Unload(domain);
        }

        public class Wrapper : MarshalByRefObject
        {
            private readonly Assembly assembly;
            public Wrapper()
            {
                assembly = typeof(IRepository).Assembly;
            }

            public void CanInitANewRepositoryFromAShadowCopiedAssembly(string path)
            {
                var gitDirPath = Repository.Init(path);

                using (var repo = new Repository(gitDirPath))
                {
                    Assert.NotNull(repo);
                }
            }

            public string AssemblyLocation
            {
                get
                {
                    return assembly.Location;
                }
            }

            public string AssemblyEscapedCodeBase
            {
                get
                {
                    return assembly.EscapedCodeBase;
                }
            }
        }
    }
}