2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use Time::Local qw(timegm);
7 use PublicInbox::TestCommon;
8 require_mods(qw(-imapd));
9 use_ok 'PublicInbox::IMAPsearchqp';
10 use_ok 'PublicInbox::IMAP';
12 my $imap = bless {}, 'PublicInbox::IMAP';
14 my $parse = sub { PublicInbox::IMAPsearchqp::parse($imap, $_[0]) };
16 $q = $parse->(qq{BODY oops});
17 is($q->{xap}, 'b:"oops"', 'BODY key supported');
19 $q = $parse->(qq{OR HEADER TO Brian (OR FROM Ryan (OR TO Joe CC Scott))});
20 is($q->{sql}, undef, 'not using SQLite for complex query');
21 is($q->{xap}, '(t:"brian" OR (f:"ryan" OR (t:"joe" OR c:"scott")))',
22 'complex query matches Xapian query string');
24 $q = $parse->(qq{HEADER CC b SENTSINCE 2-Oct-1993});
25 is($q->{xap}, 'c:"b" d:19931002..', 'compound query');
27 $q = $parse->(qq{CHARSET UTF-8 From b});
28 is($q->{xap}, 'f:"b"', 'charset handled');
29 $q = $parse->(qq{CHARSET WTF-8 From b});
30 like($q, qr/\ANO \[/, 'bad charset rejected');
32 # TODO: squelch errors by default? clients could flood logs
33 open my $fh, '>:scalar', \(my $buf) or die;
35 $q = $parse->(qq{CHARSET});
37 like($q, qr/\ABAD /, 'bad charset rejected');
39 $q = $parse->(qq{HEADER CC B (SENTBEFORE 2-Oct-1993)});
40 is($q->{xap}, 'c:"b" d:..19931002', 'compound query w/ parens');
42 { # limit recursion, stack and CPU cycles ain't free
44 my $s = ('('x$n ). 'To a' . ( ')'x$n );
46 is($q->{xap}, 't:"a"', 'nesting works');
48 $s = ('('x$n ). 'To a' . ( ')'x$n );
49 my $err = $parse->($s);
50 like($err, qr/\ABAD /, 'reject deep nesting');
53 # IMAP has at least 6 ways of interpreting a date
55 my $t0 = timegm(0, 0, 0, 2, 10 - 1, 1993);
56 my $t1 = $t0 + 86399; # no leap (day|second) support
59 $q = $parse->($s = qq{SENTBEFORE 2-Oct-1993});
60 is_deeply($q->{sql}, \" AND ds <= $t0", 'SENTBEFORE SQL');
61 $q = $parse->("FROM z $s");
62 is($q->{xap}, 'f:"z" d:..19931002', 'SENTBEFORE Xapian');
64 $q = $parse->($s = qq{SENTSINCE 2-Oct-1993});
65 is_deeply($q->{sql}, \" AND ds >= $t0", 'SENTSINCE SQL');
66 $q = $parse->("FROM z $s");
67 is($q->{xap}, 'f:"z" d:19931002..', 'SENTSINCE Xapian');
69 $q = $parse->($s = qq{SENTON 2-Oct-1993});
70 is_deeply($q->{sql}, \" AND ds >= $t0 AND ds <= $t1", 'SENTON SQL');
71 $q = $parse->("FROM z $s");
72 is($q->{xap}, 'f:"z" dt:19931002000000..19931002235959',
75 $q = $parse->($s = qq{BEFORE 2-Oct-1993});
76 is_deeply($q->{sql}, \" AND ts <= $t0", 'BEFORE SQL');
77 $q = $parse->("FROM z $s");
78 is($q->{xap}, qq{f:"z" rt:..$t0}, 'BEFORE Xapian');
80 $q = $parse->($s = qq{SINCE 2-Oct-1993});
81 is_deeply($q->{sql}, \" AND ts >= $t0", 'SINCE SQL');
82 $q = $parse->("FROM z $s");
83 is($q->{xap}, qq{f:"z" rt:$t0..}, 'SINCE Xapian');
85 $q = $parse->($s = qq{ON 2-Oct-1993});
86 is_deeply($q->{sql}, \" AND ts >= $t0 AND ts <= $t1", 'ON SQL');
87 $q = $parse->("FROM z $s");
88 is($q->{xap}, qq{f:"z" rt:$t0..$t1}, 'ON Xapian');
92 $imap->{uo2m} = pack('S*', (1..50000));
93 $imap->{uid_base} = 50000;
94 my $err = $parse->(qq{9:});
97 like($err, qr/\ABAD /, 'bad MSN range');
98 $err = $parse->(qq{UID 9:});
99 like($err, qr/\ABAD /, 'bad UID range');
100 $err = $parse->(qq{FROM x UID 9:});
101 like($err, qr/\ABAD /, 'bad UID range with Xapian');
102 $err = $parse->(qq{FROM x 9:});
103 like($err, qr/\ABAD /, 'bad UID range with Xapian');
105 $q = $parse->($s = qq{UID 50009:50099});
106 is_deeply($q->{sql}, \' AND (num >= 50009 AND num <= 50099)',
107 'SQL generated for UID range');
108 $q = $parse->("CC x $s");
109 is($q->{xap}, qq{c:"x" uid:50009..50099},
110 'Xapian generated for UID range');
112 $q = $parse->($s = qq{9:99});
113 is_deeply($q->{sql}, \' AND (num >= 50009 AND num <= 50099)',
114 'SQL generated for MSN range');
115 $q = $parse->("CC x $s");
116 is($q->{xap}, qq{c:"x" uid:50009..50099},
117 'Xapian generated for MSN range');