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