From 9766d392f590730cb31ec2d6aba53548f4a930f0 Mon Sep 17 00:00:00 2001 From: megala21 Date: Thu, 12 Oct 2017 00:38:35 +0530 Subject: [PATCH] Adding some more test cases for BST Authenticator --- .../authenticator/BSTAuthenticatorTest.java | 65 ++++++++++++++++++- .../framework/util/TestInputBuffer.java | 34 ++++++++++ .../src/test/resources/requests/BST.xml | 11 ++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/util/TestInputBuffer.java create mode 100644 components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/requests/BST.xml diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BSTAuthenticatorTest.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BSTAuthenticatorTest.java index 9eeaca651f..fb4122f437 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BSTAuthenticatorTest.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BSTAuthenticatorTest.java @@ -19,12 +19,27 @@ package org.wso2.carbon.webapp.authenticator.framework.authenticator; +import org.apache.catalina.Context; +import org.apache.catalina.connector.InputBuffer; +import org.apache.catalina.connector.Request; +import org.apache.catalina.core.StandardContext; +import org.apache.commons.io.FileUtils; +import org.apache.coyote.http11.filters.BufferedInputFilter; +import org.apache.tomcat.util.buf.ByteChunk; +import org.apache.tomcat.util.buf.MessageBytes; +import org.apache.tomcat.util.http.MimeHeaders; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import org.wso2.carbon.webapp.authenticator.framework.BaseWebAppAuthenticatorFrameworkTest; import org.wso2.carbon.webapp.authenticator.framework.authenticator.oauth.OAuth2TokenValidator; +import org.wso2.carbon.webapp.authenticator.framework.util.TestInputBuffer; +import javax.validation.constraints.AssertFalse; +import java.io.File; +import java.io.IOException; import java.lang.reflect.Field; +import java.net.URL; import java.util.Properties; /** @@ -33,11 +48,14 @@ import java.util.Properties; public class BSTAuthenticatorTest { private BSTAuthenticator bstAuthenticator; private Properties properties; + private Field headersField; @BeforeTest - public void init() { + public void init() throws NoSuchFieldException { bstAuthenticator = new BSTAuthenticator(); properties = new Properties(); + headersField = org.apache.coyote.Request.class.getDeclaredField("headers"); + headersField.setAccessible(true); } @Test(description = "This test case is used to test the behaviour of BST Authenticator when the properties are " @@ -89,4 +107,49 @@ public class BSTAuthenticatorTest { Assert.assertNotNull(oAuth2TokenValidator, "Token validation creation failed even with the required " + "parameters."); } + + @Test(description = "This test case tests the facanHandle method of the BSTAuthenticator") + public void testCanHandle() throws IllegalAccessException, IOException { + Request request = new Request(); + org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request(); + request.setCoyoteRequest(coyoteRequest); + Assert.assertFalse(bstAuthenticator.canHandle(request), + "BST Authenticator can handle a request without content type"); + + MimeHeaders mimeHeaders = new MimeHeaders(); + MessageBytes bytes = mimeHeaders.addValue("content-type"); + bytes.setString("test"); + headersField.set(coyoteRequest, mimeHeaders); + request.setCoyoteRequest(coyoteRequest); + Assert.assertFalse(bstAuthenticator.canHandle(request), + "BST Authenticator can handle a request with content type test"); + + ClassLoader classLoader = getClass().getClassLoader(); + URL resourceUrl = classLoader.getResource("requests" + File.separator + "BST.xml"); + File bst = new File(resourceUrl.getFile()); + String bytes1 = FileUtils.readFileToString(bst); + coyoteRequest = new org.apache.coyote.Request(); + +// coyoteRequest.setInputBuffer(byte); + mimeHeaders = new MimeHeaders(); + bytes = mimeHeaders.addValue("content-type"); + bytes.setString("application/xml"); + bytes = mimeHeaders.addValue("custom"); + bytes.setString(bytes1); + headersField.set(coyoteRequest, mimeHeaders); + MessageBytes messageBytes = coyoteRequest.getMimeHeaders().getValue("custom"); + bytes.toBytes(); + ByteChunk byteChunk = bytes.getByteChunk(); + + TestInputBuffer inputBuffer = new TestInputBuffer(); + + coyoteRequest.setInputBuffer(inputBuffer); + Context context = new StandardContext(); + request.setContext(context); + request.setCoyoteRequest(coyoteRequest); + bstAuthenticator.canHandle(request); + + + + } } diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/util/TestInputBuffer.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/util/TestInputBuffer.java new file mode 100644 index 0000000000..44d05b5391 --- /dev/null +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/util/TestInputBuffer.java @@ -0,0 +1,34 @@ +package org.wso2.carbon.webapp.authenticator.framework.util; + +import org.apache.catalina.connector.InputBuffer; +import org.apache.coyote.Request; +import org.apache.tomcat.util.buf.ByteChunk; +import org.apache.tomcat.util.buf.MessageBytes; +import org.apache.tomcat.util.http.MimeHeaders; + +import java.io.IOException; +import java.lang.reflect.Field; + +public class TestInputBuffer implements org.apache.coyote.InputBuffer { + @Override + public int doRead(ByteChunk byteChunk, Request request) throws IOException { + String string = request.getHeader("custom"); + MimeHeaders mimeHeaders = new MimeHeaders(); + Field byteC = null; + try { + byteC = MessageBytes.class.getDeclaredField("byteC"); + byteC.setAccessible(true); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + MessageBytes bytes = mimeHeaders.addValue("content-type"); + try { + byteC.set(bytes, byteChunk); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + bytes.setString(string); + bytes.toBytes(); + return byteChunk.getLength(); + } +} diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/requests/BST.xml b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/requests/BST.xml new file mode 100644 index 0000000000..66d70f5975 --- /dev/null +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/requests/BST.xml @@ -0,0 +1,11 @@ + + + + test + + + + + \ No newline at end of file