]> git.cworth.org Git - notmuch/blob - test/T610-message-property.sh
test: reveal notmuch_message_remove_all_properties as broken
[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 <notmuch-test.h>
10
11 void print_properties (notmuch_message_t *message, const char *prefix, notmuch_bool_t exact) {
12     notmuch_message_properties_t *list;
13     for (list = notmuch_message_get_properties (message, prefix, exact);
14          notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
15        printf("%s = %s\n", notmuch_message_properties_key(list), notmuch_message_properties_value(list));
16     }
17     notmuch_message_properties_destroy (list);
18 }
19
20 int main (int argc, char** argv)
21 {
22    notmuch_database_t *db;
23    notmuch_message_t *message = NULL;
24    const char *val;
25    notmuch_status_t stat;
26    char* msg = NULL;
27
28    EXPECT0(notmuch_database_open_with_config (argv[1],
29                                               NOTMUCH_DATABASE_MODE_READ_WRITE,
30                                               "", NULL, &db, &msg));
31    if (msg) fputs (msg, stderr);
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 "testing string map binary search (via message properties)"
93 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
94 {
95    char *keys[] = {"a", "b", "c", "d", "e", NULL};
96    for (int i=0; keys[i]; i++)
97        EXPECT0(notmuch_message_add_property (message, keys[i], keys[i]));
98
99    for (int i=0; keys[i]; i++) {
100       EXPECT0(notmuch_message_get_property (message, keys[i], &val));
101       printf("%s = %s\n", keys[i], val);
102    }
103
104    for (int i=0; keys[i]; i++) {
105       EXPECT0(notmuch_message_remove_property (message, keys[i], keys[i]));
106       EXPECT0(notmuch_message_get_property (message, keys[i], &val));
107       printf("%s = %s\n", keys[i], val == NULL ? "NULL" : val);
108    }
109 }
110 EOF
111 cat <<EOF > EXPECTED
112 == stdout ==
113 a = a
114 b = b
115 c = c
116 d = d
117 e = e
118 a = NULL
119 b = NULL
120 c = NULL
121 d = NULL
122 e = NULL
123 == stderr ==
124 EOF
125 test_expect_equal_file EXPECTED OUTPUT
126
127 test_begin_subtest "notmuch_message_get_properties: empty list"
128 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
129 {
130    notmuch_message_properties_t *list;
131    list = notmuch_message_get_properties (message, "nonexistent", TRUE);
132    printf("valid = %d\n", notmuch_message_properties_valid (list));
133    notmuch_message_properties_destroy (list);
134 }
135 EOF
136 cat <<'EOF' >EXPECTED
137 == stdout ==
138 valid = 0
139 == stderr ==
140 EOF
141 test_expect_equal_file EXPECTED OUTPUT
142
143 test_begin_subtest "notmuch_message_properties: one value"
144 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
145 print_properties (message, "testkey1", TRUE);
146 EOF
147 cat <<'EOF' >EXPECTED
148 == stdout ==
149 testkey1 = testvalue1
150 == stderr ==
151 EOF
152 test_expect_equal_file EXPECTED OUTPUT
153
154 test_begin_subtest "notmuch_message_remove_all_properties"
155 test_subtest_known_broken
156 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
157 EXPECT0(notmuch_message_remove_all_properties (message, NULL));
158 EXPECT0(notmuch_database_destroy(db));
159 EXPECT0(notmuch_database_open_with_config (argv[1],
160                                            NOTMUCH_DATABASE_MODE_READ_WRITE,
161                                            "", NULL, &db, &msg));
162 if (msg) fputs (msg, stderr);
163 EXPECT0(notmuch_database_find_message(db, "4EFC743A.3060609@april.org", &message));
164 if (message == NULL) {
165   fprintf (stderr, "unable to find message");
166   exit (1);
167 }
168 print_properties (message, "", FALSE);
169 EOF
170 cat <<'EOF' >EXPECTED
171 == stdout ==
172 == stderr ==
173 EOF
174 test_expect_equal_file EXPECTED OUTPUT
175
176 test_begin_subtest "notmuch_message_properties: multiple values"
177 test_subtest_known_broken
178 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
179 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
180 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
181 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
182 print_properties (message, "testkey1", TRUE);
183 EOF
184 cat <<'EOF' >EXPECTED
185 == stdout ==
186 testkey1 = alice
187 testkey1 = bob
188 testkey1 = testvalue2
189 == stderr ==
190 EOF
191 test_expect_equal_file EXPECTED OUTPUT
192
193 test_begin_subtest "notmuch_message_properties: prefix"
194 test_subtest_known_broken
195 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
196 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
197 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
198 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
199 print_properties (message, "testkey", FALSE);
200 EOF
201 cat <<'EOF' >EXPECTED
202 == stdout ==
203 testkey1 = alice
204 testkey1 = bob
205 testkey1 = testvalue2
206 testkey3 = alice3
207 testkey3 = bob3
208 testkey3 = testvalue3
209 == stderr ==
210 EOF
211 test_expect_equal_file EXPECTED OUTPUT
212
213 test_begin_subtest "notmuch_message_properties: modify during iteration"
214 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
215 {
216     const char *keys[1000] = {NULL};
217     const char *vals[1000] = {NULL};
218     notmuch_message_properties_t *properties;
219     int i;
220
221     for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
222          notmuch_message_properties_valid (properties);
223          notmuch_message_properties_move_to_next (properties), i++)
224     {
225         const char *key, *value;
226
227         keys[i]=talloc_strdup(message,
228                     notmuch_message_properties_key (properties));
229         vals[i]=talloc_strdup(message,
230                     notmuch_message_properties_value (properties));
231
232         EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
233     }
234
235     print_properties (message, "", FALSE);
236
237     for (i = 0; keys[i] && vals[i]; i++) {
238         EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
239     }
240 }
241 EOF
242 cat <<'EOF' >EXPECTED
243 == stdout ==
244 == stderr ==
245 EOF
246 test_expect_equal_file EXPECTED OUTPUT
247
248 test_begin_subtest "dump message properties"
249 test_subtest_known_broken
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=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 test_subtest_known_broken
261 cat <<EOF > EXPECTED
262 #notmuch-dump batch-tag:3 properties
263 #= 4EFC743A.3060609@april.org fancy%20key%20with%20%c3%a1cc%c3%a8nts=import%20value%20with%20= testkey1=alice testkey1=bob testkey1=testvalue2 testkey3=alice3 testkey3=bob3 testkey3=testvalue3
264 EOF
265 notmuch dump --include=properties > OUTPUT
266 test_expect_equal_file EXPECTED OUTPUT
267
268
269 test_begin_subtest "restore missing message property (single line)"
270 test_subtest_known_broken
271 notmuch dump | grep '^#=' > BEFORE1
272 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
273 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
274 EOF
275 notmuch restore < BEFORE1
276 notmuch dump | grep '^#=' > OUTPUT
277 test_expect_equal_file PROPERTIES OUTPUT
278
279
280 test_begin_subtest "restore missing message property (full dump)"
281 test_subtest_known_broken
282 notmuch dump > BEFORE2
283 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
284 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
285 EOF
286 notmuch restore < BEFORE2
287 notmuch dump | grep '^#=' > OUTPUT
288 test_expect_equal_file PROPERTIES OUTPUT
289
290 test_begin_subtest "restore clear extra message property"
291 test_subtest_known_broken
292 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
293 EXPECT0(notmuch_message_add_property (message, "testkey1", "charles"));
294 EOF
295 notmuch restore < BEFORE2
296 notmuch dump | grep '^#=' > OUTPUT
297 test_expect_equal_file PROPERTIES OUTPUT
298
299 test_begin_subtest "test 'property:' queries: empty"
300 notmuch search property:testkey1=charles > OUTPUT
301 test_expect_equal_file /dev/null OUTPUT
302
303 test_begin_subtest "test 'property:' queries: single message"
304 notmuch search --output=messages property:testkey1=alice > OUTPUT
305 cat <<EOF >EXPECTED
306 id:4EFC743A.3060609@april.org
307 EOF
308 test_expect_equal_file EXPECTED OUTPUT
309
310 test_begin_subtest "msg.get_property (python)"
311 test_python <<'EOF'
312 import notmuch
313 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
314 msg = db.find_message("4EFC743A.3060609@april.org")
315 print("testkey1 = {0}".format(msg.get_property("testkey1")))
316 print("testkey3 = {0}".format(msg.get_property("testkey3")))
317 EOF
318 cat <<'EOF' > EXPECTED
319 testkey1 = alice
320 testkey3 = alice3
321 EOF
322 test_expect_equal_file EXPECTED OUTPUT
323
324 test_begin_subtest "msg.get_properties (python)"
325 test_subtest_known_broken
326 test_python <<'EOF'
327 import notmuch
328 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
329 msg = db.find_message("4EFC743A.3060609@april.org")
330 for (key,val) in msg.get_properties("testkey1"):
331         print("{0} = {1}".format(key,val))
332 EOF
333 cat <<'EOF' > EXPECTED
334 testkey1 = alice
335 testkey1 = bob
336 testkey1 = testvalue2
337 EOF
338 test_expect_equal_file EXPECTED OUTPUT
339
340 test_begin_subtest "msg.get_properties (python, prefix)"
341 test_subtest_known_broken
342 test_python <<'EOF'
343 import notmuch
344 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
345 msg = db.find_message("4EFC743A.3060609@april.org")
346 for (key,val) in msg.get_properties("testkey"):
347         print("{0} = {1}".format(key,val))
348 EOF
349 cat <<'EOF' > EXPECTED
350 testkey1 = alice
351 testkey1 = bob
352 testkey1 = testvalue2
353 testkey3 = alice3
354 testkey3 = bob3
355 testkey3 = testvalue3
356 EOF
357 test_expect_equal_file EXPECTED OUTPUT
358
359 test_begin_subtest "msg.get_properties (python, exact)"
360 test_python <<'EOF'
361 import notmuch
362 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
363 msg = db.find_message("4EFC743A.3060609@april.org")
364 for (key,val) in msg.get_properties("testkey",True):
365         print("{0} = {1}".format(key,val))
366 EOF
367 test_expect_equal_file /dev/null OUTPUT
368
369 test_begin_subtest "edit property on removed message without uncaught exception"
370 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
371 EXPECT0(notmuch_database_remove_message (db, notmuch_message_get_filename (message)));
372 stat = notmuch_message_remove_property (message, "example", "example");
373 if (stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION)
374     fprintf (stderr, "unable to remove properties on message");
375 EOF
376 cat <<'EOF' >EXPECTED
377 == stdout ==
378 == stderr ==
379 unable to remove properties on message
380 EOF
381 test_expect_equal_file EXPECTED OUTPUT
382
383 add_email_corpus
384
385 test_begin_subtest "remove all properties on removed message without uncaught exception"
386 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
387 EXPECT0(notmuch_database_remove_message (db, notmuch_message_get_filename (message)));
388 stat = notmuch_message_remove_all_properties_with_prefix (message, "");
389 if (stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION)
390     fprintf (stderr, "unable to remove properties on message");
391 EOF
392 cat <<'EOF' >EXPECTED
393 == stdout ==
394 == stderr ==
395 unable to remove properties on message
396 EOF
397 test_expect_equal_file EXPECTED OUTPUT
398
399 test_done