/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.shardingproxy.transport.mysql.packet.handshake;

import com.google.common.primitives.Bytes;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.rule.Authentication;
import org.apache.shardingsphere.shardingproxy.context.ShardingProxyContext;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Field;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public final class MySQLAuthenticationHandlerTest {
    
    private final MySQLAuthenticationHandler authenticationHandler = new MySQLAuthenticationHandler();
    
    private final byte[] part1 = {84, 85, 115, 77, 68, 116, 85, 78};

    private final byte[] part2 = {83, 121, 75, 81, 87, 56, 120, 112, 73, 109, 77, 69};
    
    @Before
    public void setUp() {
        initAuthPluginDataForAuthenticationHandler();
    }
    
    @SneakyThrows
    private void initAuthPluginDataForAuthenticationHandler() {
        MySQLAuthPluginData authPluginData = new MySQLAuthPluginData(part1, part2);
        Field field = MySQLAuthenticationHandler.class.getDeclaredField("authPluginData");
        field.setAccessible(true);
        field.set(authenticationHandler, authPluginData);
    }
    
    @Test
    public void assertLoginWithPassword() {
        setAuthentication(new Authentication("root", "root"));
        byte[] authResponse = {-27, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113};
        assertTrue(authenticationHandler.login("root", authResponse));
    }
    
    @Test
    public void assertLoginWithoutPassword() {
        setAuthentication(new Authentication("root", null));
        byte[] authResponse = {-27, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113};
        assertTrue(authenticationHandler.login("root", authResponse));
    }
    
    @SneakyThrows
    private void setAuthentication(final Authentication authentication) {
        Field field = ShardingProxyContext.class.getDeclaredField("authentication");
        field.setAccessible(true);
        field.set(ShardingProxyContext.getInstance(), authentication);
    }
    
    @Test
    public void assertGetAuthPluginData() {
        assertThat(authenticationHandler.getAuthPluginData().getAuthPluginData(), is(Bytes.concat(part1, part2)));
    }
}

@@ -1009,16 +1009,65 @@
 cation;%0A
+import org.apache.shardingsphere.core.rule.User;%0A
 import o
@@ -2219,38 +2219,28 @@
 ication(new 
-Authentication
+User
 (%22root%22, %22ro
@@ -2237,20 +2237,19 @@
 root%22, %22
-root
+db1
 %22));%0A   
@@ -2550,29 +2550,17 @@
 new 
-Authentication(%22root%22
+User(null
 , nu
@@ -2838,16 +2838,37 @@
 n(final 
+User user) %7B%0A        
 Authenti
@@ -2888,19 +2888,93 @@
 tication
-) %7B
+ = new Authentication();%0A        authentication.getUsers().put(%22root%22, user);
 %0A       

modify unit cases in MySQLAuthenticationHandlerTest.java