]> git.notmuchmail.org Git - notmuch/blob - test/T720-lib-lifetime.sh
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / test / T720-lib-lifetime.sh
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) 2018 rhn
4 #
5
6
7 test_description="Lifetime constraints for library"
8
9 . $(dirname "$0")/test-lib.sh || exit 1
10
11 add_email_corpus threading
12
13 test_begin_subtest "building database"
14 test_expect_success "NOTMUCH_NEW"
15
16 test_begin_subtest "Message outlives parent Messages from replies"
17
18 test_C ${MAIL_DIR} <<'EOF'
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <notmuch.h>
22 int main (int argc, char** argv)
23 {
24    notmuch_database_t *db;
25    notmuch_status_t stat;
26    char* msg = NULL;
27
28    stat = notmuch_database_open_with_config (argv[1],
29                                              NOTMUCH_DATABASE_MODE_READ_ONLY,
30                                              "", NULL, &db, &msg);
31    if (msg) fputs (msg, stderr);
32
33    if (stat != NOTMUCH_STATUS_SUCCESS) {
34      fprintf (stderr, "error opening database: %d\n", stat);
35      exit (1);
36    }
37
38    notmuch_query_t *query = notmuch_query_create (db, "id:B00-root@example.org");
39    notmuch_threads_t *threads;
40
41    stat = notmuch_query_search_threads (query, &threads);
42    if (stat != NOTMUCH_STATUS_SUCCESS) {
43      fprintf (stderr, "error querying threads: %d\n", stat);
44      exit (1);
45    }
46
47    if (!notmuch_threads_valid (threads)) {
48      fprintf (stderr, "invalid threads");
49      exit (1);
50    }
51
52    notmuch_thread_t *thread = notmuch_threads_get (threads);
53    notmuch_messages_t *messages = notmuch_thread_get_messages (thread);
54
55    if (!notmuch_messages_valid (messages)) {
56      fprintf (stderr, "invalid messages");
57      exit (1);
58    }
59
60    notmuch_message_t *message = notmuch_messages_get (messages);
61    notmuch_messages_t *replies = notmuch_message_get_replies (message);
62    if (!notmuch_messages_valid (replies)) {
63      fprintf (stderr, "invalid replies");
64      exit (1);
65    }
66
67    notmuch_message_t *reply = notmuch_messages_get (replies);
68
69    notmuch_messages_destroy (replies); // the reply should not get destroyed here
70    notmuch_message_destroy (message);
71    notmuch_messages_destroy (messages); // nor here
72
73    const char *mid = notmuch_message_get_message_id (reply); // should not crash when accessing
74    fprintf (stdout, "Reply id: %s\n", mid);
75    notmuch_message_destroy (reply);
76    notmuch_thread_destroy (thread); // this destroys the reply
77    notmuch_threads_destroy (threads);
78    notmuch_query_destroy (query);
79    notmuch_database_destroy (db);
80 }
81 EOF
82 cat <<'EOF' >EXPECTED
83 == stdout ==
84 Reply id: B01-child@example.org
85 == stderr ==
86 EOF
87 test_expect_equal_file EXPECTED OUTPUT
88
89 test_done