public void InitCallbackMadeWhenUsingTheFilter()
        {
            bool   called             = false;
            Action initializeCallback = () =>
            {
                called = true;
            };

            var filter = new FakeFilter(FilterName,
                                        attributes,
                                        successCallback,
                                        successCallback,
                                        initializeCallback);
            var registration = GlobalSettings.RegisterFilter(filter);

            try
            {
                Assert.False(called);

                string repoPath = InitNewRepository();
                using (var repo = CreateTestRepository(repoPath))
                {
                    StageNewFile(repo);
                    Assert.True(called);
                }
            }
            finally
            {
                GlobalSettings.DeregisterFilter(registration);
            }
        }
        public void WhenCheckingOutAFileFileSmudgeWritesCorrectFileToWorkingDirectory()
        {
            const string decodedInput = "This is a substitution cipher";
            const string encodedInput = "Guvf vf n fhofgvghgvba pvcure";

            const string branchName = "branch";
            string       repoPath   = InitNewRepository();

            Action <Stream, Stream> smudgeCallback = SubstitutionCipherFilter.RotateByThirteenPlaces;

            var filter       = new FakeFilter(FilterName, attributes, null, smudgeCallback);
            var registration = GlobalSettings.RegisterFilter(filter);

            try
            {
                FileInfo expectedFile = CheckoutFileForSmudge(repoPath, branchName, encodedInput);

                string combine     = Path.Combine(repoPath, "..", expectedFile.Name);
                string readAllText = File.ReadAllText(combine);
                Assert.Equal(decodedInput, readAllText);
            }
            finally
            {
                GlobalSettings.DeregisterFilter(registration);
            }
        }
        public void WhenStagingFileApplyIsCalledWithCleanForCorrectPath()
        {
            string repoPath = InitNewRepository();
            bool   called   = false;

            Action <Stream, Stream> clean = (reader, writer) =>
            {
                called = true;
                reader.CopyTo(writer);
            };

            var filter       = new FakeFilter(FilterName, attributes, clean);
            var registration = GlobalSettings.RegisterFilter(filter);

            try
            {
                using (var repo = CreateTestRepository(repoPath))
                {
                    StageNewFile(repo);
                    Assert.True(called);
                }
            }
            finally
            {
                GlobalSettings.DeregisterFilter(registration);
            }
        }
        public void CleanFilterWritesOutputToObjectTree()
        {
            const string decodedInput = "This is a substitution cipher";
            const string encodedInput = "Guvf vf n fhofgvghgvba pvcure";

            string repoPath = InitNewRepository();

            Action <Stream, Stream> cleanCallback = SubstitutionCipherFilter.RotateByThirteenPlaces;

            var filter       = new FakeFilter(FilterName, attributes, cleanCallback);
            var registration = GlobalSettings.RegisterFilter(filter);

            try
            {
                using (var repo = CreateTestRepository(repoPath))
                {
                    FileInfo expectedFile = StageNewFile(repo, decodedInput);
                    var      commit       = repo.Commit("Clean that file", Constants.Signature, Constants.Signature);
                    var      blob         = (Blob)commit.Tree[expectedFile.Name].Target;

                    var textDetected = blob.GetContentText();
                    Assert.Equal(encodedInput, textDetected);
                }
            }
            finally
            {
                GlobalSettings.DeregisterFilter(registration);
            }
        }
        public void WhenStagedFileDoesNotMatchPathSpecFileIsNotFiltered(string pathSpec, string fileExtension, int cleanCount, int smudgeCount)
        {
            const string filterName         = "rot13";
            const string decodedInput       = "This is a substitution cipher";
            string       attributeFileEntry = string.Format("{0} filter={1}", pathSpec, filterName);

            var filterForAttributes = new List <FilterAttributeEntry> {
                new FilterAttributeEntry(filterName)
            };
            var filter = new SubstitutionCipherFilter("cipher-filter", filterForAttributes);

            var filterRegistration = GlobalSettings.RegisterFilter(filter);

            string repoPath = InitNewRepository();
            string fileName = Guid.NewGuid() + fileExtension;

            using (var repo = new Repository(repoPath))
            {
                CreateConfigurationWithDummyUser(repo, Constants.Identity);
                CreateAttributesFile(repo, attributeFileEntry);

                CommitOnBranchAndReturnDatabaseBlob(repo, fileName, decodedInput);

                Assert.Equal(cleanCount, filter.CleanCalledCount);
                Assert.Equal(smudgeCount, filter.SmudgeCalledCount);
            }

            GlobalSettings.DeregisterFilter(filterRegistration);
        }
        public void SmudgeIsCalledIfAttributeEntryMatches(string filterAttribute, string attributeEntry, int smudgeCount)
        {
            const string decodedInput = "This is a substitution cipher";

            var filterForAttributes = new List <FilterAttributeEntry> {
                new FilterAttributeEntry(filterAttribute)
            };
            var filter = new SubstitutionCipherFilter("cipher-filter", filterForAttributes);

            var filterRegistration = GlobalSettings.RegisterFilter(filter);

            string repoPath = InitNewRepository();
            string fileName = Guid.NewGuid() + ".txt";

            using (var repo = new Repository(repoPath))
            {
                CreateConfigurationWithDummyUser(repo, Constants.Identity);
                CreateAttributesFile(repo, attributeEntry);

                CommitOnBranchAndReturnDatabaseBlob(repo, fileName, decodedInput);

                var branch = repo.CreateBranch("delete-files");
                repo.Checkout(branch.FriendlyName);

                DeleteFile(repo, fileName);

                repo.Checkout("master");

                Assert.Equal(smudgeCount, filter.SmudgeCalledCount);
            }

            GlobalSettings.DeregisterFilter(filterRegistration);
        }
