Skip to contentSkip to conversation

strbuf: introduce strbuf_addstrings() to repeatedly add a string

PATCH Superseded Achu Luma 6 participants 35 messages Feb 26, 2024 – Jun 21, 2024 Kbuild

lore
Share & tools
lore

Achu Luma

author
#0 lore raw

PATCH 1/2 strbuf: introduce strbuf_addstrings() to repeatedly add a string

In a following commit we are going to port code from "t/helper/test-sha256.c", t/helper/test-hash.c and "t/t0015-hash.sh" to a new "t/unit-tests/t-hash.c" file using the recently added unit test framework.

To port code like: perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" we are going to need a new strbuf_addstrings() function that repeatedly adds the same string a number of times to a buffer.

Such a strbuf_addstrings() function would already be useful in "json-writer.c" and "builtin/submodule-helper.c" as both of these files already have code that repeatedly adds the same string. So let's introduce such a strbuf_addstrings() function in "strbuf.{c,h}" and use it in both "json-writer.c" and "builtin/submodule-helper.c".

We use the "strbuf_addstrings" name as this way strbuf_addstr() and strbuf_addstrings() would be similar for strings as strbuf_addch() and strbuf_addchars() for characters.

---
 builtin/submodule--helper.c |  4 +---
 json-writer.c               |  5 +----
 strbuf.c                    | 11 +++++++++++
 strbuf.h                    |  5 +++++
 4 files changed, 18 insertions(+), 7 deletions(-)
builtin/submodule--helper.c +1 -3
index fda50f2af1..bed08af410 100644
@@ -257,11 +257,9 @@ static void module_list_active(struct module_list *list)
 
 static char *get_up_path(const char *path)
 {
-	int i;
 	struct strbuf sb = STRBUF_INIT;
 
-	for (i = count_slashes(path); i; i--)
-		strbuf_addstr(&sb, "../");
+	strbuf_addstrings(&sb, "../", count_slashes(path));
 
 	/*
 	 * Check if 'path' ends with slash or not
json-writer.c +1 -4
index 005c820aa4..25b9201f9c 100644
@@ -46,10 +46,7 @@ static void append_quoted_string(struct strbuf *out, const char *in)
 
 static void indent_pretty(struct json_writer *jw)
 {
-	int k;
-
-	for (k = 0; k < jw->open_stack.len; k++)
-		strbuf_addstr(&jw->json, "  ");
+	strbuf_addstrings(&jw->json, "  ", jw->open_stack.len);
 }
 
 /*
strbuf.c +11 -0
index 7827178d8e..eb2b3299ce 100644
@@ -302,6 +302,17 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 	strbuf_setlen(sb, sb->len + len);
 }
 
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n)
+{
+	size_t len = strlen(s);
+	if (unsigned_mult_overflows(len, n))
+		die("you want to use way too much memory");
+	strbuf_grow(sb, len * n);
+	for (size_t i = 0; i < n; i++)
+		memcpy(sb->buf + sb->len + len * i, s, len);
+	strbuf_setlen(sb, sb->len + len * n);
+}
+
 void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
 {
 	strbuf_grow(sb, sb2->len);
strbuf.h +5 -0
index e959caca87..0fb1b5e81e 100644
@@ -310,6 +310,11 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
 	strbuf_add(sb, s, strlen(s));
 }
 
+/**
+ * Add a NUL-terminated string the specified number of times to the buffer.
+ */
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n);
+
 /**
  * Copy the contents of another buffer at the end of the current one.
  */

--
2.43.0.windows.1

author
#1 lore raw

PATCH 2/2 Port helper/test-sha256.c and helper/test-sha1.c to unit-tests/t-hash.c

In the recent codebase update (8bf6fbd (Merge branch 'js/doc-unit-tests', 2023-12-09)), a new unit testing framework was merged, providing a standardized approach for testing C code. Prior to this update, some unit tests relied on the test helper mechanism, lacking a dedicated unit testing framework. It's more natural to perform these unit tests using the new unit test framework.

This commit migrates the unit tests for hash functionality from the legacy approach using the test-tool command test-tool sha1and test-tool sha256 in helper/test-sha256.c and helper/test-sha1.c to the new unit testing framework (t/unit-tests/test-lib.h). Porting t0013-sha1dc.sh is left for later.

The migration involves refactoring the tests to utilize the testing macros provided by the framework (TEST() and check_*()).

---
 Makefile              |  2 +-
 t/helper/test-tool.c  |  1 -
 t/helper/test-tool.h  |  1 -
 t/t0015-hash.sh       | 56 --------------------------------
 t/unit-tests/t-hash.c | 75 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 59 deletions(-)
 delete mode 100755 t/t0015-hash.sh
 create mode 100644 t/unit-tests/t-hash.c
Makefile +1 -1
index 78e874099d..b73d2270f9 100644
@@ -847,7 +847,6 @@ TEST_BUILTINS_OBJS += test-run-command.o
 TEST_BUILTINS_OBJS += test-scrap-cache-tree.o
 TEST_BUILTINS_OBJS += test-serve-v2.o
 TEST_BUILTINS_OBJS += test-sha1.o
-TEST_BUILTINS_OBJS += test-sha256.o
 TEST_BUILTINS_OBJS += test-sigchain.o
 TEST_BUILTINS_OBJS += test-simple-ipc.o
 TEST_BUILTINS_OBJS += test-strcmp-offset.o
@@ -1346,6 +1345,7 @@ UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-strbuf
 UNIT_TEST_PROGRAMS += t-ctype
 UNIT_TEST_PROGRAMS += t-prio-queue
+UNIT_TEST_PROGRAMS += t-hash
 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
t/helper/test-tool.c +0 -1
index 482a1e58a4..7bfbb75c9b 100644
@@ -73,7 +73,6 @@ static struct test_cmd cmds[] = {
 	{ "serve-v2", cmd__serve_v2 },
 	{ "sha1", cmd__sha1 },
 	{ "sha1-is-sha1dc", cmd__sha1_is_sha1dc },
-	{ "sha256", cmd__sha256 },
 	{ "sigchain", cmd__sigchain },
 	{ "simple-ipc", cmd__simple_ipc },
 	{ "strcmp-offset", cmd__strcmp_offset },
t/helper/test-tool.h +0 -1
index b1be7cfcf5..8139c9664d 100644
@@ -66,7 +66,6 @@ int cmd__serve_v2(int argc, const char **argv);
 int cmd__sha1(int argc, const char **argv);
 int cmd__sha1_is_sha1dc(int argc, const char **argv);
 int cmd__oid_array(int argc, const char **argv);
-int cmd__sha256(int argc, const char **argv);
 int cmd__sigchain(int argc, const char **argv);
 int cmd__simple_ipc(int argc, const char **argv);
 int cmd__strcmp_offset(int argc, const char **argv);
t/t0015-hash.sh deleted +0 -56

56 lines not on this page · raw message

t/unit-tests/t-hash.c new file +75 -0

75 lines not on this page · raw message

--
2.43.0.windows.1

#2 lore raw
In reply to Achu Luma
Quoted 8 lines · Achu Luma

In a following commit we are going to port code from "t/helper/test-sha256.c", t/helper/test-hash.c and "t/t0015-hash.sh" to a new "t/unit-tests/t-hash.c" file using the recently added unit test framework.

To port code like: perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" we are going to need a new strbuf_addstrings() function that repeatedly adds the same string a number of times to a buffer.

We do not need to call such a function "addstrings", though. The name on the subject line made me expect a varargs function:

(bad) strbuf_addstrings(&sb, "foo", "bar", "baz", NULL);

It would have been clearer if the name hinted what it does, clearer than just a single "s" that says it is talking about plural. What would be a good name that hints "n times add a single same string"? I dunno.

I also would have expected that the order of parameters are repeat-count followed by what gets repeated.

Having said all of the above, we already have "addchars" that is equally strange, so let's let it pass ;-).

strbuf.c void strbuf_add(struct strbuf *sb, const void *data, size_t len) · line 307

Quoted 11 lines
strbuf.c +3 -0
index 7827178d8e..eb2b3299ce 100644
@@ -302,6 +302,17 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 	strbuf_setlen(sb, sb->len + len);
 }
 
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n)
+{
+	size_t len = strlen(s);

Let's have a blank line here to separate decls from the first statement.

strbuf.c void strbuf_add(struct strbuf *sb, const void *data, size_t len) · line 310

Quoted 3 lines
strbuf.c +3 -0
+	if (unsigned_mult_overflows(len, n))
+		die("you want to use way too much memory");
+	strbuf_grow(sb, len * n);

The error message given by

	strbuf_grow(sb, st_mult(len, n));

would be equally informative and takes only a single line.

strbuf.c void strbuf_add(struct strbuf *sb, const void *data, size_t len) · line 312

Quoted 2 lines
strbuf.c +2 -0
+	for (size_t i = 0; i < n; i++)
+		memcpy(sb->buf + sb->len + len * i, s, len);

Wouldn't it be sufficient to run strbuf_add() n times at this point, as we have already called strbuf_grow() to avoid repeated reallocation? Repeated manual memcpy() that involves manual offset computation makes me nervous.

Quoted 24 lines
strbuf.c +3 -0
+	strbuf_setlen(sb, sb->len + len * n);
+}
+
 void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
 {
 	strbuf_grow(sb, sb2->len);
strbuf.h +5 -1
index e959caca87..0fb1b5e81e 100644
@@ -310,6 +310,11 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
 	strbuf_add(sb, s, strlen(s));
 }
 
+/**
+ * Add a NUL-terminated string the specified number of times to the buffer.
+ */
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n);
+
 /**
  * Copy the contents of another buffer at the end of the current one.
  */
--

2.43.0.windows.1

#3 lore raw
Quoted 22 lines · Junio C Hamano
Quoted 8 lines · Achu Luma

In a following commit we are going to port code from "t/helper/test-sha256.c", t/helper/test-hash.c and "t/t0015-hash.sh" to a new "t/unit-tests/t-hash.c" file using the recently added unit test framework.

To port code like: perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" we are going to need a new strbuf_addstrings() function that repeatedly adds the same string a number of times to a buffer.

We do not need to call such a function "addstrings", though. The name on the subject line made me expect a varargs function:

(bad) strbuf_addstrings(&sb, "foo", "bar", "baz", NULL);

It would have been clearer if the name hinted what it does, clearer than just a single "s" that says it is talking about plural. What would be a good name that hints "n times add a single same string"? I dunno.

Quoted 5 lines

I also would have expected that the order of parameters are repeat-count followed by what gets repeated.

Having said all of the above, we already have "addchars" that is equally strange, so let's let it pass ;-).

Yeah, we thought about naming it strbuf_repeatstr() first, but then seeing addchars() I thought it would be better to imitate it.

Quoted 8 lines
Quoted 8 lines
strbuf.c +0 -0
index 7827178d8e..eb2b3299ce 100644
@@ -302,6 +302,17 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
      strbuf_setlen(sb, sb->len + len);
 }
 
Quoted 6 lines

strbuf.c void strbuf_add(struct strbuf *sb, const void *data, size_t len) · line 307

