fs_path: mock ownership checks Provide a mock for file ownership for testability.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
diff --git a/src/fs_path.c b/src/fs_path.c
index cb52b29..a67617e 100644
--- a/src/fs_path.c
+++ b/src/fs_path.c
@@ -1779,6 +1779,13 @@ done:
return supported;
}
+static git_fs_path__mock_owner_t mock_owner = GIT_FS_PATH_MOCK_OWNER_NONE;
+
+void git_fs_path__set_owner(git_fs_path__mock_owner_t owner)
+{
+ mock_owner = owner;
+}
+
#ifdef GIT_WIN32
static PSID *sid_dup(PSID sid)
{
@@ -1871,6 +1878,11 @@ int git_fs_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_FS_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;
@@ -1888,6 +1900,11 @@ int git_fs_path_owner_is_system(bool *out, const char *path)
{
PSID owner_sid;
+ if (mock_owner) {
+ *out = (mock_owner == GIT_FS_PATH_MOCK_OWNER_SYSTEM);
+ return 0;
+ }
+
if (file_owner_sid(&owner_sid, path) < 0)
return -1;
@@ -1903,6 +1920,12 @@ int git_fs_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_FS_PATH_MOCK_OWNER_SYSTEM ||
+ mock_owner == GIT_FS_PATH_MOCK_OWNER_CURRENT_USER);
+ return 0;
+ }
+
if (file_owner_sid(&owner_sid, path) < 0)
goto done;
@@ -1955,18 +1978,37 @@ static int fs_path_owner_is(bool *out, const char *path, uid_t *uids, size_t uid
int git_fs_path_owner_is_current_user(bool *out, const char *path)
{
uid_t userid = geteuid();
+
+ if (mock_owner) {
+ *out = (mock_owner == GIT_FS_PATH_MOCK_OWNER_CURRENT_USER);
+ return 0;
+ }
+
return fs_path_owner_is(out, path, &userid, 1);
}
int git_fs_path_owner_is_system(bool *out, const char *path)
{
uid_t userid = 0;
+
+ if (mock_owner) {
+ *out = (mock_owner == GIT_FS_PATH_MOCK_OWNER_SYSTEM);
+ return 0;
+ }
+
return fs_path_owner_is(out, path, &userid, 1);
}
int git_fs_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_FS_PATH_MOCK_OWNER_SYSTEM ||
+ mock_owner == GIT_FS_PATH_MOCK_OWNER_CURRENT_USER);
+ return 0;
+ }
+
return fs_path_owner_is(out, path, userids, 2);
}
diff --git a/src/fs_path.h b/src/fs_path.h
index 90c7dd9..7e6a22d 100644
--- a/src/fs_path.h
+++ b/src/fs_path.h
@@ -731,6 +731,20 @@ int git_fs_path_normalize_slashes(git_str *out, const char *path);
bool git_fs_path_supports_symlinks(const char *dir);
+typedef enum {
+ GIT_FS_PATH_MOCK_OWNER_NONE = 0, /* do filesystem lookups as normal */
+ GIT_FS_PATH_MOCK_OWNER_SYSTEM = 1,
+ GIT_FS_PATH_MOCK_OWNER_CURRENT_USER = 2,
+ GIT_FS_PATH_MOCK_OWNER_OTHER = 3
+} git_fs_path__mock_owner_t;
+
+/**
+ * Sets the mock ownership for files; subsequent calls to
+ * `git_fs_path_owner_is_*` functions will return this data until cleared
+ * with `GIT_FS_PATH_MOCK_OWNER_NONE`.
+ */
+void git_fs_path__set_owner(git_fs_path__mock_owner_t owner);
+
/**
* Verify that the file in question is owned by an administrator or system
* account.