Exemplo n.º 7
0
        public void CleanIsCalledIfAttributeEntryMatches(string filterAttribute, string attributeEntry, int cleanCount)
        {
            const string decodedInput = "This is a substitution cipher";

            var filterForAttributes = new List <FilterAttributeEntry> {
                new FilterAttributeEntry(filterAttribute)
            };
            var filter = new SubstitutionCipherFilter("cipher-filter", filterForAttributes);

            var filterRegistration = GlobalSettings.RegisterFilter(filter);

            string repoPath = InitNewRepository();
            string fileName = Guid.NewGuid() + ".txt";

            string configPath        = CreateConfigurationWithDummyUser(Constants.Identity);
            var    repositoryOptions = new RepositoryOptions {
                GlobalConfigurationLocation = configPath
            };

            using (var repo = new Repository(repoPath, repositoryOptions))
            {
                CreateAttributesFile(repo, attributeEntry);

                CommitOnBranchAndReturnDatabaseBlob(repo, fileName, decodedInput);

                Assert.Equal(cleanCount, filter.CleanCalledCount);
            }

            GlobalSettings.DeregisterFilter(filterRegistration);
        }
        public void CanRegisterAndDeregisterAfterGarbageCollection()
        {
            var filterRegistration = GlobalSettings.RegisterFilter(new EmptyFilter(FilterName, attributes));

            GC.Collect();

            GlobalSettings.DeregisterFilter(filterRegistration);
        }
        public void CanRegisterAndUnregisterTheSameFilter()
        {
            var filter = new EmptyFilter(FilterName, attributes);

            var registration = GlobalSettings.RegisterFilter(filter);

            GlobalSettings.DeregisterFilter(registration);

            var secondRegistration = GlobalSettings.RegisterFilter(filter);

            GlobalSettings.DeregisterFilter(secondRegistration);
        }
Exemplo n.º 10
0
        public void CorrectlyEncodesAndDecodesInput()
        {
            const string decodedInput = "This is a substitution cipher";
            const string encodedInput = "Guvf vf n fhofgvghgvba pvcure";

            var attributes = new List <FilterAttributeEntry> {
                new FilterAttributeEntry("rot13")
            };
            var filter             = new SubstitutionCipherFilter("cipher-filter", attributes);
            var filterRegistration = GlobalSettings.RegisterFilter(filter);

            string repoPath          = InitNewRepository();
            string fileName          = Guid.NewGuid() + ".rot13";
            string configPath        = CreateConfigurationWithDummyUser(Constants.Identity);
            var    repositoryOptions = new RepositoryOptions {
                GlobalConfigurationLocation = configPath
            };

            using (var repo = new Repository(repoPath, repositoryOptions))
            {
                CreateAttributesFile(repo, "*.rot13 filter=rot13");

                var blob         = CommitOnBranchAndReturnDatabaseBlob(repo, fileName, decodedInput);
                var textDetected = blob.GetContentText();

                Assert.Equal(encodedInput, textDetected);
                Assert.Equal(1, filter.CleanCalledCount);
                Assert.Equal(0, filter.SmudgeCalledCount);

                var branch = repo.CreateBranch("delete-files");
                repo.Checkout(branch.FriendlyName);

                DeleteFile(repo, fileName);

                repo.Checkout("master");

                var fileContents = ReadTextFromFile(repo, fileName);
                Assert.Equal(1, filter.SmudgeCalledCount);
                Assert.Equal(decodedInput, fileContents);
            }

            GlobalSettings.DeregisterFilter(filterRegistration);
        }
