]> git.notmuchmail.org Git - notmuch/blob - test/random-corpus.c
Fix typos
[notmuch] / test / random-corpus.c
1 /*
2  * Generate a random corpus of stub messages.
3  *
4  * Initial use case is testing dump and restore, so we only have
5  * message-ids and tags.
6  *
7  * Generated message-id's and tags are intentionally nasty.
8  *
9  * Copyright (c) 2012 David Bremner
10  *
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.
15  *
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.
20  *
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/ .
23  *
24  * Author: David Bremner <david@tethera.net>
25  */
26
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <talloc.h>
30 #include <string.h>
31 #include <glib.h>
32 #include <math.h>
33
34 #include "notmuch-client.h"
35 #include "command-line-arguments.h"
36 #include "database-test.h"
37
38 /* Current largest Unicode value defined. Note that most of these will
39  * be printed as boxes in most fonts.
40  */
41
42 #define GLYPH_MAX 0x10FFFE
43
44
45 typedef struct {
46     int weight;
47     int start;
48     int stop;
49 } char_class_t;
50
51 /*
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.
55  *
56  *  We then favour code points with 2 byte encodings. Note that
57  *  code points 0xD800-0xDFFF are forbidden in UTF-8.
58  */
59
60 static const
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 } };
66
67 static gunichar
68 random_unichar ()
69 {
70     int i;
71     int class = random () % GLYPH_MAX;
72     int size;
73
74     for (i = 0; char_class[i].weight < class; i++) /* nothing */;
75
76     size = char_class[i].stop - char_class[i].start + 1;
77
78     return char_class[i].start + (random () % size);
79 }
80
81 static char *
82 random_utf8_string (void *ctx, size_t char_count)
83 {
84     size_t offset = 0;
85     size_t i;
86     gchar *buf = NULL;
87     size_t buf_size = 0;
88
89     for (i = 0; i < char_count; i++) {
90         gunichar randomchar;
91         size_t written;
92
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);
97         }
98
99         do {
100             randomchar = random_unichar ();
101         } while (randomchar == '\n');
102
103         written = g_unichar_to_utf8 (randomchar, buf + offset);
104
105         if (written <= 0) {
106             fprintf (stderr, "error converting to utf8\n");
107             exit (1);
108         }
109
110         offset += written;
111
112     }
113     buf[offset] = 0;
114     return buf;
115 }
116
117 /* stubs since we cannot link with notmuch.o */
118 const notmuch_opt_desc_t notmuch_shared_options[] = {
119     { }
120 };
121
122 const char *notmuch_requested_db_uuid = NULL;
123
124 void
125 notmuch_process_shared_options (unused (const char *dummy))
126 {
127 }
128
129 int
130 notmuch_minimal_options (unused (const char *subcommand),
131                          unused (int argc),
132                          unused (char **argv))
133 {
134     return 0;
135 }
136
137 int
138 main (int argc, char **argv)
139 {
140
141     void *ctx = talloc_new (NULL);
142
143     const char *config_path = NULL;
144     notmuch_config_t *config;
145     notmuch_database_t *notmuch;
146
147     int num_messages = 500;
148     int max_tags = 10;
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;
154
155     int seed = 734569;
156
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" },
164         { }
165     };
166
167     int opt_index = parse_arguments (argc, argv, options, 1);
168
169     if (opt_index < 0)
170         exit (1);
171
172     if (message_id_len < 1) {
173         fprintf (stderr, "message id's must be least length 1\n");
174         exit (1);
175     }
176
177     if (config_path == NULL) {
178         fprintf (stderr, "configuration path must be specified");
179         exit (1);
180     }
181
182     config = notmuch_config_open (ctx, config_path, false);
183     if (config == NULL)
184         return 1;
185
186     if (notmuch_database_open (notmuch_config_get_database_path (config),
187                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
188         return 1;
189
190     srandom (seed);
191
192     int count;
193     for (count = 0; count < num_messages; count++) {
194         int j;
195         /* explicitly allow zero tags */
196         int num_tags = random () % (max_tags + 1);
197         /* message ids should be non-empty */
198         int this_mid_len = (random () % message_id_len) + 1;
199         const char **tag_list;
200         char *mid;
201         notmuch_status_t status;
202
203         do {
204             mid = random_utf8_string (ctx, this_mid_len);
205
206             tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 1);
207
208             for (j = 0; j < num_tags; j++) {
209                 int this_tag_len = random () % tag_len + 1;
210
211                 tag_list[j] = random_utf8_string (ctx, this_tag_len);
212             }
213
214             tag_list[j] = NULL;
215
216             status = notmuch_database_add_stub_message (notmuch, mid, tag_list);
217         } while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);
218
219         if (status != NOTMUCH_STATUS_SUCCESS) {
220             fprintf (stderr, "error %d adding message", status);
221             exit (status);
222         }
223     }
224
225     notmuch_database_destroy (notmuch);
226
227     talloc_free (ctx);
228
229     return 0;
230 }