]> git.notmuchmail.org Git - notmuch/blob - test/T560-lib-error.sh
Merge tag '0.31.4'
[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: Cannot open a database for a NULL path.
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 opening database at CWD/nonexistent/foo/.notmuch: 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: Cannot open a database for a NULL path.
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 create 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_verbose (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db, &msg);
210    if (stat != NOTMUCH_STATUS_SUCCESS) {
211      fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
212      exit (1);
213    }
214    fd = open(argv[2],O_WRONLY|O_TRUNC);
215    if (fd < 0) {
216        fprintf (stderr, "error opening %s\n", argv[1]);
217        exit (1);
218    }
219 EOF
220 cat <<'EOF' > c_tail
221    if (stat) {
222        const char *stat_str = notmuch_database_status_string (db);
223        if (stat_str)
224            fputs (stat_str, stderr);
225     }
226
227 }
228 EOF
229
230 POSTLIST_PATH=(${MAIL_DIR}/.notmuch/xapian/postlist.*)
231 backup_database
232 test_begin_subtest "Xapian exception finding message"
233 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
234    {
235        notmuch_message_t *message = NULL;
236        stat = notmuch_database_find_message (db, "id:nonexistent", &message);
237    }
238 EOF
239 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
240 cat <<'EOF' >EXPECTED
241 == stdout ==
242 == stderr ==
243 A Xapian exception occurred finding message
244 EOF
245 test_expect_equal_file EXPECTED OUTPUT.clean
246 restore_database
247
248 backup_database
249 test_begin_subtest "Xapian exception getting tags"
250 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
251    {
252        notmuch_tags_t *tags = NULL;
253        tags = notmuch_database_get_all_tags (db);
254        stat = (tags == NULL);
255    }
256 EOF
257 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
258 cat <<'EOF' >EXPECTED
259 == stdout ==
260 == stderr ==
261 A Xapian exception occurred getting tags
262 EOF
263 test_expect_equal_file EXPECTED OUTPUT.clean
264 restore_database
265
266 backup_database
267 test_begin_subtest "Xapian exception creating directory"
268 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
269    {
270        notmuch_directory_t *directory = NULL;
271        stat = notmuch_database_get_directory (db, "none/existing", &directory);
272    }
273 EOF
274 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
275 cat <<'EOF' >EXPECTED
276 == stdout ==
277 == stderr ==
278 A Xapian exception occurred finding/creating a directory
279 EOF
280 test_expect_equal_file EXPECTED OUTPUT.clean
281 restore_database
282
283 backup_database
284 test_begin_subtest "Xapian exception searching messages"
285 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
286    {
287        notmuch_messages_t *messages = NULL;
288        notmuch_query_t *query=notmuch_query_create (db, "*");
289        stat = notmuch_query_search_messages (query, &messages);
290    }
291 EOF
292 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
293 cat <<'EOF' >EXPECTED
294 == stdout ==
295 == stderr ==
296 A Xapian exception occurred performing query
297 Query string was: *
298 EOF
299 test_expect_equal_file EXPECTED OUTPUT.clean
300 restore_database
301
302 backup_database
303 test_begin_subtest "Xapian exception counting messages"
304 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
305    {
306        int count;
307        notmuch_query_t *query=notmuch_query_create (db, "id:87ocn0qh6d.fsf@yoom.home.cworth.org");
308        stat = notmuch_query_count_messages (query, &count);
309    }
310 EOF
311 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
312 cat <<'EOF' >EXPECTED
313 == stdout ==
314 == stderr ==
315 A Xapian exception occurred performing query
316 Query string was: id:87ocn0qh6d.fsf@yoom.home.cworth.org
317 EOF
318 test_expect_equal_file EXPECTED OUTPUT.clean
319 restore_database
320
321 test_done