public void CatchesFileNotFoundAfterFileDeleted()
        {
            MockFileSystemWithCallbacks fileSystem = new MockFileSystemWithCallbacks();

            fileSystem.OnFileExists     = () => true;
            fileSystem.OnOpenFileStream = (path, fileMode, fileAccess) =>
            {
                if (fileAccess == FileAccess.Write)
                {
                    return(new MemoryStream());
                }

                throw new FileNotFoundException();
            };

            MockHttpGitObjects httpObjects = new MockHttpGitObjects();

            using (httpObjects.InputStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(ValidTestObjectFileContents)))
            {
                httpObjects.MediaType = GVFSConstants.MediaTypes.LooseObjectMediaType;
                GVFSGitObjects dut = this.CreateTestableGVFSGitObjects(httpObjects, fileSystem);

                dut.TryCopyBlobContentStream(
                    ValidTestObjectFileContents,
                    new CancellationToken(),
                    GVFSGitObjects.RequestSource.FileStreamCallback,
                    (stream, length) => Assert.Fail("Should not be able to call copy stream callback"))
                .ShouldEqual(false);
            }
        }
Ejemplo n.º 2
0
        public void WriteLooseObject_Success()
        {
            ITracer        tracer     = new MockTracer();
            GVFSEnlistment enlistment = new MockGVFSEnlistment();
            MockFileSystemWithCallbacks filesystem = new MockFileSystemWithCallbacks();
            GVFSContext context = new GVFSContext(tracer, filesystem, null, enlistment);

            GitObjects gitObjects = new GVFSGitObjects(context, null);

            this.openedPaths.Clear();
            filesystem.OnOpenFileStream = this.OnOpenFileStream;
            filesystem.OnFileExists     = this.OnFileExists;

            bool moved = false;

            filesystem.OnMoveFile = (path1, path2) => { moved = true; };

            using (Stream stream = new MemoryStream())
            {
                stream.Write(this.realData, 0, this.realData.Length);
                stream.Position = 0;
                gitObjects.WriteLooseObject(stream, RealSha, true, new byte[128]);
            }

            this.openedPaths.Count.ShouldEqual(2, "Incorrect number of opened paths");
            moved.ShouldBeTrue("File was not moved");
        }
Ejemplo n.º 3
0
        public void WriteLooseObject_DetectsIncorrectData()
        {
            ITracer        tracer     = new MockTracer();
            GVFSEnlistment enlistment = new MockGVFSEnlistment();
            MockFileSystemWithCallbacks filesystem = new MockFileSystemWithCallbacks();
            GVFSContext context = new GVFSContext(tracer, filesystem, null, enlistment);

            GitObjects gitObjects = new GVFSGitObjects(context, null);

            this.openedPaths.Clear();
            filesystem.OnOpenFileStream = this.OnOpenFileStream;
            filesystem.OnFileExists     = this.OnFileExists;

            bool foundException = false;

            try
            {
                using (Stream stream = new MemoryStream())
                {
                    stream.Write(this.realData, 0, this.realData.Length);
                    stream.Position = 0;
                    gitObjects.WriteLooseObject(stream, EmptySha, true, new byte[128]);
                }
            }
            catch (SecurityException ex)
            {
                foundException = true;
                ex.Message.ShouldContain($"Requested object with hash {EmptySha} but received object with hash");
            }

            foundException.ShouldBeTrue("Failed to throw SecurityException");
            this.openedPaths.Count.ShouldEqual(1, "Incorrect number of opened paths (one to write temp file)");
            this.openedPaths[0].IndexOf(EmptySha.Substring(2)).ShouldBeAtMost(-1, "Should not have written to the loose object location");
        }
        private void AssertRetryableExceptionOnDownload(
            MemoryStream inputStream,
            string mediaType,
            Action <GVFSGitObjects> download)
        {
            MockHttpGitObjects httpObjects = new MockHttpGitObjects();

            httpObjects.InputStream = inputStream;
            httpObjects.MediaType   = mediaType;
            MockFileSystemWithCallbacks fileSystem = new MockFileSystemWithCallbacks();

            using (ReusableMemoryStream downloadDestination = new ReusableMemoryStream(string.Empty))
            {
                fileSystem.OnFileExists     = () => false;
                fileSystem.OnOpenFileStream = (path, mode, access) => downloadDestination;

                GVFSGitObjects gitObjects = this.CreateTestableGVFSGitObjects(httpObjects, fileSystem);

                Assert.Throws <RetryableException>(() => download(gitObjects));
                inputStream.Dispose();
            }
        }
        private GVFSGitObjects CreateTestableGVFSGitObjects(MockHttpGitObjects httpObjects, MockFileSystemWithCallbacks fileSystem)
        {
            MockTracer     tracer     = new MockTracer();
            GVFSEnlistment enlistment = new GVFSEnlistment(TestEnlistmentRoot, "https://fakeRepoUrl", "fakeGitBinPath", authentication: null);

            enlistment.InitializeCachePathsFromKey(TestLocalCacheRoot, TestObjectRoot);
            GitRepo repo = new GitRepo(tracer, enlistment, fileSystem, () => new MockLibGit2Repo(tracer));

            GVFSContext    context = new GVFSContext(tracer, fileSystem, repo, enlistment);
            GVFSGitObjects dut     = new GVFSGitObjects(context, httpObjects);

            return(dut);
        }
Ejemplo n.º 6
0
        private GVFSGitObjects CreateTestableGVFSGitObjects(MockHttpGitObjects httpObjects, MockFileSystemWithCallbacks fileSystem)
        {
            MockTracer     tracer     = new MockTracer();
            GVFSEnlistment enlistment = new GVFSEnlistment(TestEnlistmentRoot, "https://fakeRepoUrl", "fakeGitBinPath", gvfsHooksRoot: null);
            GitRepo        repo       = new GitRepo(tracer, enlistment, fileSystem, () => new MockLibGit2Repo(tracer));

            GVFSContext    context = new GVFSContext(tracer, fileSystem, repo, enlistment);
            GVFSGitObjects dut     = new GVFSGitObjects(context, httpObjects);

            return(dut);
        }