Adding more test cases for the api mgt handler component.

4.x.x
sinthuja 7 years ago
parent e2d4222f74
commit fd123af887

@ -25,7 +25,6 @@ import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.engine.AxisConfiguration; import org.apache.axis2.engine.AxisConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.http.ProtocolVersion; import org.apache.http.ProtocolVersion;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.entity.BasicHttpEntity; import org.apache.http.entity.BasicHttpEntity;
@ -43,21 +42,16 @@ import org.wso2.carbon.apimgt.handlers.invoker.RESTInvoker;
import org.wso2.carbon.apimgt.handlers.mock.MockClient; import org.wso2.carbon.apimgt.handlers.mock.MockClient;
import org.wso2.carbon.apimgt.handlers.mock.MockHttpResponse; import org.wso2.carbon.apimgt.handlers.mock.MockHttpResponse;
import org.wso2.carbon.apimgt.handlers.utils.AuthConstants; import org.wso2.carbon.apimgt.handlers.utils.AuthConstants;
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
import org.wso2.carbon.certificate.mgt.core.impl.CertificateGenerator;
import org.wso2.carbon.certificate.mgt.core.util.CertificateManagementConstants;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import javax.security.cert.X509Certificate;
/** /**
* This testcase will focus on covering the methods of {@link AuthenticationHandler} * This testcase will focus on covering the methods of {@link AuthenticationHandler}
@ -86,19 +80,17 @@ public class AuthenticationHandlerTest extends BaseAPIHandlerTest {
dependsOnMethods = "testHandleRequestWithEmptyTransportHeader") dependsOnMethods = "testHandleRequestWithEmptyTransportHeader")
public void testHandleRequestWithURISyntaxError() throws Exception { public void testHandleRequestWithURISyntaxError() throws Exception {
HashMap<String, String> transportHeaders = new HashMap<>(); HashMap<String, String> transportHeaders = new HashMap<>();
List<X509Certificate> certificates = loadCertificates(); transportHeaders.put(AuthConstants.MDM_SIGNATURE, "some cert");
transportHeaders.put(AuthConstants.MDM_SIGNATURE, new String(certificates.get(0).getSignature()));
boolean response = this.handler.handleRequest(createSynapseMessageContext("<empty/>", this.synapseConfiguration, boolean response = this.handler.handleRequest(createSynapseMessageContext("<empty/>", this.synapseConfiguration,
transportHeaders, "https://test.com/testservice")); transportHeaders, "https://test.com/testservice"));
Assert.assertFalse(response); Assert.assertFalse(response);
} }
@Test(description = "Handle request with device type URI", @Test(description = "Handle request with device type URI with MDM ceritificate",
dependsOnMethods = "testHandleRequestWithURISyntaxError") dependsOnMethods = "testHandleRequestWithURISyntaxError")
public void testHandleRequestWithDeviceTypeURI() throws Exception { public void testHandleSuccessfulRequestMDMCertificate() throws Exception {
HashMap<String, String> transportHeaders = new HashMap<>(); HashMap<String, String> transportHeaders = new HashMap<>();
List<X509Certificate> certificates = loadCertificates(); transportHeaders.put(AuthConstants.MDM_SIGNATURE, "some cert");
transportHeaders.put(AuthConstants.MDM_SIGNATURE, new String(certificates.get(0).getSignature()));
setMockClient(); setMockClient();
this.mockClient.setResponse(getDCRResponse()); this.mockClient.setResponse(getDCRResponse());
this.mockClient.setResponse(getAccessTokenReponse()); this.mockClient.setResponse(getAccessTokenReponse());
@ -109,6 +101,54 @@ public class AuthenticationHandlerTest extends BaseAPIHandlerTest {
this.mockClient.reset(); this.mockClient.reset();
} }
@Test(description = "Handle request with device type URI with Proxy Mutual Auth Header",
dependsOnMethods = "testHandleSuccessfulRequestMDMCertificate")
public void testHandleSuccessRequestProxyMutualAuthHeader() throws Exception {
HashMap<String, String> transportHeaders = new HashMap<>();
transportHeaders.put(AuthConstants.PROXY_MUTUAL_AUTH_HEADER, "Test Header");
setMockClient();
this.mockClient.setResponse(getAccessTokenReponse());
this.mockClient.setResponse(getValidationResponse());
boolean response = this.handler.handleRequest(createSynapseMessageContext("<empty/>", this.synapseConfiguration,
transportHeaders, "https://test.com/testservice/api/testdevice"));
Assert.assertTrue(response);
this.mockClient.reset();
}
@Test(description = "Handle request with device type URI with Mutual Auth Header",
dependsOnMethods = "testHandleSuccessRequestProxyMutualAuthHeader")
public void testHandleSuccessRequestMutualAuthHeader() throws Exception {
HashMap<String, String> transportHeaders = new HashMap<>();
transportHeaders.put(AuthConstants.MUTUAL_AUTH_HEADER, "Test Header");
setMockClient();
this.mockClient.setResponse(getAccessTokenReponse());
this.mockClient.setResponse(getValidationResponse());
MessageContext messageContext = createSynapseMessageContext("<empty/>", this.synapseConfiguration,
transportHeaders, "https://test.com/testservice/api/testdevice");
org.apache.axis2.context.MessageContext axisMC = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
String certStr = getContent(TestUtils.getAbsolutePathOfConfig("ra_cert.pem"));
X509Certificate cert = X509Certificate.getInstance(new ByteArrayInputStream(certStr.
getBytes(StandardCharsets.UTF_8.name())));
axisMC.setProperty(AuthConstants.CLIENT_CERTIFICATE, new X509Certificate[]{cert});
boolean response = this.handler.handleRequest(messageContext);
Assert.assertTrue(response);
this.mockClient.reset();
}
@Test(description = "Handle request with device type URI with Encoded Pem",
dependsOnMethods = "testHandleSuccessRequestMutualAuthHeader")
public void testHandleSuccessRequestEncodedPem() throws Exception {
HashMap<String, String> transportHeaders = new HashMap<>();
transportHeaders.put(AuthConstants.ENCODED_PEM, "encoded pem");
setMockClient();
this.mockClient.setResponse(getAccessTokenReponse());
this.mockClient.setResponse(getValidationResponse());
MessageContext messageContext = createSynapseMessageContext("<empty/>", this.synapseConfiguration,
transportHeaders, "https://test.com/testservice/api/testdevice");
boolean response = this.handler.handleRequest(messageContext);
Assert.assertTrue(response);
this.mockClient.reset();
}
private static MessageContext createSynapseMessageContext( private static MessageContext createSynapseMessageContext(
String payload, SynapseConfiguration config, HashMap<String, String> transportHeaders, String payload, SynapseConfiguration config, HashMap<String, String> transportHeaders,
@ -137,18 +177,6 @@ public class AuthenticationHandlerTest extends BaseAPIHandlerTest {
return synMc; return synMc;
} }
private List<X509Certificate> loadCertificates() throws IOException, KeystoreException {
File caPemFile = new File(TestUtils.getAbsolutePathOfConfig("ca_cert.pem"));
File raPemFile = new File(TestUtils.getAbsolutePathOfConfig("ra_cert.pem"));
byte[] ca = FileUtils.readFileToByteArray(caPemFile);
byte[] ra = FileUtils.readFileToByteArray(raPemFile);
List<X509Certificate> rootCertificates = new CertificateGenerator().getRootCertificates(ca, ra);
Assert.assertNotNull("Root certificates retrieved", rootCertificates);
Assert.assertEquals(rootCertificates.get(0).getType(), CertificateManagementConstants.X_509);
Assert.assertEquals(rootCertificates.get(1).getType(), CertificateManagementConstants.X_509);
return rootCertificates;
}
private void setMockClient() throws NoSuchFieldException, IllegalAccessException { private void setMockClient() throws NoSuchFieldException, IllegalAccessException {
Field restInvokerField = this.handler.getClass().getDeclaredField("restInvoker"); Field restInvokerField = this.handler.getClass().getDeclaredField("restInvoker");
restInvokerField.setAccessible(true); restInvokerField.setAccessible(true);
@ -200,7 +228,6 @@ public class AuthenticationHandlerTest extends BaseAPIHandlerTest {
return mockDCRResponse; return mockDCRResponse;
} }
private String getContent(String filePath) throws IOException { private String getContent(String filePath) throws IOException {
FileReader fileReader = new FileReader(filePath); FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader); BufferedReader bufferedReader = new BufferedReader(fileReader);

@ -1,34 +0,0 @@
-----BEGIN CERTIFICATE-----
MIIF+zCCA+OgAwIBAgIJAJE458QXNuiLMA0GCSqGSIb3DQEBBQUAMIGLMQswCQYD
VQQGEwJVUzENMAsGA1UECBMEVGVzdDENMAsGA1UEB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m3ujtCIHprjUgcn6aOR9Ms/NkUJCziKKAb4KoohNFgr/AgMBAAGjYDBeMB0G
A1UdDgQWBBSDhLDYVCYhJsxvK1ZNV05qGGVajjAfBgNVHSMEGDAWgBSDhLDYVCYh
JsxvK1ZNV05qGGVajjAPBgNVHRMBAf8EBTADAQH/MAsGA1UdDwQEAwIBhjANBgkq
hkiG9w0BAQUFAAOCAgEAykqOsxHV43Bx24+7DfxLNYyafBayHacQ4uwtldwexyQB
fIyJKjhzZUSvl37zhFPhJRJHogFIds+FoqaQsF8PvI/YSKs3UYRhje2mJan79lEA
rCd+3zDGmzQhmutVo7C1bCQuujV8YLIJGvvcnMcHnMLpc5CfjzmI2C6qMZ5XgpHx
/Mhindllqr0ZVvqRive0A2svW1k47XWB7BIfx/aoZ1viPHDNYVuYZ6j/NAFv8/Fu
3n/TfYOJ5rz0NPGHYXnmFcgGxtYTu5u6Q9YVdDLZv9lqYbMRSdiQ8SVDzwxft9N5
g6/VoXLoMpCS7/6jR3J0GbG2r/vr024QMOHDZHQDjkAVUBni6/bRHqj389RnOXhQ
+TSlx/hGgtdTpZRv63PjAqTCdDAhazWAgG/W+dxUhAywiOYHeXincuuDER0ypkfG
caUvbN9/mWtGJvtW+L9OlTj3LQlXD2ORehz5itS3eV0DVkscCOLzzkVLtIJeew1o
RmiADNOUe5A6V0cW5HIFi9F7Recqv9lGphwQeq+2cmvUKkSPcx+Z/SHTT/nIOioq
xxafJhci5dAEsPgtzxnA6QqPQtxOj46aZxQh5+hzZ/1CQq3UThDdQreJL51c+NOS
ZFQh6YVpJH6ZdSldBJnHjbS7RL/bv2kl1Pmv808T+iG+GpDw2XljwsI6TL8ACok=
-----END CERTIFICATE-----

@ -1163,7 +1163,12 @@
<artifactId>commons-codec</artifactId> <artifactId>commons-codec</artifactId>
<version>${version.commons.codec}</version> <version>${version.commons.codec}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${apache.http.compnents.core}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>commons-lang.wso2</groupId> <groupId>commons-lang.wso2</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>

Loading…
Cancel
Save