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