Skip to content

Commit 3f8658f

Browse files
committed
OdbBackendFixture: disable strict hash validation
1 parent 4ef7ff2 commit 3f8658f

File tree

4 files changed

+71
-43
lines changed

4 files changed

+71
-43
lines changed

LibGit2Sharp.Tests/OdbBackendFixture.cs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,37 +90,46 @@ public void CanGeneratePredictableObjectShasWithAProvidedBackend()
9090
[Fact]
9191
public void CanRetrieveObjectsThroughOddSizedShortShas()
9292
{
93-
string repoPath = InitNewRepository();
94-
95-
using (var repo = new Repository(repoPath))
93+
try
9694
{
97-
var backend = new MockOdbBackend();
98-
repo.ObjectDatabase.AddBackend(backend, priority: 5);
95+
GlobalSettings.SetStrictHashVerification(false);
9996

100-
AddCommitToRepo(repo);
97+
string repoPath = InitNewRepository();
10198

102-
var blob1 = repo.Lookup<Blob>("9daeaf");
103-
Assert.NotNull(blob1);
99+
using (var repo = new Repository(repoPath))
100+
{
101+
var backend = new MockOdbBackend();
102+
repo.ObjectDatabase.AddBackend(backend, priority: 5);
104103

105-
const string dummy = "dummy\n";
104+
AddCommitToRepo(repo);
106105

107-
// Inserts a fake blob with a similarly prefixed sha
108-
var fakeId = new ObjectId("9daeaf0000000000000000000000000000000000");
109-
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(dummy)))
110-
{
111-
Assert.Equal(0, backend.Write(fakeId, ms, dummy.Length, ObjectType.Blob));
112-
}
106+
var blob1 = repo.Lookup<Blob>("9daeaf");
107+
Assert.NotNull(blob1);
108+
109+
const string dummy = "dummy\n";
110+
111+
// Inserts a fake blob with a similarly prefixed sha
112+
var fakeId = new ObjectId("9daeaf0000000000000000000000000000000000");
113+
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(dummy)))
114+
{
115+
Assert.Equal(0, backend.Write(fakeId, ms, dummy.Length, ObjectType.Blob));
116+
}
113117

114-
var blob2 = repo.Lookup<Blob>(fakeId);
115-
Assert.NotNull(blob2);
118+
var blob2 = repo.Lookup<Blob>(fakeId);
119+
Assert.NotNull(blob2);
116120

117-
Assert.Throws<AmbiguousSpecificationException>(() => repo.Lookup<Blob>("9daeaf"));
121+
Assert.Throws<AmbiguousSpecificationException>(() => repo.Lookup<Blob>("9daeaf"));
118122

119-
var newBlob1 = repo.Lookup("9daeafb");
120-
var newBlob2 = repo.Lookup("9daeaf0");
123+
var newBlob1 = repo.Lookup("9daeafb");
124+
var newBlob2 = repo.Lookup("9daeaf0");
121125

122-
Assert.Equal(blob1, newBlob1);
123-
Assert.Equal(blob2, newBlob2);
126+
Assert.Equal(blob1, newBlob1);
127+
Assert.Equal(blob2, newBlob2);
128+
}
129+
}
130+
finally
131+
{
132+
GlobalSettings.SetStrictHashVerification(true);
124133
}
125134
}
126135

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ internal static extern int git_filter_unregister(
636636
internal static extern int git_libgit2_opts(int option, uint level,
637637
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))]string path);
638638

639+
// git_libgit2_opts(GIT_OPT_ENABLE_*, int enabled)
640+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
641+
internal static extern int git_libgit2_opts(int option, int enabled);
639642
#endregion
640643

641644
[DllImport(libgit2)]

LibGit2Sharp/Core/Proxy.cs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,25 +3345,31 @@ public static BuiltInFeatures git_libgit2_features()
33453345
}
33463346

