Commit 7367a9d5f71ae8a4c7e3e97e5c42c0cd034672c9

Edward Thomson 2022-01-22T09:07:26

tests: don't cast raw data to a `git_oid`` Create an object id from raw data instead of casting.

diff --git a/tests/libgit2/object/raw/hash.c b/tests/libgit2/object/raw/hash.c
index 5a3e818..8f22fe0 100644
--- a/tests/libgit2/object/raw/hash.c
+++ b/tests/libgit2/object/raw/hash.c
@@ -24,20 +24,23 @@ static char *bye_text = "bye world\n";
 void test_object_raw_hash__hash_by_blocks(void)
 {
 	git_hash_ctx ctx;
+	unsigned char hash[GIT_HASH_SHA1_SIZE];
 	git_oid id1, id2;
 
 	cl_git_pass(git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1));
 
 	/* should already be init'd */
 	cl_git_pass(git_hash_update(&ctx, hello_text, strlen(hello_text)));
-	cl_git_pass(git_hash_final(id2.id, &ctx));
+	cl_git_pass(git_hash_final(hash, &ctx));
+	cl_git_pass(git_oid_fromraw(&id2, hash));
 	cl_git_pass(git_oid_fromstr(&id1, hello_id));
 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
 
 	/* reinit should permit reuse */
 	cl_git_pass(git_hash_init(&ctx));
 	cl_git_pass(git_hash_update(&ctx, bye_text, strlen(bye_text)));
-	cl_git_pass(git_hash_final(id2.id, &ctx));
+	cl_git_pass(git_hash_final(hash, &ctx));
+	cl_git_pass(git_oid_fromraw(&id2, hash));
 	cl_git_pass(git_oid_fromstr(&id1, bye_id));
 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
 
@@ -47,9 +50,11 @@ void test_object_raw_hash__hash_by_blocks(void)
 void test_object_raw_hash__hash_buffer_in_single_call(void)
 {
 	git_oid id1, id2;
+	unsigned char hash[GIT_HASH_SHA1_SIZE];
 
 	cl_git_pass(git_oid_fromstr(&id1, hello_id));
-	git_hash_buf(id2.id, hello_text, strlen(hello_text), GIT_HASH_ALGORITHM_SHA1);
+	cl_git_pass(git_hash_buf(hash, hello_text, strlen(hello_text), GIT_HASH_ALGORITHM_SHA1));
+	cl_git_pass(git_oid_fromraw(&id2, hash));
 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
 }
 
diff --git a/tests/libgit2/object/raw/short.c b/tests/libgit2/object/raw/short.c
index e8d2cf5..cc2b5f6 100644
--- a/tests/libgit2/object/raw/short.c
+++ b/tests/libgit2/object/raw/short.c
@@ -28,12 +28,15 @@ static int insert_sequential_oids(
 	int i, min_len = 0;
 	char numbuf[16];
 	git_oid oid;
+	unsigned char hashbuf[GIT_HASH_SHA1_SIZE];
 	char **oids = git__calloc(n, sizeof(char *));
 	cl_assert(oids != NULL);
 
 	for (i = 0; i < n; ++i) {
 		p_snprintf(numbuf, sizeof(numbuf), "%u", (unsigned int)i);
-		git_hash_buf(oid.id, numbuf, strlen(numbuf), GIT_HASH_ALGORITHM_SHA1);
+		git_hash_buf(hashbuf, numbuf, strlen(numbuf), GIT_HASH_ALGORITHM_SHA1);
+
+		git_oid_fromraw(&oid, hashbuf);
 
 		oids[i] = git__malloc(GIT_OID_HEXSZ + 1);
 		cl_assert(oids[i]);