package com.splicemachine.derby.security;

import com.splicemachine.derby.test.framework.SpliceNetConnection;
import com.splicemachine.derby.test.framework.SpliceUserWatcher;
import org.junit.Rule;
import org.junit.Test;

import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;

public class AuthenticationIT {

    private static final String AUTH_IT_USER = "auth_it_user";
    private static final String AUTH_IT_PASS = "test_password";

    @Rule
    public SpliceUserWatcher spliceUserWatcher1 = new SpliceUserWatcher(AUTH_IT_USER, AUTH_IT_PASS);

    @Test
    public void valid() throws SQLException {
        SpliceNetConnection.getConnectionAs(AUTH_IT_USER, AUTH_IT_PASS);
    }

    @Test
    public void validUsernameIsNotCaseSensitive() throws SQLException {
        SpliceNetConnection.getConnectionAs(AUTH_IT_USER.toUpperCase(), AUTH_IT_PASS);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // bad password
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    @Test(expected = SQLNonTransientConnectionException.class)
    public void badPassword() throws SQLException {
        SpliceNetConnection.getConnectionAs(AUTH_IT_USER, "bad_password");
    }

    @Test(expected = SQLNonTransientConnectionException.class)
    public void badPasswordExtraCharAtStart() throws SQLException {
        SpliceNetConnection.getConnectionAs(AUTH_IT_USER, "a" + AUTH_IT_PASS);
    }

    @Test(expected = SQLNonTransientConnectionException.class)
    public void badPasswordExtraCharAtEnd() throws SQLException {
        SpliceNetConnection.getConnectionAs(AUTH_IT_USER, AUTH_IT_PASS + "a");
    }

    @Test(expected = SQLNonTransientConnectionException.class)
    public void badPasswordCase() throws SQLException {
        SpliceNetConnection.getConnectionAs(AUTH_IT_USER, AUTH_IT_PASS.toUpperCase());
    }

    @Test(expected = SQLNonTransientConnectionException.class)
    public void badPasswordZeroLength() throws SQLException {
        SpliceNetConnection.getConnectionAs(AUTH_IT_USER, "");
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // bad username
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    @Test(expected = SQLNonTransientConnectionException.class)
    public void badUsername() throws SQLException {
        SpliceNetConnection.getConnectionAs("bad_username", AUTH_IT_PASS);
    }


}

@@ -168,16 +168,41 @@
 atcher;%0A
+import org.junit.Assert;%0A
 import o
@@ -240,16 +240,47 @@
 .Test;%0A%0A
+import java.sql.DriverManager;%0A
 import j
@@ -352,16 +352,83 @@
 ception;
+%0Aimport java.util.Properties;%0A%0Aimport static org.junit.Assert.fail;
 %0A%0Apublic
@@ -2575,10 +2575,453 @@
     %7D%0A%0A%0A
+    //DB-4618%0A    @Test%0A    public void invalidDbname() throws SQLException %7B%0A        String url = %22jdbc:splice://localhost:1527/anotherdb;user=user;password=passwd%22;%0A        try %7B%0A            DriverManager.getConnection(url, new Properties());%0A            fail(%22Expected authentication failure%22);%0A        %7D catch (SQLNonTransientConnectionException e) %7B%0A            Assert.assertTrue(e.getSQLState().compareTo(%2208004%22) == 0);%0A        %7D%0A    %7D%0A
 %7D%0A

add an IT