Adding some more test cases for BST Authenticator

merge-requests/1/head
megala21 7 years ago
parent d09222d972
commit 9766d392f5

@ -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);
}
}

@ -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();
}
}

@ -0,0 +1,11 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:w="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
>
<soap:Header>
<w:Security>
<w:BinarySecurityToken>test</w:BinarySecurityToken>
</w:Security>
</soap:Header>
<soap:Body>
</soap:Body>
</soap:Envelope>
Loading…
Cancel
Save