Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / 08 / 017bbacb646fa5dbaf6af8f8b85b0940f2a99b
1 Return-Path: <bremner@tethera.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 750C0431FD7\r
6         for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 13:20:44 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id GHTerZEsoPnH for <notmuch@notmuchmail.org>;\r
16         Sat, 24 Nov 2012 13:20:41 -0800 (PST)\r
17 Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
18         (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 5A28D431FD5\r
21         for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 13:20:33 -0800 (PST)\r
22 Received: from fctnnbsc30w-156034089108.dhcp-dynamic.fibreop.nb.bellaliant.net\r
23         ([156.34.89.108] helo=zancas.localnet)\r
24         by tesseract.cs.unb.ca with esmtpsa\r
25         (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
26         (envelope-from <bremner@tethera.net>)\r
27         id 1TcN9L-0006SL-O6; Sat, 24 Nov 2012 17:20:32 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1TcN9G-0008Ci-8c; Sat, 24 Nov 2012 17:20:26 -0400\r
31 From: david@tethera.net\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [Patch v2 05/17] test: add generator for random "stub" messages\r
34 Date: Sat, 24 Nov 2012 17:20:05 -0400\r
35 Message-Id: <1353792017-31459-6-git-send-email-david@tethera.net>\r
36 X-Mailer: git-send-email 1.7.10.4\r
37 In-Reply-To: <1353792017-31459-1-git-send-email-david@tethera.net>\r
38 References: <1353792017-31459-1-git-send-email-david@tethera.net>\r
39 X-Spam_bar: -\r
40 Cc: David Bremner <bremner@debian.org>\r
41 X-BeenThere: notmuch@notmuchmail.org\r
42 X-Mailman-Version: 2.1.13\r
43 Precedence: list\r
44 List-Id: "Use and development of the notmuch mail system."\r
45         <notmuch.notmuchmail.org>\r
46 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
47         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
48 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
49 List-Post: <mailto:notmuch@notmuchmail.org>\r
50 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
51 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
52         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
53 X-List-Received-Date: Sat, 24 Nov 2012 21:20:45 -0000\r
54 \r
55 From: David Bremner <bremner@debian.org>\r
56 \r
57 Initial use case is testing dump and restore, so we only have\r
58 message-ids and tags.\r
59 \r
60 The message ID's are nothing like RFC compliant, but it doesn't seem\r
61 any harder to roundtrip random UTF-8 strings than RFC-compliant ones.\r
62 \r
63 Tags are UTF-8, even though notmuch is in principle more generous than\r
64 that.\r
65 \r
66 updated for id:m2wr04ocro.fsf@guru.guru-group.fi\r
67 \r
68 - talk about Unicode value rather some specific encoding\r
69 - call talloc_realloc less times\r
70 ---\r
71  test/.gitignore      |    1 +\r
72  test/Makefile.local  |   10 +++\r
73  test/basic           |    1 +\r
74  test/random-corpus.c |  204 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
75  4 files changed, 216 insertions(+)\r
76  create mode 100644 test/random-corpus.c\r
77 \r
78 diff --git a/test/.gitignore b/test/.gitignore\r
79 index be7ab5e..1eff7ce 100644\r
80 --- a/test/.gitignore\r
81 +++ b/test/.gitignore\r
82 @@ -4,4 +4,5 @@ smtp-dummy\r
83  symbol-test\r
84  arg-test\r
85  hex-xcode\r
86 +random-corpus\r
87  tmp.*\r
88 diff --git a/test/Makefile.local b/test/Makefile.local\r
89 index 8479f91..6a9f15e 100644\r
90 --- a/test/Makefile.local\r
91 +++ b/test/Makefile.local\r
92 @@ -16,6 +16,14 @@ $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a\r
93  $(dir)/hex-xcode: $(dir)/hex-xcode.o command-line-arguments.o util/libutil.a\r
94         $(call quiet,CC) -I. $^ -o $@ -ltalloc\r
95  \r
96 +random_corpus_deps =  $(dir)/random-corpus.o  $(dir)/database-test.o \\r
97 +                       notmuch-config.o command-line-arguments.o \\r
98 +                       lib/libnotmuch.a util/libutil.a \\r
99 +                       parse-time-string/libparse-time-string.a\r
100 +\r
101 +$(dir)/random-corpus: $(random_corpus_deps)\r
102 +       $(call quiet,CC) $(CFLAGS_FINAL) $^ -o $@ $(CONFIGURE_LDFLAGS)\r
103 +\r
104  $(dir)/smtp-dummy: $(smtp_dummy_modules)\r
105         $(call quiet,CC) $^ -o $@\r
106  \r
107 @@ -29,6 +37,7 @@ $(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o\r
108  \r
109  TEST_BINARIES=$(dir)/arg-test \\r
110               $(dir)/hex-xcode \\r
111 +             $(dir)/random-corpus \\r
112               $(dir)/parse-time \\r
113               $(dir)/smtp-dummy \\r
114               $(dir)/symbol-test\r
115 @@ -46,5 +55,6 @@ CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \\r
116          $(dir)/arg-test $(dir)/arg-test.o \\r
117          $(dir)/hex-xcode $(dir)/hex-xcode.o \\r
118          $(dir)/database-test.o \\r
119 +        $(dir)/random-corpus $(dir)/random-corpus.o \\r
120          $(dir)/parse-time $(dir)/parse-time.o \\r
121          $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.*\r
122 diff --git a/test/basic b/test/basic\r
123 index 2a571ac..f93469f 100755\r
124 --- a/test/basic\r
125 +++ b/test/basic\r
126 @@ -59,6 +59,7 @@ available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111  \\r
127      ! -name hex-xcode                  \\r
128      ! -name notmuch-test               \\r
129      ! -name parse-time                 \\r
130 +    ! -name random-corpus              \\r
131      ! -name smtp-dummy                 \\r
132      ! -name symbol-test                        \\r
133      ! -name test-verbose               \\r
134 diff --git a/test/random-corpus.c b/test/random-corpus.c\r
135 new file mode 100644\r
136 index 0000000..085bda0\r
137 --- /dev/null\r
138 +++ b/test/random-corpus.c\r
139 @@ -0,0 +1,204 @@\r
140 +/*\r
141 + * Generate a random corpus of stub messages.\r
142 + *\r
143 + * Initial use case is testing dump and restore, so we only have\r
144 + * message-ids and tags.\r
145 + *\r
146 + * Generated message-id's and tags are intentionally nasty.\r
147 + *\r
148 + * Copyright (c) 2012 David Bremner\r
149 + *\r
150 + * This program is free software: you can redistribute it and/or modify\r
151 + * it under the terms of the GNU General Public License as published by\r
152 + * the Free Software Foundation, either version 3 of the License, or\r
153 + * (at your option) any later version.\r
154 + *\r
155 + * This program is distributed in the hope that it will be useful,\r
156 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
157 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
158 + * GNU General Public License for more details.\r
159 + *\r
160 + * You should have received a copy of the GNU General Public License\r
161 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
162 + *\r
163 + * Author: David Bremner <david@tethera.net>\r
164 + */\r
165 +\r
166 +#include <stdlib.h>\r
167 +#include <assert.h>\r
168 +#include <talloc.h>\r
169 +#include <string.h>\r
170 +#include <glib.h>\r
171 +#include <math.h>\r
172 +\r
173 +#include "notmuch-client.h"\r
174 +#include "command-line-arguments.h"\r
175 +#include "database-test.h"\r
176 +\r
177 +/* Current largest Unicode value defined. Note that most of these will\r
178 + * be printed as boxes in most fonts.\r
179 + */\r
180 +\r
181 +#define GLYPH_MAX 0x10FFFE\r
182 +\r
183 +static gunichar\r
184 +random_unichar ()\r
185 +{\r
186 +    int start = 1, stop = GLYPH_MAX;\r
187 +    int class = random() % 2;\r
188 +\r
189 +    /*\r
190 +     *  Choose about half ascii as test characters, as ascii\r
191 +     *  punctation and whitespace is the main cause of problems for\r
192 +     *  the (old) restore parser\r
193 +    */\r
194 +    switch (class) {\r
195 +    case 0:\r
196 +       /* ascii */\r
197 +       start = 0x01;\r
198 +       stop = 0x7f;\r
199 +       break;\r
200 +    case 1:\r
201 +       /* the rest of unicode */\r
202 +       start = 0x80;\r
203 +       stop = GLYPH_MAX;\r
204 +    }\r
205 +\r
206 +    if (start == stop)\r
207 +       return start;\r
208 +    else\r
209 +       return start + (random() % (stop - start + 1));\r
210 +}\r
211 +\r
212 +static char *\r
213 +random_utf8_string (void *ctx, size_t char_count)\r
214 +{\r
215 +    size_t offset = 0;\r
216 +    size_t i;\r
217 +\r
218 +    gchar *buf = NULL;\r
219 +    size_t buf_size = 0;\r
220 +\r
221 +    for (i = 0; i < char_count; i++) {\r
222 +       gunichar randomchar;\r
223 +       size_t written;\r
224 +\r
225 +       /* 6 for one glyph, one for null, one for luck */\r
226 +       while (buf_size - offset < 8) {\r
227 +           buf_size = 2 * buf_size + 8;\r
228 +           buf = talloc_realloc (ctx, buf, gchar, buf_size);\r
229 +       }\r
230 +\r
231 +       randomchar = random_unichar();\r
232 +\r
233 +       written = g_unichar_to_utf8 (randomchar, buf + offset);\r
234 +\r
235 +       if (written <= 0) {\r
236 +           fprintf (stderr, "error converting to utf8\n");\r
237 +           exit (1);\r
238 +       }\r
239 +\r
240 +       offset += written;\r
241 +\r
242 +    }\r
243 +    buf[offset] = 0;\r
244 +    return buf;\r
245 +}\r
246 +\r
247 +\r
248 +int\r
249 +main (int argc, char **argv)\r
250 +{\r
251 +\r
252 +    void *ctx = talloc_new (NULL);\r
253 +\r
254 +    char *config_path  = NULL;\r
255 +    notmuch_config_t *config;\r
256 +    notmuch_database_t *notmuch;\r
257 +\r
258 +    int num_messages = 500;\r
259 +    int max_tags = 10;\r
260 +    // leave room for UTF-8 encoding.\r
261 +    int tag_len = NOTMUCH_TAG_MAX / 6;\r
262 +    // NOTMUCH_MESSAGE_ID_MAX is not exported, so we make a\r
263 +    // conservative guess.\r
264 +    int message_id_len = (NOTMUCH_TAG_MAX - 20) / 6;\r
265 +\r
266 +    int seed = 734569;\r
267 +\r
268 +    notmuch_opt_desc_t options[] = {\r
269 +       { NOTMUCH_OPT_STRING, &config_path, "config-path", 'c', 0 },\r
270 +       { NOTMUCH_OPT_INT, &num_messages, "num-messages", 'n', 0 },\r
271 +       { NOTMUCH_OPT_INT, &max_tags, "max-tags", 'm', 0 },\r
272 +       { NOTMUCH_OPT_INT, &message_id_len, "message-id-len", 'M', 0 },\r
273 +       { NOTMUCH_OPT_INT, &tag_len, "tag-len", 't', 0 },\r
274 +       { NOTMUCH_OPT_INT, &seed, "seed", 's', 0 },\r
275 +       { 0, 0, 0, 0, 0 }\r
276 +    };\r
277 +\r
278 +    int opt_index = parse_arguments (argc, argv, options, 1);\r
279 +\r
280 +    if (opt_index < 0)\r
281 +       exit (1);\r
282 +\r
283 +    if (message_id_len < 1) {\r
284 +       fprintf (stderr, "message id's must be least length 1\n");\r
285 +       exit (1);\r
286 +    }\r
287 +\r
288 +    if (config_path == NULL) {\r
289 +       fprintf (stderr, "configuration path must be specified");\r
290 +       exit (1);\r
291 +    }\r
292 +\r
293 +    config = notmuch_config_open (ctx, config_path, NULL);\r
294 +    if (config == NULL)\r
295 +       return 1;\r
296 +\r
297 +    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
298 +                              NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
299 +       return 1;\r
300 +\r
301 +    srandom (seed);\r
302 +\r
303 +    int count;\r
304 +    for (count = 0; count < num_messages; count++) {\r
305 +       int j;\r
306 +       /* explicitly allow zero tags */\r
307 +       int num_tags = random () % (max_tags + 1);\r
308 +       /* message ids should be non-empty */\r
309 +       int this_mid_len = (random () % message_id_len) + 1;\r
310 +       const char **tag_list;\r
311 +       char *mid;\r
312 +       notmuch_status_t status;\r
313 +\r
314 +       do {\r
315 +           mid = random_utf8_string (ctx, this_mid_len);\r
316 +\r
317 +           tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 2);\r
318 +\r
319 +           tag_list[0] = "random-corpus";\r
320 +\r
321 +           for (j = 0; j < num_tags; j++) {\r
322 +               int this_tag_len = random () % tag_len + 1;\r
323 +\r
324 +               tag_list[j + 1] = random_utf8_string (ctx, this_tag_len);\r
325 +           }\r
326 +\r
327 +           tag_list[j + 1] = NULL;\r
328 +\r
329 +           status = notmuch_database_add_stub_message (notmuch, mid, tag_list);\r
330 +       } while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);\r
331 +\r
332 +       if (status != NOTMUCH_STATUS_SUCCESS) {\r
333 +           fprintf (stderr, "error %d adding message", status);\r
334 +           exit (status);\r
335 +       }\r
336 +    }\r
337 +\r
338 +    notmuch_database_destroy (notmuch);\r
339 +\r
340 +    talloc_free (ctx);\r
341 +\r
342 +    return 0;\r
343 +}\r
344 -- \r
345 1.7.10.4\r
346 \r