2 test_description="notmuch_database_* API"
4 . $(dirname "$0")/test-lib.sh || exit 1
8 test_begin_subtest "building database"
9 test_expect_success "NOTMUCH_NEW"
12 #include <notmuch-test.h>
14 int main (int argc, char** argv)
16 notmuch_database_t *db;
17 notmuch_status_t stat = NOTMUCH_STATUS_SUCCESS;
20 stat = notmuch_database_open_verbose (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db, &msg);
21 if (stat != NOTMUCH_STATUS_SUCCESS) {
22 fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
29 const char *stat_str = notmuch_database_status_string (db);
31 fputs (stat_str, stderr);
37 test_begin_subtest "get status_string with closed db"
38 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
41 EXPECT0(notmuch_database_close (db));
42 str = notmuch_database_status_string (db);
43 printf("%d\n", str == NULL);
51 test_expect_equal_file EXPECTED OUTPUT
53 test_begin_subtest "get path with closed db"
54 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
57 EXPECT0(notmuch_database_close (db));
58 path = notmuch_database_get_path (db);
67 test_expect_equal_file EXPECTED OUTPUT
69 test_begin_subtest "get version with closed db"
70 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
73 EXPECT0(notmuch_database_close (db));
74 version = notmuch_database_get_version (db);
75 printf ("%u\n", version);
76 stat = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
83 A Xapian exception occurred at lib/database.cc:XXX: Database has been closed
85 test_expect_equal_file EXPECTED OUTPUT
87 test_begin_subtest "re-close a closed db"
88 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
90 EXPECT0(notmuch_database_close (db));
91 stat = notmuch_database_close (db);
92 printf ("%d\n", stat);
100 test_expect_equal_file EXPECTED OUTPUT
102 test_begin_subtest "destroy a closed db"
103 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
105 unsigned int version;
106 EXPECT0(notmuch_database_close (db));
107 stat = notmuch_database_destroy (db);
108 printf ("%d\n", stat);
116 test_expect_equal_file EXPECTED OUTPUT
118 test_begin_subtest "destroy an open db"
119 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
121 unsigned int version;
122 stat = notmuch_database_destroy (db);
123 printf ("%d\n", stat);
131 test_expect_equal_file EXPECTED OUTPUT
133 test_begin_subtest "check a closed db for upgrade"
134 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
138 EXPECT0(notmuch_database_close (db));
139 ret = notmuch_database_needs_upgrade (db);
140 printf ("%d\n", ret == FALSE);
141 stat = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
148 A Xapian exception occurred at lib/database.cc:XXX: Database has been closed
150 test_expect_equal_file EXPECTED OUTPUT
152 test_begin_subtest "upgrade a closed db"
153 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
155 EXPECT0(notmuch_database_close (db));
156 stat = notmuch_database_upgrade (db, NULL, NULL);
157 printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS);
165 test_expect_equal_file EXPECTED OUTPUT
167 test_begin_subtest "begin atomic section for a closed db"
168 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
170 EXPECT0(notmuch_database_close (db));
171 stat = notmuch_database_begin_atomic (db);
172 printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS ||
173 stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
174 stat = NOTMUCH_STATUS_SUCCESS;
182 test_expect_equal_file EXPECTED OUTPUT
184 test_begin_subtest "end atomic section for a closed db"
185 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
187 EXPECT0(notmuch_database_close (db));
188 EXPECT0(notmuch_database_begin_atomic (db));
189 stat = notmuch_database_end_atomic (db);
190 printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS ||
191 stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
192 stat = NOTMUCH_STATUS_SUCCESS;
200 test_expect_equal_file EXPECTED OUTPUT
202 test_begin_subtest "get revision for a closed db"
203 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
208 EXPECT0(notmuch_database_close (db));
209 rev = notmuch_database_get_revision (db, &uuid);
210 printf ("%d\n", rev, uuid);
218 test_expect_equal_file EXPECTED OUTPUT
220 test_begin_subtest "get directory for a closed db"
221 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
223 notmuch_directory_t *dir;
224 EXPECT0(notmuch_database_close (db));
225 stat = notmuch_database_get_directory (db, "/nonexistent", &dir);
226 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
233 A Xapian exception occurred finding/creating a directory: Database has been closed.
235 test_expect_equal_file EXPECTED OUTPUT
237 test_begin_subtest "index file with a closed db"
238 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
240 notmuch_message_t *msg;
241 const char *path = talloc_asprintf(db, "%s/01:2,", argv[1]);
242 EXPECT0(notmuch_database_close (db));
243 stat = notmuch_database_index_file (db, path, NULL, &msg);
244 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
251 A Xapian exception occurred finding message: Database has been closed.
253 test_expect_equal_file EXPECTED OUTPUT
255 generate_message '[filename]=relative_path'
256 test_begin_subtest "index file (relative path)"
257 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
259 notmuch_message_t *msg;
260 stat = notmuch_database_index_file (db, "relative_path", NULL, &msg);
261 printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS);
269 test_expect_equal_file EXPECTED OUTPUT
271 test_begin_subtest "index file (absolute path outside mail root)"
272 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
274 notmuch_message_t *msg;
275 stat = notmuch_database_index_file (db, "/dev/zero", NULL, &msg);
276 printf ("%d\n", stat == NOTMUCH_STATUS_FILE_ERROR);
283 Error opening /dev/zero: path outside mail root
285 test_expect_equal_file EXPECTED OUTPUT
287 test_begin_subtest "remove message file with a closed db"
288 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
290 EXPECT0(notmuch_database_close (db));
291 stat = notmuch_database_remove_message (db, "01:2,");
292 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
299 A Xapian exception occurred finding/creating a directory: Database has been closed.
301 test_expect_equal_file EXPECTED OUTPUT
303 test_begin_subtest "find message by filename with a closed db"
304 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
306 notmuch_message_t *msg;
307 EXPECT0(notmuch_database_close (db));
308 stat = notmuch_database_find_message_by_filename (db, "01:2,", &msg);
309 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
316 A Xapian exception occurred finding/creating a directory: Database has been closed.
318 test_expect_equal_file EXPECTED OUTPUT
320 test_begin_subtest "Handle getting tags from closed database"
321 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
323 notmuch_tags_t *result;
324 EXPECT0(notmuch_database_close (db));
325 result = notmuch_database_get_all_tags (db);
326 printf("%d\n", result == NULL);
327 stat = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
334 A Xapian exception occurred getting tags: Database has been closed.
336 test_expect_equal_file EXPECTED OUTPUT
338 test_begin_subtest "get config from closed database"
339 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
342 EXPECT0(notmuch_database_close (db));
343 stat = notmuch_database_get_config (db, "foo", &result);
344 printf("%d\n", stat == NOTMUCH_STATUS_SUCCESS);
352 test_expect_equal_file EXPECTED OUTPUT
354 test_begin_subtest "set config in closed database"
355 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
357 EXPECT0(notmuch_database_close (db));
358 stat = notmuch_database_set_config (db, "foo", "bar");
359 printf("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
366 Error: A Xapian exception occurred setting metadata: Database has been closed
368 test_expect_equal_file EXPECTED OUTPUT
370 test_begin_subtest "get indexopts from closed database"
371 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
373 notmuch_indexopts_t *result;
374 EXPECT0(notmuch_database_close (db));
375 result = notmuch_database_get_default_indexopts (db);
376 printf("%d\n", result != NULL);
384 test_expect_equal_file EXPECTED OUTPUT
386 test_begin_subtest "get decryption policy from closed database"
387 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
389 notmuch_indexopts_t *result;
390 result = notmuch_database_get_default_indexopts (db);
391 EXPECT0(notmuch_database_close (db));
392 notmuch_decryption_policy_t policy = notmuch_indexopts_get_decrypt_policy (result);
393 printf ("%d\n", policy == NOTMUCH_DECRYPT_AUTO);
394 notmuch_indexopts_destroy (result);
395 printf ("SUCCESS\n");
404 test_expect_equal_file EXPECTED OUTPUT
406 test_begin_subtest "set decryption policy with closed database"
407 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
409 notmuch_indexopts_t *result;
410 result = notmuch_database_get_default_indexopts (db);
411 EXPECT0(notmuch_database_close (db));
412 notmuch_decryption_policy_t policy = notmuch_indexopts_get_decrypt_policy (result);
413 stat = notmuch_indexopts_set_decrypt_policy (result, policy);
414 printf("%d\n%d\n", policy == NOTMUCH_DECRYPT_AUTO, stat == NOTMUCH_STATUS_SUCCESS);
423 test_expect_equal_file EXPECTED OUTPUT