2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use autodie qw(open seek read);
7 use Fcntl qw(SEEK_SET);
8 use Time::Local qw(timegm);
9 use PublicInbox::TestCommon;
10 require_mods(qw(-imapd));
11 use_ok 'PublicInbox::IMAPsearchqp';
12 use_ok 'PublicInbox::IMAP';
14 my $imap = bless {}, 'PublicInbox::IMAP';
16 my $parse = sub { PublicInbox::IMAPsearchqp::parse($imap, $_[0]) };
18 $q = $parse->(qq{BODY oops});
19 is($q->{xap}, 'b:"oops"', 'BODY key supported');
21 $q = $parse->(qq{OR HEADER TO Brian (OR FROM Ryan (OR TO Joe CC Scott))});
22 is($q->{sql}, undef, 'not using SQLite for complex query');
23 is($q->{xap}, '(t:"brian" OR (f:"ryan" OR (t:"joe" OR c:"scott")))',
24 'complex query matches Xapian query string');
26 $q = $parse->(qq{HEADER CC b SENTSINCE 2-Oct-1993});
27 is($q->{xap}, 'c:"b" d:19931002..', 'compound query');
29 $q = $parse->(qq{CHARSET UTF-8 From b});
30 is($q->{xap}, 'f:"b"', 'charset handled');
31 $q = $parse->(qq{CHARSET WTF-8 From b});
32 like($q, qr/\ANO \[/, 'bad charset rejected');
35 open my $tmperr, '+>', undef;
36 open my $olderr, '>&', \*STDERR;
37 open STDERR, '>&', $tmperr;
38 $q = $parse->(qq{CHARSET});
39 open STDERR, '>&', $olderr;
40 seek $tmperr, 0, SEEK_SET;
41 read($tmperr, my $buf, -s $tmperr);
42 is($buf, '', 'nothing spewed to STDERR on bad query') or warn <<'EOM';
43 FIXME: this sometimes fails despite setting $::RD_ERRORS + $::RD_WARN to
44 undef in PublicInbox::IMAPsearchqp::parse and I don't understand why.
45 It usually suceeds. Let us know if you can help us understand this
46 occasional failure at meta@public-inbox.org
50 like($q, qr/\ABAD /, 'bad charset rejected');
52 $q = $parse->(qq{HEADER CC B (SENTBEFORE 2-Oct-1993)});
53 is($q->{xap}, 'c:"b" d:..19931002', 'compound query w/ parens');
55 { # limit recursion, stack and CPU cycles ain't free
57 my $s = ('('x$n ). 'To a' . ( ')'x$n );
59 is($q->{xap}, 't:"a"', 'nesting works');
61 $s = ('('x$n ). 'To a' . ( ')'x$n );
62 my $err = $parse->($s);
63 like($err, qr/\ABAD /, 'reject deep nesting');
66 # IMAP has at least 6 ways of interpreting a date
68 my $t0 = timegm(0, 0, 0, 2, 10 - 1, 1993);
69 my $t1 = $t0 + 86399; # no leap (day|second) support
72 $q = $parse->($s = qq{SENTBEFORE 2-Oct-1993});
73 is_deeply($q->{sql}, \" AND ds <= $t0", 'SENTBEFORE SQL');
74 $q = $parse->("FROM z $s");
75 is($q->{xap}, 'f:"z" d:..19931002', 'SENTBEFORE Xapian');
77 $q = $parse->($s = qq{SENTSINCE 2-Oct-1993});
78 is_deeply($q->{sql}, \" AND ds >= $t0", 'SENTSINCE SQL');
79 $q = $parse->("FROM z $s");
80 is($q->{xap}, 'f:"z" d:19931002..', 'SENTSINCE Xapian');
82 $q = $parse->($s = qq{SENTON 2-Oct-1993});
83 is_deeply($q->{sql}, \" AND ds >= $t0 AND ds <= $t1", 'SENTON SQL');
84 $q = $parse->("FROM z $s");
85 is($q->{xap}, 'f:"z" dt:19931002000000..19931002235959',
88 $q = $parse->($s = qq{BEFORE 2-Oct-1993});
89 is_deeply($q->{sql}, \" AND ts <= $t0", 'BEFORE SQL');
90 $q = $parse->("FROM z $s");
91 is($q->{xap}, qq{f:"z" rt:..$t0}, 'BEFORE Xapian');
93 $q = $parse->($s = qq{SINCE 2-Oct-1993});
94 is_deeply($q->{sql}, \" AND ts >= $t0", 'SINCE SQL');
95 $q = $parse->("FROM z $s");
96 is($q->{xap}, qq{f:"z" rt:$t0..}, 'SINCE Xapian');
98 $q = $parse->($s = qq{ON 2-Oct-1993});
99 is_deeply($q->{sql}, \" AND ts >= $t0 AND ts <= $t1", 'ON SQL');
100 $q = $parse->("FROM z $s");
101 is($q->{xap}, qq{f:"z" rt:$t0..$t1}, 'ON Xapian');
105 $imap->{uo2m} = pack('S*', (1..50000));
106 $imap->{uid_base} = 50000;
107 my $err = $parse->(qq{9:});
110 like($err, qr/\ABAD /, 'bad MSN range');
111 $err = $parse->(qq{UID 9:});
112 like($err, qr/\ABAD /, 'bad UID range');
113 $err = $parse->(qq{FROM x UID 9:});
114 like($err, qr/\ABAD /, 'bad UID range with Xapian');
115 $err = $parse->(qq{FROM x 9:});
116 like($err, qr/\ABAD /, 'bad UID range with Xapian');
118 $q = $parse->($s = qq{UID 50009:50099});
119 is_deeply($q->{sql}, \' AND (num >= 50009 AND num <= 50099)',
120 'SQL generated for UID range');
121 $q = $parse->("CC x $s");
122 is($q->{xap}, qq{c:"x" uid:50009..50099},
123 'Xapian generated for UID range');
125 $q = $parse->($s = qq{9:99});
126 is_deeply($q->{sql}, \' AND (num >= 50009 AND num <= 50099)',
127 'SQL generated for MSN range');
128 $q = $parse->("CC x $s");
129 is($q->{xap}, qq{c:"x" uid:50009..50099},
130 'Xapian generated for MSN range');