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