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