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