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