unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: Tomi Ollila <tomi.ollila@iki.fi>,
	David Bremner <david@tethera.net>,
	notmuch@notmuchmail.org
Subject: [PATCH v2 2/3] test/count: replace use of gdb with a LD_PRELOAD shim
Date: Thu, 28 Oct 2021 10:46:24 -0300	[thread overview]
Message-ID: <20211028134625.204930-2-david@tethera.net> (raw)
In-Reply-To: <20211028134625.204930-1-david@tethera.net>

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  | 27 +++++++++++++++------------
 test/notmuch-test.h | 27 +++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/test/T060-count.sh b/test/T060-count.sh
index 6ad80df9..48146706 100755
--- a/test/T060-count.sh
+++ b/test/T060-count.sh
@@ -102,22 +102,25 @@ output=$(sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' OUTPUT)
 test_expect_equal "${output}" "A Xapian exception occurred opening database"
 restore_database
 
-cat <<EOF > 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<<EOF
+#include <notmuch-test.h>
+
+WRAP_DLFUNC (notmuch_status_t, notmuch_query_search_messages, (notmuch_query_t *query, notmuch_messages_t **messages))
+
+  /* XXX WARNING THIS CORRUPTS THE DATABASE */
+  int fd = open ("target_postlist", O_WRONLY|O_TRUNC);
+  if (fd < 0)
+    exit (8);
+  close (fd);
+
+  return notmuch_query_search_messages_orig(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 <<EOF > 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 3a0e90a3..ed713099 100644
--- a/test/notmuch-test.h
+++ b/test/notmuch-test.h
@@ -1,9 +1,17 @@
 #ifndef _NOTMUCH_TEST_H
 #define _NOTMUCH_TEST_H
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
 #include <assert.h>
+#include <dlfcn.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <sys/wait.h>
 #include <talloc.h>
 #include <unistd.h>
@@ -20,4 +28,23 @@ expect0 (int line, notmuch_status_t ret)
 }
 
 #define EXPECT0(v)  expect0 (__LINE__, v);
+
+inline static void *
+dlsym_next (const char *symbol)
+{
+    void *sym = dlsym (RTLD_NEXT, symbol);
+    char *str = dlerror ();
+
+    if (str != NULL) {
+	fprintf (stderr, "finding symbol '%s' failed: %s", symbol, str);
+	exit (77);
+    }
+    return sym;
+}
+
+#define WRAP_DLFUNC(_rtype, _func, _args)                               \
+    _rtype _func _args;                                                 \
+    _rtype _func _args {                                                \
+	static _rtype (*_func##_orig) _args = NULL;                         \
+	if (! _func##_orig ) *(void **) (&_func##_orig) = dlsym_next (#_func);
 #endif
-- 
2.33.0

  reply	other threads:[~2021-10-28 13:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-25  1:15 Reduce test suite reliance on gdb David Bremner
2021-10-25  1:15 ` [PATCH 1/2] test/count: replace use of gdb with a LD_PRELOAD shim David Bremner
2021-10-25 19:04   ` Tomi Ollila
2021-10-25  1:15 ` [PATCH 2/2] test/new: replace use of gdb in vanishing file test David Bremner
2021-10-25 19:14   ` Tomi Ollila
2021-10-28 13:46     ` [PATCH v2 1/3] test: move system includes to notmuch-test.h David Bremner
2021-10-28 13:46       ` David Bremner [this message]
2021-10-28 13:46       ` [PATCH v2 3/3] test/new: replace use of gdb in vanishing file test David Bremner
2021-10-28 13:50         ` David Bremner
2021-12-04 16:51       ` [PATCH v2 1/3] test: move system includes to notmuch-test.h David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211028134625.204930-2-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    --cc=tomi.ollila@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).