| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Copyright (c) 2011-2023 FastMail Pty Ltd. All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions |
| 7 |
# are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer in |
| 14 |
# the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# |
| 17 |
# 3. The name "Fastmail Pty Ltd" must not be used to |
| 18 |
# endorse or promote products derived from this software without |
| 19 |
# prior written permission. For permission or any legal |
| 20 |
# details, please contact |
| 21 |
# FastMail Pty Ltd |
| 22 |
# PO Box 234 |
| 23 |
# Collins St West 8007 |
| 24 |
# Victoria |
| 25 |
# Australia |
| 26 |
# |
| 27 |
# 4. Redistributions of any form whatsoever must retain the following |
| 28 |
# acknowledgment: |
| 29 |
# "This product includes software developed by Fastmail Pty. Ltd." |
| 30 |
# |
| 31 |
# FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 32 |
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 33 |
# EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT |
| 34 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF |
| 35 |
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 36 |
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 37 |
# OF THIS SOFTWARE. |
| 38 |
# |
| 39 |
|
| 40 |
package Cassandane::Cyrus::Notify; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use DateTime; |
| 44 |
use Data::Dumper; |
| 45 |
|
| 46 |
use lib '.'; |
| 47 |
use base qw(Cassandane::Cyrus::TestCase); |
| 48 |
use Cassandane::Util::Log; |
| 49 |
|
| 50 |
sub new |
| 51 |
{ |
| 52 |
my $class = shift; |
| 53 |
my $config = Cassandane::Config->default()->clone(); |
| 54 |
$config->set(imapidlepoll => 2); |
| 55 |
return $class->SUPER::new({ |
| 56 |
config => $config, |
| 57 |
deliver => 1, |
| 58 |
start_instances => 0, |
| 59 |
}, @_); |
| 60 |
} |
| 61 |
|
| 62 |
sub set_up |
| 63 |
{ |
| 64 |
my ($self) = @_; |
| 65 |
$self->SUPER::set_up(); |
| 66 |
} |
| 67 |
|
| 68 |
sub tear_down |
| 69 |
{ |
| 70 |
my ($self) = @_; |
| 71 |
$self->SUPER::tear_down(); |
| 72 |
} |
| 73 |
|
| 74 |
sub test_bad |
| 75 |
:needs_component_idled :min_version_3_9 |
| 76 |
{ |
| 77 |
my ($self) = @_; |
| 78 |
|
| 79 |
xlog $self, "Message test of the NOTIFY command (idled required)"; |
| 80 |
|
| 81 |
$self->{instance}->{config}->set(imapidlepoll => '2'); |
| 82 |
$self->{instance}->add_start(name => 'idled', |
| 83 |
argv => [ 'idled' ]); |
| 84 |
$self->{instance}->start(); |
| 85 |
|
| 86 |
my $svc = $self->{instance}->get_service('imap'); |
| 87 |
|
| 88 |
my $store = $svc->create_store(); |
| 89 |
my $talk = $store->get_client(); |
| 90 |
|
| 91 |
xlog $self, "The server should report the NOTIFY capability"; |
| 92 |
$self->assert($talk->capability()->{notify}); |
| 93 |
|
| 94 |
xlog $self, "Enable Notify with a missing arg"; |
| 95 |
my $res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS'); |
| 96 |
$self->assert_str_equals('bad', $talk->get_last_completion_response()); |
| 97 |
|
| 98 |
xlog $self, "Enable Notify with an invalid arg"; |
| 99 |
$res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'FOO'); |
| 100 |
$self->assert_str_equals('bad', $talk->get_last_completion_response()); |
| 101 |
|
| 102 |
xlog $self, "Enable Notify with a missing filter"; |
| 103 |
$res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET'); |
| 104 |
$self->assert_str_equals('bad', $talk->get_last_completion_response()); |
| 105 |
|
| 106 |
xlog $self, "Enable Notify with an invalid filter"; |
| 107 |
$res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', |
| 108 |
"(FOO (MessageNew MessageExpunge))"); |
| 109 |
$self->assert_str_equals('bad', $talk->get_last_completion_response()); |
| 110 |
|
| 111 |
xlog $self, "Enable Notify with a duplicate filter"; |
| 112 |
$res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', |
| 113 |
"(SELECTED (MessageNew MessageExpunge))", |
| 114 |
"(SELECTED-DELAYED (MessageNew MessageExpunge))"); |
| 115 |
$self->assert_str_equals('bad', $talk->get_last_completion_response()); |
| 116 |
|
| 117 |
xlog $self, "Enable Notify with another duplicate filter"; |
| 118 |
$res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', |
| 119 |
"(INBOXES (MessageNew MessageExpunge))", |
| 120 |
"(INBOXES (MessageNew MessageExpunge FlagChange))"); |
| 121 |
$self->assert_str_equals('bad', $talk->get_last_completion_response()); |
| 122 |
|
| 123 |
xlog $self, "Enable Notify with an invalid event"; |
| 124 |
$res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', |
| 125 |
"(INBOXES (MessageNew MessageExpunge Foo))"); |
| 126 |
$self->assert_str_equals('no', $talk->get_last_completion_response()); |
| 127 |
|
| 128 |
xlog $self, "Enable Notify with an invalid event group"; |
| 129 |
$res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', |
| 130 |
"(SELECTED-DELAYED (MessageNew))"); |
| 131 |
$self->assert_str_equals('bad', $talk->get_last_completion_response()); |
| 132 |
|
| 133 |
xlog $self, "Enable Notify with another invalid event group"; |
| 134 |
$res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', |
| 135 |
"(PERSONAL (MessageExpunge FlagChange))"); |
| 136 |
$self->assert_str_equals('bad', $talk->get_last_completion_response()); |
| 137 |
|
| 138 |
xlog $self, "Enable Notify with an empty mailbox list"; |
| 139 |
$res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', |
| 140 |
"(MAILBOXES () (MessageNew MessageExpunge))"); |
| 141 |
$self->assert_str_equals('bad', $talk->get_last_completion_response()); |
| 142 |
} |
| 143 |
|
| 144 |
sub test_message |
| 145 |
:needs_component_idled :min_version_3_9 |
| 146 |
{ |
| 147 |
my ($self) = @_; |
| 148 |
|
| 149 |
xlog $self, "Message test of the NOTIFY command (idled required)"; |
| 150 |
|
| 151 |
$self->{instance}->{config}->set(imapidlepoll => '2'); |
| 152 |
$self->{instance}->add_start(name => 'idled', |
| 153 |
argv => [ 'idled' ]); |
| 154 |
$self->{instance}->start(); |
| 155 |
|
| 156 |
my $svc = $self->{instance}->get_service('imap'); |
| 157 |
|
| 158 |
my $store = $svc->create_store(); |
| 159 |
my $talk = $store->get_client(); |
| 160 |
|
| 161 |
my $otherstore = $svc->create_store(); |
| 162 |
my $othertalk = $otherstore->get_client(); |
| 163 |
|
| 164 |
xlog $self, "The server should report the NOTIFY capability"; |
| 165 |
$self->assert($talk->capability()->{notify}); |
| 166 |
|
| 167 |
xlog $self, "Create two mailboxes"; |
| 168 |
$talk->create("INBOX.foo"); |
| 169 |
$talk->create("INBOX.bar"); |
| 170 |
|
| 171 |
xlog $self, "Deliver a message"; |
| 172 |
my $msg = $self->{gen}->generate(subject => "Message 1"); |
| 173 |
$self->{instance}->deliver($msg); |
| 174 |
|
| 175 |
xlog $self, "Examine INBOX.foo"; |
| 176 |
$talk->examine("INBOX.foo"); |
| 177 |
|
| 178 |
xlog $self, "Enable Notify"; |
| 179 |
my $res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', 'STATUS', |
| 180 |
"(SELECTED (MessageNew" . |
| 181 |
" (UID BODY.PEEK[HEADER.FIELDS (From Subject)])" . |
| 182 |
" MessageExpunge FlagChange))", |
| 183 |
"(PERSONAL (MessageNew MessageExpunge))"); |
| 184 |
|
| 185 |
# Should get STATUS responses for unselected mailboxes |
| 186 |
my $status = $talk->get_response_code('status'); |
| 187 |
$self->assert_num_equals(1, $status->{'INBOX'}{messages}); |
| 188 |
$self->assert_num_equals(2, $status->{'INBOX'}{uidnext}); |
| 189 |
$self->assert_num_equals(0, $status->{'INBOX.bar'}{messages}); |
| 190 |
$self->assert_num_equals(1, $status->{'INBOX.bar'}{uidnext}); |
| 191 |
|
| 192 |
xlog $self, "Deliver a message"; |
| 193 |
$msg = $self->{gen}->generate(subject => "Message 2"); |
| 194 |
$self->{instance}->deliver($msg); |
| 195 |
|
| 196 |
# Should get STATUS response for INBOX |
| 197 |
$res = $store->idle_response('STATUS', 3); |
| 198 |
$self->assert($res, "received an unsolicited response"); |
| 199 |
$res = $store->idle_response({}, 1); |
| 200 |
$self->assert(!$res, "no more unsolicited responses"); |
| 201 |
|
| 202 |
$status = $talk->get_response_code('status'); |
| 203 |
$self->assert_num_equals(2, $status->{'INBOX'}{messages}); |
| 204 |
$self->assert_num_equals(3, $status->{'INBOX'}{uidnext}); |
| 205 |
|
| 206 |
xlog $self, "EXPUNGE message from INBOX in other session"; |
| 207 |
$othertalk->select("INBOX"); |
| 208 |
$res = $othertalk->store('1', '+flags', '(\\Deleted)'); |
| 209 |
$res = $othertalk->expunge(); |
| 210 |
|
| 211 |
# Should get STATUS response for INBOX |
| 212 |
$res = $store->idle_response('STATUS', 3); |
| 213 |
$self->assert($res, "received an unsolicited response"); |
| 214 |
$res = $store->idle_response({}, 1); |
| 215 |
$self->assert(!$res, "no more unsolicited responses"); |
| 216 |
|
| 217 |
$status = $talk->get_response_code('status'); |
| 218 |
$self->assert_num_equals(1, $status->{'INBOX'}{messages}); |
| 219 |
$self->assert_num_equals(3, $status->{'INBOX'}{uidnext}); |
| 220 |
|
| 221 |
xlog $self, "Select INBOX"; |
| 222 |
$talk->examine("INBOX"); |
| 223 |
|
| 224 |
xlog $self, "Deliver a message"; |
| 225 |
$msg = $self->{gen}->generate(subject => "Message 3"); |
| 226 |
$self->{instance}->deliver($msg); |
| 227 |
|
| 228 |
# Should get EXISTS, RECENT, FETCH response |
| 229 |
$res = $store->idle_response({}, 3); |
| 230 |
$self->assert($res, "received an unsolicited response"); |
| 231 |
$res = $store->idle_response({}, 3); |
| 232 |
$self->assert($res, "received an unsolicited response"); |
| 233 |
$res = $store->idle_response('FETCH', 3); |
| 234 |
$self->assert($res, "received an unsolicited response"); |
| 235 |
$res = $store->idle_response({}, 1); |
| 236 |
$self->assert(!$res, "no more unsolicited responses"); |
| 237 |
|
| 238 |
$self->assert_num_equals(2, $talk->get_response_code('exists')); |
| 239 |
$self->assert_num_equals(1, $talk->get_response_code('recent')); |
| 240 |
|
| 241 |
my $fetch = $talk->get_response_code('fetch'); |
| 242 |
$self->assert_num_equals(3, $fetch->{2}{uid}); |
| 243 |
$self->assert_str_equals('Message 3', $fetch->{2}{headers}{subject}[0]); |
| 244 |
$self->assert_not_null($fetch->{2}{headers}{from}); |
| 245 |
|
| 246 |
xlog $self, "DELETE message from INBOX in other session"; |
| 247 |
$res = $othertalk->store('1', '+flags', '(\\Deleted)'); |
| 248 |
|
| 249 |
# Should get FETCH response for INBOX |
| 250 |
$res = $store->idle_response('FETCH', 3); |
| 251 |
$self->assert($res, "received an unsolicited response"); |
| 252 |
$res = $store->idle_response({}, 1); |
| 253 |
$self->assert(!$res, "no more unsolicited responses"); |
| 254 |
|
| 255 |
$fetch = $talk->get_response_code('fetch'); |
| 256 |
$self->assert_num_equals(2, $fetch->{1}{uid}); |
| 257 |
$self->assert_str_equals('\\Deleted', $fetch->{1}{flags}[0]); |
| 258 |
|
| 259 |
xlog $self, "EXPUNGE message from INBOX in other session"; |
| 260 |
$res = $othertalk->expunge(); |
| 261 |
|
| 262 |
# Should get EXPUNGE response for INBOX |
| 263 |
$res = $store->idle_response('EXPUNGE', 3); |
| 264 |
$self->assert($res, "received an unsolicited response"); |
| 265 |
$res = $store->idle_response({}, 1); |
| 266 |
$self->assert(!$res, "no more unsolicited responses"); |
| 267 |
|
| 268 |
$self->assert_num_equals(1, $talk->get_response_code('expunge')); |
| 269 |
|
| 270 |
xlog $self, "Disable Notify"; |
| 271 |
$res = $talk->_imap_cmd('NOTIFY', 0, "", "NONE"); |
| 272 |
|
| 273 |
xlog $self, "Deliver a message"; |
| 274 |
$msg = $self->{gen}->generate(subject => "Message 4"); |
| 275 |
$self->{instance}->deliver($msg); |
| 276 |
|
| 277 |
# Should get no unsolicited responses |
| 278 |
$res = $store->idle_response({}, 1); |
| 279 |
$self->assert(!$res, "no unsolicited responses"); |
| 280 |
|
| 281 |
# make sure that the connection is ended so that imapd reset happens |
| 282 |
$talk->logout(); |
| 283 |
undef $talk; |
| 284 |
|
| 285 |
# we enabled NOTIFY, so we should see it in client behaviors |
| 286 |
my $pat = qr/session ended.*notify=<1>/; |
| 287 |
$self->assert_syslog_matches($self->{instance}, $pat); |
| 288 |
} |
| 289 |
|
| 290 |
sub test_mailbox |
| 291 |
:needs_component_idled :min_version_3_9 |
| 292 |
{ |
| 293 |
my ($self) = @_; |
| 294 |
|
| 295 |
xlog $self, "Mailbox test of the NOTIFY command (idled required)"; |
| 296 |
|
| 297 |
$self->{instance}->{config}->set(imapidlepoll => '2'); |
| 298 |
$self->{instance}->add_start(name => 'idled', |
| 299 |
argv => [ 'idled' ]); |
| 300 |
$self->{instance}->start(); |
| 301 |
|
| 302 |
my $svc = $self->{instance}->get_service('imap'); |
| 303 |
|
| 304 |
my $store = $svc->create_store(); |
| 305 |
my $talk = $store->get_client(); |
| 306 |
|
| 307 |
my $otherstore = $svc->create_store(); |
| 308 |
my $othertalk = $otherstore->get_client(); |
| 309 |
|
| 310 |
xlog $self, "The server should report the NOTIFY capability"; |
| 311 |
$self->assert($talk->capability()->{notify}); |
| 312 |
|
| 313 |
xlog $self, "Enable Notify"; |
| 314 |
my $res = $talk->_imap_cmd('NOTIFY', 0, "", "SET", |
| 315 |
"(PERSONAL (MailboxName SubscriptionChange))"); |
| 316 |
|
| 317 |
xlog $self, "Create mailbox in other session"; |
| 318 |
$othertalk->create("INBOX.rename-me"); |
| 319 |
|
| 320 |
# Should get LIST response |
| 321 |
$res = $store->idle_response('LIST', 3); |
| 322 |
$self->assert($res, "received an unsolicited response"); |
| 323 |
$res = $store->idle_response({}, 1); |
| 324 |
$self->assert(!$res, "no more unsolicited responses"); |
| 325 |
|
| 326 |
my $list = $talk->get_response_code('list'); |
| 327 |
$self->assert_str_equals('INBOX.rename-me', $list->[0][2]); |
| 328 |
|
| 329 |
xlog $self, "Subscribe mailbox in other session"; |
| 330 |
$othertalk->subscribe("INBOX.rename-me"); |
| 331 |
|
| 332 |
# Should get LIST response with \Subscribed |
| 333 |
$res = $store->idle_response('LIST', 3); |
| 334 |
$self->assert($res, "received an unsolicited response"); |
| 335 |
$res = $store->idle_response({}, 1); |
| 336 |
$self->assert(!$res, "no more unsolicited responses"); |
| 337 |
|
| 338 |
$list = $talk->get_response_code('list'); |
| 339 |
$self->assert_str_equals('\\Subscribed', $list->[0][0][0]); |
| 340 |
$self->assert_str_equals('INBOX.rename-me', $list->[0][2]); |
| 341 |
|
| 342 |
xlog $self, "Rename mailbox in other session"; |
| 343 |
$othertalk->rename("INBOX.rename-me", "INBOX.delete-me"); |
| 344 |
|
| 345 |
# Use our own handler since IMAPTalk will lose OLDNAME |
| 346 |
my %handlers = |
| 347 |
( |
| 348 |
list => sub |
| 349 |
{ |
| 350 |
my (undef, $data) = @_; |
| 351 |
$list = [ $data ]; |
| 352 |
}, |
| 353 |
); |
| 354 |
|
| 355 |
# Should get LIST response with OLDNAME |
| 356 |
$res = $store->idle_response(\%handlers, 3); |
| 357 |
$self->assert($res, "received an unsolicited response"); |
| 358 |
$res = $store->idle_response({}, 1); |
| 359 |
$self->assert(!$res, "no more unsolicited responses"); |
| 360 |
|
| 361 |
$self->assert_str_equals('INBOX.delete-me', $list->[0][2]); |
| 362 |
$self->assert_str_equals('OLDNAME', $list->[0][3][0]); |
| 363 |
$self->assert_str_equals('INBOX.rename-me', $list->[0][3][1][0]); |
| 364 |
|
| 365 |
xlog $self, "Delete mailbox in other session"; |
| 366 |
$othertalk->delete("INBOX.delete-me"); |
| 367 |
|
| 368 |
# Should get LIST response with \NonExistent |
| 369 |
$res = $store->idle_response({}, 3); |
| 370 |
$self->assert($res, "received an unsolicited response"); |
| 371 |
$res = $store->idle_response({}, 1); |
| 372 |
$self->assert(!$res, "no more unsolicited responses"); |
| 373 |
|
| 374 |
$list = $talk->get_response_code('list'); |
| 375 |
$self->assert_str_equals('\\NonExistent', $list->[0][0][0]); |
| 376 |
$self->assert_str_equals('INBOX.delete-me', $list->[0][2]); |
| 377 |
|
| 378 |
xlog $self, "Disable Notify"; |
| 379 |
$res = $talk->_imap_cmd('NOTIFY', 0, "", "NONE"); |
| 380 |
|
| 381 |
xlog $self, "Create mailbox in other session"; |
| 382 |
$othertalk->create("INBOX.foo"); |
| 383 |
|
| 384 |
# Should get no unsolicited responses |
| 385 |
$res = $store->idle_response({}, 1); |
| 386 |
$self->assert(!$res, "no unsolicited responses"); |
| 387 |
} |
| 388 |
|
| 389 |
sub test_idle |
| 390 |
:needs_component_idled :min_version_3_9 |
| 391 |
{ |
| 392 |
my ($self) = @_; |
| 393 |
|
| 394 |
xlog $self, "Test of the NOTIFY + IDLE commands (idled required)"; |
| 395 |
|
| 396 |
$self->{instance}->{config}->set(imapidlepoll => '2'); |
| 397 |
$self->{instance}->add_start(name => 'idled', |
| 398 |
argv => [ 'idled' ]); |
| 399 |
$self->{instance}->start(); |
| 400 |
|
| 401 |
my $svc = $self->{instance}->get_service('imap'); |
| 402 |
|
| 403 |
my $store = $svc->create_store(); |
| 404 |
my $talk = $store->get_client(); |
| 405 |
|
| 406 |
my $otherstore = $svc->create_store(); |
| 407 |
my $othertalk = $otherstore->get_client(); |
| 408 |
|
| 409 |
xlog $self, "The server should report the NOTIFY capability"; |
| 410 |
$self->assert($talk->capability()->{notify}); |
| 411 |
|
| 412 |
xlog $self, "Enable Notify"; |
| 413 |
my $res = $talk->_imap_cmd('NOTIFY', 0, "", 'SET', |
| 414 |
"(SELECTED (MessageNew" . |
| 415 |
" (UID BODY.PEEK[HEADER.FIELDS (From Subject)])" . |
| 416 |
" MessageExpunge))", |
| 417 |
"(PERSONAL (MessageNew MessageExpunge MailboxName))"); |
| 418 |
|
| 419 |
# Should NOT get STATUS response for INBOX |
| 420 |
$res = $store->idle_response({}, 1); |
| 421 |
$self->assert(!$res, "no more unsolicited responses"); |
| 422 |
|
| 423 |
xlog $self, "Examine INBOX"; |
| 424 |
$talk->examine("INBOX"); |
| 425 |
$self->assert_num_equals(0, $talk->get_response_code('exists')); |
| 426 |
$self->assert_num_equals(0, $talk->get_response_code('recent')); |
| 427 |
$self->assert_num_equals(1, $talk->get_response_code('uidnext')); |
| 428 |
|
| 429 |
xlog $self, "Sending the IDLE command"; |
| 430 |
$store->idle_begin() |
| 431 |
or die "IDLE failed: $@"; |
| 432 |
|
| 433 |
xlog $self, "Deliver a message"; |
| 434 |
my $msg = $self->{gen}->generate(subject => "Message 1"); |
| 435 |
$self->{instance}->deliver($msg); |
| 436 |
|
| 437 |
# Should get EXISTS, RECENT, FETCH response |
| 438 |
$res = $store->idle_response({}, 3); |
| 439 |
$self->assert($res, "received an unsolicited response"); |
| 440 |
$res = $store->idle_response({}, 3); |
| 441 |
$self->assert($res, "received an unsolicited response"); |
| 442 |
$res = $store->idle_response('FETCH', 3); |
| 443 |
$self->assert($res, "received an unsolicited response"); |
| 444 |
$res = $store->idle_response({}, 1); |
| 445 |
$self->assert(!$res, "no more unsolicited responses"); |
| 446 |
|
| 447 |
$self->assert_num_equals(1, $talk->get_response_code('exists')); |
| 448 |
$self->assert_num_equals(1, $talk->get_response_code('recent')); |
| 449 |
|
| 450 |
my $fetch = $talk->get_response_code('fetch'); |
| 451 |
$self->assert_num_equals(1, $fetch->{1}{uid}); |
| 452 |
$self->assert_str_equals('Message 1', $fetch->{1}{headers}{subject}[0]); |
| 453 |
|
| 454 |
xlog $self, "Create mailbox in other session"; |
| 455 |
$othertalk->create("INBOX.foo"); |
| 456 |
|
| 457 |
# Should get LIST response |
| 458 |
$res = $store->idle_response('LIST', 3); |
| 459 |
$self->assert($res, "received an unsolicited response"); |
| 460 |
$res = $store->idle_response({}, 1); |
| 461 |
$self->assert(!$res, "no more unsolicited responses"); |
| 462 |
|
| 463 |
my $list = $talk->get_response_code('list'); |
| 464 |
$self->assert_str_equals('INBOX.foo', $list->[0][2]); |
| 465 |
|
| 466 |
$othertalk->select("INBOX"); |
| 467 |
|
| 468 |
xlog $self, "Add \Flagged to message in INBOX in other session"; |
| 469 |
$res = $othertalk->store('1', '+flags', '(\\Flagged)'); |
| 470 |
|
| 471 |
# Should NOT get FETCH response for INBOX |
| 472 |
$res = $store->idle_response('FETCH', 1); |
| 473 |
$self->assert(!$res, "no more unsolicited responses"); |
| 474 |
|
| 475 |
xlog $self, "MOVE message from INBOX to INBOX.foo in other session"; |
| 476 |
$res = $othertalk->move('1', "INBOX.foo"); |
| 477 |
|
| 478 |
# Should get STATUS response for INBOX.foo and EXPUNGE response for INBOX |
| 479 |
$res = $store->idle_response('STATUS', 3); |
| 480 |
$self->assert($res, "received an unsolicited response"); |
| 481 |
$res = $store->idle_response({}, 3); |
| 482 |
$self->assert($res, "received an unsolicited response"); |
| 483 |
$res = $store->idle_response({}, 1); |
| 484 |
$self->assert(!$res, "no more unsolicited responses"); |
| 485 |
|
| 486 |
my $status = $talk->get_response_code('status'); |
| 487 |
$self->assert_num_equals(1, $status->{'INBOX.foo'}{messages}); |
| 488 |
$self->assert_num_equals(2, $status->{'INBOX.foo'}{uidnext}); |
| 489 |
$self->assert_num_equals(1, $talk->get_response_code('expunge')); |
| 490 |
|
| 491 |
xlog $self, "Sending DONE continuation"; |
| 492 |
$store->idle_end({}); |
| 493 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 494 |
|
| 495 |
xlog $self, "Deliver a message"; |
| 496 |
$msg = $self->{gen}->generate(subject => "Message 2"); |
| 497 |
$self->{instance}->deliver($msg); |
| 498 |
|
| 499 |
# Should get EXISTS, RECENT, FETCH response |
| 500 |
$res = $store->idle_response({}, 3); |
| 501 |
$self->assert($res, "received an unsolicited response"); |
| 502 |
$res = $store->idle_response({}, 3); |
| 503 |
$self->assert($res, "received an unsolicited response"); |
| 504 |
$res = $store->idle_response('FETCH', 3); |
| 505 |
$self->assert($res, "received an unsolicited response"); |
| 506 |
$res = $store->idle_response({}, 1); |
| 507 |
$self->assert(!$res, "no more unsolicited responses"); |
| 508 |
|
| 509 |
$self->assert_num_equals(1, $talk->get_response_code('exists')); |
| 510 |
$self->assert_num_equals(1, $talk->get_response_code('recent')); |
| 511 |
|
| 512 |
$fetch = $talk->get_response_code('fetch'); |
| 513 |
$self->assert_num_equals(2, $fetch->{1}{uid}); |
| 514 |
$self->assert_str_equals('Message 2', $fetch->{1}{headers}{subject}[0]); |
| 515 |
|
| 516 |
xlog $self, "Unselect INBOX"; |
| 517 |
$talk->unselect(); |
| 518 |
|
| 519 |
xlog $self, "Deliver a message"; |
| 520 |
$msg = $self->{gen}->generate(subject => "Message 3"); |
| 521 |
$self->{instance}->deliver($msg); |
| 522 |
|
| 523 |
# Should get STATUS response for INBOX |
| 524 |
$res = $store->idle_response('STATUS', 3); |
| 525 |
$self->assert($res, "received an unsolicited response"); |
| 526 |
$res = $store->idle_response({}, 1); |
| 527 |
$self->assert(!$res, "no more unsolicited responses"); |
| 528 |
|
| 529 |
$status = $talk->get_response_code('status'); |
| 530 |
$self->assert_num_equals(2, $status->{'INBOX'}{messages}); |
| 531 |
$self->assert_num_equals(4, $status->{'INBOX'}{uidnext}); |
| 532 |
|
| 533 |
xlog $self, "Delete mailbox in other session"; |
| 534 |
$othertalk->delete("INBOX.foo"); |
| 535 |
|
| 536 |
# Should get LIST response with \NonExistent |
| 537 |
$res = $store->idle_response({}, 3); |
| 538 |
$self->assert($res, "received an unsolicited response"); |
| 539 |
$res = $store->idle_response({}, 1); |
| 540 |
$self->assert(!$res, "no more unsolicited responses"); |
| 541 |
|
| 542 |
$list = $talk->get_response_code('list'); |
| 543 |
$self->assert_str_equals('\\NonExistent', $list->[0][0][0]); |
| 544 |
$self->assert_str_equals('INBOX.foo', $list->[0][2]); |
| 545 |
} |
| 546 |
|
| 547 |
sub test_selected_delayed |
| 548 |
:needs_component_idled :min_version_3_9 |
| 549 |
{ |
| 550 |
my ($self) = @_; |
| 551 |
|
| 552 |
xlog $self, "Selected-delayed test of the NOTIFY command (idled required)"; |
| 553 |
|
| 554 |
$self->{instance}->{config}->set(imapidlepoll => '2'); |
| 555 |
$self->{instance}->add_start(name => 'idled', |
| 556 |
argv => [ 'idled' ]); |
| 557 |
$self->{instance}->start(); |
| 558 |
|
| 559 |
my $svc = $self->{instance}->get_service('imap'); |
| 560 |
|
| 561 |
my $store = $svc->create_store(); |
| 562 |
my $talk = $store->get_client(); |
| 563 |
|
| 564 |
my $otherstore = $svc->create_store(); |
| 565 |
my $othertalk = $otherstore->get_client(); |
| 566 |
|
| 567 |
xlog $self, "The server should report the NOTIFY capability"; |
| 568 |
$self->assert($talk->capability()->{notify}); |
| 569 |
|
| 570 |
xlog $self, "Enable Notify"; |
| 571 |
my $res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', |
| 572 |
"(SELECTED-DELAYED (MessageNew MessageExpunge FlagChange))"); |
| 573 |
|
| 574 |
xlog $self, "Examine INBOX"; |
| 575 |
$talk->examine("INBOX"); |
| 576 |
|
| 577 |
xlog $self, "Deliver a message"; |
| 578 |
my $msg = $self->{gen}->generate(subject => "Message 1"); |
| 579 |
$self->{instance}->deliver($msg); |
| 580 |
|
| 581 |
# Should get EXISTS, RECENT response |
| 582 |
$res = $store->idle_response({}, 3); |
| 583 |
$self->assert($res, "received an unsolicited response"); |
| 584 |
$res = $store->idle_response({}, 3); |
| 585 |
$self->assert($res, "received an unsolicited response"); |
| 586 |
$res = $store->idle_response({}, 1); |
| 587 |
$self->assert(!$res, "no more unsolicited responses"); |
| 588 |
|
| 589 |
$self->assert_num_equals(1, $talk->get_response_code('exists')); |
| 590 |
$self->assert_num_equals(1, $talk->get_response_code('recent')); |
| 591 |
|
| 592 |
xlog $self, "EXPUNGE message from INBOX in other session"; |
| 593 |
$othertalk->select("INBOX"); |
| 594 |
$res = $othertalk->store('1', '+flags', '(\\Deleted)'); |
| 595 |
$res = $othertalk->expunge(); |
| 596 |
|
| 597 |
# Should get FETCH response, but NO EXPUNGE response |
| 598 |
$res = $store->idle_response('FETCH', 3); |
| 599 |
$self->assert($res, "received an unsolicited response"); |
| 600 |
$res = $store->idle_response({}, 1); |
| 601 |
$self->assert(!$res, "no more unsolicited responses"); |
| 602 |
|
| 603 |
my $fetch = $talk->get_response_code('fetch'); |
| 604 |
$self->assert_num_equals(1, $fetch->{1}{uid}); |
| 605 |
$self->assert_str_equals('\\Recent', $fetch->{1}{flags}[0]); |
| 606 |
$self->assert_str_equals('\\Deleted', $fetch->{1}{flags}[1]); |
| 607 |
|
| 608 |
xlog $self, "Poll for changes"; |
| 609 |
$talk->noop(); |
| 610 |
|
| 611 |
# Should get EXPUNGE response |
| 612 |
$self->assert_num_equals(1, $talk->get_response_code('expunge')); |
| 613 |
} |
| 614 |
|
| 615 |
sub test_change_selected |
| 616 |
:needs_component_idled :min_version_3_9 |
| 617 |
{ |
| 618 |
my ($self) = @_; |
| 619 |
|
| 620 |
xlog $self, "Test of NOTIFY events following SELECTED mailbox"; |
| 621 |
|
| 622 |
$self->{instance}->{config}->set(imapidlepoll => '2'); |
| 623 |
$self->{instance}->add_start(name => 'idled', |
| 624 |
argv => [ 'idled' ]); |
| 625 |
$self->{instance}->start(); |
| 626 |
|
| 627 |
my $svc = $self->{instance}->get_service('imap'); |
| 628 |
|
| 629 |
my $store = $svc->create_store(); |
| 630 |
my $talk = $store->get_client(); |
| 631 |
|
| 632 |
my $otherstore = $svc->create_store(); |
| 633 |
my $othertalk = $otherstore->get_client(); |
| 634 |
|
| 635 |
xlog $self, "The server should report the NOTIFY capability"; |
| 636 |
$self->assert($talk->capability()->{notify}); |
| 637 |
|
| 638 |
xlog $self, "Create another mailbox"; |
| 639 |
$talk->create("INBOX.foo"); |
| 640 |
|
| 641 |
xlog $self, "Enable Notify"; |
| 642 |
my $res = $talk->_imap_cmd('NOTIFY', 0, "", 'SET', |
| 643 |
"(SELECTED (MessageNew MessageExpunge))", |
| 644 |
"(PERSONAL (MessageNew MessageExpunge))"); |
| 645 |
|
| 646 |
xlog $self, "Examine INBOX"; |
| 647 |
$talk->examine("INBOX"); |
| 648 |
$self->assert_num_equals(0, $talk->get_response_code('exists')); |
| 649 |
$self->assert_num_equals(0, $talk->get_response_code('recent')); |
| 650 |
$self->assert_num_equals(1, $talk->get_response_code('uidnext')); |
| 651 |
|
| 652 |
xlog $self, "Deliver a message"; |
| 653 |
my $msg = $self->{gen}->generate(subject => "Message 1"); |
| 654 |
$self->{instance}->deliver($msg); |
| 655 |
|
| 656 |
# Should get EXISTS, RECENT response |
| 657 |
$res = $store->idle_response({}, 3); |
| 658 |
$self->assert($res, "received an unsolicited response"); |
| 659 |
$res = $store->idle_response({}, 3); |
| 660 |
$self->assert($res, "received an unsolicited response"); |
| 661 |
$res = $store->idle_response({}, 1); |
| 662 |
$self->assert(!$res, "no more unsolicited responses"); |
| 663 |
|
| 664 |
$self->assert_num_equals(1, $talk->get_response_code('exists')); |
| 665 |
$self->assert_num_equals(1, $talk->get_response_code('recent')); |
| 666 |
|
| 667 |
xlog $self, "Examine INBOX.foo"; |
| 668 |
$talk->examine("INBOX.foo"); |
| 669 |
$self->assert_num_equals(0, $talk->get_response_code('exists')); |
| 670 |
$self->assert_num_equals(0, $talk->get_response_code('recent')); |
| 671 |
$self->assert_num_equals(1, $talk->get_response_code('uidnext')); |
| 672 |
|
| 673 |
xlog $self, "MOVE message from INBOX to INBOX.foo in other session"; |
| 674 |
$othertalk->select("INBOX"); |
| 675 |
$res = $othertalk->move('1', "INBOX.foo"); |
| 676 |
|
| 677 |
# Should get EXISTS, RECENT response for INBOX.foo and STATUS response for INBOX |
| 678 |
$res = $store->idle_response({}, 3); |
| 679 |
$self->assert($res, "received an unsolicited response"); |
| 680 |
$res = $store->idle_response({}, 3); |
| 681 |
$self->assert($res, "received an unsolicited response"); |
| 682 |
$res = $store->idle_response('STATUS', 3); |
| 683 |
$self->assert($res, "received an unsolicited response"); |
| 684 |
$res = $store->idle_response({}, 1); |
| 685 |
$self->assert(!$res, "no more unsolicited responses"); |
| 686 |
|
| 687 |
$self->assert_num_equals(1, $talk->get_response_code('exists')); |
| 688 |
$self->assert_num_equals(1, $talk->get_response_code('recent')); |
| 689 |
|
| 690 |
my $status = $talk->get_response_code('status'); |
| 691 |
$self->assert_num_equals(0, $status->{'INBOX'}{messages}); |
| 692 |
$self->assert_num_equals(2, $status->{'INBOX'}{uidnext}); |
| 693 |
} |
| 694 |
|
| 695 |
1; |