Quoted 3 lines
strbuf.c +3 -0
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n)
+{
+     size_t len = strlen(s);

Let's have a blank line here to separate decls from the first statement.

Yeah, right.

Quoted 9 lines

strbuf.c void strbuf_add(struct strbuf *sb, const void *data, size_t len) · line 310

Quoted 3 lines
strbuf.c +3 -0
+     if (unsigned_mult_overflows(len, n))
+             die("you want to use way too much memory");
+     strbuf_grow(sb, len * n);

The error message given by

        strbuf_grow(sb, st_mult(len, n));

would be equally informative and takes only a single line.

It seems that the pattern in strbuf.c, for example in strbuf_splice() and strbuf_vinsertf(), is to do a check first using an *_overflows() function, die if the check fails, and only then call strbuf_grow(). So we did the same. I am fine with using your suggestion though.

Quoted 6 lines

strbuf.c void strbuf_add(struct strbuf *sb, const void *data, size_t len) · line 312

Quoted 2 lines
strbuf.c +2 -0
+     for (size_t i = 0; i < n; i++)
+             memcpy(sb->buf + sb->len + len * i, s, len);

Wouldn't it be sufficient to run strbuf_add() n times at this point, as we have already called strbuf_grow() to avoid repeated reallocation?

Unfortunately strbuf_add() calls strbuf_grow() itself which is not needed as we have already called strbuf_grow() to avoid repeated reallocation.

Quoted 2 lines

Repeated manual memcpy() that involves manual offset
computation makes me nervous.

I would have prefered to avoid it too, but didn't find a good way to do it.

Quoted 2 lines
Quoted 2 lines
strbuf.c +2 -0
+     strbuf_setlen(sb, sb->len + len * n);
+}
#4 lore raw
Quoted 3 lines · Christian Couder

Unfortunately strbuf_add() calls strbuf_grow() itself which is not needed as we have already called strbuf_grow() to avoid repeated reallocation.

Why is it unfortunate? If the current allocation is sufficient, it is a cheap no-op, isn't it (if not, we should make it so)?

#5 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 3 lines · Christian Couder

Unfortunately strbuf_add() calls strbuf_grow() itself which is not needed as we have already called strbuf_grow() to avoid repeated reallocation.

Why is it unfortunate? If the current allocation is sufficient, it is a cheap no-op, isn't it (if not, we should make it so)?

Yeah, I agree that we don't need to be extra efficient and calling strbuf_grow() doesn't add much overhead. So let's just use strbuf_add() repeatedly after calling it once.

author
#6 lore raw

PATCH v2 1/2 strbuf: introduce strbuf_addstrings() to repeatedly add a string

In reply to Achu Luma

In a following commit we are going to port code from "t/helper/test-sha256.c", t/helper/test-hash.c and "t/t0015-hash.sh" to a new "t/unit-tests/t-hash.c" file using the recently added unit test framework.

To port code like: perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" we are going to need a new strbuf_addstrings() function that repeatedly adds the same string a number of times to a buffer.

Such a strbuf_addstrings() function would already be useful in "json-writer.c" and "builtin/submodule-helper.c" as both of these files already have code that repeatedly adds the same string. So let's introduce such a strbuf_addstrings() function in "strbuf.{c,h}" and use it in both "json-writer.c" and "builtin/submodule-helper.c".

We use the "strbuf_addstrings" name as this way strbuf_addstr() and strbuf_addstrings() would be similar for strings as strbuf_addch() and strbuf_addchars() for characters.

---
The changes between version 1 and version 2 are the following:
- Remove memcpy() that involves manual offset computation
- Use strbuf_add() repeatedly after calling strbuf_grow() once to avoid repeated allocation.

Thanks to Junio for pointing this out.
Here is a diff between v1 and v2:

- size_t len = strlen(s);
- if (unsigned_mult_overflows(len, n))
- die("you want to use way too much memory");
- strbuf_grow(sb, len * n);
- for (size_t i = 0; i < n; i++)
- memcpy(sb->buf + sb->len + len * i, s, len);
- strbuf_setlen(sb, sb->len + len * n);
+ size_t len = strlen(s);
+
+ if (unsigned_mult_overflows(len, n))
+ die("you want to use way too much memory");
+ strbuf_grow(sb, len * n);
+ for (size_t i = 0; i < n; i++)
+ strbuf_add(sb, s, len);

 builtin/submodule--helper.c |  4 +---
 json-writer.c               |  5 +----
 strbuf.c                    | 10 ++++++++++
 strbuf.h                    |  5 +++++
 4 files changed, 17 insertions(+), 7 deletions(-)
builtin/submodule--helper.c +1 -3
index fda50f2af1..bed08af410 100644
@@ -257,11 +257,9 @@ static void module_list_active(struct module_list *list)
 
 static char *get_up_path(const char *path)
 {
-	int i;
 	struct strbuf sb = STRBUF_INIT;
 
-	for (i = count_slashes(path); i; i--)
-		strbuf_addstr(&sb, "../");
+	strbuf_addstrings(&sb, "../", count_slashes(path));
 
 	/*
 	 * Check if 'path' ends with slash or not
json-writer.c +1 -4
index 005c820aa4..25b9201f9c 100644
@@ -46,10 +46,7 @@ static void append_quoted_string(struct strbuf *out, const char *in)
 
 static void indent_pretty(struct json_writer *jw)
 {
-	int k;
-
-	for (k = 0; k < jw->open_stack.len; k++)
-		strbuf_addstr(&jw->json, "  ");
+	strbuf_addstrings(&jw->json, "  ", jw->open_stack.len);
 }
 
 /*
strbuf.c +11 -0
index 7827178d8e..f4282b70ad 100644
@@ -302,6 +302,16 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 	strbuf_setlen(sb, sb->len + len);
 }
 
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n)
+{
+       size_t len = strlen(s);
+
+       if (unsigned_mult_overflows(len, n))
+               die("you want to use way too much memory");
+       strbuf_grow(sb, len * n);
+       for (size_t i = 0; i < n; i++)
+               strbuf_add(sb, s, len);
+}
+
 void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
 {

strbuf_grow(sb, sb2->len);

strbuf.h +5 -0
index e959caca87..0fb1b5e81e 100644
@@ -310,6 +310,11 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
 	strbuf_add(sb, s, strlen(s));
 }
 
+/**
+ * Add a NUL-terminated string the specified number of times to the buffer.
+ */
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n);
+
 /**
  * Copy the contents of another buffer at the end of the current one.
  */

--
2.43.0.windows.1

author
#7 lore raw

PATCH v2 2/2 Port helper/test-sha256.c and helper/test-sha1.c to unit-tests/t-hash.c

In the recent codebase update (8bf6fbd (Merge branch 'js/doc-unit-tests', 2023-12-09)), a new unit testing framework was merged, providing a standardized approach for testing C code. Prior to this update, some unit tests relied on the test helper mechanism, lacking a dedicated unit testing framework. It's more natural to perform these unit tests using the new unit test framework.

This commit migrates the unit tests for hash functionality from the legacy approach using the test-tool command test-tool sha1and test-tool sha256 in helper/test-sha256.c and helper/test-sha1.c to the new unit testing framework (t/unit-tests/test-lib.h). Porting t0013-sha1dc.sh is left for later.

The migration involves refactoring the tests to utilize the testing macros provided by the framework (TEST() and check_*()).

---
The change between version 1 and version 2 is:
- Deleted t/helper/test-sha256.c

Here is a diff between v1 and v2:

-#include "test-tool.h"
-#include "hash-ll.h"
-
-int cmd__sha256(int ac, const char **av)
-{
- return cmd_hash_impl(ac, av, GIT_HASH_SHA256);
-}

 Makefile               |  2 +-
 t/helper/test-sha256.c |  7 ----
 t/helper/test-tool.c   |  1 -
 t/helper/test-tool.h   |  1 -
 t/t0015-hash.sh        | 56 -------------------------------
 t/unit-tests/t-hash.c  | 75 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 76 insertions(+), 66 deletions(-)
 delete mode 100644 t/helper/test-sha256.c
 delete mode 100755 t/t0015-hash.sh
 create mode 100644 t/unit-tests/t-hash.c
Makefile +1 -1
index 4e255c81f2..c85f24f813 100644
@@ -847,7 +847,6 @@ TEST_BUILTINS_OBJS += test-run-command.o
 TEST_BUILTINS_OBJS += test-scrap-cache-tree.o
 TEST_BUILTINS_OBJS += test-serve-v2.o
 TEST_BUILTINS_OBJS += test-sha1.o
-TEST_BUILTINS_OBJS += test-sha256.o
 TEST_BUILTINS_OBJS += test-sigchain.o
 TEST_BUILTINS_OBJS += test-simple-ipc.o
 TEST_BUILTINS_OBJS += test-strcmp-offset.o
@@ -1347,6 +1346,7 @@ UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-strbuf
 UNIT_TEST_PROGRAMS += t-ctype
 UNIT_TEST_PROGRAMS += t-prio-queue
+UNIT_TEST_PROGRAMS += t-hash
 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
