]> git.notmuchmail.org Git - notmuch/blob - test/T560-lib-error.sh
470537cc30c02091384a50b31d09a6c82b2b0727
[notmuch] / test / T560-lib-error.sh
1 #!/usr/bin/env bash
2 test_description="error reporting for library"
3
4 . $(dirname "$0")/test-lib.sh || exit 1
5
6 add_email_corpus
7
8 test_begin_subtest "building database"
9 test_expect_success "NOTMUCH_NEW"
10
11 test_begin_subtest "Open null pointer"
12 test_C <<'EOF'
13 #include <stdio.h>
14 #include <notmuch.h>
15 int main (int argc, char** argv)
16 {
17     notmuch_database_t *db;
18     notmuch_status_t stat;
19     char* msg = NULL;
20     stat = notmuch_database_open_with_config (NULL,
21                                               NOTMUCH_DATABASE_MODE_READ_ONLY,
22                                               "", NULL, &db, &msg);
23     if (msg) fputs (msg, stderr);
24 }
25 EOF
26 cat <<'EOF' >EXPECTED
27 == stdout ==
28 == stderr ==
29 Error: could not locate database.
30 EOF
31 test_expect_equal_file EXPECTED OUTPUT
32
33 test_begin_subtest "Open relative path"
34 test_C <<'EOF'
35 #include <stdio.h>
36 #include <notmuch.h>
37 int main (int argc, char** argv)
38 {
39     notmuch_database_t *db;
40     notmuch_status_t stat;
41     char *msg = NULL;
42     stat = notmuch_database_open_with_config ("./nonexistent/foo",
43                                              NOTMUCH_DATABASE_MODE_READ_ONLY,
44                                              "", NULL, &db, &msg);
45     if (msg) fputs (msg, stderr);
46 }
47 EOF
48 cat <<'EOF' >EXPECTED
49 == stdout ==
50 == stderr ==
51 Error: Database path must be absolute.
52 EOF
53 test_expect_equal_file EXPECTED OUTPUT
54
55 test_begin_subtest "Create database in relative path"
56 test_C <<'EOF'
57 #include <stdio.h>
58 #include <notmuch.h>
59 int main (int argc, char** argv)
60 {
61     notmuch_database_t *db;
62     notmuch_status_t stat;
63     stat = notmuch_database_create ("./nonexistent/foo", &db);
64 }
65 EOF
66 cat <<'EOF' >EXPECTED
67 == stdout ==
68 == stderr ==
69 Error: Database path must be absolute.
70 EOF
71 test_expect_equal_file EXPECTED OUTPUT
72
73 test_begin_subtest "Open nonexistent database"
74 test_C ${PWD}/nonexistent/foo <<'EOF'
75 #include <stdio.h>
76 #include <notmuch.h>
77 int main (int argc, char** argv)
78 {
79     notmuch_database_t *db;
80     notmuch_status_t stat;
81     char* msg = NULL;
82     stat = notmuch_database_open_with_config (argv[1],
83                                               NOTMUCH_DATABASE_MODE_READ_ONLY,
84                                               "", NULL, &db, &msg);
85     if (msg) fputs (msg, stderr);
86 }
87 EOF
88 cat <<'EOF' >EXPECTED
89 == stdout ==
90 == stderr ==
91 Error: Cannot open database at CWD/nonexistent/foo: No such file or directory.
92 EOF
93 test_expect_equal_file EXPECTED OUTPUT
94
95 test_begin_subtest "create NULL path"
96 test_C <<'EOF'
97 #include <stdio.h>
98 #include <notmuch.h>
99 int main (int argc, char** argv)
100 {
101     notmuch_status_t stat;
102     stat = notmuch_database_create (NULL, NULL);
103 }
104 EOF
105 cat <<'EOF' >EXPECTED
106 == stdout ==
107 == stderr ==
108 Error: could not locate database.
109 EOF
110 test_expect_equal_file EXPECTED OUTPUT
111
112 test_begin_subtest "Create database in nonexistent directory"
113 test_C ${PWD}/nonexistent/foo<<'EOF'
114 #include <stdio.h>
115 #include <notmuch.h>
116 int main (int argc, char** argv)
117 {
118     notmuch_database_t *db;
119     notmuch_status_t stat;
120     stat = notmuch_database_create (argv[1], &db);
121 }
122 EOF
123 cat <<'EOF' >EXPECTED
124 == stdout ==
125 == stderr ==
126 Error: Cannot open database at CWD/nonexistent/foo: No such file or directory.
127 EOF
128 test_expect_equal_file EXPECTED OUTPUT
129
130 test_begin_subtest "Write to read-only database"
131 test_C ${MAIL_DIR} <<'EOF'
132 #include <stdio.h>
133 #include <notmuch.h>
134 int main (int argc, char** argv)
135 {
136    notmuch_database_t *db;
137    notmuch_status_t stat;
138    char* msg = NULL;
139    stat = notmuch_database_open_with_config (argv[1],
140                                              NOTMUCH_DATABASE_MODE_READ_ONLY,
141                                              "", NULL, &db, &msg);
142    if (msg) fputs (msg, stderr);
143    if (stat != NOTMUCH_STATUS_SUCCESS) {
144      fprintf (stderr, "error opening database: %d\n", stat);
145    }
146    stat = notmuch_database_index_file (db, "/dev/null", NULL, NULL);
147    if (stat)
148        fputs (notmuch_database_status_string (db), stderr);
149
150 }
151 EOF
152 cat <<'EOF' >EXPECTED
153 == stdout ==
154 == stderr ==
155 Cannot write to a read-only database.
156 EOF
157 test_expect_equal_file EXPECTED OUTPUT
158
159 test_begin_subtest "Add non-existent file"
160 test_C ${MAIL_DIR} <<'EOF'
161 #include <stdio.h>
162 #include <notmuch.h>
163 int main (int argc, char** argv)
164 {
165    notmuch_database_t *db;
166    notmuch_status_t stat;
167    stat = notmuch_database_open_with_config (argv[1],
168                                              NOTMUCH_DATABASE_MODE_READ_WRITE,
169                                              "", NULL, &db, NULL);
170    if (stat != NOTMUCH_STATUS_SUCCESS) {
171      fprintf (stderr, "error opening database: %d\n", stat);
172    }
173    stat = notmuch_database_index_file (db, "./nonexistent", NULL, NULL);
174    if (stat) {
175        char *status_string = notmuch_database_status_string (db);
176        if (status_string) fputs (status_string, stderr);
177    }
178 }
179 EOF
180 cat <<'EOF' >EXPECTED
181 == stdout ==
182 == stderr ==
183 Error opening ./nonexistent: No such file or directory
184 EOF
185 test_expect_equal_file EXPECTED OUTPUT
186
187 test_begin_subtest "compact, overwriting existing backup"
188 test_C ${MAIL_DIR} <<'EOF'
189 #include <stdio.h>
190 #include <notmuch.h>
191 static void
192 status_cb (const char *msg, void *closure)
193 {
194     printf ("%s\n", msg);
195 }
196 int main (int argc, char** argv)
197 {
198    notmuch_database_t *db;
199    notmuch_status_t stat;
200    stat = notmuch_database_compact (argv[1], argv[1], status_cb, NULL);
201 }
202 EOF
203 cat <<'EOF' >EXPECTED
204 == stdout ==
205 Path already exists: MAIL_DIR
206
207 == stderr ==
208 EOF
209 test_expect_equal_file EXPECTED OUTPUT
210
211 cat <<EOF > c_head
212 #include <stdio.h>
213 #include <sys/types.h>
214 #include <sys/stat.h>
215 #include <fcntl.h>
216 #include <talloc.h>
217 #include <notmuch.h>
218
219 int main (int argc, char** argv)
220 {
221    notmuch_database_t *db;
222    notmuch_status_t stat;
223    char *path;
224    char *msg = NULL;
225    int fd;
226
227    stat = notmuch_database_open_with_config (argv[1],
228                                              NOTMUCH_DATABASE_MODE_READ_WRITE,
229                                              NULL, NULL, &db, &msg);
230    if (stat != NOTMUCH_STATUS_SUCCESS) {
231      fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
232      exit (1);
233    }
234    fd = open(argv[2],O_WRONLY|O_TRUNC);
235    if (fd < 0) {
236        fprintf (stderr, "error opening %s\n", argv[1]);
237        exit (1);
238    }
239 EOF
240 cat <<'EOF' > c_tail
241    if (stat) {
242        const char *stat_str = notmuch_database_status_string (db);
243        if (stat_str)
244            fputs (stat_str, stderr);
245     }
246
247 }
248 EOF
249
250 POSTLIST_PATH=(${MAIL_DIR}/.notmuch/xapian/postlist.*)
251 backup_database
252 test_begin_subtest "Xapian exception finding message"
253 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
254    {
255        notmuch_message_t *message = NULL;
256        stat = notmuch_database_find_message (db, "id:nonexistent", &message);
257    }
258 EOF
259 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
260 cat <<'EOF' >EXPECTED
261 == stdout ==
262 == stderr ==
263 A Xapian exception occurred finding message
264 EOF
265 test_expect_equal_file EXPECTED OUTPUT.clean
266 restore_database
267
268 backup_database
269 test_begin_subtest "Xapian exception getting tags"
270 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
271    {
272        notmuch_tags_t *tags = NULL;
273        tags = notmuch_database_get_all_tags (db);
274        stat = (tags == NULL);
275    }
276 EOF
277 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
278 cat <<'EOF' >EXPECTED
279 == stdout ==
280 == stderr ==
281 A Xapian exception occurred getting tags
282 EOF
283 test_expect_equal_file EXPECTED OUTPUT.clean
284 restore_database
285
286 backup_database
287 test_begin_subtest "Xapian exception creating directory"
288 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
289    {
290        notmuch_directory_t *directory = NULL;
291        stat = notmuch_database_get_directory (db, "none/existing", &directory);
292    }
293 EOF
294 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
295 cat <<'EOF' >EXPECTED
296 == stdout ==
297 == stderr ==
298 A Xapian exception occurred finding/creating a directory
299 EOF
300 test_expect_equal_file EXPECTED OUTPUT.clean
301 restore_database
302
303 backup_database
304 test_begin_subtest "Xapian exception searching messages"
305 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
306    {
307        notmuch_messages_t *messages = NULL;
308        notmuch_query_t *query=notmuch_query_create (db, "*");
309        stat = notmuch_query_search_messages (query, &messages);
310    }
311 EOF
312 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
313 cat <<'EOF' >EXPECTED
314 == stdout ==
315 == stderr ==
316 A Xapian exception occurred performing query
317 Query string was: *
318 EOF
319 test_expect_equal_file EXPECTED OUTPUT.clean
320 restore_database
321
322 backup_database
323 test_begin_subtest "Xapian exception counting messages"
324 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
325    {
326        int count;
327        notmuch_query_t *query=notmuch_query_create (db, "id:87ocn0qh6d.fsf@yoom.home.cworth.org");
328        stat = notmuch_query_count_messages (query, &count);
329    }
330 EOF
331 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
332 cat <<'EOF' >EXPECTED
333 == stdout ==
334 == stderr ==
335 A Xapian exception occurred performing query
336 Query string was: id:87ocn0qh6d.fsf@yoom.home.cworth.org
337 EOF
338 test_expect_equal_file EXPECTED OUTPUT.clean
339 restore_database
340
341 test_done