33473347
// C# equivalent of libgit2's git_libgit2_opt_t
3348-
private enum LibGitOption
3349-
{
3350-
GetMWindowSize, // GIT_OPT_GET_MWINDOW_SIZE
3351-
SetMWindowSize, // GIT_OPT_SET_MWINDOW_SIZE
3352-
GetMWindowMappedLimit, // GIT_OPT_GET_MWINDOW_MAPPED_LIMIT
3353-
SetMWindowMappedLimit, // GIT_OPT_SET_MWINDOW_MAPPED_LIMIT
3354-
GetSearchPath, // GIT_OPT_GET_SEARCH_PATH
3355-
SetSearchPath, // GIT_OPT_SET_SEARCH_PATH
3356-
SetCacheObjectLimit, // GIT_OPT_SET_CACHE_OBJECT_LIMIT
3357-
SetCacheMaxSize, // GIT_OPT_SET_CACHE_MAX_SIZE
3358-
EnableCaching, // GIT_OPT_ENABLE_CACHING
3359-
GetCachedMemory, // GIT_OPT_GET_CACHED_MEMORY
3360-
GetTemplatePath, // GIT_OPT_GET_TEMPLATE_PATH
3361-
SetTemplatePath, // GIT_OPT_SET_TEMPLATE_PATH
3362-
SetSslCertLocations, // GIT_OPT_SET_SSL_CERT_LOCATIONS
3363-
SetUserAgent, // GIT_OPT_SET_USER_AGENT
3364-
EnableStrictObjectCreation, // GIT_OPT_ENABLE_STRICT_OBJECT_CREATION
3365-
SetSslCiphers, // GIT_OPT_SET_SSL_CIPHERS
3366-
GetUserAgent, // GIT_OPT_GET_USER_AGENT
3348+
private enum LibGit2Option
3349+
{
3350+
GetMWindowSize, // GIT_OPT_GET_MWINDOW_SIZE
3351+
SetMWindowSize, // GIT_OPT_SET_MWINDOW_SIZE
3352+
GetMWindowMappedLimit, // GIT_OPT_GET_MWINDOW_MAPPED_LIMIT
3353+
SetMWindowMappedLimit, // GIT_OPT_SET_MWINDOW_MAPPED_LIMIT
3354+
GetSearchPath, // GIT_OPT_GET_SEARCH_PATH
3355+
SetSearchPath, // GIT_OPT_SET_SEARCH_PATH
3356+
SetCacheObjectLimit, // GIT_OPT_SET_CACHE_OBJECT_LIMIT
3357+
SetCacheMaxSize, // GIT_OPT_SET_CACHE_MAX_SIZE
3358+
EnableCaching, // GIT_OPT_ENABLE_CACHING
3359+
GetCachedMemory, // GIT_OPT_GET_CACHED_MEMORY
3360+
GetTemplatePath, // GIT_OPT_GET_TEMPLATE_PATH
3361+
SetTemplatePath, // GIT_OPT_SET_TEMPLATE_PATH
3362+
SetSslCertLocations, // GIT_OPT_SET_SSL_CERT_LOCATIONS
3363+
SetUserAgent, // GIT_OPT_SET_USER_AGENT
3364+
EnableStrictObjectCreation, // GIT_OPT_ENABLE_STRICT_OBJECT_CREATION
3365+
EnableStrictSymbolicRefCreation, // GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION
3366+
SetSslCiphers, // GIT_OPT_SET_SSL_CIPHERS
3367+
GetUserAgent, // GIT_OPT_GET_USER_AGENT
3368+
EnableOfsDelta, // GIT_OPT_ENABLE_OFS_DELTA
3369+
EnableFsyncGitdir, // GIT_OPT_ENABLE_FSYNC_GITDIR
3370+
GetWindowsSharemode, // GIT_OPT_GET_WINDOWS_SHAREMODE
3371+
SetWindowsSharemode, // GIT_OPT_SET_WINDOWS_SHAREMODE
3372+
EnableStrictHashVerification, // GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION
33673373
}
33683374

33693375
/// <summary>
@@ -3379,7 +3385,7 @@ public static string git_libgit2_opts_get_search_path(ConfigurationLevel level)
33793385

33803386
using (var buf = new GitBuf())
33813387
{
3382-
var res = NativeMethods.git_libgit2_opts((int)LibGitOption.GetSearchPath, (uint)level, buf);
3388+
var res = NativeMethods.git_libgit2_opts((int)LibGit2Option.GetSearchPath, (uint)level, buf);
33833389
Ensure.ZeroResult(res);
33843390

33853391
path = LaxUtf8Marshaler.FromNative(buf.ptr) ?? string.Empty;
@@ -3388,6 +3394,11 @@ public static string git_libgit2_opts_get_search_path(ConfigurationLevel level)
33883394
return path;
33893395
}
33903396

3397+
public static void git_libgit2_opts_enable_strict_hash_verification(bool enabled)
3398+
{
3399+
NativeMethods.git_libgit2_opts((int)LibGit2Option.EnableStrictHashVerification, enabled ? 1 : 0);
3400+
}
3401+
33913402
/// <summary>
33923403
/// Set the path(s) under which libgit2 searches for the configuration file of a given level.
33933404
/// </summary>
@@ -3398,7 +3409,7 @@ public static string git_libgit2_opts_get_search_path(ConfigurationLevel level)
33983409
/// </param>
33993410
public static void git_libgit2_opts_set_search_path(ConfigurationLevel level, string path)
34003411
{
3401-
var res = NativeMethods.git_libgit2_opts((int)LibGitOption.SetSearchPath, (uint)level, path);
3412+
var res = NativeMethods.git_libgit2_opts((int)LibGit2Option.SetSearchPath, (uint)level, path);
34023413
Ensure.ZeroResult(res);
34033414
}
34043415

LibGit2Sharp/GlobalSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,10 @@ public static void SetConfigSearchPaths(ConfigurationLevel level, params string[
318318
var pathString = (paths == null) ? null : string.Join(Path.PathSeparator.ToString(), paths);
319319
Proxy.git_libgit2_opts_set_search_path(level, pathString);
320320
}
321+
322+
public static void SetStrictHashVerification(bool enabled)
323+
{
324+
Proxy.git_libgit2_opts_enable_strict_hash_verification(enabled);
325+
}
321326
}
322327
}

0 commit comments

Comments
 (0)