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_with_config (argv[1],
21 NOTMUCH_DATABASE_MODE_READ_WRITE,
22 NULL, NULL, &db, &msg);
23 if (stat != NOTMUCH_STATUS_SUCCESS) {
24 fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
31 const char *stat_str = notmuch_database_status_string (db);
33 fputs (stat_str, stderr);
39 test_begin_subtest "get status_string with closed db"
40 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
43 EXPECT0(notmuch_database_close (db));
44 str = notmuch_database_status_string (db);
45 printf("%d\n", str == NULL);
53 test_expect_equal_file EXPECTED OUTPUT
55 test_begin_subtest "get path with closed db"
56 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
59 EXPECT0(notmuch_database_close (db));
60 path = notmuch_database_get_path (db);
69 test_expect_equal_file EXPECTED OUTPUT
71 test_begin_subtest "get version with closed db"
72 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
75 EXPECT0(notmuch_database_close (db));
76 version = notmuch_database_get_version (db);
77 printf ("%u\n", version);
78 stat = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
85 A Xapian exception occurred at database.cc:XXX: Database has been closed
87 test_expect_equal_file EXPECTED OUTPUT
89 test_begin_subtest "re-close a closed db"
90 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
92 EXPECT0(notmuch_database_close (db));
93 stat = notmuch_database_close (db);
94 printf ("%d\n", stat);
102 test_expect_equal_file EXPECTED OUTPUT
104 test_begin_subtest "destroy a closed db"
105 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
107 unsigned int version;
108 EXPECT0(notmuch_database_close (db));
109 stat = notmuch_database_destroy (db);
110 printf ("%d\n", stat);
118 test_expect_equal_file EXPECTED OUTPUT
120 test_begin_subtest "destroy an open db"
121 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
123 unsigned int version;
124 stat = notmuch_database_destroy (db);
125 printf ("%d\n", stat);
133 test_expect_equal_file EXPECTED OUTPUT
135 test_begin_subtest "check a closed db for upgrade"
136 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
140 EXPECT0(notmuch_database_close (db));
141 ret = notmuch_database_needs_upgrade (db);
142 printf ("%d\n", ret == FALSE);
143 stat = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
150 A Xapian exception occurred at database.cc:XXX: Database has been closed
152 test_expect_equal_file EXPECTED OUTPUT
154 test_begin_subtest "upgrade a closed db"
155 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
157 EXPECT0(notmuch_database_close (db));
158 stat = notmuch_database_upgrade (db, NULL, NULL);
159 printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS);
167 test_expect_equal_file EXPECTED OUTPUT
169 test_begin_subtest "begin atomic section for a closed db"
170 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
172 EXPECT0(notmuch_database_close (db));
173 stat = notmuch_database_begin_atomic (db);
174 printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS ||
175 stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
176 stat = NOTMUCH_STATUS_SUCCESS;
184 test_expect_equal_file EXPECTED OUTPUT
186 test_begin_subtest "end atomic section for a closed db"
187 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
189 EXPECT0(notmuch_database_close (db));
190 EXPECT0(notmuch_database_begin_atomic (db));
191 stat = notmuch_database_end_atomic (db);
192 printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS ||
193 stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
194 stat = NOTMUCH_STATUS_SUCCESS;
202 test_expect_equal_file EXPECTED OUTPUT
204 test_begin_subtest "get revision for a closed db"
205 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
210 EXPECT0(notmuch_database_close (db));
211 rev = notmuch_database_get_revision (db, &uuid);
212 printf ("%d\n", rev, uuid);
220 test_expect_equal_file EXPECTED OUTPUT
222 test_begin_subtest "get directory for a closed db"
223 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
225 notmuch_directory_t *dir;
226 EXPECT0(notmuch_database_close (db));
227 stat = notmuch_database_get_directory (db, "/nonexistent", &dir);
228 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
235 A Xapian exception occurred finding/creating a directory: Database has been closed.
237 test_expect_equal_file EXPECTED OUTPUT
239 test_begin_subtest "index file with a closed db"
240 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
242 notmuch_message_t *msg;
243 const char *path = talloc_asprintf(db, "%s/01:2,", argv[1]);
244 EXPECT0(notmuch_database_close (db));
245 stat = notmuch_database_index_file (db, path, NULL, &msg);
246 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
253 A Xapian exception occurred finding message: Database has been closed.
255 test_expect_equal_file EXPECTED OUTPUT
257 generate_message '[filename]=relative_path'
258 test_begin_subtest "index file (relative path)"
259 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
261 notmuch_message_t *msg;
262 stat = notmuch_database_index_file (db, "relative_path", NULL, &msg);
263 printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS);
271 test_expect_equal_file EXPECTED OUTPUT
273 test_begin_subtest "index file (absolute path outside mail root)"
274 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
276 notmuch_message_t *msg;
277 stat = notmuch_database_index_file (db, "/dev/zero", NULL, &msg);
278 printf ("%d\n", stat == NOTMUCH_STATUS_FILE_ERROR);
285 Error opening /dev/zero: path outside mail root
287 test_expect_equal_file EXPECTED OUTPUT
289 test_begin_subtest "remove message file with a closed db"
290 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
292 EXPECT0(notmuch_database_close (db));
293 stat = notmuch_database_remove_message (db, "01:2,");
294 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
301 A Xapian exception occurred finding/creating a directory: Database has been closed.
303 test_expect_equal_file EXPECTED OUTPUT
305 test_begin_subtest "find message by filename with a closed db"
306 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
308 notmuch_message_t *msg;
309 EXPECT0(notmuch_database_close (db));
310 stat = notmuch_database_find_message_by_filename (db, "01:2,", &msg);
311 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
318 A Xapian exception occurred finding/creating a directory: Database has been closed.
320 test_expect_equal_file EXPECTED OUTPUT
322 test_begin_subtest "Handle getting tags from closed database"
323 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
325 notmuch_tags_t *result;
326 EXPECT0(notmuch_database_close (db));
327 result = notmuch_database_get_all_tags (db);
328 printf("%d\n", result == NULL);
329 stat = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
336 A Xapian exception occurred getting tags: Database has been closed.
338 test_expect_equal_file EXPECTED OUTPUT
340 test_begin_subtest "get config from closed database"
341 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
344 EXPECT0(notmuch_database_close (db));
345 stat = notmuch_database_get_config (db, "foo", &result);
346 printf("%d\n", stat == NOTMUCH_STATUS_SUCCESS);
354 test_expect_equal_file EXPECTED OUTPUT
356 test_begin_subtest "set config in closed database"
357 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
359 EXPECT0(notmuch_database_close (db));
360 stat = notmuch_database_set_config (db, "foo", "bar");
361 printf("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
368 Error: A Xapian exception occurred setting metadata: Database has been closed
370 test_expect_equal_file EXPECTED OUTPUT
372 test_begin_subtest "get indexopts from closed database"
373 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
375 notmuch_indexopts_t *result;
376 EXPECT0(notmuch_database_close (db));
377 result = notmuch_database_get_default_indexopts (db);
378 printf("%d\n", result != NULL);
386 test_expect_equal_file EXPECTED OUTPUT
388 test_begin_subtest "get decryption policy from closed database"
389 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
391 notmuch_indexopts_t *result;
392 result = notmuch_database_get_default_indexopts (db);
393 EXPECT0(notmuch_database_close (db));
394 notmuch_decryption_policy_t policy = notmuch_indexopts_get_decrypt_policy (result);
395 printf ("%d\n", policy == NOTMUCH_DECRYPT_AUTO);
396 notmuch_indexopts_destroy (result);
397 printf ("SUCCESS\n");
406 test_expect_equal_file EXPECTED OUTPUT
408 test_begin_subtest "set decryption policy with closed database"
409 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
411 notmuch_indexopts_t *result;
412 result = notmuch_database_get_default_indexopts (db);
413 EXPECT0(notmuch_database_close (db));
414 notmuch_decryption_policy_t policy = notmuch_indexopts_get_decrypt_policy (result);
415 stat = notmuch_indexopts_set_decrypt_policy (result, policy);
416 printf("%d\n%d\n", policy == NOTMUCH_DECRYPT_AUTO, stat == NOTMUCH_STATUS_SUCCESS);
425 test_expect_equal_file EXPECTED OUTPUT