/*
 * Copyright 2020 Netflix, Inc.
 *
 *      Licensed 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 com.netflix.netty.common.proxyprotocol;

import static com.netflix.zuul.netty.server.ssl.SslHandshakeInfoHandler.ATTR_SSL_INFO;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableList;
import com.netflix.netty.common.proxyprotocol.StripUntrustedProxyHeadersHandler.AllowWhen;
import com.netflix.netty.common.ssl.SslHandshakeInfo;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.ssl.ClientAuth;
import io.netty.util.AttributeKey;
import io.netty.util.DefaultAttributeMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

/**
 * Strip Untrusted Proxy Headers Handler Test
 *
 * @author Arthur Gonigberg
 * @since May 27, 2020
 */
@RunWith(MockitoJUnitRunner.class)
public class StripUntrustedProxyHeadersHandlerTest {

    @Mock
    private ChannelHandlerContext channelHandlerContext;
    @Mock
    private HttpRequest msg;
    @Mock
    private HttpHeaders headers;
    @Mock
    private Channel channel;
    @Mock
    private SslHandshakeInfo sslHandshakeInfo;


    @Before
    public void before() {
        when(channelHandlerContext.channel()).thenReturn(channel);

        DefaultAttributeMap attributeMap = new DefaultAttributeMap();
        attributeMap.attr(ATTR_SSL_INFO).set(sslHandshakeInfo);
        when(channel.attr(any())).thenAnswer(arg -> attributeMap.attr((AttributeKey) arg.getArguments()[0]));

        when(msg.headers()).thenReturn(headers);
        when(headers.get(eq(HttpHeaderNames.HOST))).thenReturn("netflix.com");
    }

    @Test
    public void allow_never() throws Exception {
        StripUntrustedProxyHeadersHandler stripHandler = getHandler(AllowWhen.NEVER);

        stripHandler.channelRead(channelHandlerContext, msg);

        verify(stripHandler).stripXFFHeaders(any());
    }

    @Test
    public void allow_always() throws Exception {
        StripUntrustedProxyHeadersHandler stripHandler = getHandler(AllowWhen.ALWAYS);

        stripHandler.channelRead(channelHandlerContext, msg);

        verify(stripHandler, never()).stripXFFHeaders(any());
        verify(stripHandler).checkBlacklist(any(), any());
    }

    @Test
    public void allow_mtls_noCert() throws Exception {
        StripUntrustedProxyHeadersHandler stripHandler = getHandler(AllowWhen.MUTUAL_SSL_AUTH);

        stripHandler.channelRead(channelHandlerContext, msg);

        verify(stripHandler).stripXFFHeaders(any());
    }

    @Test
    public void allow_mtls_cert() throws Exception {
        StripUntrustedProxyHeadersHandler stripHandler = getHandler(AllowWhen.MUTUAL_SSL_AUTH);
        when(sslHandshakeInfo.getClientAuthRequirement()).thenReturn(ClientAuth.REQUIRE);

        stripHandler.channelRead(channelHandlerContext, msg);

        verify(stripHandler, never()).stripXFFHeaders(any());
        verify(stripHandler).checkBlacklist(any(), any());
    }

    @Test
    public void blacklist_noMatch() throws Exception {
        StripUntrustedProxyHeadersHandler stripHandler = getHandler(AllowWhen.MUTUAL_SSL_AUTH);

        stripHandler.checkBlacklist(msg, ImmutableList.of("netflix.net"));

        verify(stripHandler, never()).stripXFFHeaders(any());
    }

    @Test
    public void blacklist_match() throws Exception {
        StripUntrustedProxyHeadersHandler stripHandler = getHandler(AllowWhen.MUTUAL_SSL_AUTH);

        stripHandler.checkBlacklist(msg, ImmutableList.of("netflix.com"));

        verify(stripHandler).stripXFFHeaders(any());
    }

    private StripUntrustedProxyHeadersHandler getHandler(AllowWhen allowWhen) {
        return spy(new StripUntrustedProxyHeadersHandler(allowWhen));
    }

}
@@ -794,36 +794,32 @@
 org.
-mock
+jun
 it
-o
 .A
-rgumentMatchers.any
+ssert.assertFalse
 ;%0Aim
@@ -859,18 +859,19 @@
 atchers.
-eq
+any
 ;%0Aimport
@@ -1299,16 +1299,71 @@
 ontext;%0A
+import io.netty.handler.codec.http.DefaultHttpHeaders;%0A
 import i
@@ -2074,26 +2074,16 @@
 st msg;%0A
-    @Mock%0A
     priv
@@ -2553,24 +2553,68 @@
 ts()%5B0%5D));%0A%0A
+        headers = new DefaultHttpHeaders();%0A
         when
@@ -2662,21 +2662,16 @@
     
-when(
 headers.
 get(
@@ -2670,14 +2670,11 @@
 ers.
-get(eq
+add
 (Htt
@@ -2694,23 +2694,10 @@
 HOST
-))).thenReturn(
+, 
 %22net
@@ -4094,33 +4094,16 @@
 oMatch()
- throws Exception
  %7B%0A     
@@ -4382,33 +4382,16 @@
 _match()
- throws Exception
  %7B%0A     
@@ -4554,24 +4554,24 @@
 ix.com%22));%0A%0A
-
         veri
@@ -4614,24 +4614,320 @@
 ());%0A    %7D%0A%0A
+    @Test%0A    public void strip_match() %7B%0A        StripUntrustedProxyHeadersHandler stripHandler = getHandler(AllowWhen.MUTUAL_SSL_AUTH);%0A%0A        headers.add(%22x-forwarded-for%22, %22abcd%22);%0A        stripHandler.stripXFFHeaders(msg);%0A%0A        assertFalse(headers.contains(%22x-forwarded-for%22));%0A    %7D%0A%0A
     private 

add test for strip