2 # Copyright (C) 2020 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(DBD::SQLite Email::Address::XS||Mail::Address
10 use_ok 'PublicInbox::IMAPsearchqp';
11 use_ok 'PublicInbox::IMAP';
13 my $imap = bless {}, 'PublicInbox::IMAP';
15 my $parse = sub { PublicInbox::IMAPsearchqp::parse($imap, $_[0]) };
17 $q = $parse->(qq{BODY oops});
18 is($q->{xap}, 'b:"oops"', 'BODY key supported');
20 $q = $parse->(qq{OR HEADER TO Brian (OR FROM Ryan (OR TO Joe CC Scott))});
21 is($q->{sql}, undef, 'not using SQLite for complex query');
22 is($q->{xap}, '(t:"brian" OR (f:"ryan" OR (t:"joe" OR c:"scott")))',
23 'complex query matches Xapian query string');
25 $q = $parse->(qq{HEADER CC b SENTSINCE 2-Oct-1993});
26 is($q->{xap}, 'c:"b" d:19931002..', 'compound query');
28 $q = $parse->(qq{CHARSET UTF-8 From b});
29 is($q->{xap}, 'f:"b"', 'charset handled');
30 $q = $parse->(qq{CHARSET WTF-8 From b});
31 like($q, qr/\ANO \[/, 'bad charset rejected');
33 # TODO: squelch errors by default? clients could flood logs
34 open my $fh, '>:scalar', \(my $buf) or die;
36 $q = $parse->(qq{CHARSET});
38 like($q, qr/\ABAD /, 'bad charset rejected');
40 $q = $parse->(qq{HEADER CC B (SENTBEFORE 2-Oct-1993)});
41 is($q->{xap}, 'c:"b" d:..19931002', 'compound query w/ parens');
43 { # limit recursion, stack and CPU cycles ain't free
45 my $s = ('('x$n ). 'To a' . ( ')'x$n );
47 is($q->{xap}, 't:"a"', 'nesting works');
49 $s = ('('x$n ). 'To a' . ( ')'x$n );
50 my $err = $parse->($s);
51 like($err, qr/\ABAD /, 'reject deep nesting');
54 # IMAP has at least 6 ways of interpreting a date
56 my $t0 = timegm(0, 0, 0, 2, 10 - 1, 1993);
57 my $t1 = $t0 + 86399; # no leap (day|second) support
60 $q = $parse->($s = qq{SENTBEFORE 2-Oct-1993});
61 is_deeply($q->{sql}, \" AND ds <= $t0", 'SENTBEFORE SQL');
62 $q = $parse->("FROM z $s");
63 is($q->{xap}, 'f:"z" d:..19931002', 'SENTBEFORE Xapian');
65 $q = $parse->($s = qq{SENTSINCE 2-Oct-1993});
66 is_deeply($q->{sql}, \" AND ds >= $t0", 'SENTSINCE SQL');
67 $q = $parse->("FROM z $s");
68 is($q->{xap}, 'f:"z" d:19931002..', 'SENTSINCE Xapian');
70 $q = $parse->($s = qq{SENTON 2-Oct-1993});
71 is_deeply($q->{sql}, \" AND ds >= $t0 AND ds <= $t1", 'SENTON SQL');
72 $q = $parse->("FROM z $s");
73 is($q->{xap}, 'f:"z" dt:19931002000000..19931002235959',
76 $q = $parse->($s = qq{BEFORE 2-Oct-1993});
77 is_deeply($q->{sql}, \" AND ts <= $t0", 'BEFORE SQL');
78 $q = $parse->("FROM z $s");
79 is($q->{xap}, qq{f:"z" ts:..$t0}, 'BEFORE Xapian');
81 $q = $parse->($s = qq{SINCE 2-Oct-1993});
82 is_deeply($q->{sql}, \" AND ts >= $t0", 'SINCE SQL');
83 $q = $parse->("FROM z $s");
84 is($q->{xap}, qq{f:"z" ts:$t0..}, 'SINCE Xapian');
86 $q = $parse->($s = qq{ON 2-Oct-1993});
87 is_deeply($q->{sql}, \" AND ts >= $t0 AND ts <= $t1", 'ON SQL');
88 $q = $parse->("FROM z $s");
89 is($q->{xap}, qq{f:"z" ts:$t0..$t1}, 'ON Xapian');
93 $imap->{uo2m} = pack('S*', (1..50000));
94 $imap->{uid_base} = 50000;
95 my $err = $parse->(qq{9:});
98 like($err, qr/\ABAD /, 'bad MSN range');
99 $err = $parse->(qq{UID 9:});
100 like($err, qr/\ABAD /, 'bad UID range');
101 $err = $parse->(qq{FROM x UID 9:});
102 like($err, qr/\ABAD /, 'bad UID range with Xapian');
103 $err = $parse->(qq{FROM x 9:});
104 like($err, qr/\ABAD /, 'bad UID range with Xapian');
106 $q = $parse->($s = qq{UID 50009:50099});
107 is_deeply($q->{sql}, \' AND (num >= 50009 AND num <= 50099)',
108 'SQL generated for UID range');
109 $q = $parse->("CC x $s");
110 is($q->{xap}, qq{c:"x" uid:50009..50099},
111 'Xapian generated for UID range');
113 $q = $parse->($s = qq{9:99});
114 is_deeply($q->{sql}, \' AND (num >= 50009 AND num <= 50099)',
115 'SQL generated for MSN range');
116 $q = $parse->("CC x $s");
117 is($q->{xap}, qq{c:"x" uid:50009..50099},
118 'Xapian generated for MSN range');