bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen#include "lib.h"
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen#include "str.h"
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen#include "test-common.h"
fccd110b494a7e31f23d31d9e3bc3e986c9bb1a8Timo Sirainen#include "mail-storage-private.h"
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen#include "mail-search-build.h"
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen#include "mail-search-parser.h"
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen#include "mail-search.h"
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainenstatic const struct {
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen const char *input;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen const char *output;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen} tests[] = {
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen { "ALL", "ALL" },
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen { "NOT ALL", "NOT ALL" },
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen { "ALL NOT ALL", "NOT ALL" },
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen { "ALL NOT ALL TEXT foo", "NOT ALL" },
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen { "OR ALL NOT ALL", "ALL" },
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen { "OR ALL OR NOT ALL TEXT foo", "ALL" },
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen { "OR ALL OR TEXT foo TEXT bar", "ALL" },
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen { "OR TEXT FOO ( ALL NOT ALL )", "TEXT FOO" },
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen { "TEXT FOO OR ALL NOT ALL", "TEXT FOO" },
c9099b08b3cae8a849098ca776b4363c6d5f5f36Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "TEXT foo", "TEXT foo" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "( TEXT foo )", "TEXT foo" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "( ( TEXT foo ) )", "TEXT foo" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "( ( TEXT foo ) ( TEXT bar ) )", "TEXT foo TEXT bar" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT foo ) ( TEXT bar )", "OR TEXT foo TEXT bar" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "OR ( TEXT foo ) OR ( TEXT bar ) ( TEXT baz )",
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen "OR TEXT foo OR TEXT bar TEXT baz" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "OR ( ( TEXT foo TEXT foo2 ) ) ( ( TEXT bar ( TEXT baz ) ) )",
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen "OR (TEXT foo TEXT foo2) (TEXT bar TEXT baz)" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "NOT ( TEXT foo )", "NOT TEXT foo" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "NOT ( NOT ( TEXT foo ) )", "TEXT foo" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "NOT OR ( TEXT foo ) ( TEXT bar )", "NOT TEXT foo NOT TEXT bar" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "NOT ( OR ( TEXT foo ) ( TEXT bar ) )", "NOT TEXT foo NOT TEXT bar" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "NOT ( TEXT foo TEXT bar )", "OR NOT TEXT foo NOT TEXT bar" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "ANSWERED FLAGGED SEEN", "(ANSWERED FLAGGED SEEN)" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( ANSWERED FLAGGED SEEN ) DRAFT", "OR (ANSWERED FLAGGED SEEN) DRAFT" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen { "ANSWERED TEXT foo FLAGGED SEEN", "(ANSWERED FLAGGED SEEN) TEXT foo" },
fb79b36eb34532dbe67caf99eefe3660b8c841e0Timo Sirainen { "NOT ( ANSWERED FLAGGED SEEN )", "NOT (ANSWERED FLAGGED SEEN)" },
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen { "OR NOT ANSWERED OR NOT FLAGGED NOT SEEN", "NOT (ANSWERED FLAGGED SEEN)" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR NOT ANSWERED OR NOT FLAGGED SEEN", "OR NOT (ANSWERED FLAGGED) SEEN" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR NOT ANSWERED OR FLAGGED NOT SEEN", "OR NOT (ANSWERED SEEN) FLAGGED" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "NOT ANSWERED OR FLAGGED NOT SEEN", "NOT ANSWERED OR FLAGGED NOT SEEN" },
47daf6e810a4c2dd52640092092900dbcb12f265Timo Sirainen { "NOT ANSWERED OR NOT FLAGGED NOT SEEN", "NOT ANSWERED NOT (FLAGGED SEEN)" },
92f5ea24e989266539e97c6fe59ede0565aec6fdTimo Sirainen { "ANSWERED NOT FLAGGED SEEN NOT DRAFT", "(ANSWERED SEEN) NOT FLAGGED NOT DRAFT" },
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen { "OR NOT ANSWERED NOT SEEN", "NOT (ANSWERED SEEN)" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR NOT ANSWERED OR NOT SEEN TEXT foo", "OR NOT (ANSWERED SEEN) TEXT foo" },
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
cc23ad7b8ab96d93d5ab5139c431fcdd8d9e1d72Timo Sirainen { "ANSWERED ANSWERED", "ANSWERED" },
cc23ad7b8ab96d93d5ab5139c431fcdd8d9e1d72Timo Sirainen { "ANSWERED NOT ANSWERED", "NOT ALL" },
cc23ad7b8ab96d93d5ab5139c431fcdd8d9e1d72Timo Sirainen { "ANSWERED ANSWERED NOT ANSWERED", "NOT ALL" },
cc23ad7b8ab96d93d5ab5139c431fcdd8d9e1d72Timo Sirainen { "ANSWERED NOT ANSWERED ANSWERED NOT ANSWERED", "NOT ALL" },
cc23ad7b8ab96d93d5ab5139c431fcdd8d9e1d72Timo Sirainen { "NOT ANSWERED NOT ANSWERED", "NOT ANSWERED" },
cc23ad7b8ab96d93d5ab5139c431fcdd8d9e1d72Timo Sirainen { "NOT SEEN NOT ANSWERED NOT ANSWERED", "NOT SEEN NOT ANSWERED" },
cc23ad7b8ab96d93d5ab5139c431fcdd8d9e1d72Timo Sirainen { "OR NOT SEEN OR NOT ANSWERED NOT ANSWERED", "NOT (ANSWERED SEEN)" },
cc23ad7b8ab96d93d5ab5139c431fcdd8d9e1d72Timo Sirainen
1c0020171b04d14adc4966ed963361abc9a86787Timo Sirainen { "KEYWORD foo", "KEYWORD foo" },
1c0020171b04d14adc4966ed963361abc9a86787Timo Sirainen { "KEYWORD foo KEYWORD bar", "KEYWORD foo KEYWORD bar" },
1c0020171b04d14adc4966ed963361abc9a86787Timo Sirainen { "NOT KEYWORD foo", "NOT KEYWORD foo" },
1c0020171b04d14adc4966ed963361abc9a86787Timo Sirainen { "NOT KEYWORD foo NOT KEYWORD bar", "NOT KEYWORD foo NOT KEYWORD bar" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR KEYWORD foo KEYWORD bar", "OR KEYWORD foo KEYWORD bar" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR NOT KEYWORD foo NOT KEYWORD bar", "OR NOT KEYWORD foo NOT KEYWORD bar" },
1c0020171b04d14adc4966ed963361abc9a86787Timo Sirainen
1c0020171b04d14adc4966ed963361abc9a86787Timo Sirainen { "KEYWORD foo KEYWORD foo", "KEYWORD foo" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "KEYWORD foo NOT KEYWORD foo", "NOT ALL" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "OR KEYWORD foo NOT KEYWORD foo", "ALL" },
1c0020171b04d14adc4966ed963361abc9a86787Timo Sirainen { "OR KEYWORD foo KEYWORD foo", "KEYWORD foo" },
1c0020171b04d14adc4966ed963361abc9a86787Timo Sirainen { "NOT KEYWORD foo NOT KEYWORD foo", "NOT KEYWORD foo" },
1c0020171b04d14adc4966ed963361abc9a86787Timo Sirainen
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "1:* 1:*", "ALL" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "OR 1:5 6:*", "ALL" },
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "UID 1:* UID 1:*", "ALL" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "OR UID 1:5 UID 6:*", "ALL" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "2:* 2:*", "2:4294967295" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "OR 2:* 2:*", "2:4294967295" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "UID 2:* UID 2:*", "UID 2:4294967295" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "OR UID 2:* UID 2:*", "UID 2:4294967295" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "1:5 6:7", "NOT ALL" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "1:5 3:7", "3:5" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "1:5 3:7 4:9", "4:5" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "1:5 OR 3:4 4:6", "3:5" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "OR 1 2", "1:2" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "NOT 1,3:5", "2,6:4294967294" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "NOT 1:100 NOT 50:200", "201:4294967294" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "OR NOT 1:100 NOT 50:200", "1:49,101:4294967294" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "UID 1:5 UID 6:7", "NOT ALL" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "UID 1:5 UID 3:7", "UID 3:5" },
df8046c9a4f6bc2a478ad1e74504d50f3110c906Timo Sirainen { "OR UID 1 UID 2", "UID 1:2" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "NOT UID 1,3:5", "UID 2,6:4294967294" },
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen
7000810786f2959f02cd6d2f4151a9eb61ff5db8Timo Sirainen { "1:5 UID 10:20", "1:5 UID 10:20" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "1:5 NOT UID 10:20", "1:5 UID 1:9,21:4294967294" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "ALL NOT UID 3:*", "NOT UID 3:4294967295" },
abb5d20d3155db02a1afec4066d52707ba9d4e52Timo Sirainen { "NOT 1:10 NOT *", "11:4294967294 NOT 4294967295" },
2aee623fcad4b931c27435ceaa495c3d3edd69b6Aki Tuomi
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "BEFORE 03-Aug-2014 BEFORE 01-Aug-2014 BEFORE 02-Aug-2014", "BEFORE \"01-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "OR BEFORE 01-Aug-2014 BEFORE 02-Aug-2014", "BEFORE \"02-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "OR BEFORE 01-Aug-2014 OR BEFORE 03-Aug-2014 BEFORE 02-Aug-2014", "BEFORE \"03-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "BEFORE 03-Aug-2014 NOT BEFORE 01-Aug-2014 BEFORE 02-Aug-2014", "BEFORE \"02-Aug-2014\" NOT BEFORE \"01-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "SENTBEFORE 03-Aug-2014 SENTBEFORE 01-Aug-2014 SENTBEFORE 02-Aug-2014", "SENTBEFORE \"01-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "SENTBEFORE 03-Aug-2014 BEFORE 01-Aug-2014 SENTBEFORE 02-Aug-2014", "SENTBEFORE \"02-Aug-2014\" BEFORE \"01-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "ON 03-Aug-2014 ON 03-Aug-2014", "ON \"03-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "ON 03-Aug-2014 ON 04-Aug-2014", "ON \"03-Aug-2014\" ON \"04-Aug-2014\"" }, /* this could be replaced with e.g. NOT ALL */
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ON 03-Aug-2014 ON 04-Aug-2014", "OR ON \"03-Aug-2014\" ON \"04-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "SINCE 03-Aug-2014 SINCE 01-Aug-2014 SINCE 02-Aug-2014", "SINCE \"03-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "OR SINCE 01-Aug-2014 SINCE 02-Aug-2014", "SINCE \"01-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "OR SINCE 01-Aug-2014 OR SINCE 03-Aug-2014 SINCE 02-Aug-2014", "SINCE \"01-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "SINCE 03-Aug-2014 NOT SINCE 01-Aug-2014 SINCE 02-Aug-2014", "SINCE \"03-Aug-2014\" NOT SINCE \"01-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "SENTSINCE 03-Aug-2014 SENTSINCE 01-Aug-2014 SENTSINCE 02-Aug-2014", "SENTSINCE \"03-Aug-2014\"" },
d6a7cb184cc882a90aa3d9312082e0029f354ff6Timo Sirainen { "SENTSINCE 03-Aug-2014 SINCE 01-Aug-2014 SENTSINCE 02-Aug-2014", "SENTSINCE \"03-Aug-2014\" SINCE \"01-Aug-2014\"" },
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen { "SMALLER 1 SMALLER 2", "SMALLER 1" },
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen { "OR SMALLER 1 SMALLER 2", "SMALLER 2" },
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen { "OR SMALLER 1 OR SMALLER 3 SMALLER 2", "SMALLER 3" },
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen { "SMALLER 3 NOT SMALLER 1 SMALLER 2", "SMALLER 2 NOT SMALLER 1" },
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen { "SMALLER 3 LARGER 5", "SMALLER 3 LARGER 5" }, /* this could be replaced with e.g. NOT ALL */
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR SMALLER 3 LARGER 5", "OR SMALLER 3 LARGER 5" },
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen { "LARGER 3 LARGER 1 LARGER 2", "LARGER 3" },
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen { "OR LARGER 1 LARGER 2", "LARGER 1" },
f5e8a76a128d4e92f0641135183c164fd5c5ce5eTimo Sirainen { "OR LARGER 1 OR LARGER 3 LARGER 2", "LARGER 1" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "LARGER 3 NOT LARGER 1 LARGER 2", "LARGER 3 NOT LARGER 1" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "SUBJECT foo SUBJECT foo", "SUBJECT foo" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "SUBJECT foo NOT SUBJECT foo", "NOT ALL" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "OR SUBJECT foo NOT SUBJECT foo", "ALL" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "SUBJECT foo SUBJECT foob", "SUBJECT foo SUBJECT foob" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "OR SUBJECT foo SUBJECT foo", "SUBJECT foo" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "FROM foo FROM foo", "FROM foo" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "FROM foo NOT FROM foo", "NOT ALL" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "OR FROM foo NOT FROM foo", "ALL" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "FROM foo FROM bar", "FROM foo FROM bar" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "FROM foo TO foo", "FROM foo TO foo" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "TEXT foo TEXT foo", "TEXT foo" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "TEXT foo TEXT foob", "TEXT foo TEXT foob" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "OR TEXT foo TEXT foo", "TEXT foo" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "OR NOT TEXT foo TEXT foo", "ALL" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "OR TEXT foo NOT TEXT foo", "ALL" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "TEXT foo NOT TEXT foo", "NOT ALL" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "NOT TEXT foo TEXT foo", "NOT ALL" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "BODY foo BODY foo", "BODY foo" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "BODY foo NOT BODY foo", "NOT ALL" },
92f9871ac981201fe0a47f6c909f790cce14b240Timo Sirainen { "OR BODY foo NOT BODY foo", "ALL" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "OR BODY foo BODY foo", "BODY foo" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "TEXT foo BODY foo", "TEXT foo BODY foo" },
cc935aff970ed6c24d136cc560c7e705a49d536cTimo Sirainen { "OR ( TEXT foo OR TEXT foo TEXT foo ) ( TEXT foo ( TEXT foo ) )", "TEXT foo" },
27e859cee42654bff801ba96677cfc4e4e0108c7Timo Sirainen
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen /* value="" tests */
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen { "HEADER foo ", "HEADER FOO \"\"" },
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen { "SUBJECT ", "SUBJECT \"\"" },
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen { "BODY ", "ALL" },
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen { "TEXT ", "ALL" },
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen { "HEADER foo .", "HEADER FOO ." },
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen { "SUBJECT .", "SUBJECT ." },
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen { "BODY .", "BODY ." },
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen { "TEXT .", "TEXT ." },
279c6b6d0b0a159c8533102e7e914db21dadcb03Timo Sirainen
387f9e3b4120273ad0213206a0e9cc2dc0e62ccaTimo Sirainen /* OR: drop redundant args */
27e859cee42654bff801ba96677cfc4e4e0108c7Timo Sirainen { "OR ( TEXT common1 TEXT unique1 ) TEXT common1", "TEXT common1" },
27e859cee42654bff801ba96677cfc4e4e0108c7Timo Sirainen { "OR ( TEXT unique1 TEXT common1 ) TEXT common1", "TEXT common1" },
27e859cee42654bff801ba96677cfc4e4e0108c7Timo Sirainen { "OR TEXT common1 ( TEXT common1 TEXT unique1 )", "TEXT common1" },
27e859cee42654bff801ba96677cfc4e4e0108c7Timo Sirainen { "OR TEXT common1 ( TEXT unique1 TEXT common1 )", "TEXT common1" },
071543cc13df9600d2e97aa35f28907be5a79477Timo Sirainen { "OR ( TEXT common1 TEXT common2 ) ( TEXT common1 TEXT common2 TEXT unique1 )", "TEXT common1 TEXT common2" },
27e859cee42654bff801ba96677cfc4e4e0108c7Timo Sirainen { "OR TEXT common1 OR ( TEXT unique1 TEXT common1 ) ( TEXT unique3 TEXT common1 )", "TEXT common1" },
632018810af689442569cbb0139c55868923ccfeTimo Sirainen
387f9e3b4120273ad0213206a0e9cc2dc0e62ccaTimo Sirainen /* OR: extract common AND */
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT common1 TEXT unique1 ) ( TEXT common1 TEXT unique2 )", "OR TEXT unique1 TEXT unique2 TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT unique1 TEXT common1 ) ( TEXT unique2 TEXT common1 )", "OR TEXT unique1 TEXT unique2 TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT common1 TEXT unique1 ) ( TEXT unique2 TEXT common1 )", "OR TEXT unique1 TEXT unique2 TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT unique1 TEXT common1 ) ( TEXT common1 TEXT unique2 )", "OR TEXT unique1 TEXT unique2 TEXT common1" },
632018810af689442569cbb0139c55868923ccfeTimo Sirainen
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT unique1 TEXT common1 ) ( TEXT common1 TEXT unique2 TEXT unique3 )", "OR TEXT unique1 (TEXT unique2 TEXT unique3) TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT common1 TEXT common2 TEXT unique1 ) ( TEXT common1 TEXT common2 TEXT unique2 )", "OR TEXT unique1 TEXT unique2 TEXT common2 TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT common1 TEXT common2 TEXT unique1 TEXT unique2 ) ( TEXT common1 TEXT common2 TEXT unique3 TEXT unique4 )", "OR (TEXT unique1 TEXT unique2) (TEXT unique3 TEXT unique4) TEXT common2 TEXT common1" },
632018810af689442569cbb0139c55868923ccfeTimo Sirainen
387f9e3b4120273ad0213206a0e9cc2dc0e62ccaTimo Sirainen /* non-matching cases */
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT unique1 TEXT unique2 ) TEXT unique3", "OR (TEXT unique1 TEXT unique2) TEXT unique3" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT unique1 TEXT unique2 ) ( TEXT unique3 TEXT unique4 )", "OR (TEXT unique1 TEXT unique2) (TEXT unique3 TEXT unique4)" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT common1 TEXT unique1 ) OR ( TEXT common1 TEXT unique2 ) TEXT unique3", "OR (TEXT common1 TEXT unique1) OR (TEXT common1 TEXT unique2) TEXT unique3" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT common1 TEXT unique1 ) OR ( TEXT common1 TEXT common2 ) ( TEXT common2 TEXT unique2 )", "OR (TEXT common1 TEXT unique1) OR (TEXT common1 TEXT common2) (TEXT common2 TEXT unique2)" },
8d3af185ae454653fad60e41c5f36edb1d45c868Timo Sirainen
8d3af185ae454653fad60e41c5f36edb1d45c868Timo Sirainen /* SUB: drop redundant args */
8d3af185ae454653fad60e41c5f36edb1d45c868Timo Sirainen { "( OR TEXT common1 TEXT unique1 ) TEXT common1", "TEXT common1" },
8d3af185ae454653fad60e41c5f36edb1d45c868Timo Sirainen { "( OR TEXT unique1 TEXT common1 ) TEXT common1", "TEXT common1" },
8d3af185ae454653fad60e41c5f36edb1d45c868Timo Sirainen { "TEXT common1 ( OR TEXT common1 TEXT unique1 )", "TEXT common1" },
8d3af185ae454653fad60e41c5f36edb1d45c868Timo Sirainen { "TEXT common1 ( OR TEXT unique1 TEXT common1 )", "TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT common1 TEXT common2 ) ( OR TEXT common1 OR TEXT common2 TEXT unique1 )", "OR TEXT common1 TEXT common2" },
8d3af185ae454653fad60e41c5f36edb1d45c868Timo Sirainen { "TEXT common1 ( OR TEXT unique1 TEXT common1 ) ( OR TEXT unique3 TEXT common1 )", "TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "OR ( TEXT common1 ( OR TEXT unique1 TEXT common1 ) ) TEXT unique1", "OR TEXT common1 TEXT unique1" },
dea8bfa31729dbdde1b12718f1ef98fac4e99db9Timo Sirainen
dea8bfa31729dbdde1b12718f1ef98fac4e99db9Timo Sirainen /* SUB: extract common OR */
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT common1 TEXT unique1 ) ( OR TEXT common1 TEXT unique2 )", "OR (TEXT unique1 TEXT unique2) TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT unique1 TEXT common1 ) ( OR TEXT unique2 TEXT common1 )", "OR (TEXT unique1 TEXT unique2) TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT common1 TEXT unique1 ) ( OR TEXT unique2 TEXT common1 )", "OR (TEXT unique1 TEXT unique2) TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT unique1 TEXT common1 ) ( OR TEXT common1 TEXT unique2 )", "OR (TEXT unique1 TEXT unique2) TEXT common1" },
dea8bfa31729dbdde1b12718f1ef98fac4e99db9Timo Sirainen
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT unique1 TEXT common1 ) ( OR TEXT common1 OR TEXT unique2 TEXT unique3 )", "OR (TEXT unique1 OR TEXT unique2 TEXT unique3) TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT common1 OR TEXT common2 TEXT unique1 ) ( OR TEXT common1 OR TEXT common2 TEXT unique2 )", "OR (TEXT unique1 TEXT unique2) OR TEXT common2 TEXT common1" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT common1 OR TEXT common2 OR TEXT unique1 TEXT unique2 ) ( OR TEXT common1 OR TEXT common2 OR TEXT unique3 TEXT unique4 )", "OR (OR TEXT unique1 TEXT unique2 OR TEXT unique3 TEXT unique4) OR TEXT common2 TEXT common1" },
dea8bfa31729dbdde1b12718f1ef98fac4e99db9Timo Sirainen
dea8bfa31729dbdde1b12718f1ef98fac4e99db9Timo Sirainen /* non-matching cases */
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT unique1 TEXT unique2 ) TEXT unique3", "OR TEXT unique1 TEXT unique2 TEXT unique3" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT unique1 TEXT unique2 ) ( OR TEXT unique3 TEXT unique4 )", "OR TEXT unique1 TEXT unique2 OR TEXT unique3 TEXT unique4" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT common1 TEXT unique1 ) ( OR TEXT common1 TEXT unique2 ) TEXT unique3", "OR TEXT common1 TEXT unique1 OR TEXT common1 TEXT unique2 TEXT unique3" },
5b2062e40f54535b212e800ffc79de0e405d4560Timo Sirainen { "( OR TEXT common1 TEXT unique1 ) ( OR TEXT common1 TEXT common2 ) ( OR TEXT common2 TEXT unique2 )", "OR TEXT common1 TEXT unique1 OR TEXT common1 TEXT common2 OR TEXT common2 TEXT unique2" },
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen};
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainenstatic struct mail_search_args *
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainentest_build_search_args(const char *args)
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen{
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen struct mail_search_parser *parser;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen struct mail_search_args *sargs;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen const char *error, *charset = "UTF-8";
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen parser = mail_search_parser_init_cmdline(t_strsplit(args, " "));
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen if (mail_search_build(mail_search_register_get_imap(),
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen parser, &charset, &sargs, &error) < 0)
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen i_panic("%s", error);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen mail_search_parser_deinit(&parser);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen return sargs;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen}
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainenstatic bool test_search_args_are_initialized(struct mail_search_arg *arg)
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen{
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen for (; arg != NULL; arg = arg->next) {
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen switch (arg->type) {
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen case SEARCH_MODSEQ:
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen if (arg->value.str != NULL &&
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen arg->initialized.keywords == NULL)
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen return FALSE;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen break;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen case SEARCH_KEYWORDS:
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen if (arg->initialized.keywords == NULL)
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen return FALSE;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen break;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen case SEARCH_MAILBOX_GLOB:
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen if (arg->initialized.mailbox_glob == NULL)
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen return FALSE;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen break;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen case SEARCH_INTHREAD:
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen case SEARCH_SUB:
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen case SEARCH_OR:
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen if (!test_search_args_are_initialized(arg->value.subargs))
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen return FALSE;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen break;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen default:
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen break;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen }
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen }
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen return TRUE;
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen}
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainenstatic void test_mail_search_args_simplify(void)
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen{
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen struct mail_search_args *args;
fccd110b494a7e31f23d31d9e3bc3e986c9bb1a8Timo Sirainen struct mail_storage_settings set = { .mail_max_keyword_length = 100 };
fccd110b494a7e31f23d31d9e3bc3e986c9bb1a8Timo Sirainen struct mail_storage storage = { .set = &set };
fccd110b494a7e31f23d31d9e3bc3e986c9bb1a8Timo Sirainen struct mailbox box = { .opened = TRUE, .storage = &storage };
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen string_t *str = t_str_new(256);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen const char *error;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen unsigned int i;
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen test_begin("mail search args simplify");
82d158d37db5cfb4e26affe4bc2f2a235901d1b9Timo Sirainen box.index = mail_index_alloc(NULL, NULL, "dovecot.index.");
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen for (i = 0; i < N_ELEMENTS(tests); i++) {
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen args = test_build_search_args(tests[i].input);
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen /* delay simplification until after init. that way we can test
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen that the simplification works correctly when working on
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen already-initialized args. */
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen args->simplified = TRUE;
fccd110b494a7e31f23d31d9e3bc3e986c9bb1a8Timo Sirainen mail_search_args_init(args, &box, FALSE, NULL);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen mail_search_args_simplify(args);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen str_truncate(str, 0);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen test_assert(mail_search_args_to_imap(str, args->args, &error));
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen test_assert_idx(strcmp(str_c(str), tests[i].output) == 0, i);
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen
e5834cb5a1d79a886fe81648a1b8cc57f973dd1fTimo Sirainen test_assert_idx(test_search_args_are_initialized(args->args), i);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen mail_search_args_unref(&args);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen }
fccd110b494a7e31f23d31d9e3bc3e986c9bb1a8Timo Sirainen mail_index_free(&box.index);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen test_end();
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen}
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainenstatic void test_mail_search_args_simplify_empty_lists(void)
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen{
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen struct mail_search_args *args;
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen test_begin("mail search args simplify empty args");
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen args = mail_search_build_init();
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen mail_search_args_simplify(args);
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen mail_search_args_unref(&args);
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen test_end();
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen}
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainenint main(void)
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen{
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {
fccd110b494a7e31f23d31d9e3bc3e986c9bb1a8Timo Sirainen mail_storage_init,
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen test_mail_search_args_simplify,
6de0cbb4a498ce0519b0f28e221164ce8d39736aTimo Sirainen test_mail_search_args_simplify_empty_lists,
fccd110b494a7e31f23d31d9e3bc3e986c9bb1a8Timo Sirainen mail_storage_deinit,
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen NULL
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen };
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen return test_run(test_functions);
f8740ac53310cd28ba4ec6dc9e9ce6e9a3688f39Timo Sirainen}