From 35905738072eb0be24ea718dc2ccef1663c226f9 Mon Sep 17 00:00:00 2001
From: Edward Thomson <ethomson@edwardthomson.com>
Date: Sun, 3 Jul 2022 09:07:32 -0400
Subject: [PATCH 15/20] fs: remove mock naming from change ownership constants

The file ownership concepts can reflect the actual file ownership, they
are not necessarily limited to mocking the interface. Rename them so
that they can be more broadly applicable.
---
 src/path.c        | 28 ++++++++++++++--------------
 src/path.h        | 29 +++++++++++++++++++++--------
 tests/repo/open.c | 38 +++++++++++++++++++-------------------
 3 files changed, 54 insertions(+), 41 deletions(-)

diff --git a/src/path.c b/src/path.c
index 02c4e5e04..e9c30f4a6 100644
--- a/src/path.c
+++ b/src/path.c
@@ -2024,9 +2024,9 @@ done:
# 	return supported;
 	}
 }
 
-static git_path__mock_owner_t mock_owner = GIT_PATH_MOCK_OWNER_NONE;
+static git_path_owner_t mock_owner = GIT_PATH_OWNER_NONE;
 
-void git_path__set_owner(git_path__mock_owner_t owner)
+void git_path__set_owner(git_path_owner_t owner)
 {
 	mock_owner = owner;
 }
@@ -2124,7 +2124,7 @@ int git_path_owner_is_current_user(bool *out, const char *path)
 	int error = -1;
 
 	if (mock_owner) {
-		*out = (mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
+		*out = (mock_owner == GIT_PATH_OWNER_CURRENT_USER);
 		return 0;
 	}
 
@@ -2146,7 +2146,7 @@ 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);
+		*out = (mock_owner == GIT_PATH_OWNER_ADMINISTRATOR);
 		return 0;
 	}
 
@@ -2166,8 +2166,8 @@ int git_path_owner_is_system_or_current_user(bool *out, const char *path)
 	int error = -1;
 
 	if (mock_owner) {
-		*out = (mock_owner == GIT_PATH_MOCK_OWNER_SYSTEM ||
-		        mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
+		*out = (mock_owner == GIT_PATH_OWNER_ADMINISTRATOR ||
+		        mock_owner == GIT_PATH_OWNER_CURRENT_USER);
 		return 0;
 	}
 
@@ -2195,7 +2195,7 @@ done:
 
 #else
 
-static int path_owner_is(bool *out, const char *path, uid_t *uids, size_t uids_len)
+static int fs_path_owner_is(bool *out, const char *path, uid_t *uids, size_t uids_len)
 {
 	struct stat st;
 	size_t i;
@@ -2225,11 +2225,11 @@ 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);
+		*out = (mock_owner == GIT_PATH_OWNER_CURRENT_USER);
 		return 0;
 	}
 
-	return path_owner_is(out, path, &userid, 1);
+	return fs_path_owner_is(out, path, &userid, 1);
 }
 
 int git_path_owner_is_system(bool *out, const char *path)
@@ -2237,11 +2237,11 @@ 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);
+		*out = (mock_owner == GIT_PATH_OWNER_ADMINISTRATOR);
 		return 0;
 	}
 
-	return path_owner_is(out, path, &userid, 1);
+	return fs_path_owner_is(out, path, &userid, 1);
 }
 
 int git_path_owner_is_system_or_current_user(bool *out, const char *path)
@@ -2249,12 +2249,12 @@ 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);
+		*out = (mock_owner == GIT_PATH_OWNER_ADMINISTRATOR ||
+		        mock_owner == GIT_PATH_OWNER_CURRENT_USER);
 		return 0;
 	}
 
-	return path_owner_is(out, path, userids, 2);
+	return fs_path_owner_is(out, path, userids, 2);
 }
 
 #endif
