2 * Generate a random corpus of stub messages.
4 * Initial use case is testing dump and restore, so we only have
5 * message-ids and tags.
7 * Generated message-id's and tags are intentionally nasty.
9 * Copyright (c) 2012 David Bremner
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see https://www.gnu.org/licenses/ .
24 * Author: David Bremner <david@tethera.net>
34 #include "notmuch-client.h"
35 #include "command-line-arguments.h"
36 #include "database-test.h"
38 /* Current largest Unicode value defined. Note that most of these will
39 * be printed as boxes in most fonts.
42 #define GLYPH_MAX 0x10FFFE
52 * Choose about half ascii as test characters, as ascii
53 * punctuation and whitespace is the main cause of problems for
54 * the (old) restore parser.
56 * We then favour code points with 2 byte encodings. Note that
57 * code points 0xD800-0xDFFF are forbidden in UTF-8.
61 char_class_t char_class[] = { { 0.50 * GLYPH_MAX, 0x0001, 0x007f },
62 { 0.75 * GLYPH_MAX, 0x0080, 0x07ff },
63 { 0.88 * GLYPH_MAX, 0x0800, 0xd7ff },
64 { 0.90 * GLYPH_MAX, 0xE000, 0xffff },
65 { GLYPH_MAX, 0x10000, GLYPH_MAX } };
71 int class = random () % GLYPH_MAX;
74 for (i = 0; char_class[i].weight < class; i++) /* nothing */;
76 size = char_class[i].stop - char_class[i].start + 1;
78 return char_class[i].start + (random () % size);
82 random_utf8_string (void *ctx, size_t char_count)
89 for (i = 0; i < char_count; i++) {
93 /* 6 for one glyph, one for null, one for luck */
94 while (buf_size <= offset + 8) {
95 buf_size = 2 * buf_size + 8;
96 buf = talloc_realloc (ctx, buf, gchar, buf_size);
100 randomchar = random_unichar ();
101 } while (randomchar == '\n');
103 written = g_unichar_to_utf8 (randomchar, buf + offset);
106 fprintf (stderr, "error converting to utf8\n");
117 /* stubs since we cannot link with notmuch.o */
118 const notmuch_opt_desc_t notmuch_shared_options[] = {
122 const char *notmuch_requested_db_uuid = NULL;
125 notmuch_process_shared_options (unused (notmuch_database_t *notmuch),
126 unused (const char *dummy))
131 notmuch_minimal_options (unused (const char *subcommand),
133 unused (char **argv))
139 main (int argc, char **argv)
142 void *ctx = talloc_new (NULL);
144 const char *config_path = NULL;
145 notmuch_database_t *notmuch;
147 int num_messages = 500;
149 // leave room for UTF-8 encoding.
150 int tag_len = NOTMUCH_TAG_MAX / 6;
151 // NOTMUCH_MESSAGE_ID_MAX is not exported, so we make a
152 // conservative guess.
153 int message_id_len = (NOTMUCH_TAG_MAX - 20) / 6;
157 notmuch_opt_desc_t options[] = {
158 { .opt_string = &config_path, .name = "config-path" },
159 { .opt_int = &num_messages, .name = "num-messages" },
160 { .opt_int = &max_tags, .name = "max-tags" },
161 { .opt_int = &message_id_len, .name = "message-id-len" },
162 { .opt_int = &tag_len, .name = "tag-len" },
163 { .opt_int = &seed, .name = "seed" },
167 int opt_index = parse_arguments (argc, argv, options, 1);
172 if (message_id_len < 1) {
173 fprintf (stderr, "message id's must be least length 1\n");
177 if (config_path == NULL) {
178 fprintf (stderr, "configuration path must be specified");
182 if (notmuch_database_open_with_config (NULL,
183 NOTMUCH_DATABASE_MODE_READ_WRITE,
194 for (count = 0; count < num_messages; count++) {
196 /* explicitly allow zero tags */
197 int num_tags = random () % (max_tags + 1);
198 /* message ids should be non-empty */
199 int this_mid_len = (random () % message_id_len) + 1;
200 const char **tag_list;
202 notmuch_status_t status;
205 mid = random_utf8_string (ctx, this_mid_len);
207 tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 1);
209 for (j = 0; j < num_tags; j++) {
210 int this_tag_len = random () % tag_len + 1;
212 tag_list[j] = random_utf8_string (ctx, this_tag_len);
217 status = notmuch_database_add_stub_message (notmuch, mid, tag_list);
218 } while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);
220 if (status != NOTMUCH_STATUS_SUCCESS) {
221 fprintf (stderr, "error %d adding message", status);
226 notmuch_database_destroy (notmuch);