1 package org.jacorb.test.orb.connection; 2 3 import org.jacorb.orb.giop.*; 4 import org.jacorb.orb.iiop.*; 5 6 import java.io.*; 7 import java.util.*; 8 9 import org.omg.ETF.BufferHolder; 10 import org.omg.ETF.Profile; 11 import org.omg.GIOP.*; 12 import org.jacorb.orb.*; 13 14 import junit.framework.*; 15 import org.jacorb.test.common.*; 16 17 import org.jacorb.config.Configuration; 18 19 29 30 public class GIOPConnectionTest 31 extends JacORBTestCase 32 { 33 private Configuration config; 34 private ORB orb; 35 36 public void setUp() 37 throws Exception 38 { 39 orb = (ORB) ORB.init(new String [0], null); 40 config = Configuration.getConfiguration(null, orb, false); 41 } 42 43 public static Test suite() 44 { 45 TestSuite suite = new JacORBTestSuite ("GIOPConnection Test", 46 GIOPConnectionTest.class); 47 suite.addTest (new GIOPConnectionTest ("testGIOP_1_0_CorrectRefusing")); 48 suite.addTest (new GIOPConnectionTest ("testGIOP_1_1_IllegalMessageType")); 49 suite.addTest (new GIOPConnectionTest ("testGIOP_1_1_NoImplement")); 50 suite.addTest (new GIOPConnectionTest ("testGIOP_1_2_CorrectFragmentedRequest")); 51 52 return suite; 53 } 54 55 56 private class DummyTransport extends org.omg.ETF._ConnectionLocalBase 57 { 58 private boolean closed = false; 59 private byte[] data = null; 60 private int index = 0; 61 private ByteArrayOutputStream b_out = new ByteArrayOutputStream(); 62 private org.omg.ETF.Profile profile = new IIOPProfile 63 ( 64 new IIOPAddress ("127.0.0.1", 4711), 65 null 66 ); 67 68 public DummyTransport( List messages ) 69 { 70 72 int size = 0; 73 for (Iterator i = messages.iterator(); i.hasNext();) 74 { 75 size += ((byte[])i.next()).length; 76 } 77 data = new byte[size]; 78 int index = 0; 79 for (Iterator i = messages.iterator(); i.hasNext();) 80 { 81 byte[] msg = (byte[])i.next(); 82 System.arraycopy(msg, 0, data, index, msg.length); 83 index += msg.length; 84 } 85 } 86 87 public byte[] getWrittenMessage() 88 { 89 return b_out.toByteArray(); 90 } 91 92 public void connect (org.omg.ETF.Profile profile, long time_out) 93 { 94 } 96 97 public boolean hasBeenClosed() 98 { 99 return closed; 100 } 101 102 public boolean is_connected() 103 { 104 return !closed; 105 } 106 107 public void write( boolean is_first, boolean is_last, 108 byte[] message, int start, int size, 109 long timeout ) 110 { 111 b_out.write( message, start, size ); 112 } 113 114 115 public void flush() 116 { 117 } 118 119 public void close() 120 { 121 closed = true; 122 } 123 124 public boolean isSSL() 125 { 126 return false; 127 } 128 129 public void turnOnFinalTimeout() 130 { 131 } 132 133 public Profile get_server_profile() 134 { 135 return profile; 136 } 137 138 public void read (BufferHolder data, int offset, 139 int min_length, int max_length, long time_out) 140 { 141 if (this.index + min_length > this.data.length) 142 { 143 throw new org.omg.CORBA.COMM_FAILURE ("end of stream"); 144 } 145 System.arraycopy(this.data, this.index, data.value, offset, min_length); 146 this.index += min_length; 147 } 148 149 public boolean is_data_available() 150 { 151 return true; 152 } 153 154 public boolean supports_callback() 155 { 156 return false; 157 } 158 159 public boolean use_handle_time_out() 160 { 161 return false; 162 } 163 164 public boolean wait_next_data(long time_out) 165 { 166 return false; 167 } 168 169 } 170 171 172 private class DummyRequestListener 173 implements RequestListener 174 { 175 private byte[] request = null; 176 177 public DummyRequestListener() 178 { 179 } 180 181 public byte[] getRequest() 182 { 183 return request; 184 } 185 186 public void requestReceived( byte[] request, 187 GIOPConnection connection ) 188 { 189 this.request = request; 190 } 191 192 public void locateRequestReceived( byte[] request, 193 GIOPConnection connection ) 194 { 195 this.request = request; 196 } 197 public void cancelRequestReceived( byte[] request, 198 GIOPConnection connection ) 199 { 200 this.request = request; 201 } 202 } 203 204 private class DummyReplyListener 205 implements ReplyListener 206 { 207 private byte[] reply = null; 208 209 public DummyReplyListener() 210 { 211 } 212 213 public byte[] getReply() 214 { 215 return reply; 216 } 217 218 public void replyReceived( byte[] reply, 219 GIOPConnection connection ) 220 { 221 this.reply = reply; 222 } 223 224 public void locateReplyReceived( byte[] reply, 225 GIOPConnection connection ) 226 { 227 this.reply = reply; 228 } 229 230 public void closeConnectionReceived( byte[] close_conn, 231 GIOPConnection connection ) 232 { 233 this.reply = close_conn; 234 } 235 236 } 237 238 public GIOPConnectionTest( String name ) 239 { 240 super( name ); 241 } 242 243 public void testGIOP_1_2_CorrectFragmentedRequest() 244 { 245 List messages = new Vector(); 246 247 RequestOutputStream r_out = 248 new RequestOutputStream( (ClientConnection) null, 0, "foo", true, (short)-1, null, null, null, new byte[1], 2 ); 259 260 r_out.write_ulong( 6 ); r_out.write_octet( (byte) 'b' ); 263 r_out.write_octet( (byte) 'a' ); 264 r_out.write_octet( (byte) 'r' ); 265 r_out.insertMsgSize(); 266 267 byte[] b = r_out.getBufferCopy(); 268 269 b[6] |= 0x02; 271 messages.add( b ); 272 273 MessageOutputStream m_out = 274 new MessageOutputStream(); 275 m_out.writeGIOPMsgHeader( MsgType_1_1._Fragment, 276 2 ); 278 m_out.write_ulong( 0 ); m_out.write_octet( (byte) 'b' ); 280 m_out.write_octet( (byte) 'a' ); 281 m_out.write_octet( (byte) 'z' ); 282 m_out.insertMsgSize(); 283 284 messages.add( m_out.getBufferCopy() ); 285 286 DummyTransport transport = 287 new DummyTransport( messages ); 288 289 DummyRequestListener request_listener = 290 new DummyRequestListener(); 291 292 DummyReplyListener reply_listener = 293 new DummyReplyListener(); 294 295 GIOPConnectionManager giopconn_mg = 296 new GIOPConnectionManager(); 297 try 298 { 299 giopconn_mg.configure (config); 300 } 301 catch (Exception e) 302 { 303 } 304 305 ServerGIOPConnection conn = 306 giopconn_mg.createServerGIOPConnection( null, 307 transport, 308 request_listener, 309 reply_listener ); 310 311 try 312 { 313 conn.receiveMessages(); 316 } 317 catch( IOException e ) 318 { 319 } 321 catch( Exception e ) 322 { 323 e.printStackTrace(); 324 fail( "Caught exception: " + e ); 325 } 326 327 assertTrue( request_listener.getRequest() != null ); 330 331 RequestInputStream r_in = 332 new RequestInputStream( null, request_listener.getRequest() ); 333 334 assertEquals( "barbaz", r_in.read_string() ); 336 } 337 338 public void testGIOP_1_0_CorrectRefusing() 339 { 340 List messages = new Vector(); 341 342 RequestOutputStream r_out = 343 new RequestOutputStream( null, 0, "foo", true, (short)-1, null, null, null, new byte[1], 0 ); 354 355 r_out.write_string( "bar" ); 356 r_out.insertMsgSize(); 357 358 byte[] b = r_out.getBufferCopy(); 359 360 b[6] |= 0x02; 362 messages.add( b ); 363 364 DummyTransport transport = 365 new DummyTransport( messages ); 366 367 DummyRequestListener request_listener = 368 new DummyRequestListener(); 369 370 DummyReplyListener reply_listener = 371 new DummyReplyListener(); 372 373 GIOPConnectionManager giopconn_mg = 374 new GIOPConnectionManager(); 375 try 376 { 377 giopconn_mg.configure (config); 378 } 379 catch (Exception e) 380 { 381 } 382 383 GIOPConnection conn = 384 giopconn_mg.createServerGIOPConnection( null, 385 transport, 386 request_listener, 387 reply_listener ); 388 389 try 390 { 391 conn.receiveMessages(); 394 } 395 catch( IOException e ) 396 { 397 } 399 catch( Exception e ) 400 { 401 e.printStackTrace(); 402 fail( "Caught exception: " + e ); 403 } 404 405 assertTrue( request_listener.getRequest() == null ); 407 assertTrue( reply_listener.getReply() == null ); 408 409 assertTrue( transport.getWrittenMessage() != null ); 412 413 byte[] result = transport.getWrittenMessage(); 414 415 assertTrue( Messages.getMsgType( result ) == MsgType_1_1._MessageError ); 416 MessageOutputStream m_out = 417 new MessageOutputStream(); 418 m_out.writeGIOPMsgHeader( MsgType_1_1._Fragment, 419 0 ); 421 m_out.write_ulong( 0 ); m_out.write_octet( (byte) 'b' ); 423 m_out.write_octet( (byte) 'a' ); 424 m_out.write_octet( (byte) 'z' ); 425 m_out.insertMsgSize(); 426 427 messages.add( m_out.getBufferCopy() ); 428 429 try 430 { 431 conn.receiveMessages(); 434 } 435 catch( IOException e ) 436 { 437 } 439 catch( Exception e ) 440 { 441 e.printStackTrace(); 442 fail( "Caught exception: " + e ); 443 } 444 445 assertTrue( request_listener.getRequest() == null ); 447 assertTrue( reply_listener.getReply() == null ); 448 449 assertTrue( transport.getWrittenMessage() != null ); 452 453 assertTrue( transport.getWrittenMessage() != result ); 455 result = transport.getWrittenMessage(); 456 457 assertTrue( Messages.getMsgType( result ) == MsgType_1_1._MessageError ); 458 459 } 460 461 public void testGIOP_1_1_IllegalMessageType() 462 { 463 List messages = new Vector(); 464 465 LocateRequestOutputStream r_out = 466 new LocateRequestOutputStream( 467 new byte[1], 0, 1 ); 471 472 r_out.insertMsgSize(); 473 474 byte[] b = r_out.getBufferCopy(); 475 476 b[6] |= 0x02; 478 messages.add( b ); 479 480 483 DummyTransport transport = 484 new DummyTransport( messages ); 485 486 DummyRequestListener request_listener = 487 new DummyRequestListener(); 488 489 DummyReplyListener reply_listener = 490 new DummyReplyListener(); 491 492 GIOPConnectionManager giopconn_mg = 493 new GIOPConnectionManager(); 494 try 495 { 496 giopconn_mg.configure (config); 497 } 498 catch (Exception e) 499 { 500 } 501 502 GIOPConnection conn = 503 giopconn_mg.createServerGIOPConnection( null, 504 transport, 505 request_listener, 506 reply_listener ); 507 508 try 509 { 510 conn.receiveMessages(); 513 } 514 catch( IOException e ) 515 { 516 } 518 catch( Exception e ) 519 { 520 e.printStackTrace(); 521 fail( "Caught exception: " + e ); 522 } 523 524 assertTrue( request_listener.getRequest() == null ); 526 assertTrue( reply_listener.getReply() == null ); 527 528 assertTrue( transport.getWrittenMessage() != null ); 531 532 byte[] result = transport.getWrittenMessage(); 533 534 assertTrue( Messages.getMsgType( result ) == MsgType_1_1._MessageError ); 535 } 536 537 public void testGIOP_1_1_NoImplement() 538 { 539 List messages = new Vector(); 540 541 RequestOutputStream r_out = 542 new RequestOutputStream( null, 0, "foo", true, (short)-1, null, null, null, new byte[1], 1 ); 553 554 r_out.write_string( "bar" ); 555 r_out.insertMsgSize(); 556 557 byte[] b = r_out.getBufferCopy(); 558 559 b[6] |= 0x02; 561 messages.add( b ); 562 563 DummyTransport transport = 564 new DummyTransport( messages ); 565 566 DummyRequestListener request_listener = 567 new DummyRequestListener(); 568 569 DummyReplyListener reply_listener = 570 new DummyReplyListener(); 571 572 GIOPConnectionManager giopconn_mg = 573 new GIOPConnectionManager(); 574 try 575 { 576 giopconn_mg.configure (config); 577 } 578 catch (Exception e) 579 { 580 } 581 582 GIOPConnection conn = 583 giopconn_mg.createServerGIOPConnection( null, 584 transport, 585 request_listener, 586 reply_listener ); 587 588 try 589 { 590 conn.receiveMessages(); 593 } 594 catch( IOException e ) 595 { 596 } 598 catch( Exception e ) 599 { 600 e.printStackTrace(); 601 fail( "Caught exception: " + e ); 602 } 603 604 assertTrue( request_listener.getRequest() == null ); 606 assertTrue( reply_listener.getReply() == null ); 607 608 assertTrue( transport.getWrittenMessage() != null ); 611 612 byte[] result = transport.getWrittenMessage(); 613 614 ReplyInputStream r_in = new ReplyInputStream( null, result ); 615 616 Exception ex = r_in.getException(); 617 if ( ex != null && ex.getClass() == org.omg.CORBA.NO_IMPLEMENT .class ) 618 { 619 } 621 else 622 { 623 fail(); 624 } 625 626 MessageOutputStream m_out = 627 new MessageOutputStream(); 628 m_out.writeGIOPMsgHeader( MsgType_1_1._Fragment, 629 1 ); 631 m_out.write_ulong( 0 ); m_out.write_octet( (byte) 'b' ); 633 m_out.write_octet( (byte) 'a' ); 634 m_out.write_octet( (byte) 'z' ); 635 m_out.insertMsgSize(); 636 637 messages.add( m_out.getBufferCopy() ); 638 639 try 640 { 641 conn.receiveMessages(); 644 } 645 catch( IOException e ) 646 { 647 } 649 catch( Exception e ) 650 { 651 e.printStackTrace(); 652 fail( "Caught exception: " + e ); 653 } 654 655 assertTrue( request_listener.getRequest() == null ); 657 assertTrue( reply_listener.getReply() == null ); 658 659 } 661 } | Popular Tags |