]> git.notmuchmail.org Git - notmuch/blob - test/random-corpus.c
Merge tag '0.20.2'
[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 http://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  *  punctation 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         { 0, 0, 0, 0, 0 }
120 };
121
122 void
123 notmuch_process_shared_options (unused (const char *dummy))
124 {
125 }
126
127 int
128 notmuch_minimal_options (unused (const char *subcommand),
129                          unused (int argc),
130                          unused (char **argv))
131 {
132     return 0;
133 }
134
135 int
136 main (int argc, char **argv)
137 {
138
139     void *ctx = talloc_new (NULL);
140
141     char *config_path  = NULL;
142     notmuch_config_t *config;
143     notmuch_database_t *notmuch;
144
145     int num_messages = 500;
146     int max_tags = 10;
147     // leave room for UTF-8 encoding.
148     int tag_len = NOTMUCH_TAG_MAX / 6;
149     // NOTMUCH_MESSAGE_ID_MAX is not exported, so we make a
150     // conservative guess.
151     int message_id_len = (NOTMUCH_TAG_MAX - 20) / 6;
152
153     int seed = 734569;
154
155     notmuch_opt_desc_t options[] = {
156         { NOTMUCH_OPT_STRING, &config_path, "config-path", 'c', 0 },
157         { NOTMUCH_OPT_INT, &num_messages, "num-messages", 'n', 0 },
158         { NOTMUCH_OPT_INT, &max_tags, "max-tags", 'm', 0 },
159         { NOTMUCH_OPT_INT, &message_id_len, "message-id-len", 'M', 0 },
160         { NOTMUCH_OPT_INT, &tag_len, "tag-len", 't', 0 },
161         { NOTMUCH_OPT_INT, &seed, "seed", 's', 0 },
162         { 0, 0, 0, 0, 0 }
163     };
164
165     int opt_index = parse_arguments (argc, argv, options, 1);
166
167     if (opt_index < 0)
168         exit (1);
169
170     if (message_id_len < 1) {
171         fprintf (stderr, "message id's must be least length 1\n");
172         exit (1);
173     }
174
175     if (config_path == NULL) {
176         fprintf (stderr, "configuration path must be specified");
177         exit (1);
178     }
179
180     config = notmuch_config_open (ctx, config_path, FALSE);
181     if (config == NULL)
182         return 1;
183
184     if (notmuch_database_open (notmuch_config_get_database_path (config),
185                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
186         return 1;
187
188     srandom (seed);
189
190     int count;
191     for (count = 0; count < num_messages; count++) {
192         int j;
193         /* explicitly allow zero tags */
194         int num_tags = random () % (max_tags + 1);
195         /* message ids should be non-empty */
196         int this_mid_len = (random () % message_id_len) + 1;
197         const char **tag_list;
198         char *mid;
199         notmuch_status_t status;
200
201         do {
202             mid = random_utf8_string (ctx, this_mid_len);
203
204             tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 1);
205
206             for (j = 0; j < num_tags; j++) {
207                 int this_tag_len = random () % tag_len + 1;
208
209                 tag_list[j] = random_utf8_string (ctx, this_tag_len);
210             }
211
212             tag_list[j] = NULL;
213
214             status = notmuch_database_add_stub_message (notmuch, mid, tag_list);
215         } while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);
216
217         if (status != NOTMUCH_STATUS_SUCCESS) {
218             fprintf (stderr, "error %d adding message", status);
219             exit (status);
220         }
221     }
222
223     notmuch_database_destroy (notmuch);
224
225     talloc_free (ctx);
226
227     return 0;
228 }