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