PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/driver/mina/src/test/java/com/starlight/intrepid/auth/AuthTest.java

https://bitbucket.org/robeden/intrepid
Java | 129 lines | 75 code | 24 blank | 30 comment | 7 complexity | 2fa1c5074f40bbe7a868e8a389d848ee MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. // Copyright (c) 2010 Rob Eden.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are met:
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above copyright
  9. // notice, this list of conditions and the following disclaimer in the
  10. // documentation and/or other materials provided with the distribution.
  11. // * Neither the name of Intrepid nor the
  12. // names of its contributors may be used to endorse or promote products
  13. // derived from this software without specific prior written permission.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. // DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  19. // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. package com.starlight.intrepid.auth;
  26. import com.starlight.intrepid.*;
  27. import com.starlight.intrepid.exception.ConnectionFailureException;
  28. import org.junit.After;
  29. import org.junit.Assert;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import java.net.InetAddress;
  33. import java.net.SocketAddress;
  34. /**
  35. *
  36. */
  37. public class AuthTest {
  38. private Intrepid client_instance = null;
  39. private Intrepid server_instance = null;
  40. @After
  41. public void tearDown() throws Exception {
  42. // Re-enable
  43. IntrepidTesting.setInterInstanceBridgeDisabled( false );
  44. if ( client_instance != null ) client_instance.close();
  45. if ( server_instance != null ) server_instance.close();
  46. }
  47. @Before
  48. public void setUp() throws Exception {
  49. // Make sure we test the full stack. See comment on
  50. // "Intrepid.disable_inter_instance_bridge" for more info.
  51. IntrepidTesting.setInterInstanceBridgeDisabled( true );
  52. server_instance = Intrepid.create( new IntrepidSetup().vmidHint( "server" )
  53. .serverPort( 0 )
  54. .authHandler( new UserTestAuthenticationHandler() ) );
  55. CommTest.ServerImpl original_instance =
  56. new CommTest.ServerImpl( true, server_instance.getLocalVMID() );
  57. server_instance.getLocalRegistry().bind( "server", original_instance );
  58. client_instance = Intrepid.create( new IntrepidSetup().vmidHint( "client" ) );
  59. }
  60. @Test( expected = ConnectionFailureException.class )
  61. public void testUserAuth_noCredentials() throws Exception {
  62. client_instance.connect( InetAddress.getLoopbackAddress(),
  63. server_instance.getServerPort(), null, null );
  64. }
  65. @Test( expected = ConnectionFailureException.class )
  66. public void testUserAuth_badUser() throws Exception {
  67. client_instance.connect( InetAddress.getLoopbackAddress(),
  68. server_instance.getServerPort(),
  69. new UserCredentialsConnectionArgs( "baduser", "blah".toCharArray() ), null );
  70. }
  71. @Test( expected = ConnectionFailureException.class )
  72. public void testUserAuth_badPassword() throws Exception {
  73. client_instance.connect( InetAddress.getLoopbackAddress(),
  74. server_instance.getServerPort(),
  75. new UserCredentialsConnectionArgs( "reden", "badpass".toCharArray() ), null );
  76. }
  77. @Test
  78. public void testUserAuth_succeed() throws Exception {
  79. VMID server_vmid = client_instance.connect(
  80. InetAddress.getLoopbackAddress(), server_instance.getServerPort(),
  81. new UserCredentialsConnectionArgs( "reden", "12345".toCharArray() ),
  82. null );
  83. Assert.assertEquals( server_instance.getLocalVMID(), server_vmid );
  84. }
  85. private class UserTestAuthenticationHandler implements AuthenticationHandler {
  86. @Override
  87. public UserContextInfo checkConnection( ConnectionArgs connection_args,
  88. SocketAddress remote_address, Object session_source )
  89. throws ConnectionAuthFailureException {
  90. if ( !( connection_args instanceof UserCredentialsConnectionArgs ) ) {
  91. throw new ConnectionAuthFailureException(
  92. "Bad args type: " + connection_args );
  93. }
  94. UserCredentialsConnectionArgs credentials =
  95. ( UserCredentialsConnectionArgs ) connection_args;
  96. if ( !"reden".equals( credentials.getUser() ) ) {
  97. throw new ConnectionAuthFailureException( "Bad user" );
  98. }
  99. if ( !"12345".equals( new String( credentials.getPassword() ) ) ) {
  100. throw new ConnectionAuthFailureException( "Bad password" );
  101. }
  102. return new SimpleUserContextInfo( credentials.getUser() );
  103. }
  104. }
  105. }