2 test_description="message property API"
4 . ./test-lib.sh || exit 1
13 #include <notmuch-test.h>
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));
21 notmuch_message_properties_destroy (list);
24 int main (int argc, char** argv)
26 notmuch_database_t *db;
27 notmuch_message_t *message = NULL;
29 notmuch_status_t stat;
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");
40 EXPECT0(notmuch_database_destroy(db));
44 test_begin_subtest "notmuch_message_{add,get,remove}_property"
45 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
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));
55 EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
56 printf("testkey2 = %s\n", val);
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);
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);
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);
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);
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
90 test_expect_equal_file EXPECTED OUTPUT
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);
101 test_expect_equal_file EXPECTED OUTPUT
103 test_begin_subtest "notmuch_message_get_properties: empty list"
104 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
106 notmuch_message_properties_t *list;
107 list = notmuch_message_get_properties (message, "nonexistent", TRUE);
108 printf("valid = %d\n", notmuch_message_properties_valid (list));
109 notmuch_message_properties_destroy (list);
112 cat <<'EOF' >EXPECTED
117 test_expect_equal_file EXPECTED OUTPUT
119 test_begin_subtest "notmuch_message_properties: one value"
120 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
121 print_properties (message, "testkey1", TRUE);
123 cat <<'EOF' >EXPECTED
128 test_expect_equal_file EXPECTED OUTPUT
130 test_begin_subtest "notmuch_message_properties: multiple values"
131 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
132 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
133 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
134 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
135 print_properties (message, "testkey1", TRUE);
137 cat <<'EOF' >EXPECTED
145 test_expect_equal_file EXPECTED OUTPUT
147 test_begin_subtest "notmuch_message_properties: prefix"
148 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
149 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
150 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
151 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
152 print_properties (message, "testkey", FALSE);
154 cat <<'EOF' >EXPECTED
165 test_expect_equal_file EXPECTED OUTPUT
167 test_begin_subtest "notmuch_message_properties: modify during iteration"
168 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
170 const char *keys[1000] = {NULL};
171 const char *vals[1000] = {NULL};
172 notmuch_message_properties_t *properties;
175 for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
176 notmuch_message_properties_valid (properties);
177 notmuch_message_properties_move_to_next (properties), i++)
179 const char *key, *value;
181 keys[i]=talloc_strdup(message,
182 notmuch_message_properties_key (properties));
183 vals[i]=talloc_strdup(message,
184 notmuch_message_properties_value (properties));
186 EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
189 print_properties (message, "", FALSE);
191 for (i = 0; keys[i] && vals[i]; i++) {
192 EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
196 cat <<'EOF' >EXPECTED
200 test_expect_equal_file EXPECTED OUTPUT
202 test_begin_subtest "dump message properties"
203 cat <<EOF > PROPERTIES
204 #= 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
206 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
207 EXPECT0(notmuch_message_add_property (message, "fancy key with áccènts", "import value with ="));
209 notmuch dump | grep '^#=' > OUTPUT
210 test_expect_equal_file PROPERTIES OUTPUT
213 test_begin_subtest "restore missing message property (single line)"
214 notmuch dump | grep '^#=' > BEFORE1
215 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
216 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
218 notmuch restore < BEFORE1
219 notmuch dump | grep '^#=' > OUTPUT
220 test_expect_equal_file PROPERTIES OUTPUT
223 test_begin_subtest "restore missing message property (full dump)"
224 notmuch dump > BEFORE2
225 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
226 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
228 notmuch restore < BEFORE2
229 notmuch dump | grep '^#=' > OUTPUT
230 test_expect_equal_file PROPERTIES OUTPUT
232 test_begin_subtest "restore clear extra message property"
233 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
234 EXPECT0(notmuch_message_add_property (message, "testkey1", "charles"));
236 notmuch restore < BEFORE2
237 notmuch dump | grep '^#=' > OUTPUT
238 test_expect_equal_file PROPERTIES OUTPUT
240 test_begin_subtest "test 'property:' queries: empty"
241 notmuch search property:testkey1=charles > OUTPUT
242 test_expect_equal_file /dev/null OUTPUT
244 test_begin_subtest "test 'property:' queries: single message"
245 notmuch search --output=messages property:testkey1=alice > OUTPUT
247 id:4EFC743A.3060609@april.org
249 test_expect_equal_file EXPECTED OUTPUT