[build-path-prefix-map-spec] 09/50: Refactor to make it easier to add new test-cases

Ximin Luo infinity0 at debian.org
Fri Mar 10 15:17:18 UTC 2017


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository build-path-prefix-map-spec.

commit 63315c515b60a3335fdf05813759e7a6d6d503df
Author: Ximin Luo <infinity0 at debian.org>
Date:   Mon Jan 23 19:47:35 2017 +0100

    Refactor to make it easier to add new test-cases
---
 .gitignore                         |   3 +-
 consume/0.0-hexsplit.env           |   1 -
 consume/Makefile                   |  68 +++++++++++++++++++++++--------------
 consume/afl-in-hexsplit/0.0.in     | Bin 68 -> 0 bytes
 consume/prefix_map.h               |  25 ++++++++------
 consume/testcases/0.0.hexsplit.env |   1 +
 consume/{0.in => testcases/0.0.in} |   0
 consume/{ => testcases}/0.0.out    |   0
 consume/testcases/README           |   5 +++
 9 files changed, 65 insertions(+), 38 deletions(-)

diff --git a/.gitignore b/.gitignore
index 557eb48..054f20e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
-/consume/hexsplit
+/consume/*.out
+/consume/afl-in-*
 /consume/afl-out-*
 /produce/*.html
diff --git a/consume/0.0-hexsplit.env b/consume/0.0-hexsplit.env
deleted file mode 100644
index 263eceb..0000000
--- a/consume/0.0-hexsplit.env
+++ /dev/null
@@ -1 +0,0 @@
-/a/b%3dyyy=ERROR:/a=lol:/b=foo:/a/b%3dyyy=secreteh
\ No newline at end of file
diff --git a/consume/Makefile b/consume/Makefile
index a3ed39b..77cdf54 100644
--- a/consume/Makefile
+++ b/consume/Makefile
@@ -1,50 +1,68 @@
 TMPDIR = /run/shm/rb-prefix-map
+T = testcases/
 
-ALL = hexsplit
-ALLCHECK_hexsplit = hexsplit hexsplit.js hexsplit.py
+ALL_FORMATS = hexsplit
+ALL_SOURCE = hexsplit.c
+ALLCHECK_hexsplit = hexsplit.c.out hexsplit.js hexsplit.py
+
+TESTCASES_0 = $(patsubst $(T)0.%.in,0.%,$(wildcard $(T)0.*.in))
+TESTCASES_1 = $(patsubst $(T)1.%.in,1.%,$(wildcard $(T)1.*.in))
 
 .PHONY: all
-all: $(ALL)
+all: $(ALL_SOURCE:%=%.out)
 
-%: %.c prefix_map.h
+%.c.out: %.c prefix_map.h
 	$(CC) -Wall -o "$@" "$<"
 
-check-apply-generic = \
-	set -ex; for i in $(1); do \
-	  BUILD_PATH_PREFIX_MAP="$$(cat 0.$(2)-$(3).env)" \
-	  ./$$i $$(cat 0.in) | diff -ru - 0.$(2).out; \
-	done
+.PHONY: check
+check: $(ALL_FORMATS:%=check-%)
 
-check-apply-none = \
-	set -ex; for i in $(1); do \
-	  ./$$i $$(cat 0.in) | diff -ru - 0.in; \
+check-generic = \
+	set -x; for i in $(1); do \
+	  ./$$i $$(cat $(T)$(4).in) | diff -ru - "$(T)$(4).in" || exit 1; \
+	  tmpout=$(T)$(4).$(2).env.tmpout; \
+	  BUILD_PATH_PREFIX_MAP="$$(cat $(T)$(4).$(2).env)" \
+	  ./$$i $$(cat $(T)$(4).in) > "$$tmpout"; \
+	  test $$? = $(3) && diff -ru "$$tmpout" $(T)$(4).out && \
+	    rm -f "$$tmpout" || { rm -f "$$tmpout"; exit 1; }; \
 	done
 
-.PHONY: check
-check: check-apply-hexsplit
-
-.PHONY: check-apply-%
-check-apply-%:
+.PHONY: check-%
+check-%:
 	$(MAKE) $(ALLCHECK_$*)
-	$(call check-apply-none,$(ALLCHECK_$*))
-	$(call check-apply-generic,$(ALLCHECK_$*),0,$*)
+	$(foreach case,$(TESTCASES_0),$(call check-generic,$(ALLCHECK_$*),$*,0,$(case)))
+	$(foreach case,$(TESTCASES_1),$(call check-generic,$(ALLCHECK_$*),$*,1,$(case)))
+
+make-afl-test-case = \
+	cat testcases/$(3).$(1).env testcases/$(3).in > afl-in-$(1)/$(3).in
+
+afl-in-%: $(wildcard $(T)/*.in) $(wildcard $(T)/*.env)
+	mkdir -p "$@"
+	$(foreach case,$(TESTCASES_0),$(call make-afl-test-case,$*,0,$(case)))
+	$(foreach case,$(TESTCASES_1),$(call make-afl-test-case,$*,1,$(case)))
+	touch "$@"
 
 .PHONY: fuzz-%
-fuzz-%: %
+fuzz-%: %.out
+	$(MAKE) afl-in-$(basename $*)
 	@echo "$(CC)" | grep -i afl || \
 	echo >&2 "warning: you didn't set CC=afl-gcc, fuzzing might not work"
-	@set -e; if test -d "afl-out-$*"; then \
+	set -e; if test -d "afl-out-$*"; then \
 	echo >&2 "afl-out-$* exists, reusing. run 'make reset-fuzz-$* to delete it."; \
-	afl-fuzz -i - -o "afl-out-$*" -- "./$*" -; else \
+	afl-fuzz -i - -o "afl-out-$*" -- "./$<" -; else \
 	mkdir -p $(TMPDIR); \
 	ln -s "$$(mktemp -d -p $(TMPDIR))" "afl-out-$*"; \
-	afl-fuzz -i "afl-in-$(basename $*)" -o "afl-out-$*" -- "./$*" -; fi
+	afl-fuzz -i "afl-in-$(basename $*)" -o "afl-out-$*" -- "./$<" -; fi
 
 .PHONY: reset-fuzz-%
-reset-fuzz-%: %
+reset-fuzz-%:
 	rm -rf "$$(readlink -f "afl-out-$*")" && rm -rf "afl-out-$*"
 	rmdir -p "$(TMPDIR)" 2>/dev/null || true
 
 .PHONY: clean
 clean:
-	rm -f $(ALL)
+	rm -f $(ALL_SOURCE:%=%.out)
+	rm -rf $(ALL_FORMATS:%=afl-in-%)
+
+.PHONY: clean-all
+clean-all: clean $(ALL_SOURCE:%=reset-fuzz-%)
diff --git a/consume/afl-in-hexsplit/0.0.in b/consume/afl-in-hexsplit/0.0.in
deleted file mode 100644
index 8baf759..0000000
Binary files a/consume/afl-in-hexsplit/0.0.in and /dev/null differ
diff --git a/consume/prefix_map.h b/consume/prefix_map.h
index 179e285..46fac3a 100644
--- a/consume/prefix_map.h
+++ b/consume/prefix_map.h
@@ -123,20 +123,23 @@ generic_main (int (*parse_prefix_maps) (const char *, struct prefix_maps *), int
   struct prefix_maps build_path_prefix_map = { NULL, 0 };
 
   int using_stdin = 0; // 0 = BUILD_PATH_PREFIX_MAP envvar, 1 = stdin (for afl)
-  char *mapstr = NULL;
+  char *str = NULL;
+  ssize_t read;
+  size_t len_allocated = 0;
+
   if (argc > 1 && strncmp (argv[1], "-", 1) == 0)
     {
-      size_t len = 0;
-      getdelim (&mapstr, &len, 0, stdin);
+      read = getline (&str, &len_allocated, stdin);
+      *(str + read - 1) = 0;
       if (ferror (stdin))
 	goto err_stdin;
       using_stdin = 1;
     }
   else
-    mapstr = getenv ("BUILD_PATH_PREFIX_MAP");
+    str = getenv ("BUILD_PATH_PREFIX_MAP");
 
-  if (mapstr)
-    if (!parse_prefix_maps (mapstr, &build_path_prefix_map))
+  if (str)
+    if (!parse_prefix_maps (str, &build_path_prefix_map))
       {
 	fprintf (stderr, "parse_prefix_maps failed\n");
 	return 1;
@@ -144,13 +147,13 @@ generic_main (int (*parse_prefix_maps) (const char *, struct prefix_maps *), int
 
   if (using_stdin)
     {
-      free (mapstr); // as per contract of getdelim()
+      free (str); // as per contract of getdelim()
+      str = NULL;
 
-      char *arg = NULL;
-      size_t len = 0;
-      while (getdelim (&arg, &len, 0, stdin) != -1)
+      while (-1 != (read = getline (&str, &len_allocated, stdin)))
 	{
-	  printf ("%s\n", remap_prefix (arg, &build_path_prefix_map));
+	  *(str + read - 1) = 0;
+	  printf ("%s\n", remap_prefix (str, &build_path_prefix_map));
 	}
 
       if (ferror (stdin))
diff --git a/consume/testcases/0.0.hexsplit.env b/consume/testcases/0.0.hexsplit.env
new file mode 100644
index 0000000..4ec0741
--- /dev/null
+++ b/consume/testcases/0.0.hexsplit.env
@@ -0,0 +1 @@
+/a/b%3dyyy=ERROR:/a=lol:/b=foo:/a/b%3dyyy=secreteh
diff --git a/consume/0.in b/consume/testcases/0.0.in
similarity index 100%
rename from consume/0.in
rename to consume/testcases/0.0.in
diff --git a/consume/0.0.out b/consume/testcases/0.0.out
similarity index 100%
rename from consume/0.0.out
rename to consume/testcases/0.0.out
diff --git a/consume/testcases/README b/consume/testcases/README
new file mode 100644
index 0000000..a5beac2
--- /dev/null
+++ b/consume/testcases/README
@@ -0,0 +1,5 @@
+Naming convention is like:
+
+<expected return code>.<unique number>.in
+<expected return code>.<unique number>.out
+<expected return code>.<unique number>.<format-name>.env

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/build-path-prefix-map-spec.git



More information about the Reproducible-commits mailing list