File 0003-fs_path-mock-ownership-checks.patch of Package libgit2.28344
From e4eabb030ebfdf818a116ad6e8ca9c0d0a3c9e92 Mon Sep 17 00:00:00 2001
From: Edward Thomson <ethomson@edwardthomson.com>
Date: Mon, 11 Apr 2022 23:47:01 -0400
Subject: [PATCH 03/20] fs_path: mock ownership checks
Provide a mock for file ownership for testability.
---
src/path.c | 42 ++++++++++++++++++++++++++++++++++++++++++
src/path.h | 14 ++++++++++++++
2 files changed, 56 insertions(+)
diff --git a/src/path.c b/src/path.c
index b3bb0b408..02c4e5e04 100644
--- a/src/path.c
+++ b/src/path.c
@@ -2024,6 +2024,13 @@ done:
# return supported;
# }
}
}
+static git_path__mock_owner_t mock_owner = GIT_PATH_MOCK_OWNER_NONE;
+
+void git_path__set_owner(git_path__mock_owner_t owner)
+{
+ mock_owner = owner;
+}
+
#ifdef GIT_WIN32
static PSID *sid_dup(PSID sid)
{
@@ -2116,6 +2123,11 @@ int git_path_owner_is_current_user(bool *out, const char *path)
PSID owner_sid = NULL, user_sid = NULL;
int error = -1;
+ if (mock_owner) {
+ *out = (mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
+ return 0;
+ }
+
if ((error = file_owner_sid(&owner_sid, path)) < 0 ||
(error = current_user_sid(&user_sid)) < 0)
goto done;
@@ -2133,6 +2145,11 @@ int git_path_owner_is_system(bool *out, const char *path)
{
PSID owner_sid;
+ if (mock_owner) {
+ *out = (mock_owner == GIT_PATH_MOCK_OWNER_SYSTEM);
+ return 0;
+ }
+
if (file_owner_sid(&owner_sid, path) < 0)
return -1;
@@ -2148,6 +2165,12 @@ int git_path_owner_is_system_or_current_user(bool *out, const char *path)
PSID owner_sid = NULL, user_sid = NULL;
int error = -1;
+ if (mock_owner) {
+ *out = (mock_owner == GIT_PATH_MOCK_OWNER_SYSTEM ||
+ mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
+ return 0;
+ }
+
if (file_owner_sid(&owner_sid, path) < 0)
goto done;
@@ -2200,18 +2223,37 @@ static int path_owner_is(bool *out, const char *path, uid_t *uids, size_t uids_l
int git_path_owner_is_current_user(bool *out, const char *path)
{
uid_t userid = geteuid();
+
+ if (mock_owner) {
+ *out = (mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
+ return 0;
+ }
+
return path_owner_is(out, path, &userid, 1);
}
int git_path_owner_is_system(bool *out, const char *path)
{
uid_t userid = 0;
+
+ if (mock_owner) {
+ *out = (mock_owner == GIT_PATH_MOCK_OWNER_SYSTEM);
+ return 0;
+ }
+
return path_owner_is(out, path, &userid, 1);
}
int git_path_owner_is_system_or_current_user(bool *out, const char *path)
{
uid_t userids[2] = { geteuid(), 0 };
+
+ if (mock_owner) {
+ *out = (mock_owner == GIT_PATH_MOCK_OWNER_SYSTEM ||
+ mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
+ return 0;
+ }
+
return path_owner_is(out, path, userids, 2);
}
diff --git a/src/path.h b/src/path.h
index 699945bd7..e0447d748 100644
--- a/src/path.h
+++ b/src/path.h
@@ -722,6 +722,20 @@ int git_path_normalize_slashes(git_buf *out, const char *path);
#
# bool git_path_supports_symlinks(const char *dir);
*/
int git_path_normalize_slashes(git_buf *out, const char *path);
+typedef enum {
+ GIT_PATH_MOCK_OWNER_NONE = 0, /* do filesystem lookups as normal */
+ GIT_PATH_MOCK_OWNER_SYSTEM = 1,
+ GIT_PATH_MOCK_OWNER_CURRENT_USER = 2,
+ GIT_PATH_MOCK_OWNER_OTHER = 3
+} git_path__mock_owner_t;
+
+/**
+ * Sets the mock ownership for files; subsequent calls to
+ * `git_path_owner_is_*` functions will return this data until cleared
+ * with `GIT_PATH_MOCK_OWNER_NONE`.
+ */
+void git_path__set_owner(git_path__mock_owner_t owner);
+
/**
* Verify that the file in question is owned by an administrator or system
* account.
--
2.37.1