diff --git a/src/path.h b/src/path.h
index e0447d748..182135e65 100644
--- a/src/path.h
+++ b/src/path.h
@@ -723,18 +723,31 @@ 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;
+	GIT_PATH_OWNER_NONE = 0,
+
+	/** The file must be owned by the current user. */
+	GIT_PATH_OWNER_CURRENT_USER = (1 << 0),
+
+	/** The file must be owned by the system account. */
+	GIT_PATH_OWNER_ADMINISTRATOR = (1 << 1),
+
+	/**
+	 * The file may be owned by a system account if the current
+	 * user is in an administrator group. Windows only; this is
+	 * a noop on non-Windows systems.
+	 */
+	GIT_PATH_OWNER_CURRENT_USER_IS_ADMINISTRATOR = (1 << 2),
+
+	/** The file may be owned by another user. */
+	GIT_PATH_OWNER_OTHER = (1 << 4)
+} git_path_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`.
+ * `git_path_owner_is_*` functions will return this data until
+ * cleared with `GIT_FS_PATH_OWNER_NONE`.
  */
-void git_path__set_owner(git_path__mock_owner_t owner);
+void git_path__set_owner(git_path_owner_t owner);
 
 /**
  * Verify that the file in question is owned by an administrator or system
diff --git a/tests/repo/open.c b/tests/repo/open.c
index ac5274680..de15b3fc7 100644
--- a/tests/repo/open.c
+++ b/tests/repo/open.c
@@ -22,7 +22,7 @@ void test_repo_open__cleanup(void)
 	if (git_path_isdir("alternate"))
 		git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES);
 
-	git_path__set_owner(GIT_PATH_MOCK_OWNER_NONE);
+	git_path__set_owner(GIT_PATH_OWNER_NONE);
 
 	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, config_path.ptr));
 	git_buf_dispose(&config_path);
@@ -481,16 +481,16 @@ void test_repo_open__validates_dir_ownership(void)
 	cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
 
 	/* When the current user owns the repo config, that's acceptable */
-	git_path__set_owner(GIT_PATH_MOCK_OWNER_CURRENT_USER);
+	git_path__set_owner(GIT_PATH_OWNER_CURRENT_USER);
 	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
 	git_repository_free(repo);
 
 	/* When the system user owns the repo config, fail */
-	git_path__set_owner(GIT_PATH_MOCK_OWNER_SYSTEM);
+	git_path__set_owner(GIT_PATH_OWNER_ADMINISTRATOR);
 	cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
 
 	/* When an unknown user owns the repo config, fail */
-	git_path__set_owner(GIT_PATH_MOCK_OWNER_OTHER);
+	git_path__set_owner(GIT_PATH_OWNER_OTHER);
 	cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
 }
 
@@ -503,16 +503,16 @@ void test_repo_open__validates_bare_repo_ownership(void)
 	cl_fixture_sandbox("testrepo.git");
 
 	/* When the current user owns the repo config, that's acceptable */
-	git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_CURRENT_USER);
+	git_path__set_owner(GIT_PATH_OWNER_CURRENT_USER);
 	cl_git_pass(git_repository_open(&repo, "testrepo.git"));
 	git_repository_free(repo);
 
 	/* When the system user owns the repo config, fail */
-	git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_SYSTEM);
+	git_path__set_owner(GIT_PATH_OWNER_ADMINISTRATOR);
 	cl_git_fail(git_repository_open(&repo, "testrepo.git"));
 
 	/* When an unknown user owns the repo config, fail */
-	git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_OTHER);
+	git_path__set_owner(GIT_PATH_OWNER_OTHER);
 	cl_git_fail(git_repository_open(&repo, "testrepo.git"));
 }
 
@@ -528,7 +528,7 @@ void test_repo_open__can_allowlist_dirs_with_problematic_ownership(void)
 	cl_fixture_sandbox("empty_standard_repo");
 	cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
 
-	git_path__set_owner(GIT_PATH_MOCK_OWNER_OTHER);
+	git_path__set_owner(GIT_PATH_OWNER_OTHER);
 	cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
 
 	/* Add safe.directory options to the global configuration */
@@ -564,25 +564,25 @@ void test_repo_open__can_allowlist_dirs_with_problematic_ownership(void)
 void test_repo_open__can_allowlist_bare_gitdir(void)
 {
 	git_repository *repo;
-	git_str config_path = GIT_STR_INIT,
-	        config_filename = GIT_STR_INIT,
-	        config_data = GIT_STR_INIT;
+	git_buf config_path = GIT_BUF_INIT,
+	        config_filename = GIT_BUF_INIT,
+	        config_data = GIT_BUF_INIT;
 
 	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));
 
 	cl_fixture_sandbox("testrepo.git");
 
-	git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_OTHER);
+	git_path__set_owner(GIT_PATH_OWNER_OTHER);
 	cl_git_fail(git_repository_open(&repo, "testrepo.git"));
 
 	/* Add safe.directory options to the global configuration */
-	git_str_joinpath(&config_path, clar_sandbox_path(), "__global_config");
+	git_buf_joinpath(&config_path, clar_sandbox_path(), "__global_config");
 	cl_must_pass(p_mkdir(config_path.ptr, 0777));
 	git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, config_path.ptr);
 
-	git_str_joinpath(&config_filename, config_path.ptr, ".gitconfig");
+	git_buf_joinpath(&config_filename, config_path.ptr, ".gitconfig");
 
-	git_str_printf(&config_data,
+	git_buf_printf(&config_data,
 		"[foo]\n" \
 		"\tbar = Foobar\n" \
 		"\tbaz = Baz!\n" \
@@ -600,9 +600,9 @@ void test_repo_open__can_allowlist_bare_gitdir(void)
 	cl_git_pass(git_repository_open(&repo, "testrepo.git"));
 	git_repository_free(repo);
 
-	git_str_dispose(&config_path);
-	git_str_dispose(&config_filename);
-	git_str_dispose(&config_data);
+	git_buf_dispose(&config_path);
+	git_buf_dispose(&config_filename);
+	git_buf_dispose(&config_data);
 }
 
 void test_repo_open__can_reset_safe_directory_list(void)
@@ -617,7 +617,7 @@ void test_repo_open__can_reset_safe_directory_list(void)
 	cl_fixture_sandbox("empty_standard_repo");
 	cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
 
-	git_path__set_owner(GIT_PATH_MOCK_OWNER_OTHER);
+	git_path__set_owner(GIT_PATH_OWNER_OTHER);
 	cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
 
 	/* Add safe.directory options to the global configuration */
-- 
2.37.1