t/helper/test-sha256.c deleted +0 -7
deleted file mode 100644
index 08cf149001..0000000000
@@ -1,7 +0,0 @@
-#include "test-tool.h"
-#include "hash-ll.h"
-
-int cmd__sha256(int ac, const char **av)
-{
-	return cmd_hash_impl(ac, av, GIT_HASH_SHA256);
-}
t/helper/test-tool.c +0 -1
index 482a1e58a4..7bfbb75c9b 100644
@@ -73,7 +73,6 @@ static struct test_cmd cmds[] = {
 	{ "serve-v2", cmd__serve_v2 },
 	{ "sha1", cmd__sha1 },
 	{ "sha1-is-sha1dc", cmd__sha1_is_sha1dc },
-	{ "sha256", cmd__sha256 },
 	{ "sigchain", cmd__sigchain },
 	{ "simple-ipc", cmd__simple_ipc },
 	{ "strcmp-offset", cmd__strcmp_offset },
t/helper/test-tool.h +0 -1
index b1be7cfcf5..8139c9664d 100644
@@ -66,7 +66,6 @@ int cmd__serve_v2(int argc, const char **argv);
 int cmd__sha1(int argc, const char **argv);
 int cmd__sha1_is_sha1dc(int argc, const char **argv);
 int cmd__oid_array(int argc, const char **argv);
-int cmd__sha256(int argc, const char **argv);
 int cmd__sigchain(int argc, const char **argv);
 int cmd__simple_ipc(int argc, const char **argv);
 int cmd__strcmp_offset(int argc, const char **argv);
t/t0015-hash.sh deleted +0 -56

56 lines not on this page · raw message

t/unit-tests/t-hash.c new file +75 -0

75 lines not on this page · raw message

--
2.43.0.windows.1

#8 lore raw

Re: [Outreachy][PATCH v2 2/2] Port helper/test-sha256.c and helper/test-sha1.c to unit-tests/t-hash.c

Quoted 17 lines · Achu Luma

In the recent codebase update (8bf6fbd (Merge branch 'js/doc-unit-tests', 2023-12-09)), a new unit testing framework was merged, providing a standardized approach for testing C code. Prior to this update, some unit tests relied on the test helper mechanism, lacking a dedicated unit testing framework. It's more natural to perform these unit tests using the new unit test framework.

This commit migrates the unit tests for hash functionality from the legacy approach using the test-tool command test-tool sha1and test-tool sha256 in helper/test-sha256.c and helper/test-sha1.c to the new unit testing framework (t/unit-tests/test-lib.h). Porting t0013-sha1dc.sh is left for later.

The migration involves refactoring the tests to utilize the testing macros provided by the framework (TEST() and check_*()).

Quoted 7 lines

---
The change between version 1 and version 2 is:
- Deleted t/helper/test-sha256.c

Here is a diff between v1 and v2:

When sending a patch series, even a small one, I think it's better to have a cover letter (using for example git format-patch --cover-letter ...) and list all the changes from the previous version in the cover letter, rather than listing some changes in each patch. Also, instead of git diff, git range-diff can be used and its output can be put into the cover letter too.

I don't think it's worth resending the series just for this though.

Maybe I missed it but it seems that this patch and patch 1/2
(https://lore.kernel.org/git/[email protected]/)
in the series fell into the cracks.

#9 lore raw

Re: [Outreachy][PATCH v2 2/2] Port helper/test-sha256.c and helper/test-sha1.c to unit-tests/t-hash.c

In reply to Achu Luma
Quoted 9 lines · Achu Luma

In the recent codebase update (8bf6fbd (Merge branch 'js/doc-unit-tests', 2023-12-09)), a new unit testing framework was merged, providing a standardized approach for testing C code. Prior to this update, some unit tests relied on the test helper mechanism, lacking a dedicated unit testing framework. It's more natural to perform these unit tests using the new unit test framework.

This commit migrates the unit tests for hash functionality from the legacy approach using the test-tool command test-tool sha1and

Nit: s/sha1and/sha1 and/.

Quoted 72 lines

test-tool sha256 in helper/test-sha256.c and helper/test-sha1.c to the new unit testing framework (t/unit-tests/test-lib.h). Porting t0013-sha1dc.sh is left for later.

The migration involves refactoring the tests to utilize the testing macros provided by the framework (TEST() and check_*()).

---
The change between version 1 and version 2 is:
- Deleted t/helper/test-sha256.c

Here is a diff between v1 and v2:

-#include "test-tool.h"
-#include "hash-ll.h"
-
-int cmd__sha256(int ac, const char **av)
-{
- return cmd_hash_impl(ac, av, GIT_HASH_SHA256);
-}

 Makefile               |  2 +-
 t/helper/test-sha256.c |  7 ----
 t/helper/test-tool.c   |  1 -
 t/helper/test-tool.h   |  1 -
 t/t0015-hash.sh        | 56 -------------------------------
 t/unit-tests/t-hash.c  | 75 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 76 insertions(+), 66 deletions(-)
 delete mode 100644 t/helper/test-sha256.c
 delete mode 100755 t/t0015-hash.sh
 create mode 100644 t/unit-tests/t-hash.c
Makefile +1 -1
index 4e255c81f2..c85f24f813 100644
@@ -847,7 +847,6 @@ TEST_BUILTINS_OBJS += test-run-command.o
 TEST_BUILTINS_OBJS += test-scrap-cache-tree.o
 TEST_BUILTINS_OBJS += test-serve-v2.o
 TEST_BUILTINS_OBJS += test-sha1.o
-TEST_BUILTINS_OBJS += test-sha256.o
 TEST_BUILTINS_OBJS += test-sigchain.o
 TEST_BUILTINS_OBJS += test-simple-ipc.o
 TEST_BUILTINS_OBJS += test-strcmp-offset.o
@@ -1347,6 +1346,7 @@ UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-strbuf
 UNIT_TEST_PROGRAMS += t-ctype
 UNIT_TEST_PROGRAMS += t-prio-queue
+UNIT_TEST_PROGRAMS += t-hash
 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
t/helper/test-sha256.c deleted +0 -7
deleted file mode 100644
index 08cf149001..0000000000
@@ -1,7 +0,0 @@
-#include "test-tool.h"
-#include "hash-ll.h"
-
-int cmd__sha256(int ac, const char **av)
-{
-	return cmd_hash_impl(ac, av, GIT_HASH_SHA256);
-}
t/helper/test-tool.c +0 -0
index 482a1e58a4..7bfbb75c9b 100644

t/helper/test-tool.c static struct test_cmd cmds[] = { · line 74

Quoted 2 lines
t/helper/test-tool.c +0 -0
@@ -73,7 +73,6 @@ static struct test_cmd cmds[] = {
 	{ "serve-v2", cmd__serve_v2 },
 	{ "sha1", cmd__sha1 },

I'm a bit surprised that the cmd_sha1 subcommand still exists. I would have expected it to be removed as part of this commit, as well, judging by the commit message.

In any case I think it would be easier to review if the "sha1" and "sha256" refactorings were split out into two separate patches.

Patrick

Quoted 164 lines
t/helper/test-tool.c +0 -1
 	{ "sha1-is-sha1dc", cmd__sha1_is_sha1dc },
-	{ "sha256", cmd__sha256 },
 	{ "sigchain", cmd__sigchain },
 	{ "simple-ipc", cmd__simple_ipc },
 	{ "strcmp-offset", cmd__strcmp_offset },
t/helper/test-tool.h +0 -1
index b1be7cfcf5..8139c9664d 100644
@@ -66,7 +66,6 @@ int cmd__serve_v2(int argc, const char **argv);
 int cmd__sha1(int argc, const char **argv);
 int cmd__sha1_is_sha1dc(int argc, const char **argv);
 int cmd__oid_array(int argc, const char **argv);
-int cmd__sha256(int argc, const char **argv);
 int cmd__sigchain(int argc, const char **argv);
 int cmd__simple_ipc(int argc, const char **argv);
 int cmd__strcmp_offset(int argc, const char **argv);
t/t0015-hash.sh deleted +0 -56
deleted file mode 100755
index 0a087a1983..0000000000
@@ -1,56 +0,0 @@
-#!/bin/sh
-
-test_description='test basic hash implementation'
-
-TEST_PASSES_SANITIZE_LEAK=true
-. ./test-lib.sh
-
-test_expect_success 'test basic SHA-1 hash values' '
-	test-tool sha1 </dev/null >actual &&
-	grep da39a3ee5e6b4b0d3255bfef95601890afd80709 actual &&
-	printf "a" | test-tool sha1 >actual &&
-	grep 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8 actual &&
-	printf "abc" | test-tool sha1 >actual &&
-	grep a9993e364706816aba3e25717850c26c9cd0d89d actual &&
-	printf "message digest" | test-tool sha1 >actual &&
-	grep c12252ceda8be8994d5fa0290a47231c1d16aae3 actual &&
-	printf "abcdefghijklmnopqrstuvwxyz" | test-tool sha1 >actual &&
-	grep 32d10c7b8cf96570ca04ce37f2a19d84240d3a89 actual &&
-	perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" |
-		test-tool sha1 >actual &&
-	grep 34aa973cd4c4daa4f61eeb2bdbad27316534016f actual &&
-	printf "blob 0\0" | test-tool sha1 >actual &&
-	grep e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 actual &&
-	printf "blob 3\0abc" | test-tool sha1 >actual &&
-	grep f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f actual &&
-	printf "tree 0\0" | test-tool sha1 >actual &&
-	grep 4b825dc642cb6eb9a060e54bf8d69288fbee4904 actual
-'
-
-test_expect_success 'test basic SHA-256 hash values' '
-	test-tool sha256 </dev/null >actual &&
-	grep e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 actual &&
-	printf "a" | test-tool sha256 >actual &&
-	grep ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb actual &&
-	printf "abc" | test-tool sha256 >actual &&
-	grep ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad actual &&
-	printf "message digest" | test-tool sha256 >actual &&
-	grep f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650 actual &&
-	printf "abcdefghijklmnopqrstuvwxyz" | test-tool sha256 >actual &&
-	grep 71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73 actual &&
-	# Try to exercise the chunking code by turning autoflush on.
-	perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" |
-		test-tool sha256 >actual &&
-	grep cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0 actual &&
-	perl -e "$| = 1; print q{abcdefghijklmnopqrstuvwxyz} for 1..100000;" |
-		test-tool sha256 >actual &&
-	grep e406ba321ca712ad35a698bf0af8d61fc4dc40eca6bdcea4697962724ccbde35 actual &&
-	printf "blob 0\0" | test-tool sha256 >actual &&
-	grep 473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813 actual &&
-	printf "blob 3\0abc" | test-tool sha256 >actual &&
-	grep c1cf6e465077930e88dc5136641d402f72a229ddd996f627d60e9639eaba35a6 actual &&
-	printf "tree 0\0" | test-tool sha256 >actual &&
-	grep 6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321 actual
-'
-
-test_done
t/unit-tests/t-hash.c new file +75 -1
new file mode 100644
index 0000000000..b512e64bf1
@@ -0,0 +1,75 @@
+#include "test-lib.h"
+#include "hash-ll.h"
+#include "hex.h"
+#include "strbuf.h"
+
+static void check_hash_data(const void *data, size_t data_length, const char *expected, int algo) {
+	git_hash_ctx ctx;
+	unsigned char hash[GIT_MAX_HEXSZ];
+	const struct git_hash_algo *algop = &hash_algos[algo];
+
+	if (!check(!!data)) {
+    		test_msg("Error: No data provided when expecting: %s", expected);
+    		return;
+	}
+
+	algop->init_fn(&ctx);
+	algop->update_fn(&ctx, data, data_length);
+	algop->final_fn(hash, &ctx);
+
+	check_str(hash_to_hex_algop(hash, algop), expected);
+}
+
+/* Works with a NUL terminated string. Doesn't work if it should contain a NUL character. */
+#define TEST_SHA1_STR(data, expected) \
+    	TEST(check_hash_data(data, strlen(data), expected, GIT_HASH_SHA1), \
+        	"SHA1 (%s) works", #data)
+
+/* Only works with a literal string, useful when it contains a NUL character. */
+#define TEST_SHA1_LITERAL(literal, expected) \
+    	TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA1), \
+        	"SHA1 (%s) works", #literal)
+
+/* Works with a NUL terminated string. Doesn't work if it should contain a NUL character. */
+#define TEST_SHA256_STR(data, expected) \
+    	TEST(check_hash_data(data, strlen(data), expected, GIT_HASH_SHA256), \
+        	"SHA256 (%s) works", #data)
+
+/* Only works with a literal string, useful when it contains a NUL character. */
+#define TEST_SHA256_LITERAL(literal, expected) \
+    	TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA256), \
+        	"SHA256 (%s) works", #literal)
+
+int cmd_main(int argc, const char **argv) {
+	struct strbuf aaaaaaaaaa_100000 = STRBUF_INIT;
+	struct strbuf alphabet_100000 = STRBUF_INIT;
+
+	strbuf_addstrings(&aaaaaaaaaa_100000, "aaaaaaaaaa", 100000);
+	strbuf_addstrings(&alphabet_100000, "abcdefghijklmnopqrstuvwxyz", 100000);
+
+	TEST_SHA1_STR("", "da39a3ee5e6b4b0d3255bfef95601890afd80709");
+	TEST_SHA1_STR("a", "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8");
+	TEST_SHA1_STR("abc", "a9993e364706816aba3e25717850c26c9cd0d89d");
+	TEST_SHA1_STR("message digest", "c12252ceda8be8994d5fa0290a47231c1d16aae3");
+	TEST_SHA1_STR("abcdefghijklmnopqrstuvwxyz", "32d10c7b8cf96570ca04ce37f2a19d84240d3a89");
+	TEST_SHA1_STR(aaaaaaaaaa_100000.buf, "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
+	TEST_SHA1_LITERAL("blob 0\0", "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
+	TEST_SHA1_LITERAL("blob 3\0abc", "f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f");
+	TEST_SHA1_LITERAL("tree 0\0", "4b825dc642cb6eb9a060e54bf8d69288fbee4904");
+
+	TEST_SHA256_STR("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+	TEST_SHA256_STR("a", "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb");
+	TEST_SHA256_STR("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
+	TEST_SHA256_STR("message digest", "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650");
+	TEST_SHA256_STR("abcdefghijklmnopqrstuvwxyz", "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73");
+	TEST_SHA256_STR(aaaaaaaaaa_100000.buf, "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
+	TEST_SHA256_STR(alphabet_100000.buf, "e406ba321ca712ad35a698bf0af8d61fc4dc40eca6bdcea4697962724ccbde35");
+	TEST_SHA256_LITERAL("blob 0\0", "473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813");
+	TEST_SHA256_LITERAL("blob 3\0abc", "c1cf6e465077930e88dc5136641d402f72a229ddd996f627d60e9639eaba35a6");
+	TEST_SHA256_LITERAL("tree 0\0", "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321");
+
+	strbuf_release(&aaaaaaaaaa_100000);
+	strbuf_release(&alphabet_100000);
+
+	return test_done();
+}
--

2.43.0.windows.1

#10 lore raw

Re: [Outreachy][PATCH v2 2/2] Port helper/test-sha256.c and helper/test-sha1.c to unit-tests/t-hash.c

Quoted 14 lines · Patrick Steinhardt

t/helper/test-tool.c static struct test_cmd cmds[] = { · line 74

Quoted 7 lines · Achu Luma
t/helper/test-tool.c +0 -0
index 482a1e58a4..7bfbb75c9b 100644
@@ -73,7 +73,6 @@ static struct test_cmd cmds[] = {
      { "serve-v2", cmd__serve_v2 },
      { "sha1", cmd__sha1 },

I'm a bit surprised that the cmd_sha1 subcommand still exists. I would have expected it to be removed as part of this commit, as well, judging by the commit message.

I think that's because it is used in t0013-sha1dc.sh not just t0015-hash.sh, and porting t0013-sha1dc.sh is complex because it relies on test-tool sha1 dying when it is used on a file created to contain the known sha1 attack.

But I agree this should be better explained in the commit message.

Quoted 2 lines

In any case I think it would be easier to review if the "sha1" and "sha256" refactorings were split out into two separate patches.

Thanks for your review!

#11 lore raw

Re: [Outreachy][PATCH v2 2/2] Port helper/test-sha256.c and helper/test-sha1.c to unit-tests/t-hash.c

In reply to Achu Luma
Quoted 17 lines · Achu Luma
t/helper/test-tool.c +0 -1
index 482a1e58a4..7bfbb75c9b 100644
@@ -73,7 +73,6 @@ static struct test_cmd cmds[] = {
 	{ "serve-v2", cmd__serve_v2 },
 	{ "sha1", cmd__sha1 },
 	{ "sha1-is-sha1dc", cmd__sha1_is_sha1dc },
-	{ "sha256", cmd__sha256 },
 	{ "sigchain", cmd__sigchain },
 	{ "simple-ipc", cmd__simple_ipc },
 	{ "strcmp-offset", cmd__strcmp_offset },
t/helper/test-tool.h +0 -0
index b1be7cfcf5..8139c9664d 100644

t/helper/test-tool.h int cmd__serve_v2(int argc, const char **argv); · line 69

Quoted 4 lines
t/helper/test-tool.h +0 -1
@@ -66,7 +66,6 @@ int cmd__serve_v2(int argc, const char **argv);
 int cmd__sha1(int argc, const char **argv);
 int cmd__sha1_is_sha1dc(int argc, const char **argv);
 int cmd__oid_array(int argc, const char **argv);
-int cmd__sha256(int argc, const char **argv);

Removing 'test-tool sha256' would break many tests in t53** when GIT_TEST_DEFAULT_HASH=sha256, because it is used in pack_trailer():t/lib-pack.sh.

```
    pack_trailer () {
	    test-tool $(test_oid algo) -b <"$1" >trailer.tmp &&
	    cat trailer.tmp >>"$1" &&
	    rm -f trailer.tmp
    }
```

Also, I am planning to take over this series, so I will take care of this.

Thanks.

#12 lore raw

v3 cover Port t0015-hash to the unit testing framework

In reply to Achu Luma

Changes in v3:
- use st_mult() instead of unsigned_mult_overflows(), as suggested by Junio.
- split migration of SHA-1 and SHA-256 in different patches, as suggested by Patrick.
- Revert removal of 'sha256' subcommand of test-tool due to existing dependencies on it.
- Fix some formatting

CI for v3: https://github.com/spectre10/git/actions/runs/9216124753

Ghanshyam Thakkar (3):
strbuf: introduce strbuf_addstrings() to repeatedly add a string t/: port helper/test-sha1.c to unit-tests/t-hash.c t/: port helper/test-sha256.c to unit-tests/t-hash.c

 Makefile                    |  1 +
 builtin/submodule--helper.c |  4 +-
 json-writer.c               |  5 +--
 strbuf.c                    |  9 +++++
 strbuf.h                    |  5 +++
 t/t0015-hash.sh             | 56 --------------------------
 t/unit-tests/t-hash.c       | 79 +++++++++++++++++++++++++++++++++++++
 7 files changed, 96 insertions(+), 63 deletions(-)
 delete mode 100755 t/t0015-hash.sh
 create mode 100644 t/unit-tests/t-hash.c

Range-diff against v2:
1: b8c4ab31c6 ! 1: cd831fabf5 strbuf: introduce strbuf_addstrings() to repeatedly add a string
@@
## Metadata ##
-Author: Achu Luma <[email protected]>
+Author: Ghanshyam Thakkar <[email protected]>

## Commit message ##
strbuf: introduce strbuf_addstrings() to repeatedly add a string
@@ Commit message
strbuf_addstrings() would be similar for strings as strbuf_addch() and strbuf_addchars() for characters.

+ Helped-by: Junio C Hamano <[email protected]>

+ Mentored-by: Kaartic Sivaraam <[email protected]>
+ Co-authored-by: Achu Luma <[email protected]>

+ Signed-off-by: Ghanshyam Thakkar <[email protected]>

## builtin/submodule--helper.c ##
@@ builtin/submodule--helper.c: static void module_list_active(struct module_list *list)
@@ strbuf.c: void strbuf_add(struct strbuf *sb, const void *data, size_t len)

+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n) +{
-+ size_t len = strlen(s);
-+ if (unsigned_mult_overflows(len, n))
-+ die("you want to use way too much memory");
-+ strbuf_grow(sb, len * n);
-+ for (size_t i = 0; i < n; i++)
-+ strbuf_add(sb, s, len);
++ size_t len = strlen(s);
++
++ strbuf_grow(sb, st_mult(len, n));
++ for (size_t i = 0; i < n; i++)
++ strbuf_add(sb, s, len);
+}
+
void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)

2:  20a7ff8e24 < -:  ---------- Port helper/test-sha256.c and helper/test-sha1.c to unit-tests/t-hash.c
-:  ---------- > 2:  2c4471ce37 t/: port helper/test-sha1.c to unit-tests/t-hash.c
-:  ---------- > 3:  68dcf388d8 t/: port helper/test-sha256.c to unit-tests/t-hash.c
Signature · 1 line
2.45.1
#13 lore raw

PATCH v3 1/3 strbuf: introduce strbuf_addstrings() to repeatedly add a string

In a following commit we are going to port code from "t/helper/test-sha256.c", t/helper/test-hash.c and "t/t0015-hash.sh" to a new "t/unit-tests/t-hash.c" file using the recently added unit test framework.

To port code like: perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" we are going to need a new strbuf_addstrings() function that repeatedly adds the same string a number of times to a buffer.

Such a strbuf_addstrings() function would already be useful in "json-writer.c" and "builtin/submodule-helper.c" as both of these files already have code that repeatedly adds the same string. So let's introduce such a strbuf_addstrings() function in "strbuf.{c,h}" and use it in both "json-writer.c" and "builtin/submodule-helper.c".

We use the "strbuf_addstrings" name as this way strbuf_addstr() and strbuf_addstrings() would be similar for strings as strbuf_addch() and strbuf_addchars() for characters.

---
 builtin/submodule--helper.c | 4 +---
 json-writer.c               | 5 +----
 strbuf.c                    | 9 +++++++++
 strbuf.h                    | 5 +++++
 4 files changed, 16 insertions(+), 7 deletions(-)
builtin/submodule--helper.c +1 -3
index e604cb5ddb..5620a5bf67 100644
@@ -256,11 +256,9 @@ static void module_list_active(struct module_list *list)
 
 static char *get_up_path(const char *path)
 {
-	int i;
 	struct strbuf sb = STRBUF_INIT;
 
-	for (i = count_slashes(path); i; i--)
-		strbuf_addstr(&sb, "../");
+	strbuf_addstrings(&sb, "../", count_slashes(path));
 
 	/*
 	 * Check if 'path' ends with slash or not
json-writer.c +1 -4
index 005c820aa4..25b9201f9c 100644
@@ -46,10 +46,7 @@ static void append_quoted_string(struct strbuf *out, const char *in)
 
 static void indent_pretty(struct json_writer *jw)
 {
-	int k;
-
-	for (k = 0; k < jw->open_stack.len; k++)
-		strbuf_addstr(&jw->json, "  ");
+	strbuf_addstrings(&jw->json, "  ", jw->open_stack.len);
 }
 
 /*
strbuf.c +9 -0
index 0d929e4e19..e3ca9b1ee9 100644
@@ -313,6 +313,15 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 	strbuf_setlen(sb, sb->len + len);
 }
 
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n)
+{
+	size_t len = strlen(s);
+
+	strbuf_grow(sb, st_mult(len, n));
+	for (size_t i = 0; i < n; i++)
+		strbuf_add(sb, s, len);
+}
+
 void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
 {
 	strbuf_grow(sb, sb2->len);
strbuf.h +5 -0
index 97fa4a3d01..003f880ff7 100644
@@ -310,6 +310,11 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
 	strbuf_add(sb, s, strlen(s));
 }
 
+/**
+ * Add a NUL-terminated string the specified number of times to the buffer.
+ */
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n);
+
 /**
  * Copy the contents of another buffer at the end of the current one.
  */
Signature · 1 line
2.45.1
#14 lore raw

PATCH v3 2/3 t/: port helper/test-sha1.c to unit-tests/t-hash.c

In reply to Ghanshyam Thakkar

t/helper/test-sha1 and t/t0015-hash.sh test the hash implementation of SHA-1 in Git with basic SHA-1 hash values. Migrate them to the new unit testing framework for better debugging and runtime performance.

The sha1 subcommand from test-tool is still not removed because it is relied upon by t0013-sha1dc (which requires 'test-tool sha1' dying when it is used on a file created to contain the known sha1 attack) and pack_trailer():lib-pack.sh.

---
 Makefile              |  1 +
 t/t0015-hash.sh       | 22 ------------------
 t/unit-tests/t-hash.c | 54 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 22 deletions(-)
 create mode 100644 t/unit-tests/t-hash.c
Makefile +1 -0
index 8f4432ae57..2b19fdf6ae 100644
@@ -1335,6 +1335,7 @@ THIRD_PARTY_SOURCES += sha1collisiondetection/%
 THIRD_PARTY_SOURCES += sha1dc/%
 
 UNIT_TEST_PROGRAMS += t-ctype
+UNIT_TEST_PROGRAMS += t-hash
 UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-prio-queue
 UNIT_TEST_PROGRAMS += t-strbuf
t/t0015-hash.sh +0 -22
index 0a087a1983..2264e702d5 100755
@@ -5,28 +5,6 @@ test_description='test basic hash implementation'
 TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
-test_expect_success 'test basic SHA-1 hash values' '
-	test-tool sha1 </dev/null >actual &&
-	grep da39a3ee5e6b4b0d3255bfef95601890afd80709 actual &&
-	printf "a" | test-tool sha1 >actual &&
-	grep 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8 actual &&
-	printf "abc" | test-tool sha1 >actual &&
-	grep a9993e364706816aba3e25717850c26c9cd0d89d actual &&
-	printf "message digest" | test-tool sha1 >actual &&
-	grep c12252ceda8be8994d5fa0290a47231c1d16aae3 actual &&
-	printf "abcdefghijklmnopqrstuvwxyz" | test-tool sha1 >actual &&
-	grep 32d10c7b8cf96570ca04ce37f2a19d84240d3a89 actual &&
-	perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" |
-		test-tool sha1 >actual &&
-	grep 34aa973cd4c4daa4f61eeb2bdbad27316534016f actual &&
-	printf "blob 0\0" | test-tool sha1 >actual &&
-	grep e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 actual &&
-	printf "blob 3\0abc" | test-tool sha1 >actual &&
-	grep f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f actual &&
-	printf "tree 0\0" | test-tool sha1 >actual &&
-	grep 4b825dc642cb6eb9a060e54bf8d69288fbee4904 actual
-'
-
 test_expect_success 'test basic SHA-256 hash values' '
 	test-tool sha256 </dev/null >actual &&
 	grep e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 actual &&
t/unit-tests/t-hash.c new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.45.1
#15 lore raw

Re: [PATCH v3 2/3] t/: port helper/test-sha1.c to unit-tests/t-hash.c

Quoted 8 lines · Ghanshyam Thakkar

t/helper/test-sha1 and t/t0015-hash.sh test the hash implementation of SHA-1 in Git with basic SHA-1 hash values. Migrate them to the new unit testing framework for better debugging and runtime performance.

The sha1 subcommand from test-tool is still not removed because it is relied upon by t0013-sha1dc (which requires 'test-tool sha1' dying when it is used on a file created to contain the known sha1 attack) and pack_trailer():lib-pack.sh.

Can we refactor this test to stop doing that? E.g., would it work if we used git-hash-object(1) to check that SHA1DC does its thing? Then we could get rid of the helper altogether, as far as I understand.

Quoted 9 lines
t/unit-tests/t-hash.c new file +3 -0
new file mode 100644
index 0000000000..89dfea9cc1
@@ -0,0 +1,54 @@
+#include "test-lib.h"
+#include "hash-ll.h"
+#include "hex.h"

t/unit-tests/t-hash.c line 13

Quoted 10 lines
t/unit-tests/t-hash.c new file +10 -0
+#include "strbuf.h"
+
+static void check_hash_data(const void *data, size_t data_length,
+			    const char *expected, int algo)
+{
+	git_hash_ctx ctx;
+	unsigned char hash[GIT_MAX_HEXSZ];
+	const struct git_hash_algo *algop = &hash_algos[algo];
+
+	if (!check(!!data)) {

Is this double negation needed? Can't we just if (!check(data))?

t/unit-tests/t-hash.c line 14

Quoted 1 line
t/unit-tests/t-hash.c new file +1 -0
+		test_msg("Error: No data provided when expecting: %s", expected);

This error message is a bit atypical compared to the other callers of this function. We could say something like "BUG: test has no data", which would match something we have in "t/unit-tests/test-lib.c".

Quoted 10 lines
t/unit-tests/t-hash.c new file +9 -0
+		return;
+	}
+
+	algop->init_fn(&ctx);
+	algop->update_fn(&ctx, data, data_length);
+	algop->final_fn(hash, &ctx);
+
+	check_str(hash_to_hex_algop(hash, algop), expected);
+}

t/unit-tests/t-hash.c line 33

Quoted 10 lines
t/unit-tests/t-hash.c new file +10 -0
+
+/* Works with a NUL terminated string. Doesn't work if it should contain a NUL character. */
+#define TEST_SHA1_STR(data, expected) \
+	TEST(check_hash_data(data, strlen(data), expected, GIT_HASH_SHA1), \
+	     "SHA1 (%s) works", #data)
+
+/* Only works with a literal string, useful when it contains a NUL character. */
+#define TEST_SHA1_LITERAL(literal, expected) \
+	TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA1), \
+	     "SHA1 (%s) works", #literal)

This macro also works for TEST_SHA1_STR(), right? Is there a partiuclar reason why we don't unify them?

Patrick

#16 lore raw

Re: [PATCH v3 2/3] t/: port helper/test-sha1.c to unit-tests/t-hash.c

Quoted 6 lines · Patrick Steinhardt
Quoted 4 lines · Ghanshyam Thakkar

t/helper/test-sha1 and t/t0015-hash.sh test the hash implementation of SHA-1 in Git with basic SHA-1 hash values. Migrate them to the new unit testing framework for better debugging and runtime performance.

Quoted 8 lines
Quoted 4 lines

The sha1 subcommand from test-tool is still not removed because it is relied upon by t0013-sha1dc (which requires 'test-tool sha1' dying when it is used on a file created to contain the known sha1 attack) and pack_trailer():lib-pack.sh.

Can we refactor this test to stop doing that? E.g., would it work if we used git-hash-object(1) to check that SHA1DC does its thing? Then we could get rid of the helper altogether, as far as I understand.

It could perhaps work if we used git-hash-object(1) instead of test-tool sha1 in t0013-sha1dc to check that SHA1DC does its thing, but we could do that in a separate patch or patch series.

Quoted 13 lines
Quoted 13 lines
t/unit-tests/t-hash.c new file +7 -0
new file mode 100644
index 0000000000..89dfea9cc1
@@ -0,0 +1,54 @@
+#include "test-lib.h"
+#include "hash-ll.h"
+#include "hex.h"
+#include "strbuf.h"
+
+static void check_hash_data(const void *data, size_t data_length,
+                         const char *expected, int algo)
Quoted 8 lines

t/unit-tests/t-hash.c line 13

Quoted 6 lines
t/unit-tests/t-hash.c new file +6 -0
+{
+     git_hash_ctx ctx;
+     unsigned char hash[GIT_MAX_HEXSZ];
+     const struct git_hash_algo *algop = &hash_algos[algo];
+
+     if (!check(!!data)) {

Is this double negation needed? Can't we just if (!check(data))?

As far as I remember it is needed as check() is expecting an 'int' while 'data' is a 'void *'.

Quoted 5 lines

t/unit-tests/t-hash.c line 14

Quoted 1 line
t/unit-tests/t-hash.c new file +1 -0
+             test_msg("Error: No data provided when expecting: %s", expected);

This error message is a bit atypical compared to the other callers of this function. We could say something like "BUG: test has no data", which would match something we have in "t/unit-tests/test-lib.c".

Actually I think something like "BUG: Null data pointer provided" would be even better.

Quoted 14 lines
Quoted 14 lines
t/unit-tests/t-hash.c new file +14 -0
+             return;
+     }
+
+     algop->init_fn(&ctx);
+     algop->update_fn(&ctx, data, data_length);
+     algop->final_fn(hash, &ctx);
+
+     check_str(hash_to_hex_algop(hash, algop), expected);
+}
+
+/* Works with a NUL terminated string. Doesn't work if it should contain a NUL character. */
+#define TEST_SHA1_STR(data, expected) \
+     TEST(check_hash_data(data, strlen(data), expected, GIT_HASH_SHA1), \
+          "SHA1 (%s) works", #data)
Quoted 8 lines

t/unit-tests/t-hash.c line 33

Quoted 6 lines
t/unit-tests/t-hash.c new file +5 -0
+
+/* Only works with a literal string, useful when it contains a NUL character. */
+#define TEST_SHA1_LITERAL(literal, expected) \
+     TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA1), \
+          "SHA1 (%s) works", #literal)

This macro also works for TEST_SHA1_STR(), right?

No, it uses 'sizeof(literal)' which works only for string literals.

Quoted 2 lines

Is there a
partiuclar reason why we don't unify them?

The comments above them try to explain that the first one doesn't work when the data contains a NUL char as it uses strlen() while the second one works only for string literals including those which contain NUL characters.

Thanks for your review.

#17 lore raw

Re: [PATCH v3 2/3] t/: port helper/test-sha1.c to unit-tests/t-hash.c

Quoted 7 lines · Christian Couder
Quoted 3 lines

Can we refactor this test to stop doing that? E.g., would it work if we used git-hash-object(1) to check that SHA1DC does its thing? Then we could get rid of the helper altogether, as far as I understand.

It could perhaps work if we used git-hash-object(1) instead of test-tool sha1 in t0013-sha1dc to check that SHA1DC does its thing, but we could do that in a separate patch or patch series.

Yeah, I think such a plan to make preliminary refactoring as a separate series, and then have another series to get rid of "test-tool sha1" (and "test-tool sha256" as well?) on top of it would work well.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+ if (!check(!!data)) {

Is this double negation needed? Can't we just if (!check(data))?

As far as I remember it is needed as check() is expecting an 'int' while 'data' is a 'void *'.

It might be easier to read by being more explicit, "data != NULL", if that is the case? check() is like assert(), i.e., "we expect data is not NULL", and if (!check("expected condition")) { guards an error handling block for the case in which the expectation is not met, right?

#18 lore raw

Re: [PATCH v3 2/3] t/: port helper/test-sha1.c to unit-tests/t-hash.c

Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Christian Couder
Quoted 3 lines

Can we refactor this test to stop doing that? E.g., would it work if we used git-hash-object(1) to check that SHA1DC does its thing? Then we could get rid of the helper altogether, as far as I understand.

Quoted 8 lines
Quoted 3 lines

It could perhaps work if we used git-hash-object(1) instead of test-tool sha1 in t0013-sha1dc to check that SHA1DC does its thing, but we could do that in a separate patch or patch series.

Yeah, I think such a plan to make preliminary refactoring as a separate series, and then have another series to get rid of "test-tool sha1" (and "test-tool sha256" as well?) on top of it would work well.

It seems that git-hash-object does not die (or give an error) when providing t0013/shattered-1.pdf, and gives a different hash than the one explicitly mentioned t0013-sha1dc.sh. I suppose it is silently replacing the hash when it detects the collision. Is this an expected behaviour?

Thanks.

#19 lore raw

Re: [PATCH v3 2/3] t/: port helper/test-sha1.c to unit-tests/t-hash.c

Quoted 16 lines · Ghanshyam Thakkar
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Christian Couder
Quoted 3 lines

Can we refactor this test to stop doing that? E.g., would it work if we used git-hash-object(1) to check that SHA1DC does its thing? Then we could get rid of the helper altogether, as far as I understand.

Quoted 8 lines
Quoted 3 lines

It could perhaps work if we used git-hash-object(1) instead of test-tool sha1 in t0013-sha1dc to check that SHA1DC does its thing, but we could do that in a separate patch or patch series.

Yeah, I think such a plan to make preliminary refactoring as a separate series, and then have another series to get rid of "test-tool sha1" (and "test-tool sha256" as well?) on top of it would work well.

Quoted 5 lines

It seems that git-hash-object does not die (or give an error) when providing t0013/shattered-1.pdf, and gives a different hash than the one explicitly mentioned t0013-sha1dc.sh. I suppose it is silently replacing the hash when it detects the collision. Is this an expected behaviour?

The shattered files do not create a collision (nor trigger the detection in sha1dc) when hashed as Git objects. The reason is that Git objects are not a straight hash of the contents, but have the object type and size prepended. One _could_ use the same techniques that created the shattered files to create a colliding set of Git objects, but AFAIK nobody has done so (and it probably costs tens of thousands of USD, though perhaps getting cheaper every year).

So no, git-hash-object can't be used to test this. You have to directly hash some contents with sha1, and I don't think there is any way to do that with regular Git commands. Anything working with objects will use the type+size format. We also use sha1 for the csum-file.[ch] mechanism, where it is a straight hash of the contents (and we use this for packfiles, etc). But there's not an easy way to feed an arbitrary file to that system.

It's possible there might be a way to abuse hashfd_check() to feed an arbitrary file. E.g., stick shattered-1.pdf into a .pack file or something, then ask "index-pack --verify" to check it. But I don't think even that works, because before we even get to the final checksum, we're verifying the actual contents as we go.

So I think we need to keep some mechanism for computing the sha1 of arbitrary contents.

-Peff

#20 lore raw

Re: [PATCH v3 2/3] t/: port helper/test-sha1.c to unit-tests/t-hash.c

Quoted 6 lines · Jeff King

So no, git-hash-object can't be used to test this. You have to directly hash some contents with sha1, and I don't think there is any way to do that with regular Git commands.

So I think we need to keep some mechanism for computing the sha1 of arbitrary contents.

You're right. We'd need a separate test helper if we wanted to keep using the shattered sample files as-is (which we do).

Thanks.

#21 lore raw

Re: [PATCH v3 2/3] t/: port helper/test-sha1.c to unit-tests/t-hash.c

In reply to Jeff King
Quoted 40 lines · Jeff King
Quoted 16 lines · Ghanshyam Thakkar
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Christian Couder
Quoted 3 lines

Can we refactor this test to stop doing that? E.g., would it work if we used git-hash-object(1) to check that SHA1DC does its thing? Then we could get rid of the helper altogether, as far as I understand.

Quoted 8 lines
Quoted 3 lines

It could perhaps work if we used git-hash-object(1) instead of test-tool sha1 in t0013-sha1dc to check that SHA1DC does its thing, but we could do that in a separate patch or patch series.

Yeah, I think such a plan to make preliminary refactoring as a separate series, and then have another series to get rid of "test-tool sha1" (and "test-tool sha256" as well?) on top of it would work well.

Quoted 5 lines

It seems that git-hash-object does not die (or give an error) when providing t0013/shattered-1.pdf, and gives a different hash than the one explicitly mentioned t0013-sha1dc.sh. I suppose it is silently replacing the hash when it detects the collision. Is this an expected behaviour?

The shattered files do not create a collision (nor trigger the detection in sha1dc) when hashed as Git objects. The reason is that Git objects are not a straight hash of the contents, but have the object type and size prepended. One _could_ use the same techniques that created the shattered files to create a colliding set of Git objects, but AFAIK nobody has done so (and it probably costs tens of thousands of USD, though perhaps getting cheaper every year).

So no, git-hash-object can't be used to test this. You have to directly hash some contents with sha1, and I don't think there is any way to do that with regular Git commands. Anything working with objects will use the type+size format. We also use sha1 for the csum-file.[ch] mechanism, where it is a straight hash of the contents (and we use this for packfiles, etc). But there's not an easy way to feed an arbitrary file to that system.

Quoted 8 lines

It's possible there might be a way to abuse hashfd_check() to feed an arbitrary file. E.g., stick shattered-1.pdf into a .pack file or something, then ask "index-pack --verify" to check it. But I don't think even that works, because before we even get to the final checksum, we're verifying the actual contents as we go.

So I think we need to keep some mechanism for computing the sha1 of arbitrary contents.

Thank you for the detailed explanation. Then I suppose we should keep these helpers (test-{sha1, sha256, hash}) as it is.

#22 lore raw

PATCH v3 3/3 t/: port helper/test-sha256.c to unit-tests/t-hash.c

In reply to Ghanshyam Thakkar

t/helper/test-sha256 and t/t0015-hash test the hash implementation of SHA-256 in Git with basic SHA-256 hash values. Port them to the new unit testing framework for better debugging, simplicity and faster runtime. The necessary building blocks are already implemented in t-hash in the previous commit which ported test-sha1.

The 'sha256' subcommand of test-tool is still not removed, because it is used by pack_trailer() in lib-pack.sh, which is used in many tests of the t53** series.

---
 t/t0015-hash.sh       | 34 ----------------------------------
 t/unit-tests/t-hash.c | 25 +++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 34 deletions(-)
 delete mode 100755 t/t0015-hash.sh
t/t0015-hash.sh deleted +0 -34
deleted file mode 100755
index 2264e702d5..0000000000
@@ -1,34 +0,0 @@
-#!/bin/sh
-
-test_description='test basic hash implementation'
-
-TEST_PASSES_SANITIZE_LEAK=true
-. ./test-lib.sh
-
-test_expect_success 'test basic SHA-256 hash values' '
-	test-tool sha256 </dev/null >actual &&
-	grep e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 actual &&
-	printf "a" | test-tool sha256 >actual &&
-	grep ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb actual &&
-	printf "abc" | test-tool sha256 >actual &&
-	grep ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad actual &&
-	printf "message digest" | test-tool sha256 >actual &&
-	grep f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650 actual &&
-	printf "abcdefghijklmnopqrstuvwxyz" | test-tool sha256 >actual &&
-	grep 71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73 actual &&
-	# Try to exercise the chunking code by turning autoflush on.
-	perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" |
-		test-tool sha256 >actual &&
-	grep cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0 actual &&
-	perl -e "$| = 1; print q{abcdefghijklmnopqrstuvwxyz} for 1..100000;" |
-		test-tool sha256 >actual &&
-	grep e406ba321ca712ad35a698bf0af8d61fc4dc40eca6bdcea4697962724ccbde35 actual &&
-	printf "blob 0\0" | test-tool sha256 >actual &&
-	grep 473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813 actual &&
-	printf "blob 3\0abc" | test-tool sha256 >actual &&
-	grep c1cf6e465077930e88dc5136641d402f72a229ddd996f627d60e9639eaba35a6 actual &&
-	printf "tree 0\0" | test-tool sha256 >actual &&
-	grep 6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321 actual
-'
-
-test_done
t/unit-tests/t-hash.c +25 -0
index 89dfea9cc1..0f86cd3730 100644
@@ -32,11 +32,24 @@ static void check_hash_data(const void *data, size_t data_length,
 	TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA1), \
 	     "SHA1 (%s) works", #literal)
 
+
+/* Works with a NUL terminated string. Doesn't work if it should contain a NUL  character. */
+#define TEST_SHA256_STR(data, expected) \
+	TEST(check_hash_data(data, strlen(data), expected, GIT_HASH_SHA256), \
+	     "SHA256 (%s) works", #data)
+
+/* Only works with a literal string, useful when it contains a NUL character. */
+#define TEST_SHA256_LITERAL(literal, expected) \
+	TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA256), \
+	     "SHA256 (%s) works", #literal)
+
 int cmd_main(int argc, const char **argv)
 {
 	struct strbuf aaaaaaaaaa_100000 = STRBUF_INIT;
+	struct strbuf alphabet_100000 = STRBUF_INIT;
 
 	strbuf_addstrings(&aaaaaaaaaa_100000, "aaaaaaaaaa", 100000);
+	strbuf_addstrings(&alphabet_100000, "abcdefghijklmnopqrstuvwxyz", 100000);
 
 	TEST_SHA1_STR("", "da39a3ee5e6b4b0d3255bfef95601890afd80709");
 	TEST_SHA1_STR("a", "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8");
@@ -48,7 +61,19 @@ int cmd_main(int argc, const char **argv)
 	TEST_SHA1_LITERAL("blob 3\0abc", "f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f");
 	TEST_SHA1_LITERAL("tree 0\0", "4b825dc642cb6eb9a060e54bf8d69288fbee4904");
 
+	TEST_SHA256_STR("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+	TEST_SHA256_STR("a", "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb");
+	TEST_SHA256_STR("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
+	TEST_SHA256_STR("message digest", "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650");
+	TEST_SHA256_STR("abcdefghijklmnopqrstuvwxyz", "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73");
+	TEST_SHA256_STR(aaaaaaaaaa_100000.buf, "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
+	TEST_SHA256_STR(alphabet_100000.buf, "e406ba321ca712ad35a698bf0af8d61fc4dc40eca6bdcea4697962724ccbde35");
+	TEST_SHA256_LITERAL("blob 0\0", "473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813");
+	TEST_SHA256_LITERAL("blob 3\0abc", "c1cf6e465077930e88dc5136641d402f72a229ddd996f627d60e9639eaba35a6");
+	TEST_SHA256_LITERAL("tree 0\0", "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321");
+
 	strbuf_release(&aaaaaaaaaa_100000);
+	strbuf_release(&alphabet_100000);
 
 	return test_done();
 }
Signature · 1 line
2.45.1
#23 lore raw

Re: [PATCH v3 3/3] t/: port helper/test-sha256.c to unit-tests/t-hash.c

Quoted 9 lines · Ghanshyam Thakkar

t/helper/test-sha256 and t/t0015-hash test the hash implementation of SHA-256 in Git with basic SHA-256 hash values. Port them to the new unit testing framework for better debugging, simplicity and faster runtime. The necessary building blocks are already implemented in t-hash in the previous commit which ported test-sha1.

The 'sha256' subcommand of test-tool is still not removed, because it is used by pack_trailer() in lib-pack.sh, which is used in many tests of the t53** series.

Similar question here, are there replacements we can use for it? I also couldn't see it being used in any test other than t0015 when searing for "test-tool sha256". Maybe I'm looking for the wrong thing?

[snip]

Quoted 10 lines
t/t0015-hash.sh deleted +0 -1
-test_done
t/unit-tests/t-hash.c +1 -0
index 89dfea9cc1..0f86cd3730 100644
@@ -32,11 +32,24 @@ static void check_hash_data(const void *data, size_t data_length,
 	TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA1), \
 	     "SHA1 (%s) works", #literal)
 
+

t/unit-tests/t-hash.c static void check_hash_data(const void *data, size_t data_length, · line 45

Quoted 10 lines
t/unit-tests/t-hash.c +10 -0
+/* Works with a NUL terminated string. Doesn't work if it should contain a NUL  character. */
+#define TEST_SHA256_STR(data, expected) \
+	TEST(check_hash_data(data, strlen(data), expected, GIT_HASH_SHA256), \
+	     "SHA256 (%s) works", #data)
+
+/* Only works with a literal string, useful when it contains a NUL character. */
+#define TEST_SHA256_LITERAL(literal, expected) \
+	TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA256), \
+	     "SHA256 (%s) works", #literal)
+

Same question here regarding the macros and whether we can merge them.

Also, we do have the same test data for both hashes, and if we ever grow another hash it's likely that we'll also want to check for the same inputs there. Would it make sense to have a generic TAST_HASHES() macro where you give the input and then both the expected SHA1 and SHA256 to avoid some duplication?

Patrick

#24 lore raw

Re: [PATCH v3 3/3] t/: port helper/test-sha256.c to unit-tests/t-hash.c

Quoted 7 lines · Patrick Steinhardt
Quoted 6 lines · Ghanshyam Thakkar

t/helper/test-sha256 and t/t0015-hash test the hash implementation of SHA-256 in Git with basic SHA-256 hash values. Port them to the new unit testing framework for better debugging, simplicity and faster runtime. The necessary building blocks are already implemented in t-hash in the previous commit which ported test-sha1.

Quoted 7 lines
Quoted 3 lines

The 'sha256' subcommand of test-tool is still not removed, because it is used by pack_trailer() in lib-pack.sh, which is used in many tests of the t53** series.

Similar question here, are there replacements we can use for it? I also couldn't see it being used in any test other than t0015 when searing for "test-tool sha256". Maybe I'm looking for the wrong thing?

It is used indirectly and not explicitly like 'test-tool sha256'. e.g. in t/lib-pack.sh when GIT_TEST_DEFUALT_HASH=sha256, ...

# Compute and append pack trailer to "$1"

  pack_trailer () {
	  test-tool $(test_oid algo) -b <"$1" >trailer.tmp &&
	  cat trailer.tmp >>"$1" &&
	  rm -f trailer.tmp

}

...it will use 'test-tool sha256' on the first line of pack_trailer(). And the pack_trailer() is used in t7450, t5308, t5309, t5321.

And I will consult with Christian and Kaartic on the replacements but as Christian and Junio said, doing it in another series would be a good idea.

Quoted 24 lines

[snip]

Quoted 10 lines
t/t0015-hash.sh deleted +0 -1
-test_done
t/unit-tests/t-hash.c +1 -0
index 89dfea9cc1..0f86cd3730 100644
@@ -32,11 +32,24 @@ static void check_hash_data(const void *data, size_t data_length,
 	TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA1), \
 	     "SHA1 (%s) works", #literal)
 
+

t/unit-tests/t-hash.c static void check_hash_data(const void *data, size_t data_length, · line 45

Quoted 10 lines
t/unit-tests/t-hash.c +10 -0
+/* Works with a NUL terminated string. Doesn't work if it should contain a NUL  character. */
+#define TEST_SHA256_STR(data, expected) \
+	TEST(check_hash_data(data, strlen(data), expected, GIT_HASH_SHA256), \
+	     "SHA256 (%s) works", #data)
+
+/* Only works with a literal string, useful when it contains a NUL character. */
+#define TEST_SHA256_LITERAL(literal, expected) \
+	TEST(check_hash_data(literal, (sizeof(literal) - 1), expected, GIT_HASH_SHA256), \
+	     "SHA256 (%s) works", #literal)
+

Same question here regarding the macros and whether we can merge them.

Quoted 5 lines

Also, we do have the same test data for both hashes, and if we ever grow another hash it's likely that we'll also want to check for the same inputs there. Would it make sense to have a generic TAST_HASHES() macro where you give the input and then both the expected SHA1 and SHA256 to avoid some duplication?

Yeah, that can be done as the inputs are same for both hashes minus one extra for sha256 (though I think it can be easily obtained). It looks like a good idea to me for v4.

Thank you for the review!

#25 lore raw

v4 cover t/: port helper/test-{sha1, sha256} to unit-tests/t-hash

In reply to Ghanshyam Thakkar

Changes in v4:
- introduce new generic macros TEST_HASH_{STR, LITERAL} which accept both expected SHA-1 and expected SHA-256 for the same input, according to Patrick's feedback. A partial refactoring of check_hash_data() was done for this.
- As a consequence to above, add a missing SHA-1 value for 'abc...xyz' 100000 times, which was present for SHA-256
- improve the BUG statement

CI: https://github.com/spectre10/git/actions/runs/9241430375

Ghanshyam Thakkar (2):
strbuf: introduce strbuf_addstrings() to repeatedly add a string t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

 Makefile                    |  1 +
 builtin/submodule--helper.c |  4 +-
 json-writer.c               |  5 +--
 strbuf.c                    |  9 ++++
 strbuf.h                    |  5 +++
 t/t0015-hash.sh             | 56 ------------------------
 t/unit-tests/t-hash.c       | 86 +++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 63 deletions(-)
 delete mode 100755 t/t0015-hash.sh
 create mode 100644 t/unit-tests/t-hash.c

Range-diff against v3:

1:  cd831fabf5 = 1:  cd831fabf5 strbuf: introduce strbuf_addstrings() to repeatedly add a string
2:  2c4471ce37 < -:  ---------- t/: port helper/test-sha1.c to unit-tests/t-hash.c
3:  68dcf388d8 < -:  ---------- t/: port helper/test-sha256.c to unit-tests/t-hash.c
-:  ---------- > 2:  ea7e171014 t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash
Signature · 1 line
2.45.1
#26 lore raw

PATCH v4 1/2 strbuf: introduce strbuf_addstrings() to repeatedly add a string

In a following commit we are going to port code from "t/helper/test-sha256.c", t/helper/test-hash.c and "t/t0015-hash.sh" to a new "t/unit-tests/t-hash.c" file using the recently added unit test framework.

To port code like: perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" we are going to need a new strbuf_addstrings() function that repeatedly adds the same string a number of times to a buffer.

Such a strbuf_addstrings() function would already be useful in "json-writer.c" and "builtin/submodule-helper.c" as both of these files already have code that repeatedly adds the same string. So let's introduce such a strbuf_addstrings() function in "strbuf.{c,h}" and use it in both "json-writer.c" and "builtin/submodule-helper.c".

We use the "strbuf_addstrings" name as this way strbuf_addstr() and strbuf_addstrings() would be similar for strings as strbuf_addch() and strbuf_addchars() for characters.

---
 builtin/submodule--helper.c | 4 +---
 json-writer.c               | 5 +----
 strbuf.c                    | 9 +++++++++
 strbuf.h                    | 5 +++++
 4 files changed, 16 insertions(+), 7 deletions(-)
builtin/submodule--helper.c +1 -3
index e604cb5ddb..5620a5bf67 100644
@@ -256,11 +256,9 @@ static void module_list_active(struct module_list *list)
 
 static char *get_up_path(const char *path)
 {
-	int i;
 	struct strbuf sb = STRBUF_INIT;
 
-	for (i = count_slashes(path); i; i--)
-		strbuf_addstr(&sb, "../");
+	strbuf_addstrings(&sb, "../", count_slashes(path));
 
 	/*
 	 * Check if 'path' ends with slash or not
json-writer.c +1 -4
index 005c820aa4..25b9201f9c 100644
@@ -46,10 +46,7 @@ static void append_quoted_string(struct strbuf *out, const char *in)
 
 static void indent_pretty(struct json_writer *jw)
 {
-	int k;
-
-	for (k = 0; k < jw->open_stack.len; k++)
-		strbuf_addstr(&jw->json, "  ");
+	strbuf_addstrings(&jw->json, "  ", jw->open_stack.len);
 }
 
 /*
strbuf.c +9 -0
index 0d929e4e19..e3ca9b1ee9 100644
@@ -313,6 +313,15 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 	strbuf_setlen(sb, sb->len + len);
 }
 
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n)
+{
+	size_t len = strlen(s);
+
+	strbuf_grow(sb, st_mult(len, n));
+	for (size_t i = 0; i < n; i++)
+		strbuf_add(sb, s, len);
+}
+
 void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
 {
 	strbuf_grow(sb, sb2->len);
strbuf.h +5 -0
index 97fa4a3d01..003f880ff7 100644
@@ -310,6 +310,11 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
 	strbuf_add(sb, s, strlen(s));
 }
 
+/**
+ * Add a NUL-terminated string the specified number of times to the buffer.
+ */
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n);
+
 /**
  * Copy the contents of another buffer at the end of the current one.
  */
Signature · 1 line
2.45.1
#27 lore raw

PATCH v4 2/2 t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

In reply to Ghanshyam Thakkar

t/helper/test-{sha1, sha256} and t/t0015-hash.sh test the hash implementation of SHA-1 and SHA-256 in Git with basic hash values. Migrate them to the new unit testing framework for better debugging and runtime performance.

The 'sha1' and 'sha256' subcommands are still not removed due to pack_trailer():lib-pack.sh's reliance on them. The 'sha1' subcommand is also relied upon by t0013-sha1dc (which requires 'test-tool sha1' dying when it is used on a file created to contain the known sha1 attack).

---
 Makefile              |  1 +
 t/t0015-hash.sh       | 56 ----------------------------
 t/unit-tests/t-hash.c | 86 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 56 deletions(-)
 delete mode 100755 t/t0015-hash.sh
 create mode 100644 t/unit-tests/t-hash.c
Makefile +1 -0
index 8f4432ae57..2b19fdf6ae 100644
@@ -1335,6 +1335,7 @@ THIRD_PARTY_SOURCES += sha1collisiondetection/%
 THIRD_PARTY_SOURCES += sha1dc/%
 
 UNIT_TEST_PROGRAMS += t-ctype
+UNIT_TEST_PROGRAMS += t-hash
 UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-prio-queue
 UNIT_TEST_PROGRAMS += t-strbuf
t/t0015-hash.sh deleted +0 -56

56 lines not on this page · raw message

t/unit-tests/t-hash.c new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
2.45.1
#28 lore raw

Re: [PATCH v4 2/2] t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

Quoted 10 lines · Ghanshyam Thakkar
t/t0015-hash.sh deleted +0 -1
-test_done
t/unit-tests/t-hash.c new file +3 -0
new file mode 100644
index 0000000000..bf3e0e9e94
@@ -0,0 +1,86 @@
+#include "test-lib.h"
+#include "hex.h"
+#include "strbuf.h"

t/unit-tests/t-hash.c line 13

Quoted 10 lines
t/unit-tests/t-hash.c new file +10 -0
+
+static void check_hash_data(const void *data, size_t data_length,
+			    const char *expected_hashes[])
+{
+	if (!check(data != NULL)) {
+		test_msg("BUG: NULL data pointer provided");
+		return;
+	}
+
+	for (int i = 1; i < ARRAY_SIZE(hash_algos); i++) {

s/int/size_t/

Quoted 6 lines
t/unit-tests/t-hash.c new file +6 -0
+		git_hash_ctx ctx;
+		unsigned char hash[GIT_MAX_HEXSZ];
+		const struct git_hash_algo *algop = &hash_algos[i];
+
+		algop->init_fn(&ctx);
+		algop->update_fn(&ctx, data, data_length);

t/unit-tests/t-hash.c line 29

Quoted 10 lines
t/unit-tests/t-hash.c new file +10 -0
+		algop->final_fn(hash, &ctx);
+
+		if (!check_str(hash_to_hex_algop(hash, algop), expected_hashes[i - 1]))
+			test_msg("result does not match with the expected for %s\n", hash_algos[i].name);
+	}
+}
+
+/* Works with a NUL terminated string. Doesn't work if it should contain a NUL character. */
+#define TEST_HASH_STR(data, expected_sha1, expected_sha256) \
+	{ \

These macros should like start with do {. The reason why we do this is that the compiler will complain if there is no semicolon after the macro.

Quoted 14 lines
t/unit-tests/t-hash.c new file +14 -0
+		const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \
+		TEST(check_hash_data(data, strlen(data), expected_hashes), \
+		     "SHA1 and SHA256 (%s) works", #data); \
+	}
+
+/* Only works with a literal string, useful when it contains a NUL character. */
+#define TEST_HASH_LITERAL(literal, expected_sha1, expected_sha256) \
+	{ \
+		const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \
+		TEST(check_hash_data(literal, (sizeof(literal) - 1), expected_hashes), \
+		     "SHA1 and SHA256 (%s) works", #literal); \
+	}
+
+int cmd_main(int argc, const char **argv)

t/unit-tests/t-hash.c line 53

Quoted 10 lines
t/unit-tests/t-hash.c new file +10 -0
+{
+	struct strbuf aaaaaaaaaa_100000 = STRBUF_INIT;
+	struct strbuf alphabet_100000 = STRBUF_INIT;
+
+	strbuf_addstrings(&aaaaaaaaaa_100000, "aaaaaaaaaa", 100000);
+	strbuf_addstrings(&alphabet_100000, "abcdefghijklmnopqrstuvwxyz", 100000);
+
+	TEST_HASH_STR(
+		"", "da39a3ee5e6b4b0d3255bfef95601890afd80709",
+		"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

I think these might've been a bit easier to read if they formatted like this:

	TEST_HASH_STR("",
	    "da39a3ee5e6b4b0d3255bfef95601890afd80709",
	    "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

	TEST_HASH_STR("a",
	    "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8",
	    "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb");

It makes it a bit easier to separate the test data from the gibberish that those hashes are.

Other than that these patches look good to me.

Patrick

#29 lore raw

Re: [PATCH v4 2/2] t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

Quoted 3 lines · Patrick Steinhardt

t/unit-tests/t-hash.c line 13

Quoted 1 line
t/unit-tests/t-hash.c new file +1 -0
+	for (int i = 1; i < ARRAY_SIZE(hash_algos); i++) {

s/int/size_t/

If ARRAY_SIZE(hash_algos) is an unbounded quantity that is externally controlled, this does make very much sense, but for hash_algos[]? It is not worth the patch noise to go and fix it.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+#define TEST_HASH_STR(data, expected_sha1, expected_sha256) \
+	{ \

These macros should like start with do {. The reason why we do this is that the compiler will complain if there is no semicolon after the macro.

The idiom is

	#define foo(a,b,c) do { \
		...; \
	} while (0)

so that you can write

	foo(1,2,3);

as if it is a regular function call terminated _with_ a semicolon. Also this allows us to say

	if (condition)
		foo(1,2,3);
	else
		foo(4,5,6);

which would break if the definition were a mere

	#define foo(x,y,z) { \
		... \
	}

in which case ";" after the first foo() terminates the "if" statement and "else" triggers a syntax error.

Quoted 7 lines

t/unit-tests/t-hash.c line 53

Quoted 3 lines
t/unit-tests/t-hash.c new file +3 -0
+	TEST_HASH_STR(
+		"", "da39a3ee5e6b4b0d3255bfef95601890afd80709",
+		"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

I think these might've been a bit easier to read if they formatted like this:

Quoted 7 lines
	TEST_HASH_STR("",
	    "da39a3ee5e6b4b0d3255bfef95601890afd80709",
	    "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

	TEST_HASH_STR("a",
	    "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8",
	    "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb");

Oh, absolutely.

#30 lore raw

v5 cover t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

In reply to Ghanshyam Thakkar

Changes in v5:
- addressd Patrick's feedback about formatting
- changed macros to be in do while loop instead of simple block, according to Patrick's feedback

Ghanshyam Thakkar (2):
strbuf: introduce strbuf_addstrings() to repeatedly add a string t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

 Makefile                    |  1 +
 builtin/submodule--helper.c |  4 +-
 json-writer.c               |  5 +--
 strbuf.c                    |  9 ++++
 strbuf.h                    |  5 +++
 t/t0015-hash.sh             | 56 ------------------------
 t/unit-tests/t-hash.c       | 86 +++++++++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 63 deletions(-)
 delete mode 100755 t/t0015-hash.sh
 create mode 100644 t/unit-tests/t-hash.c

Range-diff against v4:

1:  cd831fabf5 = 1:  cd831fabf5 strbuf: introduce strbuf_addstrings() to repeatedly add a string
2:  6ce8f57af1 ! 2:  c82ead8e48 t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash
    @@ t/unit-tests/t-hash.c (new)
     +		return;
     +	}
     +
    -+	for (int i = 1; i < ARRAY_SIZE(hash_algos); i++) {
    ++	for (size_t i = 1; i < ARRAY_SIZE(hash_algos); i++) {
     +		git_hash_ctx ctx;
     +		unsigned char hash[GIT_MAX_HEXSZ];
     +		const struct git_hash_algo *algop = &hash_algos[i];
    @@ t/unit-tests/t-hash.c (new)
     +
     +/* Works with a NUL terminated string. Doesn't work if it should contain a NUL character. */
     +#define TEST_HASH_STR(data, expected_sha1, expected_sha256) \
    -+	{ \
    ++	do { \
     +		const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \
     +		TEST(check_hash_data(data, strlen(data), expected_hashes), \
     +		     "SHA1 and SHA256 (%s) works", #data); \
    -+	}
    ++	} while (0)
     +
     +/* Only works with a literal string, useful when it contains a NUL character. */
     +#define TEST_HASH_LITERAL(literal, expected_sha1, expected_sha256) \
    -+	{ \
    ++	do { \
     +		const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \
     +		TEST(check_hash_data(literal, (sizeof(literal) - 1), expected_hashes), \
     +		     "SHA1 and SHA256 (%s) works", #literal); \
    -+	}
    ++	} while (0)
     +
     +int cmd_main(int argc, const char **argv)
     +{
    @@ t/unit-tests/t-hash.c (new)
     +	strbuf_addstrings(&aaaaaaaaaa_100000, "aaaaaaaaaa", 100000);
     +	strbuf_addstrings(&alphabet_100000, "abcdefghijklmnopqrstuvwxyz", 100000);
     +
    -+	TEST_HASH_STR(
    -+		"", "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    ++	TEST_HASH_STR("",
    ++		"da39a3ee5e6b4b0d3255bfef95601890afd80709",
     +		"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
    -+	TEST_HASH_STR(
    -+		"a", "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8",
    ++	TEST_HASH_STR("a",
    ++		"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8",
     +		"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb");
    -+	TEST_HASH_STR(
    -+		"abc", "a9993e364706816aba3e25717850c26c9cd0d89d",
    ++	TEST_HASH_STR("abc",
    ++		"a9993e364706816aba3e25717850c26c9cd0d89d",
     +		"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
    -+	TEST_HASH_STR(
    -+		"message digest", "c12252ceda8be8994d5fa0290a47231c1d16aae3",
    ++	TEST_HASH_STR("message digest",
    ++		"c12252ceda8be8994d5fa0290a47231c1d16aae3",
     +		"f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650");
    -+	TEST_HASH_STR(
    -+		"abcdefghijklmnopqrstuvwxyz", "32d10c7b8cf96570ca04ce37f2a19d84240d3a89",
    ++	TEST_HASH_STR("abcdefghijklmnopqrstuvwxyz",
    ++		"32d10c7b8cf96570ca04ce37f2a19d84240d3a89",
     +		"71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73");
    -+	TEST_HASH_STR(
    -+		aaaaaaaaaa_100000.buf, "34aa973cd4c4daa4f61eeb2bdbad27316534016f",
    ++	TEST_HASH_STR(aaaaaaaaaa_100000.buf,
    ++		"34aa973cd4c4daa4f61eeb2bdbad27316534016f",
     +		"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
    -+	TEST_HASH_STR(
    -+		alphabet_100000.buf, "e7da7c55b3484fdf52aebec9cbe7b85a98f02fd4",
    ++	TEST_HASH_STR(alphabet_100000.buf,
    ++		"e7da7c55b3484fdf52aebec9cbe7b85a98f02fd4",
     +		"e406ba321ca712ad35a698bf0af8d61fc4dc40eca6bdcea4697962724ccbde35");
    -+	TEST_HASH_LITERAL(
    -+		"blob 0\0", "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
    ++	TEST_HASH_LITERAL("blob 0\0",
    ++		"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
     +		"473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813");
    -+	TEST_HASH_LITERAL(
    -+		"blob 3\0abc", "f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f",
    ++	TEST_HASH_LITERAL("blob 3\0abc",
    ++		"f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f",
     +		"c1cf6e465077930e88dc5136641d402f72a229ddd996f627d60e9639eaba35a6");
    -+	TEST_HASH_LITERAL(
    -+		"tree 0\0", "4b825dc642cb6eb9a060e54bf8d69288fbee4904",
    ++	TEST_HASH_LITERAL("tree 0\0",
    ++		"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
     +		"6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321");
     +
     +	strbuf_release(&aaaaaaaaaa_100000);
Signature · 1 line
2.45.1
#31 lore raw

PATCH v5 1/2 strbuf: introduce strbuf_addstrings() to repeatedly add a string

In a following commit we are going to port code from "t/helper/test-sha256.c", t/helper/test-hash.c and "t/t0015-hash.sh" to a new "t/unit-tests/t-hash.c" file using the recently added unit test framework.

To port code like: perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" we are going to need a new strbuf_addstrings() function that repeatedly adds the same string a number of times to a buffer.

Such a strbuf_addstrings() function would already be useful in "json-writer.c" and "builtin/submodule-helper.c" as both of these files already have code that repeatedly adds the same string. So let's introduce such a strbuf_addstrings() function in "strbuf.{c,h}" and use it in both "json-writer.c" and "builtin/submodule-helper.c".

We use the "strbuf_addstrings" name as this way strbuf_addstr() and strbuf_addstrings() would be similar for strings as strbuf_addch() and strbuf_addchars() for characters.

---
 builtin/submodule--helper.c | 4 +---
 json-writer.c               | 5 +----
 strbuf.c                    | 9 +++++++++
 strbuf.h                    | 5 +++++
 4 files changed, 16 insertions(+), 7 deletions(-)
builtin/submodule--helper.c +1 -3
index e604cb5ddb..5620a5bf67 100644
@@ -256,11 +256,9 @@ static void module_list_active(struct module_list *list)
 
 static char *get_up_path(const char *path)
 {
-	int i;
 	struct strbuf sb = STRBUF_INIT;
 
-	for (i = count_slashes(path); i; i--)
-		strbuf_addstr(&sb, "../");
+	strbuf_addstrings(&sb, "../", count_slashes(path));
 
 	/*
 	 * Check if 'path' ends with slash or not
json-writer.c +1 -4
index 005c820aa4..25b9201f9c 100644
@@ -46,10 +46,7 @@ static void append_quoted_string(struct strbuf *out, const char *in)
 
 static void indent_pretty(struct json_writer *jw)
 {
-	int k;
-
-	for (k = 0; k < jw->open_stack.len; k++)
-		strbuf_addstr(&jw->json, "  ");
+	strbuf_addstrings(&jw->json, "  ", jw->open_stack.len);
 }
 
 /*
strbuf.c +9 -0
index 0d929e4e19..e3ca9b1ee9 100644
@@ -313,6 +313,15 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 	strbuf_setlen(sb, sb->len + len);
 }
 
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n)
+{
+	size_t len = strlen(s);
+
+	strbuf_grow(sb, st_mult(len, n));
+	for (size_t i = 0; i < n; i++)
+		strbuf_add(sb, s, len);
+}
+
 void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
 {
 	strbuf_grow(sb, sb2->len);
strbuf.h +5 -0
index 97fa4a3d01..003f880ff7 100644
@@ -310,6 +310,11 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
 	strbuf_add(sb, s, strlen(s));
 }
 
+/**
+ * Add a NUL-terminated string the specified number of times to the buffer.
+ */
+void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n);
+
 /**
  * Copy the contents of another buffer at the end of the current one.
  */
Signature · 1 line
2.45.1
#32 lore raw

PATCH v5 2/2 t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

In reply to Ghanshyam Thakkar

t/helper/test-{sha1, sha256} and t/t0015-hash.sh test the hash implementation of SHA-1 and SHA-256 in Git with basic hash values. Migrate them to the new unit testing framework for better debugging and runtime performance.

The 'sha1' and 'sha256' subcommands are still not removed due to pack_trailer():lib-pack.sh's reliance on them. The 'sha1' subcommand is also relied upon by t0013-sha1dc (which requires 'test-tool sha1' dying when it is used on a file created to contain the known sha1 attack).

---
 Makefile              |  1 +
 t/t0015-hash.sh       | 56 ----------------------------
 t/unit-tests/t-hash.c | 86 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 56 deletions(-)
 delete mode 100755 t/t0015-hash.sh
 create mode 100644 t/unit-tests/t-hash.c
Makefile +1 -0
index 8f4432ae57..2b19fdf6ae 100644
@@ -1335,6 +1335,7 @@ THIRD_PARTY_SOURCES += sha1collisiondetection/%
 THIRD_PARTY_SOURCES += sha1dc/%
 
 UNIT_TEST_PROGRAMS += t-ctype
+UNIT_TEST_PROGRAMS += t-hash
 UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-prio-queue
 UNIT_TEST_PROGRAMS += t-strbuf
t/t0015-hash.sh deleted +0 -56

56 lines not on this page · raw message

t/unit-tests/t-hash.c new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
2.45.1
#33 lore raw

Re: [GSoC][PATCH v5 0/2] t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

In reply to Ghanshyam Thakkar
Quoted 8 lines · Ghanshyam Thakkar

Changes in v5:
- addressd Patrick's feedback about formatting
- changed macros to be in do while loop instead of simple block, according to Patrick's feedback

Ghanshyam Thakkar (2):
strbuf: introduce strbuf_addstrings() to repeatedly add a string t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

Thanks, this version looks good to me based on the range diff!

Patrick

#34 lore raw

Re: [GSoC][PATCH v5 0/2] t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

Quoted 11 lines · Patrick Steinhardt
Quoted 8 lines · Ghanshyam Thakkar

Changes in v5:
- addressd Patrick's feedback about formatting
- changed macros to be in do while loop instead of simple block, according to Patrick's feedback

Ghanshyam Thakkar (2):
strbuf: introduce strbuf_addstrings() to repeatedly add a string t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hash

Thanks, this version looks good to me based on the range diff!

Thanks, both.