diff options
| author | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:06:30 +0000 |
|---|---|---|
| committer | Frederic Peters <fpeters@entrouvert.com> | 2008-04-29 12:06:30 +0000 |
| commit | 3b93e1b952d046ca20459194e8ea649e1e0794bc (patch) | |
| tree | d882a011f53ea0d7b53bcde22696d7d272bafa96 /bindings/java/tests | |
| parent | 34e4fd0b5aae872344a16267efac847f45108ca7 (diff) | |
| download | lasso-3b93e1b952d046ca20459194e8ea649e1e0794bc.tar.gz lasso-3b93e1b952d046ca20459194e8ea649e1e0794bc.tar.xz lasso-3b93e1b952d046ca20459194e8ea649e1e0794bc.zip | |
[project @ fpeters@0d.be-20080217115557-8qtcrc1vzb75f75c]
merged Benjamin branch
Original author: Frederic Peters <fpeters@0d.be>
Date: 2008-02-17 12:55:57.088000+01:00
Diffstat (limited to 'bindings/java/tests')
| -rw-r--r-- | bindings/java/tests/BindingTests.java | 282 | ||||
| -rw-r--r-- | bindings/java/tests/LoginTest.java | 244 | ||||
| -rw-r--r-- | bindings/java/tests/Test.java | 11 |
3 files changed, 537 insertions, 0 deletions
diff --git a/bindings/java/tests/BindingTests.java b/bindings/java/tests/BindingTests.java new file mode 100644 index 00000000..53a508df --- /dev/null +++ b/bindings/java/tests/BindingTests.java @@ -0,0 +1,282 @@ +/* + * $Id: BindingTests.java 3238 2007-05-30 17:24:50Z dlaniel $ + * + * Java unit tests for Lasso library + * + * Copyright (C) 2004-2007 Entr'ouvert + * http://LassoConstants.LASSO_entrouvert.org + * + * Authors: See AUTHORS file in top-level directory. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +// To run it: +// $ export LD_LIBRARY_PATH=../ +// $ javac -classpath /usr/share/java/junit.jar:../LassoConstants.LASSO_jar:. BindingTests.java +// $ java -classpath /usr/share/java/junit.jar:../LassoConstants.LASSO_jar:. BindingTests +// or for gcj: +// $ export LD_LIBRARY_PATH=../ +// $ gcj -C -classpath /usr/share/java/junit.jar:../LassoConstants.LASSO_jar:. BindingTests.java +// $ gij -classpath /usr/share/java/junit.jar:../LassoConstants.LASSO_jar:. BindingTests + + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.entrouvert.lasso.*; +import java.util.*; + + +public class BindingTests extends TestCase { + String[] toStringArray(Object[] array) { + String[] str = new String[array.length]; + int i; + for (i=0;i<array.length;i++) + str[i] = (String)array[i]; + return str; + } + SamlAssertion[] toSamlAssertionArray(Object[] array) { + SamlAssertion[] str = new SamlAssertion[array.length]; + int i; + for (i=0;i<array.length;i++) + str[i] = (SamlAssertion)array[i]; + return str; + } + public static void main(String args[]) { + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() { + return new TestSuite(BindingTests.class); + } + + public void test01() { + // Create and delete nodes. + + LibAuthnRequest authnRequest = new LibAuthnRequest(); + authnRequest = null; + } + + public void test02() { + // Get & set simple attributes of nodes. + + LibAuthnRequest authnRequest = new LibAuthnRequest(); + + // Test a string attribute. + assertNull(authnRequest.getConsent()); + authnRequest.setConsent(LassoConstants.LASSO_LIB_CONSENT_OBTAINED); + assertEquals(authnRequest.getConsent(), LassoConstants.LASSO_LIB_CONSENT_OBTAINED); + authnRequest.setConsent(null); + assertNull(authnRequest.getConsent()); + + // Test a renamed string attribute. + assertNull(authnRequest.getRelayState()); + authnRequest.setRelayState("Hello World!"); + assertEquals(authnRequest.getRelayState(), "Hello World!"); + authnRequest.setRelayState(null); + assertNull(authnRequest.getRelayState()); + + // Test an integer attribute. + assertEquals(authnRequest.getMajorVersion(), 0); + authnRequest.setMajorVersion(314); + assertEquals(authnRequest.getMajorVersion(), 314); + + authnRequest = null; + } + + public void test03() { + // Get & set attributes of nodes of type string list. + + LibAuthnRequest authnRequest = new LibAuthnRequest(); + + assertNull(authnRequest.getRespondWith()); + + List respondWith = new ArrayList(); + assertEquals(respondWith.size(), 0); + respondWith.add("first string"); + assertEquals(respondWith.size(), 1); + assertEquals(respondWith.get(0), "first string"); + assertEquals(respondWith.get(0), "first string"); + respondWith.add("second string"); + assertEquals(respondWith.size(), 2); + assertEquals(respondWith.get(0), "first string"); + assertEquals(respondWith.get(1), "second string"); + respondWith.add("third string"); + assertEquals(respondWith.size(), 3); + assertEquals(respondWith.get(0), "first string"); + assertEquals(respondWith.get(1), "second string"); + assertEquals(respondWith.get(2), "third string"); + authnRequest.setRespondWith(toStringArray(respondWith.toArray())); + assertEquals(authnRequest.getRespondWith()[0], "first string"); + assertEquals(authnRequest.getRespondWith()[1], "second string"); + assertEquals(authnRequest.getRespondWith()[2], "third string"); + assertEquals(respondWith.get(0), "first string"); + assertEquals(respondWith.get(1), "second string"); + assertEquals(respondWith.get(2), "third string"); + respondWith = null; + assertEquals(authnRequest.getRespondWith()[0], "first string"); + assertEquals(authnRequest.getRespondWith()[1], "second string"); + assertEquals(authnRequest.getRespondWith()[2], "third string"); + respondWith = Arrays.asList(authnRequest.getRespondWith()); + assertEquals(respondWith.get(0), "first string"); + assertEquals(respondWith.get(1), "second string"); + assertEquals(respondWith.get(2), "third string"); + respondWith = null; + assertEquals(authnRequest.getRespondWith()[0], "first string"); + assertEquals(authnRequest.getRespondWith()[1], "second string"); + assertEquals(authnRequest.getRespondWith()[2], "third string"); + authnRequest.setRespondWith(null); + System.out.println("coin"+authnRequest.getRespondWith()); + assertNull(authnRequest.getRespondWith()); + + authnRequest = null; + } + + public void test04() { + // Get & set attributes of nodes of type node list. + + SamlpResponse response = new SamlpResponse(); + + assertNull(response.getAssertion()); + + List assertions = new ArrayList(); + assertEquals(assertions.size(), 0); + SamlAssertion assertion1 = new SamlAssertion(); + assertion1.setAssertionId("assertion 1"); + assertions.add(assertion1); + assertEquals(assertions.size(), 1); + assertEquals(((SamlAssertion) assertions.get(0)).getAssertionId(), "assertion 1"); + assertEquals(((SamlAssertion) assertions.get(0)).getAssertionId(), "assertion 1"); + SamlAssertion assertion2 = new SamlAssertion(); + assertion2.setAssertionId("assertion 2"); + assertions.add(assertion2); + assertEquals(assertions.size(), 2); + assertEquals(((SamlAssertion) assertions.get(0)).getAssertionId(), "assertion 1"); + assertEquals(((SamlAssertion) assertions.get(1)).getAssertionId(), "assertion 2"); + SamlAssertion assertion3 = new SamlAssertion(); + assertion3.setAssertionId("assertion 3"); + assertions.add(assertion3); + assertEquals(assertions.size(), 3); + assertEquals(((SamlAssertion) assertions.get(0)).getAssertionId(), "assertion 1"); + assertEquals(((SamlAssertion) assertions.get(1)).getAssertionId(), "assertion 2"); + assertEquals(((SamlAssertion) assertions.get(2)).getAssertionId(), "assertion 3"); + response.setAssertion(toSamlAssertionArray(assertions.toArray())); + assertEquals(((SamlAssertion) response.getAssertion()[0]).getAssertionId(), + "assertion 1"); + assertEquals(((SamlAssertion) response.getAssertion()[1]).getAssertionId(), + "assertion 2"); + assertEquals(((SamlAssertion) response.getAssertion()[2]).getAssertionId(), + "assertion 3"); + assertEquals(((SamlAssertion) assertions.get(0)).getAssertionId(), "assertion 1"); + assertEquals(((SamlAssertion) assertions.get(1)).getAssertionId(), "assertion 2"); + assertEquals(((SamlAssertion) assertions.get(2)).getAssertionId(), "assertion 3"); + assertions = null;; + assertEquals(((SamlAssertion) response.getAssertion()[0]).getAssertionId(), + "assertion 1"); + assertEquals(((SamlAssertion) response.getAssertion()[1]).getAssertionId(), + "assertion 2"); + assertEquals(((SamlAssertion) response.getAssertion()[2]).getAssertionId(), + "assertion 3"); + assertions = Arrays.asList(response.getAssertion()); + assertEquals(((SamlAssertion) assertions.get(0)).getAssertionId(), "assertion 1"); + assertEquals(((SamlAssertion) assertions.get(1)).getAssertionId(), "assertion 2"); + assertEquals(((SamlAssertion) assertions.get(2)).getAssertionId(), "assertion 3"); + assertions = null; + assertEquals(((SamlAssertion) response.getAssertion()[0]).getAssertionId(), + "assertion 1"); + assertEquals(((SamlAssertion) response.getAssertion()[1]).getAssertionId(), + "assertion 2"); + assertEquals(((SamlAssertion) response.getAssertion()[2]).getAssertionId(), + "assertion 3"); + response.setAssertion(null); + assertNull(response.getAssertion()); + + response = null; + } + + public void test05() { + // Get & set attributes of nodes of type XML list. + + LibAuthnRequest authnRequest = new LibAuthnRequest(); + + assertNull(authnRequest.getExtension()); + + String actionString1 = "<lib:Extension xmlns:lib=\"urn:liberty:iff:2003-08\">\n" + + " <action>do 1</action>\n" + + "</lib:Extension>"; + String actionString2 = "<lib:Extension xmlns:lib=\"urn:liberty:iff:2003-08\">\n" + + " <action>do 2</action>\n" + + "</lib:Extension>"; + String actionString3 = "<lib:Extension xmlns:lib=\"urn:liberty:iff:2003-08\">\n" + + " <action>do 3</action>\n" + + "</lib:Extension>"; + List extension = new ArrayList(); + assertEquals(extension.size(), 0); + extension.add(actionString1); + assertEquals(extension.size(), 1); + assertEquals(extension.get(0), actionString1); + assertEquals(extension.get(0), actionString1); + extension.add(actionString2); + assertEquals(extension.size(), 2); + assertEquals(extension.get(0), actionString1); + assertEquals(extension.get(1), actionString2); + extension.add(actionString3); + assertEquals(extension.size(), 3); + assertEquals(extension.get(0), actionString1); + assertEquals(extension.get(1), actionString2); + assertEquals(extension.get(2), actionString3); + authnRequest.setExtension(toStringArray(extension.toArray())); + assertEquals(authnRequest.getExtension()[0], actionString1); + assertEquals(authnRequest.getExtension()[1], actionString2); + assertEquals(authnRequest.getExtension()[2], actionString3); + assertEquals(extension.get(0), actionString1); + assertEquals(extension.get(1), actionString2); + assertEquals(extension.get(2), actionString3); + extension = null; + assertEquals(authnRequest.getExtension()[0], actionString1); + assertEquals(authnRequest.getExtension()[1], actionString2); + assertEquals(authnRequest.getExtension()[2], actionString3); + extension = Arrays.asList(authnRequest.getExtension()); + assertEquals(extension.get(0), actionString1); + assertEquals(extension.get(1), actionString2); + assertEquals(extension.get(2), actionString3); + extension = null; + assertEquals(authnRequest.getExtension()[0], actionString1); + assertEquals(authnRequest.getExtension()[1], actionString2); + assertEquals(authnRequest.getExtension()[2], actionString3); + authnRequest.setExtension(null); + assertNull(authnRequest.getExtension()); + + authnRequest = null; + } + + public void test06() { + // Get & set attributes of nodes of type node. + + Login login = new Login(new Server(null, null, null, null)); + + assertNull(login.getRequest()); + login.setRequest((SamlpRequestAbstract) new LibAuthnRequest()); + ((LibAuthnRequest) login.getRequest()).setConsent(LassoConstants.LASSO_LIB_CONSENT_OBTAINED); + assertEquals(((LibAuthnRequest) login.getRequest()).getConsent(), + LassoConstants.LASSO_LIB_CONSENT_OBTAINED); + login.setRequest(null); + assertNull(login.getRequest()); + + login = null; + } +} diff --git a/bindings/java/tests/LoginTest.java b/bindings/java/tests/LoginTest.java new file mode 100644 index 00000000..755ffd7b --- /dev/null +++ b/bindings/java/tests/LoginTest.java @@ -0,0 +1,244 @@ +/* + * $Id: LoginTest.java 3307 2007-06-13 13:17:51Z dlaniel $ + * + * Java unit tests for Lasso library + * + * Copyright (C) 2004-2007 Entr'ouvert + * http://lasso.entrouvert.org + * + * Authors: See AUTHORS file in top-level directory. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +// To run it: +// $ export LD_LIBRARY_PATH=../ +// $ javac -classpath /usr/share/java/junit.jar:../lasso.jar:. LoginTest.java +// $ java -classpath /usr/share/java/junit.jar:../lasso.jar:. LoginTest +// or for gcj: +// $ export LD_LIBRARY_PATH=../ +// $ gcj -C -classpath /usr/share/java/junit.jar:../lasso.jar:. LoginTest.java +// $ gij -classpath /usr/share/java/junit.jar:../lasso.jar:. LoginTest + + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.entrouvert.lasso.*; + +public class LoginTest extends TestCase { + public String generateIdentityProviderDump() { + Server server = new Server( + "../../tests/data/idp1-la/metadata.xml", + "../../tests/data/idp1-la/private-key-raw.pem", + null, + "../../tests/data/idp1-la/certificate.pem"); + server.addProvider( + lasso.PROVIDER_ROLE_SP, + "../../tests/data/sp1-la/metadata.xml", + "../../tests/data/sp1-la/public-key.pem", + "../../tests/data/ca1-la/certificate.pem"); + String serverDump = server.dump(); + return serverDump; + } + + public String generateServiceProviderDump() { + Server server = new Server( + "../../tests/data/sp1-la/metadata.xml", + "../../tests/data/sp1-la/private-key-raw.pem", + null, + "../../tests/data/sp1-la/certificate.pem"); + server.addProvider( + lasso.PROVIDER_ROLE_IDP, + "../../tests/data/idp1-la/metadata.xml", + "../../tests/data/idp1-la/public-key.pem", + "../../tests/data/ca1-la/certificate.pem"); + String serverDump = server.dump(); + return serverDump; + } + + public void test01_generateServersDumps() { + String identityProviderDump = generateIdentityProviderDump(); + assertNotNull(identityProviderDump); + String serviceProviderDump = generateServiceProviderDump(); + assertNotNull(serviceProviderDump); + } + + public void test02_serviceProviderLogin() { + boolean userAuthenticated, userConsentObtained; + int method, requestType; + LibAuthnRequest authnRequest; + Identity idpIdentity, spIdentity; + Login idpLogin, spLogin; + Logout idpLogout, spLogout; + SamlAssertion assertion; + Server idp, sp; + Session spSession; + String artifact, authenticationMethod, authnRequestQuery, authnRequestUrl, idpDump, + idpIdentityDump, idpRemoteProviderId, idpSessionDump, nameIdentifier, relayState, + responseQuery, responseUrl, soapEndpoint, soapResponseMsg, soapRequestMsg, + spDump, spIdentityDump, spSessionDump; + + // Service provider login using HTTP redirect. + spDump = generateServiceProviderDump(); + assertNotNull(spDump); + sp = Server.newFromDump(spDump); + spLogin = new Login(sp); + spLogin.initAuthnRequest("https://idp1/metadata", lasso.HTTP_METHOD_REDIRECT); + authnRequest = (LibAuthnRequest) spLogin.getRequest(); + authnRequest.setIsPassive(false); + authnRequest.setNameIdPolicy(lasso.LIB_NAMEID_POLICY_TYPE_FEDERATED); + authnRequest.setConsent(lasso.LIB_CONSENT_OBTAINED); + relayState = "fake"; + authnRequest.setRelayState(relayState); + spLogin.buildAuthnRequestMsg(); + authnRequestUrl = spLogin.getMsgUrl(); + authnRequestQuery = authnRequestUrl.substring(authnRequestUrl.indexOf("?") + 1); + + // Identity provider singleSignOn, for a user having no federation. + idpDump = generateIdentityProviderDump(); + assertNotNull(idpDump); + idp = Server.newFromDump(idpDump); + idpLogin = new Login(idp); + idpLogin.processAuthnRequestMsg(authnRequestQuery); + assertTrue(idpLogin.mustAuthenticate()); + assertFalse(idpLogin.mustAskForConsent()); + + userAuthenticated = true; + userConsentObtained = false; + idpLogin.validateRequestMsg(userAuthenticated, userConsentObtained); + authenticationMethod = lasso.SAML_AUTHENTICATION_METHOD_PASSWORD; + idpLogin.buildAssertion( + authenticationMethod, + null, // authenticationInstant + null, // reauthenticateOnOrAfter + null, // notBefore + null);// notOnOrAfter + assertEquals(lasso.LOGIN_PROTOCOL_PROFILE_BRWS_ART, idpLogin.getProtocolProfile()); + idpLogin.buildArtifactMsg(lasso.HTTP_METHOD_REDIRECT); + idpIdentityDump = idpLogin.getIdentity().dump(); + assertNotNull(idpIdentityDump); + idpSessionDump = idpLogin.getSession().dump(); + assertNotNull(idpSessionDump); + responseUrl = idpLogin.getMsgUrl(); + responseQuery = responseUrl.substring(responseUrl.indexOf("?") + 1); + idpRemoteProviderId = idpLogin.getRemoteProviderId(); + nameIdentifier = ((SamlNameIdentifier)idpLogin.getNameIdentifier()).getContent(); + artifact = idpLogin.getAssertionArtifact(); + assertNotNull(artifact); + method = lasso.HTTP_METHOD_REDIRECT; + + // Service provider assertion consumer. + spDump = generateServiceProviderDump(); + assertNotNull(spDump); + sp = Server.newFromDump(spDump); + spLogin = new Login(sp); + spLogin.initRequest(responseQuery, method); + spLogin.buildRequestMsg(); + soapEndpoint = spLogin.getMsgUrl(); + assertNotNull(soapEndpoint); + soapRequestMsg = spLogin.getMsgBody(); + assertNotNull(soapRequestMsg); + + // Identity provider SOAP endpoint. + requestType = lasso.getRequestTypeFromSoapMsg(soapRequestMsg); + assertEquals(lasso.REQUEST_TYPE_LOGIN, requestType); + idpDump = generateIdentityProviderDump(); + assertNotNull(idpDump); + idp = Server.newFromDump(idpDump); + idpLogin = new Login(idp); + idpLogin.processRequestMsg(soapRequestMsg); + assertEquals(artifact, idpLogin.getAssertionArtifact()); + assertNotNull(idpSessionDump); + idpLogin.setSessionFromDump(idpSessionDump); + idpLogin.buildResponseMsg(idpRemoteProviderId); + soapResponseMsg = idpLogin.getMsgBody(); + assertNotNull(soapResponseMsg); + + // Service provider assertion consumer (step 2: process SOAP response). + spLogin.processResponseMsg(soapResponseMsg); + assertEquals(nameIdentifier, ((SamlNameIdentifier)spLogin.getNameIdentifier()).getContent()); + // The user doesn't have any federation yet. + spLogin.acceptSso(); + spIdentity = spLogin.getIdentity(); + assertNotNull(spIdentity); + spIdentityDump = spIdentity.dump(); + assertNotNull(spIdentityDump); + spSession = spLogin.getSession(); + assertNotNull(spSession); + spSessionDump = spSession.dump(); + assertNotNull(spSessionDump); + assertion = (SamlAssertion) spSession.getAssertions("https://idp1/metadata").getItem(0); + authenticationMethod = assertion.getAuthenticationStatement().getAuthenticationMethod(); + assertEquals(lasso.SAML_AUTHENTICATION_METHOD_PASSWORD, authenticationMethod); + + // Service provider logout. + spDump = generateServiceProviderDump(); + assertNotNull(spDump); + sp = Server.newFromDump(spDump); + assertNotNull(sp); + spLogout = new Logout(sp); + assertNotNull(spIdentityDump); + spLogout.setIdentityFromDump(spIdentityDump); + assertNotNull(spSessionDump); + spLogout.setSessionFromDump(spSessionDump); + spLogout.initRequest(null, lasso.HTTP_METHOD_ANY); + spLogout.buildRequestMsg(); + soapEndpoint = spLogout.getMsgUrl(); + soapRequestMsg = spLogout.getMsgBody(); + + // Identity provider SOAP endpoint. + requestType = lasso.getRequestTypeFromSoapMsg(soapRequestMsg); + assertEquals(lasso.REQUEST_TYPE_LOGOUT, requestType); + idpDump = generateIdentityProviderDump(); + assertNotNull(idpDump); + idp = Server.newFromDump(idpDump); + assertNotNull(idp); + idpLogout = new Logout(idp); + idpLogout.processRequestMsg(soapRequestMsg); + assertEquals(nameIdentifier, ((SamlNameIdentifier)idpLogout.getNameIdentifier()).getContent()); + assertNotNull(idpIdentityDump); + idpLogout.setIdentityFromDump(idpIdentityDump); + assertNotNull(idpSessionDump); + idpLogout.setSessionFromDump(idpSessionDump); + idpLogout.validateRequest(); + idpIdentity = idpLogout.getIdentity(); + assertNotNull(idpIdentity); + idpIdentityDump = idpIdentity.dump(); + assertNotNull(idpIdentityDump); + // There is no other service provider from which the user must be logged out. + assertEquals(null, idpLogout.getNextProviderId()); + idpLogout.buildResponseMsg(); + soapResponseMsg = idpLogout.getMsgBody(); + + // Service provider logout (step 2: process SOAP response). + spLogout.processResponseMsg(soapResponseMsg); + spIdentityDump = spLogout.getIdentity().dump(); + assertNotNull(spIdentityDump); + } + + public static Test suite() { + return new TestSuite(LoginTest.class); + } + + public static void main(String args[]) { + System.out.println(System.mapLibraryName("jlasso")); + lasso.init(); + junit.textui.TestRunner.run(suite()); + lasso.shutdown(); + } +} + diff --git a/bindings/java/tests/Test.java b/bindings/java/tests/Test.java new file mode 100644 index 00000000..4e1450df --- /dev/null +++ b/bindings/java/tests/Test.java @@ -0,0 +1,11 @@ +import com.entrouvert.lasso.*; + +class Test { + + public static void main(String args[]) { + Samlp2AuthnRequest p; + + p = new Samlp2AuthnRequest(); + System.out.println(p.dump()); + } +} |
