]> git.notmuchmail.org Git - notmuch/blob - test/T610-message-property.sh
b5ddb7a4d8aa3def2c258665c69614551da965fc
[notmuch] / test / T610-message-property.sh
1 #!/usr/bin/env bash
2 test_description="message property API"
3
4 . ./test-lib.sh || exit 1
5
6 add_email_corpus
7
8 cat <<EOF > c_head
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <talloc.h>
13 #include <notmuch-test.h>
14
15 void print_properties (notmuch_message_t *message, const char *prefix, notmuch_bool_t exact) {
16     notmuch_message_properties_t *list;
17     for (list = notmuch_message_get_properties (message, prefix, exact);
18          notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
19        printf("%s\n", notmuch_message_properties_value(list));
20     }
21     notmuch_message_properties_destroy (list);
22 }
23
24 int main (int argc, char** argv)
25 {
26    notmuch_database_t *db;
27    notmuch_message_t *message = NULL;
28    const char *val;
29    notmuch_status_t stat;
30
31    EXPECT0(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));
32    EXPECT0(notmuch_database_find_message(db, "4EFC743A.3060609@april.org", &message));
33    if (message == NULL) {
34         fprintf (stderr, "unable to find message");
35         exit (1);
36    }
37 EOF
38
39 cat <<EOF > c_tail
40    EXPECT0(notmuch_database_destroy(db));
41 }
42 EOF
43
44 test_begin_subtest "notmuch_message_{add,get,remove}_property"
45 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
46 {
47    EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue1"));
48    EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
49    printf("testkey1[1] = %s\n", val);
50    EXPECT0(notmuch_message_add_property (message, "testkey2", "this value has spaces and = sign"));
51    EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
52    printf("testkey1[2] = %s\n", val);
53    EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
54
55    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
56    printf("testkey2 = %s\n", val);
57
58    /* Add second value for key */
59    EXPECT0(notmuch_message_add_property (message, "testkey2", "zztestvalue3"));
60    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
61    printf("testkey2 = %s\n", val);
62
63    /* remove first value for key */
64    EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));
65    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
66    printf("testkey2 = %s\n", val);
67
68    /* remove non-existant value for key */
69    EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));
70    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
71    printf("testkey2 = %s\n", val);
72
73    /* remove only value for key */
74    EXPECT0(notmuch_message_remove_property (message, "testkey2", "zztestvalue3"));
75    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
76    printf("testkey2 = %s\n", val == NULL ? "NULL" : val);
77 }
78 EOF
79 cat <<'EOF' >EXPECTED
80 == stdout ==
81 testkey1[1] = testvalue1
82 testkey1[2] = testvalue1
83 testkey2 = this value has spaces and = sign
84 testkey2 = this value has spaces and = sign
85 testkey2 = zztestvalue3
86 testkey2 = zztestvalue3
87 testkey2 = NULL
88 == stderr ==
89 EOF
90 test_expect_equal_file EXPECTED OUTPUT
91
92 test_begin_subtest "notmuch_message_get_properties: empty list"
93 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
94 {
95    notmuch_message_properties_t *list;
96    list = notmuch_message_get_properties (message, "nonexistent", TRUE);
97    printf("valid = %d\n", notmuch_message_properties_valid (list));
98    notmuch_message_properties_destroy (list);
99 }
100 EOF
101 cat <<'EOF' >EXPECTED
102 == stdout ==
103 valid = 0
104 == stderr ==
105 EOF
106 test_expect_equal_file EXPECTED OUTPUT
107
108 test_begin_subtest "notmuch_message_properties: one value"
109 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
110 print_properties (message, "testkey1", TRUE);
111 EOF
112 cat <<'EOF' >EXPECTED
113 == stdout ==
114 testvalue1
115 == stderr ==
116 EOF
117 test_expect_equal_file EXPECTED OUTPUT
118
119 test_begin_subtest "notmuch_message_properties: multiple values"
120 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
121 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
122 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
123 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
124 print_properties (message, "testkey1", TRUE);
125 EOF
126 cat <<'EOF' >EXPECTED
127 == stdout ==
128 alice
129 bob
130 testvalue1
131 testvalue2
132 == stderr ==
133 EOF
134 test_expect_equal_file EXPECTED OUTPUT
135
136 test_begin_subtest "notmuch_message_properties: prefix"
137 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
138 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
139 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
140 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
141 print_properties (message, "testkey", FALSE);
142 EOF
143 cat <<'EOF' >EXPECTED
144 == stdout ==
145 alice
146 bob
147 testvalue1
148 testvalue2
149 alice3
150 bob3
151 testvalue3
152 == stderr ==
153 EOF
154 test_expect_equal_file EXPECTED OUTPUT
155
156 test_begin_subtest "notmuch_message_properties: modify during iteration"
157 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
158 {
159     const char *keys[1000] = {NULL};
160     const char *vals[1000] = {NULL};
161     notmuch_message_properties_t *properties;
162     int i;
163
164     for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
165          notmuch_message_properties_valid (properties);
166          notmuch_message_properties_move_to_next (properties), i++)
167     {
168         const char *key, *value;
169
170         keys[i]=talloc_strdup(message,
171                     notmuch_message_properties_key (properties));
172         vals[i]=talloc_strdup(message,
173                     notmuch_message_properties_value (properties));
174
175         EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
176     }
177
178     print_properties (message, "", FALSE);
179
180     for (i = 0; keys[i] && vals[i]; i++) {
181         EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
182     }
183 }
184 EOF
185 cat <<'EOF' >EXPECTED
186 == stdout ==
187 == stderr ==
188 EOF
189 test_expect_equal_file EXPECTED OUTPUT
190
191 test_done