Re: [PATCH 1/2] test/count: replace use of gdb with a LD_PRELOAD shim

2021-10-25 Thread Tomi Ollila
On Sun, Oct 24 2021, David Bremner wrote:

> There is a certain amount of boilerplate to pass the call on the
> original function, so abstract it out as a C preprocessor macro, plus
> some extra includes in notmuch-test.h

Looks like good progress -- some comments...

> ---
>  test/T060-count.sh  | 29 +
>  test/notmuch-test.h | 22 ++
>  2 files changed, 39 insertions(+), 12 deletions(-)
>
> diff --git a/test/T060-count.sh b/test/T060-count.sh
> index 6ad80df9..acf51d88 100755
> --- a/test/T060-count.sh
> +++ b/test/T060-count.sh
> @@ -102,22 +102,27 @@ output=$(sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' 
> OUTPUT)
>  test_expect_equal "${output}" "A Xapian exception occurred opening database"
>  restore_database
>  
> -cat < count-files.gdb
> -set breakpoint pending on
> -set logging file count-files-gdb.log
> -set logging on
> -break count_files
> -commands
> -shell cp /dev/null ${MAIL_DIR}/.notmuch/xapian/postlist.*
> -continue
> -end
> -run
> +make_shim qsm-shim< +#include 
> +notmuch_status_t
> +notmuch_query_search_messages (notmuch_query_t *query, notmuch_messages_t 
> **messages) {
> +  static notmuch_status_t (*orig_notmuch_query_search_messages)
> + (notmuch_query_t *query, notmuch_messages_t **messages) = NULL;
> +  TEST_SAVE_ORIG(notmuch_query_search_messages);

The C preprocessor macro could do both of the lines above, deduplicating
orig_notmuch_query_search_messages and notmuch_query_search_messages to
one, but perhaps this way is clearer...(?)

> +
> +  /* XXX WARNING THIS CORRUPTS THE DATABASE */
> +  int fd = open("target_postlist",O_WRONLY|O_TRUNC);
missing space after ,

> +  if (fd < 0)
> +exit (8);

close(fd) ?

> +
> +  return orig_notmuch_query_search_messages(query, messages);
> +}
>  EOF
>  
>  backup_database
>  test_begin_subtest "error message from query_search_messages"
> -${TEST_GDB} --batch-silent --return-child-result -x count-files.gdb \
> ---args notmuch count --output=files '*' 2>OUTPUT 1>/dev/null
> +ln -s ${MAIL_DIR}/.notmuch/xapian/postlist.* target_postlist
> +notmuch_with_shim qsm-shim count --output=files '*' 2>OUTPUT 1>/dev/null
>  cat < EXPECTED
>  notmuch count: A Xapian exception occurred
>  A Xapian exception occurred performing query
> diff --git a/test/notmuch-test.h b/test/notmuch-test.h
> index 34dbb8e0..8613a299 100644
> --- a/test/notmuch-test.h
> +++ b/test/notmuch-test.h
> @@ -3,6 +3,10 @@
>  #include 
>  #include 
>  #include 
> +#include 
> +#include 
> +#include 
> +#include 
>  
>  inline static void
>  expect0 (int line, notmuch_status_t ret)
> @@ -14,4 +18,22 @@ expect0 (int line, notmuch_status_t ret)
>  }
>  
>  #define EXPECT0(v)  expect0 (__LINE__, v);
> +
> +#define TEST_SAVE_ORIG(func) \
> +  if (! orig_##func) { \
> +void *handle; \
> +char *error; \
> +handle = dlopen("libnotmuch.so", RTLD_LAZY); \

could have void *handle = dlopen... to reduce one macro line

> +if (! handle) { \
> +  fputs(dlerror(), stderr); \
> +  exit(1); \
> +} \
> +orig_##func = dlsym(handle, #func); \
> +if ((error = dlerror()) != NULL) { \
> +  fprintf(stderr, "%s\n", error); \
> +  exit(1); \
> +} \
> +  }
> +
> +
>  #endif
> -- 
> 2.33.0
___
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-le...@notmuchmail.org


[PATCH 1/2] test/count: replace use of gdb with a LD_PRELOAD shim

2021-10-24 Thread David Bremner
There is a certain amount of boilerplate to pass the call on the
original function, so abstract it out as a C preprocessor macro, plus
some extra includes in notmuch-test.h
---
 test/T060-count.sh  | 29 +
 test/notmuch-test.h | 22 ++
 2 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/test/T060-count.sh b/test/T060-count.sh
index 6ad80df9..acf51d88 100755
--- a/test/T060-count.sh
+++ b/test/T060-count.sh
@@ -102,22 +102,27 @@ output=$(sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' 
OUTPUT)
 test_expect_equal "${output}" "A Xapian exception occurred opening database"
 restore_database
 
-cat < count-files.gdb
-set breakpoint pending on
-set logging file count-files-gdb.log
-set logging on
-break count_files
-commands
-shell cp /dev/null ${MAIL_DIR}/.notmuch/xapian/postlist.*
-continue
-end
-run
+make_shim qsm-shim<
+notmuch_status_t
+notmuch_query_search_messages (notmuch_query_t *query, notmuch_messages_t 
**messages) {
+  static notmuch_status_t (*orig_notmuch_query_search_messages)
+   (notmuch_query_t *query, notmuch_messages_t **messages) = NULL;
+  TEST_SAVE_ORIG(notmuch_query_search_messages);
+
+  /* XXX WARNING THIS CORRUPTS THE DATABASE */
+  int fd = open("target_postlist",O_WRONLY|O_TRUNC);
+  if (fd < 0)
+exit (8);
+
+  return orig_notmuch_query_search_messages(query, messages);
+}
 EOF
 
 backup_database
 test_begin_subtest "error message from query_search_messages"
-${TEST_GDB} --batch-silent --return-child-result -x count-files.gdb \
---args notmuch count --output=files '*' 2>OUTPUT 1>/dev/null
+ln -s ${MAIL_DIR}/.notmuch/xapian/postlist.* target_postlist
+notmuch_with_shim qsm-shim count --output=files '*' 2>OUTPUT 1>/dev/null
 cat < EXPECTED
 notmuch count: A Xapian exception occurred
 A Xapian exception occurred performing query
diff --git a/test/notmuch-test.h b/test/notmuch-test.h
index 34dbb8e0..8613a299 100644
--- a/test/notmuch-test.h
+++ b/test/notmuch-test.h
@@ -3,6 +3,10 @@
 #include 
 #include 
 #include 
+#include 
+#include 
+#include 
+#include 
 
 inline static void
 expect0 (int line, notmuch_status_t ret)
@@ -14,4 +18,22 @@ expect0 (int line, notmuch_status_t ret)
 }
 
 #define EXPECT0(v)  expect0 (__LINE__, v);
+
+#define TEST_SAVE_ORIG(func) \
+  if (! orig_##func) { \
+void *handle; \
+char *error; \
+handle = dlopen("libnotmuch.so", RTLD_LAZY); \
+if (! handle) { \
+  fputs(dlerror(), stderr); \
+  exit(1); \
+} \
+orig_##func = dlsym(handle, #func); \
+if ((error = dlerror()) != NULL) { \
+  fprintf(stderr, "%s\n", error); \
+  exit(1); \
+} \
+  }
+
+
 #endif
-- 
2.33.0

___
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-le...@notmuchmail.org