|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.knox.gateway; |
| 19 | + |
| 20 | +import org.apache.knox.gateway.config.GatewayConfig; |
| 21 | +import org.easymock.EasyMock; |
| 22 | +import org.easymock.IMocksControl; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.junit.runners.Parameterized; |
| 27 | + |
| 28 | +import javax.servlet.FilterConfig; |
| 29 | +import javax.servlet.ServletConfig; |
| 30 | +import javax.servlet.ServletContext; |
| 31 | +import javax.servlet.ServletException; |
| 32 | +import javax.servlet.http.HttpServletRequest; |
| 33 | +import javax.servlet.http.HttpServletResponse; |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.Collection; |
| 37 | + |
| 38 | +import static org.junit.Assert.assertEquals; |
| 39 | +import static org.junit.Assert.assertNull; |
| 40 | + |
| 41 | +@RunWith(Parameterized.class) |
| 42 | +public class GatewayServletTest { |
| 43 | + |
| 44 | + private static final String IP_ADDRESS_PATTERN = "\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b"; |
| 45 | + private static final String EMAIL_ADDRESS_PATTERN = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b"; |
| 46 | + private static final String CREDIT_CARD_PATTERN = "\\b\\d{4}-\\d{4}-\\d{4}-\\d{4}\\b"; |
| 47 | + |
| 48 | + @Parameterized.Parameters(name = "{index}: SanitizationEnabled={2}, Pattern={3}, Exception={0}, ExpectedMessage={1}") |
| 49 | + public static Collection<Object[]> data() { |
| 50 | + return Arrays.asList(new Object[][] { |
| 51 | + { new IOException("Connection to 192.168.1.1 failed"), "Connection to [hidden] failed", true, IP_ADDRESS_PATTERN }, |
| 52 | + { new RuntimeException("Connection to 192.168.1.1 failed"), "Connection to [hidden] failed", true, IP_ADDRESS_PATTERN }, |
| 53 | + { new NullPointerException(), null, true, IP_ADDRESS_PATTERN }, |
| 54 | + { new IOException("General failure"), "General failure", true, IP_ADDRESS_PATTERN }, |
| 55 | + { new IOException("Connection to 192.168.1.1 failed"), "Connection to 192.168.1.1 failed", false, IP_ADDRESS_PATTERN }, |
| 56 | + { new RuntimeException("Connection to 192.168.1.1 failed"), "Connection to 192.168.1.1 failed", false, IP_ADDRESS_PATTERN }, |
| 57 | + { new NullPointerException(), null, false, IP_ADDRESS_PATTERN }, |
| 58 | + { new IOException("General failure"), "General failure", false, IP_ADDRESS_PATTERN }, |
| 59 | + { new IOException("User email: user@example.com"), "User email: [hidden]", true, EMAIL_ADDRESS_PATTERN }, |
| 60 | + { new RuntimeException("Credit card number: 1234-5678-9101-1121"), "Credit card number: [hidden]", true, CREDIT_CARD_PATTERN }, |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + private final Exception exception; |
| 65 | + private final String expectedMessage; |
| 66 | + private final boolean isSanitizationEnabled; |
| 67 | + private final String sanitizationPattern; |
| 68 | + |
| 69 | + public GatewayServletTest(Exception exception, String expectedMessage, boolean isSanitizationEnabled, String sanitizationPattern) { |
| 70 | + this.exception = exception; |
| 71 | + this.expectedMessage = expectedMessage; |
| 72 | + this.isSanitizationEnabled = isSanitizationEnabled; |
| 73 | + this.sanitizationPattern = sanitizationPattern; |
| 74 | + } |
| 75 | + |
| 76 | + private IMocksControl mockControl; |
| 77 | + private ServletConfig servletConfig; |
| 78 | + private ServletContext servletContext; |
| 79 | + private GatewayConfig gatewayConfig; |
| 80 | + private HttpServletRequest request; |
| 81 | + private HttpServletResponse response; |
| 82 | + private GatewayFilter filter; |
| 83 | + |
| 84 | + @Before |
| 85 | + public void setUp() throws ServletException { |
| 86 | + mockControl = EasyMock.createControl(); |
| 87 | + servletConfig = mockControl.createMock(ServletConfig.class); |
| 88 | + servletContext = mockControl.createMock(ServletContext.class); |
| 89 | + gatewayConfig = mockControl.createMock(GatewayConfig.class); |
| 90 | + request = mockControl.createMock(HttpServletRequest.class); |
| 91 | + response = mockControl.createMock(HttpServletResponse.class); |
| 92 | + filter = mockControl.createMock(GatewayFilter.class); |
| 93 | + |
| 94 | + EasyMock.expect(servletConfig.getServletName()).andStubReturn("default"); |
| 95 | + EasyMock.expect(servletConfig.getServletContext()).andStubReturn(servletContext); |
| 96 | + EasyMock.expect(servletContext.getAttribute("org.apache.knox.gateway.config")).andStubReturn(gatewayConfig); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testExceptionSanitization() throws ServletException, IOException { |
| 101 | + GatewayServlet servlet = initializeServletWithSanitization(isSanitizationEnabled, sanitizationPattern); |
| 102 | + |
| 103 | + try { |
| 104 | + servlet.service(request, response); |
| 105 | + } catch (Exception e) { |
| 106 | + if (expectedMessage != null) { |
| 107 | + assertEquals(expectedMessage, e.getMessage()); |
| 108 | + } else { |
| 109 | + assertNull(e.getMessage()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + mockControl.verify(); |
| 114 | + } |
| 115 | + |
| 116 | + private GatewayServlet initializeServletWithSanitization(boolean isErrorMessageSanitizationEnabled, String sanitizationPattern) throws ServletException, IOException { |
| 117 | + EasyMock.expect(gatewayConfig.isErrorMessageSanitizationEnabled()).andStubReturn(isErrorMessageSanitizationEnabled); |
| 118 | + EasyMock.expect(gatewayConfig.getErrorMessageSanitizationPattern()).andStubReturn(sanitizationPattern); |
| 119 | + |
| 120 | + filter.init(EasyMock.anyObject(FilterConfig.class)); |
| 121 | + EasyMock.expectLastCall().once(); |
| 122 | + filter.doFilter(EasyMock.eq(request), EasyMock.eq(response), EasyMock.isNull()); |
| 123 | + EasyMock.expectLastCall().andThrow(exception).once(); |
| 124 | + |
| 125 | + mockControl.replay(); |
| 126 | + |
| 127 | + GatewayServlet servlet = new GatewayServlet(filter); |
| 128 | + servlet.init(servletConfig); |
| 129 | + return servlet; |
| 130 | + } |
| 131 | +} |
0 commit comments