1 36 package org.columba.ristretto.imap; 37 38 import java.util.Calendar ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 import junit.framework.TestCase; 43 44 import org.columba.ristretto.coder.Base64; 45 import org.columba.ristretto.io.ConnectionDroppedException; 46 import org.columba.ristretto.log.RistrettoLogger; 47 import org.columba.ristretto.message.MailboxInfo; 48 import org.columba.ristretto.message.MimeTree; 49 import org.columba.ristretto.testserver.SimpleTestServerSession; 50 import org.columba.ristretto.testserver.TestServer; 51 52 public class IMAPProtocolTest extends TestCase { 53 54 55 private static final Logger LOG = Logger.getLogger("org.columba.ristretto.imap"); 56 57 private TestServer testServer; 58 boolean flagsUpdated = false; 59 60 public void testOpenCloseConnection() throws Exception { 61 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 62 testSession.addDialog("0 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n0 OK LOGOUT completed\r\n"); 63 64 testServer = new TestServer(50110, testSession); 65 66 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 67 68 protocol.openPort(); 69 assertEquals( IMAPProtocol.NON_AUTHENTICATED, protocol.getState()); 70 71 protocol.logout(); 72 assertEquals( IMAPProtocol.NOT_CONNECTED, protocol.getState()); 73 74 testServer.stop(); 75 System.out.println("--- next test"); 76 } 77 78 public void testPreAuthOpenCloseConnection() throws Exception { 79 SimpleTestServerSession testSession = new SimpleTestServerSession("* PREAUTH IMAP4rev1 server logged in as Smith\r\n", "LOGOUT"); 80 testSession.addDialog("0 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n0 OK LOGOUT completed\r\n"); 81 82 testServer = new TestServer(50110, testSession); 83 84 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 85 86 protocol.openPort(); 87 assertEquals( IMAPProtocol.AUTHENTICATED, protocol.getState()); 88 89 protocol.logout(); 90 assertEquals( IMAPProtocol.NOT_CONNECTED, protocol.getState()); 91 92 testServer.stop(); 93 System.out.println("--- next test"); 94 } 95 96 public void testConnectionRefused() throws Exception { 97 SimpleTestServerSession testSession = new SimpleTestServerSession("* BYE IMAP4rev1 Connection refused\r\n","LOGOUT"); 98 99 testServer = new TestServer(50110, testSession); 100 101 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 102 103 try { 104 protocol.openPort(); 105 fail(); 106 } catch (IMAPException e) { 107 assertEquals( "IMAP4rev1 Connection refused", e.getMessage() ); 108 } 109 assertEquals( IMAPProtocol.NOT_CONNECTED, protocol.getState()); 110 111 testServer.stop(); 112 System.out.println("--- next test"); 113 } 114 115 public void testCapabilitiy() throws Exception { 116 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 117 testSession.addDialog("0 CAPABILITY\r\n", "* CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI\r\n0 OK CAPABILITY completed\r\n"); 118 testSession.addDialog("1 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n1 OK LOGOUT completed\r\n"); 119 120 testServer = new TestServer(50110, testSession); 121 String [] capas = new String [] { "IMAP4rev1","STARTTLS","AUTH=GSSAPI"}; 122 123 124 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 125 126 protocol.openPort(); 127 String [] serverCaps = protocol.capability(); 128 for( int i=0; i<capas.length; i++) { 129 assertEquals(capas[i], serverCaps[i]); 130 } 131 protocol.logout(); 132 133 testServer.stop(); 134 System.out.println("--- next test"); 135 } 136 137 public void testNoop() throws Exception { 138 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 139 testSession.addDialog("0 NOOP\r\n", "0 OK NOOP completed\r\n"); 140 testSession.addDialog("1 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n1 OK LOGOUT completed\r\n"); 141 142 testServer = new TestServer(50110, testSession); 143 144 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 145 146 protocol.openPort(); 147 protocol.noop(); 148 protocol.logout(); 149 150 testServer.stop(); 151 System.out.println("--- next test"); 152 } 153 154 public void testAuthenticate() throws Exception { 155 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 156 testSession.addDialog("0 AUTHENTICATE LOGIN\r\n", "+ \r\n"); 157 testSession.addDialog(Base64.encode("test") + "\r\n", "+ \r\n"); 158 testSession.addDialog(Base64.encode("user") + "\r\n", "0 OK AUTHENTICATE LOGIN authentication successful\r\n"); 159 testSession.addDialog("1 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n1 OK LOGOUT completed\r\n"); 160 161 testServer = new TestServer(50110, testSession); 162 163 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 164 165 protocol.openPort(); 166 protocol.authenticate("LOGIN", "test", "user".toCharArray()); 167 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 168 protocol.logout(); 169 170 testServer.stop(); 171 System.out.println("--- next test"); 172 } 173 174 public void testLogin() throws Exception { 175 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 176 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 177 testSession.addDialog("1 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n1 OK LOGOUT completed\r\n"); 178 179 testServer = new TestServer(50110, testSession); 180 181 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 182 183 protocol.openPort(); 184 protocol.login("SMITH", "SESAME".toCharArray()); 185 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 186 protocol.logout(); 187 188 testServer.stop(); 189 System.out.println("--- next test"); 190 } 191 192 public void testDropConnection() throws Exception { 193 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 194 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN\0"); 195 196 testServer = new TestServer(50110, testSession); 197 198 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 199 200 protocol.openPort(); 201 try { 202 protocol.login("SMITH", "SESAME".toCharArray()); 203 assertTrue(false); 204 } catch (ConnectionDroppedException e) { 205 assertTrue(true); 206 207 } finally { 208 testServer.stop(); 209 System.out.println("--- next test"); 210 } 211 } 212 213 public void testCreate() throws Exception { 214 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 215 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 216 testSession.addDialog("1 CREATE owatagusiam/\r\n", "1 OK CREATE completed\r\n"); 217 218 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 219 220 testServer = new TestServer(50110, testSession); 221 222 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 223 224 protocol.openPort(); 225 protocol.login("SMITH", "SESAME".toCharArray()); 226 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 227 protocol.create("owatagusiam/"); 228 protocol.logout(); 229 230 testServer.stop(); 231 System.out.println("--- next test"); 232 } 233 234 public void testDelete() throws Exception { 235 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 236 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 237 testSession.addDialog("1 DELETE blurdybloop\r\n", "1 OK DELETE completed\r\n"); 238 239 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 240 241 testServer = new TestServer(50110, testSession); 242 243 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 244 245 protocol.openPort(); 246 protocol.login("SMITH", "SESAME".toCharArray()); 247 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 248 protocol.delete("blurdybloop"); 249 protocol.logout(); 250 251 testServer.stop(); 252 System.out.println("--- next test"); 253 } 254 255 public void testRename() throws Exception { 256 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 257 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 258 testSession.addDialog("1 RENAME blurdybloop sarasoop\r\n", "1 OK RENAME completed\r\n"); 259 260 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 261 262 testServer = new TestServer(50110, testSession); 263 264 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 265 266 protocol.openPort(); 267 protocol.login("SMITH", "SESAME".toCharArray()); 268 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 269 protocol.rename("blurdybloop", "sarasoop"); 270 protocol.logout(); 271 272 testServer.stop(); 273 System.out.println("--- next test"); 274 } 275 276 public void testSubscribe() throws Exception { 277 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 278 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN completed\r\n"); 279 testSession.addDialog("1 SUBSCRIBE #news.comp.mail.mime\r\n", "1 OK SUBSCRIBE completed\r\n"); 280 281 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 282 283 testServer = new TestServer(50110, testSession); 284 285 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 286 287 protocol.openPort(); 288 protocol.login("SMITH", "SESAME".toCharArray()); 289 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 290 protocol.subscribe("#news.comp.mail.mime"); 291 protocol.logout(); 292 293 testServer.stop(); 294 System.out.println("--- next test"); 295 } 296 297 public void testList() throws Exception { 298 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 299 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 300 testSession.addDialog("1 LIST ~/Mail/ \"%\"\r\n", "* LIST (\\Noselect) \"/\" ~/Mail/foo\r\n* LIST () \"/\" ~/Mail/meetings\r\n1 OK LIST completed\r\n"); 301 302 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 303 304 testServer = new TestServer(50110, testSession); 305 306 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 307 308 protocol.openPort(); 309 protocol.login("SMITH", "SESAME".toCharArray()); 310 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 311 ListInfo[] result = protocol.list("~/Mail/","%"); 312 assertEquals(2, result.length); 313 assertEquals( "~/Mail/foo", result[0].getName()); 314 assertEquals( "~/Mail/meetings", result[1].getName()); 315 316 protocol.logout(); 317 318 testServer.stop(); 319 System.out.println("--- next test"); 320 } 321 322 public void testNamespace() throws Exception { 323 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 324 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 325 testSession.addDialog("1 NAMESPACE\r\n", "* NAMESPACE ((\"\" \"/\")) NIL NIL\r\n1 OK NAMESPACE command completed\r\n"); 326 327 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 328 329 testServer = new TestServer(50110, testSession); 330 331 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 332 333 protocol.openPort(); 334 protocol.login("SMITH", "SESAME".toCharArray()); 335 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 336 337 NamespaceCollection result = protocol.namespace(); 338 339 assertEquals( "", result.getPersonalNamespace().getPrefix()); 340 assertEquals("/", result.getPersonalNamespace().getDelimiter()); 341 assertTrue( result.getOtherUserNamespace().isNil()); 342 assertTrue( result.getSharedNamespace().isNil()); 343 344 protocol.logout(); 345 346 testServer.stop(); 347 System.out.println("--- next test"); 348 } 349 350 public void testUnsubscribe() throws Exception { 351 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 352 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 353 testSession.addDialog("1 UNSUBSCRIBE #news.comp.mail.mime\r\n", "1 OK UNSUBSCRIBE completed\r\n"); 354 355 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 356 357 testServer = new TestServer(50110, testSession); 358 359 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 360 361 protocol.openPort(); 362 protocol.login("SMITH", "SESAME".toCharArray()); 363 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 364 protocol.unsubscribe("#news.comp.mail.mime"); 365 protocol.logout(); 366 367 testServer.stop(); 368 System.out.println("--- next test"); 369 } 370 371 372 public void testLsub() throws Exception { 373 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n", "LOGOUT"); 374 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 375 testSession.addDialog("1 LSUB #news. \"comp.mail.*\"\r\n", "* LSUB () \".\" #news.comp.mail.mime\r\n1 OK LSUB completed\r\n"); 376 377 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 378 379 testServer = new TestServer(50110, testSession); 380 381 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 382 383 protocol.openPort(); 384 protocol.login("SMITH", "SESAME".toCharArray()); 385 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 386 ListInfo[] result = protocol.lsub("#news.","comp.mail.*"); 387 assertEquals(1, result.length); 388 assertEquals( "#news.comp.mail.mime", result[0].getName()); 389 390 protocol.logout(); 391 392 testServer.stop(); 393 System.out.println("--- next test"); 394 } 395 396 public void testStatus() throws Exception { 397 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 398 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 399 testSession.addDialog("1 STATUS blurdybloop (UIDNEXT MESSAGES)\r\n", "* STATUS blurdybloop (MESSAGES 231 UIDNEXT 44292)\r\n1 OK STATUS completed\r\n"); 400 401 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 402 403 testServer = new TestServer(50110, testSession); 404 405 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 406 407 protocol.openPort(); 408 protocol.login("SMITH", "SESAME".toCharArray()); 409 MailboxStatus status = protocol.status("blurdybloop", new String [] {"UIDNEXT","MESSAGES"}); 410 assertEquals(status.getName(), "blurdybloop"); 411 412 protocol.logout(); 413 414 testServer.stop(); 415 } 416 417 public void testSelect() throws Exception { 418 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 419 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 420 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 421 testSession.addDialog("2 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n2 OK LOGOUT completed\r\n"); 422 423 testServer = new TestServer(50110, testSession); 424 425 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 426 427 protocol.openPort(); 428 protocol.login("SMITH", "SESAME".toCharArray()); 429 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 430 MailboxInfo info = protocol.select("blurdybloop"); 431 assertTrue(info.isWriteAccess()); 432 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 433 protocol.logout(); 434 435 testServer.stop(); 436 System.out.println("--- next test"); 437 } 438 439 public void testClose() throws Exception { 440 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 441 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 442 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 443 testSession.addDialog("2 CLOSE\r\n", "2 OK CLOSE completed\r\n"); 444 testSession.addDialog("3 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n3 OK LOGOUT completed\r\n"); 445 446 testServer = new TestServer(50110, testSession); 447 448 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 449 450 protocol.openPort(); 451 protocol.login("SMITH", "SESAME".toCharArray()); 452 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 453 protocol.select("blurdybloop"); 454 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 455 protocol.close(); 456 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 457 protocol.logout(); 458 459 testServer.stop(); 460 System.out.println("--- next test"); 461 } 462 463 public void testExpunge() throws Exception { 464 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 465 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 466 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 467 testSession.addDialog("2 EXPUNGE\r\n", "* 3 EXPUNGE\r\n* 3 EXPUNGE\r\n* 5 EXPUNGE\r\n* 7 EXPUNGE\r\n2 OK EXPUNGE completed\r\n"); 468 testSession.addDialog("3 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n3 OK LOGOUT completed\r\n"); 469 470 testServer = new TestServer(50110, testSession); 471 472 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 473 474 protocol.openPort(); 475 protocol.login("SMITH", "SESAME".toCharArray()); 476 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 477 protocol.select("blurdybloop"); 478 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 479 int[] expunged = protocol.expunge(); 480 assertEquals( 4, expunged.length); 481 protocol.logout(); 482 483 testServer.stop(); 484 System.out.println("--- next test"); 485 } 486 487 public void testSearch() throws Exception { 488 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 489 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 490 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 491 testSession.addDialog("2 SEARCH FLAGGED (SINCE 01-Feb-1994) NOT (FROM Smith)\r\n", "* SEARCH 2 84 882\r\n2 OK SEARCH completed\r\n"); 492 testSession.addDialog("3 SEARCH (TEXT \"string not in mailbox\")\r\n", "* SEARCH\r\n3 OK SEARCH completed\r\n"); 493 testSession.addDialog("4 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n4 OK LOGOUT completed\r\n"); 494 495 testServer = new TestServer(50110, testSession); 496 497 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 498 499 protocol.openPort(); 500 protocol.login("SMITH", "SESAME".toCharArray()); 501 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 502 protocol.select("blurdybloop"); 503 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 504 505 Calendar cal = Calendar.getInstance(); 506 cal.set(1994,1,1); 507 508 SearchKey[] testSearch = new SearchKey[] { 509 new SearchKey( SearchKey.FLAGGED), 510 new SearchKey( SearchKey.SINCE, new IMAPDate(cal.getTime())), 511 new SearchKey( SearchKey.NOT), 512 new SearchKey( SearchKey.FROM, "Smith") 513 }; 514 515 Integer [] result = protocol.search(testSearch); 516 assertEquals( 3, result.length); 517 518 testSearch = new SearchKey[] { 519 new SearchKey( SearchKey.TEXT, "string not in mailbox") 520 }; 521 result = protocol.search(testSearch); 522 assertEquals( 0, result.length); 523 524 protocol.logout(); 525 526 testServer.stop(); 527 System.out.println("--- next test"); 528 } 529 530 public void testStore() throws Exception { 531 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 532 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 533 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 534 testSession.addDialog("2 STORE 3 +FLAGS.SILENT (\\Flagged \\Deleted)\r\n", "* 3 FETCH (FLAGS (\\Flagged \\Deleted))\r\n2 OK STORE completed\r\n"); 535 testSession.addDialog("3 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n3 OK LOGOUT completed\r\n"); 536 537 testServer = new TestServer(50110, testSession); 538 539 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 540 541 protocol.openPort(); 542 protocol.login("SMITH", "SESAME".toCharArray()); 543 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 544 protocol.select("blurdybloop"); 545 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 546 IMAPFlags flags = new IMAPFlags(); 547 flags.setFlagged(true); 548 flags.setDeleted(true); 549 protocol.store(new SequenceSet(3), true, flags); 550 551 protocol.logout(); 552 553 testServer.stop(); 554 System.out.println("--- next test"); 555 } 556 557 public void testUnsolicitedQuit() throws Exception { 558 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 559 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 560 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n* BYE Astalavista\r\n"); 561 testSession.addDialog("2 STORE 3 +FLAGS.SILENT (\\Flagged \\Deleted)\r\n", "* 3 FETCH (FLAGS (\\Flagged \\Deleted))\r\n2 OK STORE completed\r\n"); 562 testSession.addDialog("3 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n3 OK LOGOUT completed\r\n"); 563 564 testServer = new TestServer(50110, testSession); 565 566 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 567 568 protocol.openPort(); 569 protocol.login("SMITH", "SESAME".toCharArray()); 570 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 571 protocol.select("blurdybloop"); 572 assertEquals( IMAPProtocol.NOT_CONNECTED, protocol.getState()); 573 574 testServer.stop(); 575 System.out.println("--- next test"); 576 } 577 578 public void testFetchFlags() throws Exception { 579 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 580 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 581 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 582 testSession.addDialog("2 FETCH 1:* (FLAGS UID)\r\n", "* 1 FETCH (FLAGS (\\Flagged \\Deleted))\r\n2 OK FETCH completed\r\n"); 583 testSession.addDialog("3 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n3 OK LOGOUT completed\r\n"); 584 585 testServer = new TestServer(50110, testSession); 586 587 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 588 589 protocol.openPort(); 590 protocol.login("SMITH", "SESAME".toCharArray()); 591 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 592 protocol.select("blurdybloop"); 593 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 594 595 IMAPFlags[] flags = protocol.fetchFlags(SequenceSet.getAll()); 596 assertEquals( 1, flags.length ); 597 assertTrue( flags[0].getFlagged() ); 598 assertTrue( flags[0].getDeleted() ); 599 600 protocol.logout(); 601 602 testServer.stop(); 603 System.out.println("--- next test"); 604 } 605 606 public void testFetchHeaderLiteral() throws Exception { 607 flagsUpdated = false; 608 609 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 610 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 611 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 612 testSession.addDialog("2 UID FETCH 192 (RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Subject Date From To Reply-To Message-ID In-Reply-To References)])\r\n", "* 169 FETCH (UID 192 RFC822.SIZE 5035 BODY[HEADER.FIELDS (\"Subject\" \"Date\" \"From\" \"To\" \"Reply-To\" \"Message-ID\" \"In-Reply-To\" \"References\")] {380}\r\nMessage-ID: <35703.194.39.131.39.1118302427.squirrel@194.39.131.39>\r\nIn-Reply-To: <1118300780.27726.14.camel@pcwab.lpzu.siemens.de>\r\nReferences: <1118300780.27726.14.camel@pcwab.lpzu.siemens.de>\r\nSubject: Re: [Columba-devel] JSCF 0.3 released\r\nFrom: \"Frederik Dietz\" <mail@frederikdietz.de>\r\nTo: columba-devel@lists.sourceforge.net\r\nDate: Thu, 9 Jun 2005 09:33:47 +0200 (CEST)\r\n\r\n)\r\n* 169 FETCH (FLAGS (\\Seen))\r\n2 OK FETCH completed.\r\n"); 613 testSession.addDialog("3 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n3 OK LOGOUT completed\r\n"); 614 615 testServer = new TestServer(50110, testSession); 616 617 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 618 619 protocol.openPort(); 620 protocol.login("SMITH", "SESAME".toCharArray()); 621 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 622 protocol.select("blurdybloop"); 623 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 624 625 protocol.addIMAPListener(new IMAPListener() { 626 627 public void connectionClosed(String message, String responseCode) { 628 630 } 631 632 public void flagsChanged(String mailbox, IMAPFlags flags) { 633 flagsUpdated = true; 634 } 635 636 public void existsChanged(String mailbox, int exists) { 637 639 } 640 641 public void recentChanged(String mailbox, int recent) { 642 644 } 645 646 public void parseError(String message) { 647 649 } 650 651 public void alertMessage(String message) { 652 654 } 655 656 public void warningMessage(String message) { 657 659 } 660 661 }); 662 663 IMAPHeader[] header = protocol.uidFetchHeaderFields(new SequenceSet(192),new String [] {"Subject", "Date", "From", "To", "Reply-To", "Message-ID", "In-Reply-To", "References"}); 664 assertEquals( 1, header.length ); 665 assertTrue( flagsUpdated ); 666 667 protocol.logout(); 668 669 testServer.stop(); 670 System.out.println("--- next test"); 671 } 672 673 674 675 public void testNoUnsolicitedFlagsResponse() throws Exception { 676 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 677 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 678 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 679 testSession.addDialog("2 UID STORE 134 +FLAGS.SILENT (JUNK)\r\n", "* FLAGS (JUNK \\Draft \\Answered \\Flagged \\Deleted \\Seen \\Recent)\r\n* OK [PERMANENTFLAGS (JUNK \\* \\Draft \\Answered \\Flagged \\Deleted \\Seen)] Limited\r\n2 OK STORE completed.\r\n"); 680 testSession.addDialog("3 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n3 OK LOGOUT completed\r\n"); 681 682 testServer = new TestServer(50110, testSession); 683 684 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 685 686 protocol.openPort(); 687 protocol.login("SMITH", "SESAME".toCharArray()); 688 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 689 protocol.select("blurdybloop"); 690 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 691 692 protocol.addIMAPListener(new IMAPListener() { 693 694 public void connectionClosed(String message, String responseCode) { 695 697 } 698 699 public void flagsChanged(String mailbox, IMAPFlags flags) { 700 assertTrue(false); 701 } 702 703 public void existsChanged(String mailbox, int exists) { 704 706 } 707 708 public void recentChanged(String mailbox, int recent) { 709 711 } 712 713 public void parseError(String message) { 714 716 } 717 718 public void alertMessage(String message) { 719 721 } 722 723 public void warningMessage(String message) { 724 726 }}); 727 IMAPFlags flags = new IMAPFlags(); 728 flags.setJunk(true); 729 protocol.uidStore(new SequenceSet(134), true, flags); 730 731 protocol.logout(); 732 733 testServer.stop(); 734 System.out.println("--- next test"); 735 } 736 737 738 public void testFetchBodystructure() throws Exception { 739 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 740 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 741 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 742 testSession.addDialog("2 UID FETCH 1216 BODYSTRUCTURE\r\n", "* 612 FETCH (FLAGS (\\SEEN))\r\n* 612 FETCH (UID 1216 BODYSTRUCTURE (\"TEXT\" \"PLAIN\" (\"CHARSET\" \"ISO-8859-1\") NIL NIL \"7BIT\" 1890 35 NIL NIL NIL))\r\n2 OK FETCH completed\r\n"); 743 testSession.addDialog("3 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n3 OK LOGOUT completed\r\n"); 744 745 testServer = new TestServer(50110, testSession); 746 747 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 748 749 protocol.openPort(); 750 protocol.login("SMITH", "SESAME".toCharArray()); 751 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 752 protocol.select("blurdybloop"); 753 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 754 755 MimeTree flags = protocol.uidFetchBodystructure(1216); 756 757 protocol.logout(); 758 759 testServer.stop(); 760 System.out.println("--- next test"); 761 } 762 public void testNoUnsolicitedFlagsResponse2() throws Exception { 763 SimpleTestServerSession testSession = new SimpleTestServerSession("* OK IMAP4rev1 Service Ready\r\n","LOGOUT"); 764 testSession.addDialog("0 LOGIN SMITH SESAME\r\n", "0 OK LOGIN copmleted\r\n"); 765 testSession.addDialog("1 SELECT blurdybloop\r\n", "1 OK [READ-WRITE] SELECT completed\r\n"); 766 testSession.addDialog("2 EXPUNGE\r\n", "* 495 EXPUNGE\r\n* 507 EXISTS\r\n* 76 RECENT\r\n* FLAGS (JUNK \\Draft \\Answered \\Flagged \\Deleted \\Seen \\Recent)\r\n* OK [PERMANENTFLAGS (JUNK \\* \\Draft \\Answered \\Flagged \\Deleted \\Seen)] Limited\r\n2 OK EXPUNGE completed.\r\n"); 767 testSession.addDialog("3 LOGOUT\r\n", "* BYE IMAP4rev1 Server logging out\r\n3 OK LOGOUT completed\r\n"); 768 769 testServer = new TestServer(50110, testSession); 770 771 IMAPProtocol protocol = new IMAPProtocol("localhost", 50110); 772 773 protocol.openPort(); 774 protocol.login("SMITH", "SESAME".toCharArray()); 775 assertEquals( protocol.getState(), IMAPProtocol.AUTHENTICATED); 776 protocol.select("blurdybloop"); 777 assertEquals( IMAPProtocol.SELECTED, protocol.getState()); 778 779 protocol.addIMAPListener(new IMAPListener() { 780 781 public void connectionClosed(String message, String responseCode) { 782 784 } 785 786 public void flagsChanged(String mailbox, IMAPFlags flags) { 787 assertTrue(false); 788 } 789 790 public void existsChanged(String mailbox, int exists) { 791 793 } 794 795 public void recentChanged(String mailbox, int recent) { 796 798 } 799 800 public void parseError(String message) { 801 803 } 804 805 public void alertMessage(String message) { 806 808 } 809 810 public void warningMessage(String message) { 811 813 }}); 814 IMAPFlags flags = new IMAPFlags(); 815 flags.setJunk(true); 816 int[] expunged = protocol.expunge(); 817 assertEquals(1,expunged.length); 818 assertEquals(495,expunged[0]); 819 820 protocol.logout(); 821 822 testServer.stop(); 823 System.out.println("--- next test"); 824 } 825 828 protected void setUp() throws Exception { 829 LOG.setLevel(Level.ALL); 830 RistrettoLogger.setLogStream(System.out); 831 } 832 835 protected void tearDown() throws Exception { 836 testServer.stop(); 837 } 838 } 839 | Popular Tags |