blob: e0b3180f23c01892effce8e25bf10fc5897bb7bf [file] [log] [blame]
Seyi Kuforijia16a2ee2025-02-25 11:10:41 +01001#include "unit-test.h"
Ghanshyam Thakkared548402024-06-08 22:27:09 +05302#include "lib-oid.h"
3#include "strbuf.h"
4#include "hex.h"
5
Seyi Kuforijia16a2ee2025-02-25 11:10:41 +01006int cl_setup_hash_algo(void)
Ghanshyam Thakkared548402024-06-08 22:27:09 +05307{
8 static int algo = -1;
9
10 if (algo < 0) {
11 const char *algo_name = getenv("GIT_TEST_DEFAULT_HASH");
12 algo = algo_name ? hash_algo_by_name(algo_name) : GIT_HASH_SHA1;
13
Seyi Kuforijia16a2ee2025-02-25 11:10:41 +010014 cl_assert(algo != GIT_HASH_UNKNOWN);
Ghanshyam Thakkared548402024-06-08 22:27:09 +053015 }
16 return algo;
17}
18
Seyi Kuforijia16a2ee2025-02-25 11:10:41 +010019static void cl_parse_oid(const char *hex, struct object_id *oid,
Ghanshyam Thakkared548402024-06-08 22:27:09 +053020 const struct git_hash_algo *algop)
21{
Ghanshyam Thakkared548402024-06-08 22:27:09 +053022 size_t sz = strlen(hex);
23 struct strbuf buf = STRBUF_INIT;
24
Seyi Kuforijia16a2ee2025-02-25 11:10:41 +010025 cl_assert(sz <= algop->hexsz);
Ghanshyam Thakkared548402024-06-08 22:27:09 +053026
27 strbuf_add(&buf, hex, sz);
28 strbuf_addchars(&buf, '0', algop->hexsz - sz);
29
Seyi Kuforijia16a2ee2025-02-25 11:10:41 +010030 cl_assert_equal_i(get_oid_hex_algop(buf.buf, oid, algop), 0);
Ghanshyam Thakkared548402024-06-08 22:27:09 +053031
32 strbuf_release(&buf);
Ghanshyam Thakkared548402024-06-08 22:27:09 +053033}
34
Ghanshyam Thakkared548402024-06-08 22:27:09 +053035
Seyi Kuforijia16a2ee2025-02-25 11:10:41 +010036void cl_parse_any_oid(const char *hex, struct object_id *oid)
37{
38 int hash_algo = cl_setup_hash_algo();
39
40 cl_assert(hash_algo != GIT_HASH_UNKNOWN);
41 cl_parse_oid(hex, oid, &hash_algos[hash_algo]);
Ghanshyam Thakkared548402024-06-08 22:27:09 +053042}