[Patch v2 05/17] test: add generator for random "stub" messages
authordavid <david@tethera.net>
Sat, 24 Nov 2012 21:20:05 +0000 (17:20 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:50:51 +0000 (09:50 -0800)
08/017bbacb646fa5dbaf6af8f8b85b0940f2a99b [new file with mode: 0644]

diff --git a/08/017bbacb646fa5dbaf6af8f8b85b0940f2a99b b/08/017bbacb646fa5dbaf6af8f8b85b0940f2a99b
new file mode 100644 (file)
index 0000000..2895481
--- /dev/null
@@ -0,0 +1,346 @@
+Return-Path: <bremner@tethera.net>\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 750C0431FD7\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 13:20:44 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+       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 GHTerZEsoPnH for <notmuch@notmuchmail.org>;\r
+       Sat, 24 Nov 2012 13:20:41 -0800 (PST)\r
+Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
+       (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 5A28D431FD5\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 13:20:33 -0800 (PST)\r
+Received: from fctnnbsc30w-156034089108.dhcp-dynamic.fibreop.nb.bellaliant.net\r
+       ([156.34.89.108] helo=zancas.localnet)\r
+       by tesseract.cs.unb.ca with esmtpsa\r
+       (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
+       (envelope-from <bremner@tethera.net>)\r
+       id 1TcN9L-0006SL-O6; Sat, 24 Nov 2012 17:20:32 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.80)\r
+       (envelope-from <bremner@tethera.net>)\r
+       id 1TcN9G-0008Ci-8c; Sat, 24 Nov 2012 17:20:26 -0400\r
+From: david@tethera.net\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v2 05/17] test: add generator for random "stub" messages\r
+Date: Sat, 24 Nov 2012 17:20:05 -0400\r
+Message-Id: <1353792017-31459-6-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1353792017-31459-1-git-send-email-david@tethera.net>\r
+References: <1353792017-31459-1-git-send-email-david@tethera.net>\r
+X-Spam_bar: -\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: Sat, 24 Nov 2012 21:20:45 -0000\r
+\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
+updated for id:m2wr04ocro.fsf@guru.guru-group.fi\r
+\r
+- talk about Unicode value rather some specific encoding\r
+- call talloc_realloc less times\r
+---\r
+ test/.gitignore      |    1 +\r
+ test/Makefile.local  |   10 +++\r
+ test/basic           |    1 +\r
+ test/random-corpus.c |  204 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 4 files changed, 216 insertions(+)\r
+ create mode 100644 test/random-corpus.c\r
+\r
+diff --git a/test/.gitignore b/test/.gitignore\r
+index be7ab5e..1eff7ce 100644\r
+--- a/test/.gitignore\r
++++ b/test/.gitignore\r
+@@ -4,4 +4,5 @@ smtp-dummy\r
+ symbol-test\r
+ arg-test\r
+ hex-xcode\r
++random-corpus\r
+ tmp.*\r
+diff --git a/test/Makefile.local b/test/Makefile.local\r
+index 8479f91..6a9f15e 100644\r
+--- a/test/Makefile.local\r
++++ b/test/Makefile.local\r
+@@ -16,6 +16,14 @@ $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a\r
+ $(dir)/hex-xcode: $(dir)/hex-xcode.o command-line-arguments.o util/libutil.a\r
+       $(call quiet,CC) -I. $^ -o $@ -ltalloc\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
++                      parse-time-string/libparse-time-string.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
+@@ -29,6 +37,7 @@ $(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o\r
\r
+ TEST_BINARIES=$(dir)/arg-test \\r
+             $(dir)/hex-xcode \\r
++            $(dir)/random-corpus \\r
+             $(dir)/parse-time \\r
+             $(dir)/smtp-dummy \\r
+             $(dir)/symbol-test\r
+@@ -46,5 +55,6 @@ CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \\r
+        $(dir)/arg-test $(dir)/arg-test.o \\r
+        $(dir)/hex-xcode $(dir)/hex-xcode.o \\r
+        $(dir)/database-test.o \\r
++       $(dir)/random-corpus $(dir)/random-corpus.o \\r
+        $(dir)/parse-time $(dir)/parse-time.o \\r
+        $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.*\r
+diff --git a/test/basic b/test/basic\r
+index 2a571ac..f93469f 100755\r
+--- a/test/basic\r
++++ b/test/basic\r
+@@ -59,6 +59,7 @@ available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111  \\r
+     ! -name hex-xcode                 \\r
+     ! -name notmuch-test              \\r
+     ! -name parse-time                        \\r
++    ! -name random-corpus             \\r
+     ! -name smtp-dummy                        \\r
+     ! -name symbol-test                       \\r
+     ! -name test-verbose              \\r
+diff --git a/test/random-corpus.c b/test/random-corpus.c\r
+new file mode 100644\r
+index 0000000..085bda0\r
+--- /dev/null\r
++++ b/test/random-corpus.c\r
+@@ -0,0 +1,204 @@\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 Unicode 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() % 2;\r
++\r
++    /*\r
++     *  Choose about half ascii as test characters, as ascii\r
++     *  punctation and whitespace is the main cause of problems for\r
++     *  the (old) restore parser\r
++    */\r
++    switch (class) {\r
++    case 0:\r
++      /* ascii */\r
++      start = 0x01;\r
++      stop = 0x7f;\r
++      break;\r
++    case 1:\r
++      /* the rest of unicode */\r
++      start = 0x80;\r
++      stop = GLYPH_MAX;\r
++    }\r
++\r
++    if (start == stop)\r
++      return start;\r
++    else\r
++      return start + (random() % (stop - start + 1));\r
++}\r
++\r
++static char *\r
++random_utf8_string (void *ctx, size_t char_count)\r
++{\r
++    size_t offset = 0;\r
++    size_t i;\r
++\r
++    gchar *buf = NULL;\r
++    size_t buf_size = 0;\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, one for luck */\r
++      while (buf_size - offset < 8) {\r
++          buf_size = 2 * buf_size + 8;\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 (message_id_len < 1) {\r
++      fprintf (stderr, "message id's must be least length 1\n");\r
++      exit (1);\r
++    }\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
++      /* explicitly allow zero tags */\r
++      int num_tags = random () % (max_tags + 1);\r
++      /* message ids should be non-empty */\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