]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/imapd.t
imap: support the CLOSE command
[public-inbox.git] / t / imapd.t
index c31ac12f941f5b4dcc72fc5098fa1d19103658d5..8172a91923d3777de714a989ed03d5716942f75d 100644 (file)
--- a/t/imapd.t
+++ b/t/imapd.t
@@ -1,13 +1,14 @@
 #!perl -w
 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+# end-to-end IMAP tests, see unit tests in t/imap.t, too
 use strict;
 use Test::More;
 use Time::HiRes ();
 use PublicInbox::TestCommon;
 use PublicInbox::Config;
 use PublicInbox::Spawn qw(which);
-require_mods(qw(DBD::SQLite Mail::IMAPClient));
+require_mods(qw(DBD::SQLite Mail::IMAPClient Mail::IMAPClient::BodyStructure));
 
 my $level = '-Lbasic';
 SKIP: {
@@ -99,54 +100,6 @@ like($raw[0], qr/^\* LIST \(.*?\) "\." inbox/,
                'got an inbox.i1');
 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
 
-{ # make sure we get '%' globbing right
-       my @n = map { { newsgroup => $_ } } (qw(x.y.z x.z.y));
-       my $self = { imapd => { grouplist => \@n } };
-       PublicInbox::IMAPD::refresh_inboxlist($self->{imapd});
-       my $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x', '%');
-       is(scalar($$res =~ tr/\n/\n/), 2, 'only one result');
-       like($$res, qr/ x\r\ntag OK/, 'saw expected');
-       $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x.', '%');
-       is(scalar($$res =~ tr/\n/\n/), 3, 'only one result');
-       is(scalar(my @x = ($$res =~ m/ x\.[zy]\r\n/g)), 2, 'match expected');
-
-       $res = PublicInbox::IMAP::cmd_list($self, 't', 'x.(?{die "RCE"})', '%');
-       like($$res, qr/\At OK /, 'refname does not match attempted RCE');
-       $res = PublicInbox::IMAP::cmd_list($self, 't', '', '(?{die "RCE"})%');
-       like($$res, qr/\At OK /, 'wildcard does not match attempted RCE');
-}
-
-if ($ENV{TEST_BENCHMARK}) {
-       use Benchmark qw(:all);
-       my @n = map { { newsgroup => "inbox.comp.foo.bar.$_" } } (0..50000);
-       push @n, map { { newsgroup => "xobni.womp.foo.bar.$_" } } (0..50000);
-       my $self = { imapd => { grouplist => \@n } };
-       PublicInbox::IMAPD::refresh_inboxlist($self->{imapd});
-
-       my $n = scalar @n;
-       open my $null, '>', '/dev/null' or die;
-       my $ds = { sock => $null };
-       my $nr = 200;
-       diag "starting benchmark...";
-       my $t = timeit(1, sub {
-               for (0..$nr) {
-                       my $res = PublicInbox::IMAP::cmd_list($self, 'tag',
-                                                               '', '*');
-                       PublicInbox::DS::write($ds, $res);
-               }
-       });
-       diag timestr($t). "list all for $n inboxes $nr times";
-       $nr = 20;
-       $t = timeit(1, sub {
-               for (0..$nr) {
-                       my $res = PublicInbox::IMAP::cmd_list($self, 'tag',
-                                                               'inbox.', '%');
-                       PublicInbox::DS::write($ds, $res);
-               }
-       });
-       diag timestr($t). "list partial for $n inboxes $nr times";
-}
-
 my $ret = $mic->search('all') or BAIL_OUT "SEARCH FAIL $@";
 is_deeply($ret, [ 1 ], 'search all works');
 $ret = $mic->search('uid 1') or BAIL_OUT "SEARCH FAIL $@";
@@ -190,6 +143,36 @@ for my $r ('1:*', '1') {
 
        $ret = $mic->fetch_hash($r, 'FLAGS') or BAIL_OUT "FETCH $@";
        is_deeply($ret->{1}->{FLAGS}, '', 'no flags');
+
+       $ret = $mic->fetch_hash($r, 'BODY[1]') or BAIL_OUT "FETCH $@";
+       like($ret->{1}->{'BODY[1]'}, qr/\AThis is a test message/, 'BODY[1]');
+
+       $ret = $mic->fetch_hash($r, 'BODY[1]<1>') or BAIL_OUT "FETCH $@";
+       like($ret->{1}->{'BODY[1]<1>'}, qr/\Ahis is a test message/,
+                       'BODY[1]<1>');
+
+       $ret = $mic->fetch_hash($r, 'BODY[1]<2.3>') or BAIL_OUT "FETCH $@";
+       is($ret->{1}->{'BODY[1]<2>'}, "is ", 'BODY[1]<2.3>');
+       $ret = $mic->bodypart_string($r, 1, 3, 2) or
+                                       BAIL_OUT "bodypart_string $@";
+       is($ret, "is ", 'bodypart string');
+
+       $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS.NOT (Message-ID)]')
+               or BAIL_OUT "FETCH $@";
+       $ret = $ret->{1}->{'BODY[HEADER.FIELDS.NOT (MESSAGE-ID)]'};
+       unlike($ret, qr/message-id/i, 'Message-ID excluded');
+       like($ret, qr/\r\n\r\n\z/s, 'got header end');
+
+       $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS (Message-ID)]')
+               or BAIL_OUT "FETCH $@";
+       is($ret->{1}->{'BODY[HEADER.FIELDS (MESSAGE-ID)]'},
+               'Message-ID: <testmessage@example.com>'."\r\n\r\n",
+               'got only Message-ID');
+
+       my $bs = $mic->get_bodystructure($r) or BAIL_OUT("bodystructure: $@");
+       ok($bs, 'got a bodystructure');
+       is(lc($bs->bodytype), 'text', '->bodytype');
+       is(lc($bs->bodyenc), '8bit', '->bodyenc');
 }
 
 # Mail::IMAPClient ->compress creates cyclic reference:
@@ -217,10 +200,12 @@ $pi_config->each_inbox(sub {
        my $ng = $ibx->{newsgroup};
        my $mic = Mail::IMAPClient->new(%mic_opt);
        ok($mic && $mic->login && $mic->IsAuthenticated, "authed $name");
+       my $uidnext = $mic->uidnext($ng); # we'll fetch BODYSTRUCTURE on this
+       ok($uidnext, 'got uidnext for later fetch');
        is_deeply([$mic->has_capability('IDLE')], ['IDLE'], "IDLE capa $name");
        ok(!$mic->idle, "IDLE fails w/o SELECT/EXAMINE $name");
        ok($mic->examine($ng), "EXAMINE $ng succeeds");
-       ok($mic->idle, "IDLE succeeds on $ng");
+       ok(my $idle_tag = $mic->idle, "IDLE succeeds on $ng");
 
        open(my $fh, '<', 't/data/message_embed.eml') or BAIL_OUT("open: $!");
        run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
@@ -280,7 +265,71 @@ $pi_config->each_inbox(sub {
        ok(@res = $mic->idle_data(11), "IDLE succeeds on $ng after HUP");
        is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
        ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
-});
+       ok($mic->done($idle_tag), 'IDLE DONE');
+       my $bs = $mic->get_bodystructure($uidnext);
+       ok($bs, 'BODYSTRUCTURE ok for deeply nested');
+       $ret = $mic->fetch_hash($uidnext, 'BODY') or BAIL_OUT "FETCH $@";
+       ok($ret->{$uidnext}->{BODY}, 'got something in BODY');
+
+       # this matches dovecot behavior
+       $ret = $mic->fetch_hash($uidnext, 'BODY[1]') or BAIL_OUT "FETCH $@";
+       is($ret->{$uidnext}->{'BODY[1]'},
+               "testing embedded message harder\r\n", 'BODY[1]');
+       $ret = $mic->fetch_hash($uidnext, 'BODY[2]') or BAIL_OUT "FETCH $@";
+       like($ret->{$uidnext}->{'BODY[2]'},
+               qr/\ADate: Sat, 18 Apr 2020 22:20:20 /, 'BODY[2]');
+
+       $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.1]') or BAIL_OUT "FETCH $@";
+       is($ret->{$uidnext}->{'BODY[2.1.1]'},
+               "testing embedded message\r\n", 'BODY[2.1.1]');
+
+       $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.2]') or BAIL_OUT "FETCH $@";
+       like($ret->{$uidnext}->{'BODY[2.1.2]'}, qr/\AFrom: /,
+               'BODY[2.1.2] tip matched');
+       like($ret->{$uidnext}->{'BODY[2.1.2]'},
+                # trailing CRLF may vary depending on MIME parser
+                qr/done_testing;(?:\r\n){1,2}\z/,
+               'BODY[2.1.2] tail matched');
+
+       $ret = $mic->fetch_hash("1:$uidnext", 'BODY[2.HEADER]') or
+                                               BAIL_OUT "2.HEADER $@";
+       like($ret->{$uidnext}->{'BODY[2.HEADER]'},
+               qr/\ADate: Sat, 18 Apr 2020 22:20:20 /,
+               '2.HEADER of message/rfc822');
+
+       $ret = $mic->fetch_hash($uidnext, 'BODY[2.MIME]') or
+               BAIL_OUT "2.MIME $@";
+       is($ret->{$uidnext}->{'BODY[2.MIME]'}, <<EOF, 'BODY[2.MIME]');
+Content-Type: message/rfc822\r
+Content-Disposition: attachment; filename="embed2x\.eml"\r
+\r
+EOF
+}); # each_inbox
+
+# message sequence numbers :<
+is($mic->Uid(0), 0, 'disable UID on '.ref($mic));
+ok($mic->reconnect, 'reconnected');
+$ret = $mic->fetch_hash('1:*', 'RFC822') or BAIL_OUT "FETCH $@";
+is(scalar keys %$ret, 3, 'got all 3 messages');
+{
+       my $rdr = { 0 => \($ret->{1}->{RFC822}) };
+       my $env = { HOME => $ENV{HOME} };
+       my @cmd = qw(-learn rm --all);
+       run_script(\@cmd, $env, $rdr) or BAIL_OUT('-learn rm');
+}
+my $r2 = $mic->fetch_hash('1:*', 'BODY.PEEK[]') or BAIL_OUT "FETCH $@";
+is(scalar keys %$r2, 3, 'still got all 3 messages');
+like($r2->{1}->{'BODY[]'}, qr/dummy message #1/, 'got dummy message 1');
+is($r2->{2}->{'BODY[]'}, $ret->{2}->{RFC822}, 'message 2 unchanged');
+is($r2->{3}->{'BODY[]'}, $ret->{3}->{RFC822}, 'message 3 unchanged');
+$r2 = $mic->fetch_hash(2, 'BODY.PEEK[HEADER.FIELDS (message-id)]')
+                       or BAIL_OUT "FETCH $@";
+is($r2->{2}->{'BODY[HEADER.FIELDS (MESSAGE-ID)]'},
+       'Message-ID: <20200418222508.GA13918@dcvr>'."\r\n\r\n",
+       'BODY.PEEK[HEADER.FIELDS ...] drops .PEEK');
+ok($mic->close, 'CLOSE works');
+ok(!$mic->close, 'CLOSE not idempotent');
+ok($mic->logout, 'logged out');
 
 $td->kill;
 $td->join;