-
Notifications
You must be signed in to change notification settings - Fork 474
Fix NullReferenceException when cloning into a non-empty directory #2065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tyrielv
wants to merge
2
commits into
microsoft:master
Choose a base branch
from
tyrielv:tyrielv/fix-clone-nonempty-nre
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,4 +118,3 @@ | |
|
|
||
|
|
||
| </Project> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| using GVFS.Common; | ||
| using GVFS.CommandLine; | ||
| using GVFS.UnitTests.Mock.Common; | ||
| using NUnit.Framework; | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace GVFS.UnitTests.CommandLine | ||
| { | ||
| [TestFixture] | ||
| public class CloneVerbTests | ||
| { | ||
| private CloneVerb cloneVerb; | ||
| private string testDir; | ||
|
|
||
| [SetUp] | ||
| public void Setup() | ||
| { | ||
| this.cloneVerb = new CloneVerb(); | ||
| this.testDir = Path.Combine(Path.GetTempPath(), "CloneVerbTests_" + Guid.NewGuid().ToString("N")); | ||
| Directory.CreateDirectory(this.testDir); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown() | ||
| { | ||
| if (Directory.Exists(this.testDir)) | ||
| { | ||
| Directory.Delete(this.testDir, recursive: true); | ||
| } | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TryCreateEnlistmentFailsWithoutEnlistmentWhenTargetDirectoryIsNotEmpty() | ||
| { | ||
| File.WriteAllText(Path.Combine(this.testDir, "preexisting.txt"), "content"); | ||
|
|
||
| CloneVerb.Result result = this.cloneVerb.TryCreateEnlistment( | ||
| this.testDir, | ||
| this.testDir, | ||
| out GVFSEnlistment enlistment); | ||
|
|
||
| Assert.IsFalse(result.Success); | ||
| Assert.IsNull(enlistment); | ||
| StringAssert.Contains("exists and is not empty", result.ErrorMessage); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TryCreateEnlistmentDoesNotFailForEmptyTargetDirectory() | ||
| { | ||
| // testDir is created empty by Setup and never written to in this test. | ||
| CloneVerb.Result result = this.cloneVerb.TryCreateEnlistment( | ||
| this.testDir, | ||
| this.testDir, | ||
| out GVFSEnlistment enlistment); | ||
|
|
||
| StringAssert.DoesNotContain("exists and is not empty", result.ErrorMessage ?? string.Empty); | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void TryCreateEnlistmentReportsNormalizedPathWhenItDiffersFromFullPath() | ||
| { | ||
| File.WriteAllText(Path.Combine(this.testDir, "preexisting.txt"), "content"); | ||
| string fullPath = this.testDir + Path.DirectorySeparatorChar; | ||
|
|
||
| CloneVerb.Result result = this.cloneVerb.TryCreateEnlistment( | ||
| fullPath, | ||
| this.testDir, | ||
| out GVFSEnlistment enlistment); | ||
|
|
||
| Assert.IsFalse(result.Success); | ||
| Assert.IsNull(enlistment); | ||
| StringAssert.Contains($"'{fullPath}'", result.ErrorMessage); | ||
| StringAssert.Contains($"['{this.testDir}']", result.ErrorMessage); | ||
| } | ||
|
|
||
| // Regression test: this is the actual code path that used to throw a | ||
| // NullReferenceException when `gvfs clone` targeted a non-empty directory. | ||
| // TryCreateEnlistment (above) fails and returns a null enlistment; CloneVerb.Execute() | ||
| // used to unconditionally dereference that null enlistment to read the trustPackIndexes | ||
| // config, crashing instead of reporting the "exists and is not empty" error. Execute() | ||
| // itself cannot be unit-tested (it terminates the process via Environment.Exit()), so | ||
| // GetTrustPackIndexes was extracted as the smallest testable seam that reproduces the | ||
| // exact failure condition: a failed clone result with a null enlistment. | ||
| [TestCase] | ||
| public void GetTrustPackIndexesDoesNotThrowWhenCloneFailedAndEnlistmentIsNull() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
| CloneVerb.Result failedCloneResult = new CloneVerb.Result("Clone directory exists and is not empty"); | ||
|
|
||
| bool trustPackIndexes = true; | ||
| Assert.DoesNotThrow(() => trustPackIndexes = this.cloneVerb.GetTrustPackIndexes(tracer, failedCloneResult, enlistment: null)); | ||
|
|
||
| Assert.AreEqual(GVFSConstants.GitConfig.TrustPackIndexesDefault, trustPackIndexes); | ||
| } | ||
| } | ||
| } |
127 changes: 127 additions & 0 deletions
127
GVFS/GVFS.UnitTests/Common/LibGit2RepoConfigLookupTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| using GVFS.Common.Git; | ||
| using GVFS.Tests.Should; | ||
| using GVFS.UnitTests.Mock.Common; | ||
| using NUnit.Framework; | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace GVFS.UnitTests.Common | ||
| { | ||
| [TestFixture] | ||
| public class LibGit2RepoConfigLookupTests | ||
| { | ||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnRepoReturnsConfiguredValue() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| using (MockConfigRepo repo = new MockConfigRepo(tracer, true)) | ||
| { | ||
| bool value = repo.GetConfigBoolOrDefault("gvfs.test", false); | ||
|
|
||
| value.ShouldEqual(true); | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(0); | ||
| } | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnRepoReturnsDefaultWhenKeyIsUnset() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| using (MockConfigRepo repo = new MockConfigRepo(tracer, (bool?)null)) | ||
| { | ||
| bool value = repo.GetConfigBoolOrDefault("gvfs.test", true); | ||
|
|
||
| value.ShouldEqual(true); | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(0); | ||
| } | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnRepoReturnsDefaultOnLibGit2ExceptionAndLogsOnce() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| using (MockConfigRepo repo = new MockConfigRepo(tracer, new LibGit2Exception("boom"))) | ||
| { | ||
| bool value = repo.GetConfigBoolOrDefault("gvfs.test", false); | ||
|
|
||
| value.ShouldEqual(false); | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(1); | ||
| tracer.RelatedWarningEvents[0].ShouldContain("Failed to read gvfs.test config, using default: boom"); | ||
| } | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnRepoReturnsDefaultOnInvalidDataExceptionAndLogsOnce() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| using (MockConfigRepo repo = new MockConfigRepo(tracer, new InvalidDataException("corrupt config"))) | ||
| { | ||
| bool value = repo.GetConfigBoolOrDefault("gvfs.test", false); | ||
|
|
||
| value.ShouldEqual(false); | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(1); | ||
| tracer.RelatedWarningEvents[0].ShouldContain("Failed to read gvfs.test config, using default: corrupt config"); | ||
| } | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnPathReturnsDefaultForMissingRepoAndLogsExactlyOnce() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| // A GUID-suffixed path under the OS temp directory is guaranteed not to exist and | ||
| // does not depend on any particular drive letter being unmapped (unlike a | ||
| // hardcoded "Z:\..." path, which could resolve on a host with that drive mapped). | ||
| string missingRepoPath = Path.Combine( | ||
| Path.GetTempPath(), | ||
| "LibGit2RepoConfigLookupTests_" + Guid.NewGuid().ToString("N")); | ||
|
|
||
| bool value = LibGit2Repo.GetConfigBoolOrDefault( | ||
| tracer, | ||
| missingRepoPath, | ||
| "gvfs.test", | ||
| false); | ||
|
|
||
| value.ShouldEqual(false); | ||
|
|
||
| // The LibGit2Repo constructor logs a RelatedWarning with the native open-failure | ||
| // reason before throwing InvalidDataException; the static helper's catch does not | ||
| // log a second time for that case (see LibGit2Repo.GetConfigBoolOrDefault), so | ||
| // exactly one warning is expected here. | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(1); | ||
| tracer.RelatedWarningEvents[0].ShouldContain("Couldn't open repo at"); | ||
| } | ||
|
|
||
| private class MockConfigRepo : LibGit2Repo | ||
| { | ||
| private readonly bool? value; | ||
| private readonly Exception exceptionToThrow; | ||
|
|
||
| public MockConfigRepo(MockTracer tracer, bool? value) | ||
| : base(tracer) | ||
| { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public MockConfigRepo(MockTracer tracer, Exception exceptionToThrow) | ||
| : base(tracer) | ||
| { | ||
| this.exceptionToThrow = exceptionToThrow; | ||
| } | ||
|
|
||
| public override bool? GetConfigBool(string name) | ||
| { | ||
| if (this.exceptionToThrow != null) | ||
| { | ||
| throw this.exceptionToThrow; | ||
| } | ||
|
|
||
| return this.value; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MockConfigRepo does not override Dispose(bool), so disposing it invokes native cleanup on a repo that never initialized libgit2.