Re: [PATCH 2/3] test: add generator for random "stub" messages
authorMark Walters <markwalters1009@gmail.com>
Wed, 8 Aug 2012 07:45:38 +0000 (08:45 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:48:58 +0000 (09:48 -0800)
c5/57cb9e316cb31ae977a62610d058e25fe7565a [new file with mode: 0644]

diff --git a/c5/57cb9e316cb31ae977a62610d058e25fe7565a b/c5/57cb9e316cb31ae977a62610d058e25fe7565a
new file mode 100644 (file)
index 0000000..9bd6f55
--- /dev/null
@@ -0,0 +1,382 @@
+Return-Path: <m.walters@qmul.ac.uk>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 2C119431FAF\r
+       for <notmuch@notmuchmail.org>; Wed,  8 Aug 2012 00:45:48 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+       tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+       NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id Je9eDjexR187 for <notmuch@notmuchmail.org>;\r
+       Wed,  8 Aug 2012 00:45:47 -0700 (PDT)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id B1841431FAE\r
+       for <notmuch@notmuchmail.org>; Wed,  8 Aug 2012 00:45:46 -0700 (PDT)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+       by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1Sz0xc-0004fh-CK; Wed, 08 Aug 2012 08:45:44 +0100\r
+Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223]\r
+       helo=localhost)\r
+       by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1Sz0xb-000168-Mb; Wed, 08 Aug 2012 08:45:44 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: david@tethera.net, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 2/3] test: add generator for random "stub" messages\r
+In-Reply-To: <1344190393-22497-3-git-send-email-david@tethera.net>\r
+References: <1344190393-22497-1-git-send-email-david@tethera.net>\r
+       <1344190393-22497-3-git-send-email-david@tethera.net>\r
+User-Agent: Notmuch/0.13.2+96~g634443c (http://notmuchmail.org) Emacs/23.4.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Wed, 08 Aug 2012 08:45:38 +0100\r
+Message-ID: <87393x3jz1.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 94.192.233.223\r
+X-QM-SPAM-Info: Sender has good ham record.  :)\r
+X-QM-Body-MD5: cf197f392d6b8db2e5d884d1ac31f9dd (of first 20000 bytes)\r
+X-SpamAssassin-Score: -1.8\r
+X-SpamAssassin-SpamBar: -\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+       determine if it is\r
+       spam. We require at least 5.0 points to mark a message as spam.\r
+       This message scored -1.8 points.\r
+       Summary of the scoring: \r
+       * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
+       *      medium trust\r
+       *      [138.37.6.40 listed in list.dnswl.org]\r
+       * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+       provider *      (markwalters1009[at]gmail.com)\r
+       * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay\r
+       *      domain\r
+       *  0.5 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\r
+Cc: David Bremner <bremner@debian.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 08 Aug 2012 07:45:48 -0000\r
+\r
+\r
+Hi I don't think I know enough Xapian to sensibly review the first patch\r
+in this series.\r
+\r
+On Sun, 05 Aug 2012, david@tethera.net wrote:\r
+> From: David Bremner <bremner@debian.org>\r
+>\r
+> Initial use case is testing dump and restore, so we only have\r
+> message-ids and tags.\r
+>\r
+> The message ID's are nothing like RFC compliant, but it doesn't seem\r
+> any harder to roundtrip random UTF-8 strings than RFC-compliant ones.\r
+>\r
+> Tags are UTF-8, even though notmuch is in principle more generous than\r
+> that.\r
+> ---\r
+>  test/.gitignore      |    1 +\r
+>  test/Makefile.local  |   14 +++-\r
+>  test/basic           |    2 +-\r
+>  test/random-corpus.c |  201 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
+>  4 files changed, 216 insertions(+), 2 deletions(-)\r
+>  create mode 100644 test/random-corpus.c\r
+>\r
+> diff --git a/test/.gitignore b/test/.gitignore\r
+> index e63c689..e23017b 100644\r
+> --- a/test/.gitignore\r
+> +++ b/test/.gitignore\r
+> @@ -3,4 +3,5 @@ corpus.mail\r
+>  smtp-dummy\r
+>  symbol-test\r
+>  arg-test\r
+> +random-corpus\r
+>  tmp.*\r
+> diff --git a/test/Makefile.local b/test/Makefile.local\r
+> index c7f1435..586efc6 100644\r
+> --- a/test/Makefile.local\r
+> +++ b/test/Makefile.local\r
+> @@ -13,6 +13,13 @@ smtp_dummy_modules = $(smtp_dummy_srcs:.c=.o)\r
+>  $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a\r
+>      $(call quiet,CC) -I. $^ -o $@\r
+>  \r
+> +random_corpus_deps =  $(dir)/random-corpus.o  $(dir)/database-test.o \\r
+> +                    notmuch-config.o command-line-arguments.o \\r
+> +                    lib/libnotmuch.a util/libutil.a\r
+> +\r
+> +$(dir)/random-corpus: $(random_corpus_deps)\r
+> +    $(call quiet,CC) $(CFLAGS_FINAL) $^ -o $@ $(CONFIGURE_LDFLAGS)\r
+> +\r
+>  $(dir)/smtp-dummy: $(smtp_dummy_modules)\r
+>      $(call quiet,CC) $^ -o $@\r
+>  \r
+> @@ -21,7 +28,12 @@ $(dir)/symbol-test: $(dir)/symbol-test.o\r
+>  \r
+>  .PHONY: test check\r
+>  \r
+> -test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test\r
+> +TEST_BINARIES=$(dir)/arg-test \\r
+> +          $(dir)/random-corpus \\r
+> +          $(dir)/smtp-dummy \\r
+> +          $(dir)/symbol-test\r
+> +\r
+> +test-binaries: $(TEST_BINARIES)\r
+>  \r
+>  test:       all test-binaries\r
+>      @${dir}/notmuch-test $(OPTIONS)\r
+> diff --git a/test/basic b/test/basic\r
+> index d6aed24..589c4e2 100755\r
+> --- a/test/basic\r
+> +++ b/test/basic\r
+> @@ -54,7 +54,7 @@ test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'\r
+>  eval $(sed -n -e '/^TESTS="$/,/^"$/p' $TEST_DIRECTORY/notmuch-test)\r
+>  tests_in_suite=$(for i in $TESTS; do echo $i; done | sort)\r
+>  available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -executable -printf '%f\n' | \\r
+> -    sed -r -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test)$/d" | \\r
+> +    sed -r -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|random-corpus)$/d" | \\r
+>      sort)\r
+>  test_expect_equal "$tests_in_suite" "$available"\r
+>  \r
+> diff --git a/test/random-corpus.c b/test/random-corpus.c\r
+> new file mode 100644\r
+> index 0000000..ae900a6\r
+> --- /dev/null\r
+> +++ b/test/random-corpus.c\r
+> @@ -0,0 +1,201 @@\r
+> +/*\r
+> + * Generate a random corpus of stub messages.\r
+> + *\r
+> + * Initial use case is testing dump and restore, so we only have\r
+> + * message-ids and tags.\r
+> + *\r
+> + * Generated message-id's and tags are intentionally nasty.\r
+> + *\r
+> + * Copyright (c) 2012 David Bremner\r
+> + *\r
+> + * This program is free software: you can redistribute it and/or modify\r
+> + * it under the terms of the GNU General Public License as published by\r
+> + * the Free Software Foundation, either version 3 of the License, or\r
+> + * (at your option) any later version.\r
+> + *\r
+> + * This program is distributed in the hope that it will be useful,\r
+> + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+> + * GNU General Public License for more details.\r
+> + *\r
+> + * You should have received a copy of the GNU General Public License\r
+> + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
+> + *\r
+> + * Author: David Bremner <david@tethera.net>\r
+> + */\r
+> +\r
+> +#include <stdlib.h>\r
+> +#include <assert.h>\r
+> +#include <talloc.h>\r
+> +#include <string.h>\r
+> +#include <glib.h>\r
+> +#include <math.h>\r
+> +\r
+> +#include "notmuch-client.h"\r
+> +#include "command-line-arguments.h"\r
+> +#include "database-test.h"\r
+> +\r
+> +/* Current largest UTF-32 value defined. Note that most of these will\r
+> + * be printed as boxes in most fonts.\r
+> + */\r
+> +\r
+> +#define GLYPH_MAX 0x10FFFE\r
+> +\r
+> +static gunichar\r
+> +random_unichar ()\r
+> +{\r
+> +    int start=1, stop=GLYPH_MAX;\r
+> +    int class = random() % 4;\r
+> +\r
+> +    switch (class) {\r
+> +    case 0:\r
+> +    /* control */\r
+> +    start=0x01;\r
+> +    stop=0x20;\r
+> +    break;\r
+> +    case 1:\r
+> +    start=0x21;\r
+> +    stop=0x7E;\r
+> +    break;\r
+> +    case 2:\r
+> +    start=0x41;\r
+> +    stop=0x7a;\r
+> +    break;\r
+> +    case 3:\r
+> +    start=0x7F;\r
+> +    stop=GLYPH_MAX;\r
+> +    }\r
+\r
+I think comments on other the classes might be helpful: I think Case 2\r
+is Ascii A-z but that has a few characters between Z and a. Of course\r
+since you are generating a random character it doesn't "matter".\r
+\r
+> +\r
+> +    return start + (random() % (start - stop + 1));\r
+> +}\r
+\r
+I think you mean % (stop - start + 1)?\r
+\r
+Otherwise this looks fine (I haven't actually run it yet).\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+> +static char *\r
+> +random_utf8_string (void *ctx, size_t char_count)\r
+> +{\r
+> +\r
+> +    gchar *buf = NULL;\r
+> +    size_t buf_size = 0;\r
+> +\r
+> +    size_t offset = 0;\r
+> +\r
+> +    size_t i;\r
+> +\r
+> +    buf = talloc_realloc (ctx, NULL, gchar, char_count);\r
+> +    buf_size = char_count;\r
+> +\r
+> +    for (i = 0; i < char_count; i++) {\r
+> +    gunichar randomchar;\r
+> +    size_t written;\r
+> +\r
+> +    /* 6 for one glyph, one for null */\r
+> +    if (buf_size - offset < 8) {\r
+> +        buf_size += 16;\r
+> +        buf = talloc_realloc (ctx, buf, gchar, buf_size);\r
+> +    }\r
+> +\r
+> +    randomchar = random_unichar();\r
+> +\r
+> +    written = g_unichar_to_utf8 (randomchar, buf + offset);\r
+> +\r
+> +    if (written <= 0) {\r
+> +        fprintf (stderr, "error converting to utf8\n");\r
+> +        exit (1);\r
+> +    }\r
+> +\r
+> +    offset += written;\r
+> +\r
+> +    }\r
+> +    buf[offset] = 0;\r
+> +    return buf;\r
+> +}\r
+> +\r
+> +\r
+> +int\r
+> +main (int argc, char **argv)\r
+> +{\r
+> +\r
+> +    void *ctx = talloc_new (NULL);\r
+> +\r
+> +    char *config_path  = NULL;\r
+> +    notmuch_config_t *config;\r
+> +    notmuch_database_t *notmuch;\r
+> +\r
+> +    int num_messages = 500;\r
+> +    int max_tags = 10;\r
+> +    // leave room for UTF-8 encoding.\r
+> +    int tag_len = NOTMUCH_TAG_MAX / 6;\r
+> +    // NOTMUCH_MESSAGE_ID_MAX is not exported, so we make a\r
+> +    // conservative guess.\r
+> +    int message_id_len = (NOTMUCH_TAG_MAX - 20) / 6;\r
+> +\r
+> +    int seed = 734569;\r
+> +\r
+> +    notmuch_opt_desc_t options[] = {\r
+> +    { NOTMUCH_OPT_STRING, &config_path, "config-path", 'c', 0 },\r
+> +    { NOTMUCH_OPT_INT, &num_messages, "num-messages", 'n', 0 },\r
+> +    { NOTMUCH_OPT_INT, &max_tags, "max-tags", 'm', 0 },\r
+> +    { NOTMUCH_OPT_INT, &message_id_len, "message-id-len", 'M', 0 },\r
+> +    { NOTMUCH_OPT_INT, &tag_len, "tag-len", 't', 0 },\r
+> +    { NOTMUCH_OPT_INT, &seed, "seed", 's', 0 },\r
+> +    { 0, 0, 0, 0, 0 }\r
+> +    };\r
+> +\r
+> +    int opt_index = parse_arguments (argc, argv, options, 1);\r
+> +\r
+> +    if (opt_index < 0)\r
+> +    exit (1);\r
+> +\r
+> +    if (config_path == NULL) {\r
+> +    fprintf (stderr, "configuration path must be specified");\r
+> +    exit (1);\r
+> +    }\r
+> +\r
+> +    config = notmuch_config_open (ctx, config_path, NULL);\r
+> +    if (config == NULL)\r
+> +    return 1;\r
+> +\r
+> +    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+> +                           NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
+> +    return 1;\r
+> +\r
+> +    srandom (seed);\r
+> +\r
+> +    int count;\r
+> +    for (count = 0; count < num_messages; count++) {\r
+> +    int j;\r
+> +    int num_tags = random () % (max_tags + 1);\r
+> +    int this_mid_len = random () % message_id_len + 1;\r
+> +    const char **tag_list;\r
+> +    char *mid;\r
+> +    notmuch_status_t status;\r
+> +\r
+> +    do {\r
+> +        mid = random_utf8_string (ctx, this_mid_len);\r
+> +\r
+> +        tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 2);\r
+> +\r
+> +        tag_list[0] = "random-corpus";\r
+> +\r
+> +        for (j = 0; j < num_tags; j++) {\r
+> +            int this_tag_len = random () % tag_len + 1;\r
+> +\r
+> +            tag_list[j + 1] = random_utf8_string (ctx, this_tag_len);\r
+> +        }\r
+> +\r
+> +        tag_list[j + 1] = NULL;\r
+> +\r
+> +        status = notmuch_database_add_stub_message (notmuch, mid, tag_list);\r
+> +    } while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);\r
+> +\r
+> +    if (status != NOTMUCH_STATUS_SUCCESS) {\r
+> +        fprintf (stderr, "error %d adding message", status);\r
+> +        exit (status);\r
+> +    }\r
+> +    }\r
+> +\r
+> +    notmuch_database_destroy (notmuch);\r
+> +\r
+> +    talloc_free (ctx);\r
+> +\r
+> +    return 0;\r
+> +}\r
+> -- \r
+> 1.7.10.4\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r