]> git.cworth.org Git - notmuch/blob - test/T610-message-property.sh
replace references to freenode with references to libera
[notmuch] / test / T610-message-property.sh
1 #!/usr/bin/env bash
2 test_description="message property API"
3
4 . $(dirname "$0")/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-existent 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_remove_all_properties"
93 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
94 EXPECT0(notmuch_message_remove_all_properties (message, NULL));
95 print_properties (message, "", FALSE);
96 EOF
97 cat <<'EOF' >EXPECTED
98 == stdout ==
99 == stderr ==
100 EOF
101 test_expect_equal_file EXPECTED OUTPUT
102
103 test_begin_subtest "testing string map binary search (via message properties)"
104 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
105 {
106    char *keys[] = {"a", "b", "c", "d", "e", NULL};
107    for (int i=0; keys[i]; i++)
108        EXPECT0(notmuch_message_add_property (message, keys[i], keys[i]));
109
110    for (int i=0; keys[i]; i++) {
111       EXPECT0(notmuch_message_get_property (message, keys[i], &val));
112       printf("%s = %s\n", keys[i], val);
113    }
114
115    for (int i=0; keys[i]; i++) {
116       EXPECT0(notmuch_message_remove_property (message, keys[i], keys[i]));
117       EXPECT0(notmuch_message_get_property (message, keys[i], &val));
118       printf("%s = %s\n", keys[i], val == NULL ? "NULL" : val);
119    }
120 }
121 EOF
122 cat <<EOF > EXPECTED
123 == stdout ==
124 a = a
125 b = b
126 c = c
127 d = d
128 e = e
129 a = NULL
130 b = NULL
131 c = NULL
132 d = NULL
133 e = NULL
134 == stderr ==
135 EOF
136 test_expect_equal_file EXPECTED OUTPUT
137
138 test_begin_subtest "notmuch_message_get_properties: empty list"
139 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
140 {
141    notmuch_message_properties_t *list;
142    list = notmuch_message_get_properties (message, "nonexistent", TRUE);
143    printf("valid = %d\n", notmuch_message_properties_valid (list));
144    notmuch_message_properties_destroy (list);
145 }
146 EOF
147 cat <<'EOF' >EXPECTED
148 == stdout ==
149 valid = 0
150 == stderr ==
151 EOF
152 test_expect_equal_file EXPECTED OUTPUT
153
154 test_begin_subtest "notmuch_message_properties: one value"
155 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
156 print_properties (message, "testkey1", TRUE);
157 EOF
158 cat <<'EOF' >EXPECTED
159 == stdout ==
160 testvalue1
161 == stderr ==
162 EOF
163 test_expect_equal_file EXPECTED OUTPUT
164
165 test_begin_subtest "notmuch_message_properties: multiple values"
166 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
167 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
168 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
169 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
170 print_properties (message, "testkey1", TRUE);
171 EOF
172 cat <<'EOF' >EXPECTED
173 == stdout ==
174 alice
175 bob
176 testvalue1
177 testvalue2
178 == stderr ==
179 EOF
180 test_expect_equal_file EXPECTED OUTPUT
181
182 test_begin_subtest "notmuch_message_properties: prefix"
183 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
184 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
185 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
186 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
187 print_properties (message, "testkey", FALSE);
188 EOF
189 # expected: 4 values for testkey1, 3 values for testkey3
190 # they are not guaranteed to be sorted, so sort them, leaving the first
191 # line '== stdout ==' and the end ('== stderr ==' and whatever error
192 # may have been printed) alone
193 mv OUTPUT unsorted_OUTPUT
194 awk ' NR == 1 { print; next } \
195       NR < 6  { print | "sort"; next } \
196       NR == 6 { close("sort") } \
197       NR < 9  { print | "sort"; next } \
198       NR == 9 { close("sort") } \
199       { print }' unsorted_OUTPUT > OUTPUT
200 rm unsorted_OUTPUT
201 cat <<'EOF' >EXPECTED
202 == stdout ==
203 alice
204 bob
205 testvalue1
206 testvalue2
207 alice3
208 bob3
209 testvalue3
210 == stderr ==
211 EOF
212 test_expect_equal_file EXPECTED OUTPUT
213
214 test_begin_subtest "notmuch_message_properties: modify during iteration"
215 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
216 {
217     const char *keys[1000] = {NULL};
218     const char *vals[1000] = {NULL};
219     notmuch_message_properties_t *properties;
220     int i;
221
222     for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
223          notmuch_message_properties_valid (properties);
224          notmuch_message_properties_move_to_next (properties), i++)
225     {
226         const char *key, *value;
227
228         keys[i]=talloc_strdup(message,
229                     notmuch_message_properties_key (properties));
230         vals[i]=talloc_strdup(message,
231                     notmuch_message_properties_value (properties));
232
233         EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
234     }
235
236     print_properties (message, "", FALSE);
237
238     for (i = 0; keys[i] && vals[i]; i++) {
239         EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
240     }
241 }
242 EOF
243 cat <<'EOF' >EXPECTED
244 == stdout ==
245 == stderr ==
246 EOF
247 test_expect_equal_file EXPECTED OUTPUT
248
249 test_begin_subtest "dump message properties"
250 cat <<EOF > PROPERTIES
251 #= 4EFC743A.3060609@april.org fancy%20key%20with%20%c3%a1cc%c3%a8nts=import%20value%20with%20= testkey1=alice testkey1=bob testkey1=testvalue1 testkey1=testvalue2 testkey3=alice3 testkey3=bob3 testkey3=testvalue3
252 EOF
253 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
254 EXPECT0(notmuch_message_add_property (message, "fancy key with áccènts", "import value with ="));
255 EOF
256 notmuch dump | grep '^#=' > OUTPUT
257 test_expect_equal_file PROPERTIES OUTPUT
258
259 test_begin_subtest "dump _only_ message properties"
260 cat <<EOF > EXPECTED
261 #notmuch-dump batch-tag:3 properties
262 #= 4EFC743A.3060609@april.org fancy%20key%20with%20%c3%a1cc%c3%a8nts=import%20value%20with%20= testkey1=alice testkey1=bob testkey1=testvalue1 testkey1=testvalue2 testkey3=alice3 testkey3=bob3 testkey3=testvalue3
263 EOF
264 notmuch dump --include=properties > OUTPUT
265 test_expect_equal_file EXPECTED OUTPUT
266
267
268 test_begin_subtest "restore missing message property (single line)"
269 notmuch dump | grep '^#=' > BEFORE1
270 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
271 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
272 EOF
273 notmuch restore < BEFORE1
274 notmuch dump | grep '^#=' > OUTPUT
275 test_expect_equal_file PROPERTIES OUTPUT
276
277
278 test_begin_subtest "restore missing message property (full dump)"
279 notmuch dump > BEFORE2
280 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
281 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
282 EOF
283 notmuch restore < BEFORE2
284 notmuch dump | grep '^#=' > OUTPUT
285 test_expect_equal_file PROPERTIES OUTPUT
286
287 test_begin_subtest "restore clear extra message property"
288 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
289 EXPECT0(notmuch_message_add_property (message, "testkey1", "charles"));
290 EOF
291 notmuch restore < BEFORE2
292 notmuch dump | grep '^#=' > OUTPUT
293 test_expect_equal_file PROPERTIES OUTPUT
294
295 test_begin_subtest "test 'property:' queries: empty"
296 notmuch search property:testkey1=charles > OUTPUT
297 test_expect_equal_file /dev/null OUTPUT
298
299 test_begin_subtest "test 'property:' queries: single message"
300 notmuch search --output=messages property:testkey1=alice > OUTPUT
301 cat <<EOF >EXPECTED
302 id:4EFC743A.3060609@april.org
303 EOF
304 test_expect_equal_file EXPECTED OUTPUT
305
306 test_begin_subtest "msg.get_property (python)"
307 test_python <<'EOF'
308 import notmuch
309 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
310 msg = db.find_message("4EFC743A.3060609@april.org")
311 print("testkey1 = {0}".format(msg.get_property("testkey1")))
312 print("testkey3 = {0}".format(msg.get_property("testkey3")))
313 EOF
314 cat <<'EOF' > EXPECTED
315 testkey1 = alice
316 testkey3 = alice3
317 EOF
318 test_expect_equal_file EXPECTED OUTPUT
319
320 test_begin_subtest "msg.get_properties (python)"
321 test_python <<'EOF'
322 import notmuch
323 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
324 msg = db.find_message("4EFC743A.3060609@april.org")
325 for (key,val) in msg.get_properties("testkey1"):
326         print("{0} = {1}".format(key,val))
327 EOF
328 cat <<'EOF' > EXPECTED
329 testkey1 = alice
330 testkey1 = bob
331 testkey1 = testvalue1
332 testkey1 = testvalue2
333 EOF
334 test_expect_equal_file EXPECTED OUTPUT
335
336 test_begin_subtest "msg.get_properties (python, prefix)"
337 test_python <<'EOF'
338 import notmuch
339 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
340 msg = db.find_message("4EFC743A.3060609@april.org")
341 for (key,val) in msg.get_properties("testkey"):
342         print("{0} = {1}".format(key,val))
343 EOF
344 cat <<'EOF' > EXPECTED
345 testkey1 = alice
346 testkey1 = bob
347 testkey1 = testvalue1
348 testkey1 = testvalue2
349 testkey3 = alice3
350 testkey3 = bob3
351 testkey3 = testvalue3
352 EOF
353 test_expect_equal_file EXPECTED OUTPUT
354
355 test_begin_subtest "msg.get_properties (python, exact)"
356 test_python <<'EOF'
357 import notmuch
358 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
359 msg = db.find_message("4EFC743A.3060609@april.org")
360 for (key,val) in msg.get_properties("testkey",True):
361         print("{0} = {1}".format(key,val))
362 EOF
363 test_expect_equal_file /dev/null OUTPUT
364
365 test_done