Exemplo n.º 11
0
        public void CanHandleMultipleSmudgesConcurrently()
        {
            const string decodedInput = "This is a substitution cipher";
            const string encodedInput = "Guvf vf n fhofgvghgvba pvcure";

            const string branchName = "branch";

            Action <Stream, Stream> smudgeCallback = SubstitutionCipherFilter.RotateByThirteenPlaces;

            var filter       = new FakeFilter(FilterName, attributes, null, smudgeCallback);
            var registration = GlobalSettings.RegisterFilter(filter);

            try
            {
                int count = 30;
                var tasks = new Task <FileInfo> [count];

                for (int i = 0; i < count; i++)
                {
                    tasks[i] = Task.Factory.StartNew(() =>
                    {
                        string repoPath = InitNewRepository();
                        return(CheckoutFileForSmudge(repoPath, branchName, encodedInput));
                    });
                }

                Task.WaitAll(tasks);

                foreach (var task in tasks)
                {
                    FileInfo expectedFile = task.Result;

                    string readAllText = File.ReadAllText(expectedFile.FullName);
                    Assert.Equal(decodedInput, readAllText);
                }
            }
            finally
            {
                GlobalSettings.DeregisterFilter(registration);
            }
        }
        public void DoubleRegistrationFailsButDoubleDeregistrationDoesNot()
        {
            Assert.Equal(0, GlobalSettings.GetRegisteredFilters().Count());

            var filter       = new EmptyFilter(FilterName, attributes);
            var registration = GlobalSettings.RegisterFilter(filter);

            Assert.Throws <EntryExistsException>(() => { GlobalSettings.RegisterFilter(filter); });
            Assert.Equal(1, GlobalSettings.GetRegisteredFilters().Count());

            Assert.True(registration.IsValid, "FilterRegistration.IsValid should be true.");

            GlobalSettings.DeregisterFilter(registration);
            Assert.Equal(0, GlobalSettings.GetRegisteredFilters().Count());

            Assert.False(registration.IsValid, "FilterRegistration.IsValid should be false.");

            GlobalSettings.DeregisterFilter(registration);
            Assert.Equal(0, GlobalSettings.GetRegisteredFilters().Count());

            Assert.False(registration.IsValid, "FilterRegistration.IsValid should be false.");
        }
        public void InitCallbackNotMadeWhenFilterNeverUsed()
        {
            bool   called             = false;
            Action initializeCallback = () =>
            {
                called = true;
            };

            var filter = new FakeFilter(FilterName,
                                        attributes,
                                        successCallback,
                                        successCallback,
                                        initializeCallback);
            var registration = GlobalSettings.RegisterFilter(filter);

            try
            {
                Assert.False(called);
            }
            finally
            {
                GlobalSettings.DeregisterFilter(registration);
            }
        }
        public void CanFilterLargeFiles()
        {
            const int  ContentLength = 128 * 1024 * 1024 - 13;
            const char ContentValue  = 'x';

            char[] content = (new string(ContentValue, 1024)).ToCharArray();

            string repoPath = InitNewRepository();

            var filter       = new FileExportFilter(FilterName, attributes);
            var registration = GlobalSettings.RegisterFilter(filter);

            try
            {
                string   filePath    = Path.Combine(Directory.GetParent(repoPath).Parent.FullName, Guid.NewGuid().ToString() + ".blob");
                FileInfo contentFile = new FileInfo(filePath);
                using (var writer = new StreamWriter(contentFile.OpenWrite())
                {
                    AutoFlush = true
                })
                {
                    int remain = ContentLength;

                    while (remain > 0)
                    {
                        int chunkSize = remain > content.Length ? content.Length : remain;
                        writer.Write(content, 0, chunkSize);
                        remain -= chunkSize;
                    }
                }

                string   attributesPath = Path.Combine(Directory.GetParent(repoPath).Parent.FullName, ".gitattributes");
                FileInfo attributesFile = new FileInfo(attributesPath);

                string configPath        = CreateConfigurationWithDummyUser(Constants.Identity);
                var    repositoryOptions = new RepositoryOptions {
                    GlobalConfigurationLocation = configPath
                };

                using (Repository repo = new Repository(repoPath, repositoryOptions))
                {
                    File.WriteAllText(attributesPath, "*.blob filter=test");
                    repo.Stage(attributesFile.Name);
                    repo.Stage(contentFile.Name);
                    repo.Commit("test", Constants.Signature, Constants.Signature);
                    contentFile.Delete();
                    repo.Checkout("HEAD", new CheckoutOptions()
                    {
                        CheckoutModifiers = CheckoutModifiers.Force
                    });
                }

                contentFile = new FileInfo(filePath);
                Assert.True(contentFile.Exists, "Contents not restored correctly by forced checkout.");
                using (StreamReader reader = contentFile.OpenText())
                {
                    int    totalRead = 0;
                    char[] block     = new char[1024];
                    int    read;
                    while ((read = reader.Read(block, 0, block.Length)) > 0)
                    {
                        Assert.True(CharArrayAreEqual(block, content, read));
                        totalRead += read;
                    }

                    Assert.Equal(ContentLength, totalRead);
                }

                contentFile.Delete();
            }
            finally
            {
                GlobalSettings.DeregisterFilter(registration);
            }
        }