diff options
author | Emmanuel Raviart <eraviart@entrouvert.com> | 2005-01-19 21:12:33 +0000 |
---|---|---|
committer | Emmanuel Raviart <eraviart@entrouvert.com> | 2005-01-19 21:12:33 +0000 |
commit | 82aebdd0afcb4110fe6ca1ce9c38434a264013f3 (patch) | |
tree | d67decf7347f75f5e48da3e6b87049e09d65fa84 | |
parent | 979dda50ed08dd29b90b0b40fe4f9e9aafd59ddb (diff) | |
download | lasso-82aebdd0afcb4110fe6ca1ce9c38434a264013f3.tar.gz lasso-82aebdd0afcb4110fe6ca1ce9c38434a264013f3.tar.xz lasso-82aebdd0afcb4110fe6ca1ce9c38434a264013f3.zip |
Improved C# binding.
-rw-r--r-- | csharp/Makefile.am | 6 | ||||
-rw-r--r-- | csharp/tests/BindingTests.cs | 306 | ||||
-rw-r--r-- | java/tests/BindingTests.java | 7 | ||||
-rw-r--r-- | swig/Lasso.i | 143 |
4 files changed, 446 insertions, 16 deletions
diff --git a/csharp/Makefile.am b/csharp/Makefile.am index af06d358..e54fa9fc 100644 --- a/csharp/Makefile.am +++ b/csharp/Makefile.am @@ -43,9 +43,9 @@ clean-local: SWIG_FILES = liblassosharpglue_wrap.c \ Credentials.cs Defederation.cs Description.cs DiscoModify.cs \ DiscoModifyResponse.cs DiscoQuery.cs DiscoQueryResponse.cs \ - Discovery.cs DstModification.cs DstModify.cs DstModifyResponse.cs \ - DstQuery.cs DstQueryResponse.cs Federation.cs Identity.cs \ - InsertEntry.cs LassoHttpMethod.cs LassoLoginProtocolProfile.cs \ + Discovery.cs DowncastableNode.cs DstModification.cs DstModify.cs \ + DstModifyResponse.cs DstQuery.cs DstQueryResponse.cs Federation.cs \ + Identity.cs InsertEntry.cs LassoHttpMethod.cs LassoLoginProtocolProfile.cs \ LassoMessageType.cs LassoProviderRole.cs LassoRequestType.cs \ LassoSignatureMethod.cs Lecp.cs LibAssertion.cs LibAuthnRequest.cs \ LibAuthnResponse.cs LibFederationTerminationNotification.cs \ diff --git a/csharp/tests/BindingTests.cs b/csharp/tests/BindingTests.cs new file mode 100644 index 00000000..b88bed94 --- /dev/null +++ b/csharp/tests/BindingTests.cs @@ -0,0 +1,306 @@ +/* + * $Id$ + * + * C# unit tests for Lasso library + * + * Copyright (C) 2004, 2005 Entr'ouvert + * http://lasso.entrouvert.org + * + * Authors: See AUTHORS file. + * + * 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, install Lasso then compile with: + * export PKG_CONFIG_PATH=../ + * ln -s ../lasso.dll + * ln -s ../lasso.dll.config + * mcs -g -nologo -pkg:lasso-sharp -out:BindingTests.exe BindingTests.cs + */ + +using System; + +public class BindingTests { + static void assertEquals(int i1, int i2) { + if (i1 != i2) + Console.WriteLine("Assertion failed: %d != %d", i1, i2); + } + + static void assertEquals(String s1, String s2) { + if (s1 != s2) + Console.WriteLine("Assertion failed: %s != %s", s1, s2); + } + + static void assertNull(Object o) { + if (o != null) + Console.WriteLine("Assertion failed: %s is not null", o); + } + + static void assertNull(String s) { + if (s != null) + Console.WriteLine("Assertion failed: %s is not null", s); + } + + static void Main() { + lasso.lasso.init(); + test01(); + test02(); + test03(); + test04(); + test05(); + test06(); + lasso.lasso.shutdown(); + } + + static void test01() { + Console.Write("Create and delete nodes."); + + lasso.LibAuthnRequest authnRequest = new lasso.LibAuthnRequest(); + authnRequest = null; + + Console.WriteLine(".. OK"); + } + + static void test02() { + Console.Write("Get & set simple attributes of nodes."); + + lasso.LibAuthnRequest authnRequest = new lasso.LibAuthnRequest(); + + // Test a string attribute. + assertNull(authnRequest.consent); + authnRequest.consent = lasso.lasso.libConsentObtained; + assertEquals(authnRequest.consent, lasso.lasso.libConsentObtained); + authnRequest.consent = null; + assertNull(authnRequest.consent); + + // Test a renamed string attribute. + assertNull(authnRequest.relayState); + authnRequest.relayState = "Hello World!"; + assertEquals(authnRequest.relayState, "Hello World!"); + authnRequest.relayState = null; + assertNull(authnRequest.relayState); + + // Test an integer attribute. + assertEquals(authnRequest.majorVersion, 0); + authnRequest.majorVersion = 314; + assertEquals(authnRequest.majorVersion, 314); + + authnRequest = null; + + Console.WriteLine(".. OK"); + } + + static void test03() { + Console.Write("Get & set attributes of nodes of type string list."); + + lasso.LibAuthnRequest authnRequest = new lasso.LibAuthnRequest(); + + assertNull(authnRequest.respondWith); + + lasso.StringList respondWith = new lasso.StringList(); + assertEquals(respondWith.length(), 0); + respondWith.append("first string"); + assertEquals(respondWith.length(), 1); + assertEquals(respondWith.getItem(0), "first string"); + assertEquals(respondWith.getItem(0), "first string"); + respondWith.append("second string"); + assertEquals(respondWith.length(), 2); + assertEquals(respondWith.getItem(0), "first string"); + assertEquals(respondWith.getItem(1), "second string"); + respondWith.append("third string"); + assertEquals(respondWith.length(), 3); + assertEquals(respondWith.getItem(0), "first string"); + assertEquals(respondWith.getItem(1), "second string"); + assertEquals(respondWith.getItem(2), "third string"); + authnRequest.respondWith = respondWith; + assertEquals(authnRequest.respondWith.getItem(0), "first string"); + assertEquals(authnRequest.respondWith.getItem(1), "second string"); + assertEquals(authnRequest.respondWith.getItem(2), "third string"); + assertEquals(respondWith.getItem(0), "first string"); + assertEquals(respondWith.getItem(1), "second string"); + assertEquals(respondWith.getItem(2), "third string"); + respondWith = null; + assertEquals(authnRequest.respondWith.getItem(0), "first string"); + assertEquals(authnRequest.respondWith.getItem(1), "second string"); + assertEquals(authnRequest.respondWith.getItem(2), "third string"); + respondWith = authnRequest.respondWith; + assertEquals(respondWith.getItem(0), "first string"); + assertEquals(respondWith.getItem(1), "second string"); + assertEquals(respondWith.getItem(2), "third string"); + respondWith = null; + assertEquals(authnRequest.respondWith.getItem(0), "first string"); + assertEquals(authnRequest.respondWith.getItem(1), "second string"); + assertEquals(authnRequest.respondWith.getItem(2), "third string"); + authnRequest.respondWith = null; + assertNull(authnRequest.respondWith); + + authnRequest = null; + + Console.WriteLine(".. OK"); + } + + static void test04() { + Console.Write("Get & set attributes of nodes of type node list."); + + lasso.SamlpResponse response = new lasso.SamlpResponse(); + + assertNull(response.assertion); + lasso.NodeList assertions = new lasso.NodeList(); + assertEquals(assertions.length(), 0); + lasso.SamlAssertion assertion1 = new lasso.SamlAssertion(); + assertion1.assertionId = "assertion 1"; + assertions.append(assertion1); + assertEquals(assertions.length(), 1); + assertEquals(((lasso.SamlAssertion) assertions.getItem(0)).assertionId, + "assertion 1"); + assertEquals(((lasso.SamlAssertion) assertions.getItem(0)).assertionId, + "assertion 1"); + lasso.SamlAssertion assertion2 = new lasso.SamlAssertion(); + assertion2.assertionId = "assertion 2"; + assertions.append(assertion2); + assertEquals(assertions.length(), 2); + assertEquals(((lasso.SamlAssertion) assertions.getItem(0)).assertionId, + "assertion 1"); + assertEquals(((lasso.SamlAssertion) assertions.getItem(1)).assertionId, + "assertion 2"); + lasso.SamlAssertion assertion3 = new lasso.SamlAssertion(); + assertion3.assertionId = "assertion 3"; + assertions.append(assertion3); + assertEquals(assertions.length(), 3); + assertEquals(((lasso.SamlAssertion) assertions.getItem(0)).assertionId, + "assertion 1"); + assertEquals(((lasso.SamlAssertion) assertions.getItem(1)).assertionId, + "assertion 2"); + assertEquals(((lasso.SamlAssertion) assertions.getItem(2)).assertionId, + "assertion 3"); + response.assertion = assertions; + assertEquals(((lasso.SamlAssertion) response.assertion.getItem(0)).assertionId, + "assertion 1"); + assertEquals(((lasso.SamlAssertion) response.assertion.getItem(1)).assertionId, + "assertion 2"); + assertEquals(((lasso.SamlAssertion) response.assertion.getItem(2)).assertionId, + "assertion 3"); + assertEquals(((lasso.SamlAssertion) assertions.getItem(0)).assertionId, + "assertion 1"); + assertEquals(((lasso.SamlAssertion) assertions.getItem(1)).assertionId, + "assertion 2"); + assertEquals(((lasso.SamlAssertion) assertions.getItem(2)).assertionId, + "assertion 3"); + assertions = null;; + assertEquals(((lasso.SamlAssertion) response.assertion.getItem(0)).assertionId, + "assertion 1"); + assertEquals(((lasso.SamlAssertion) response.assertion.getItem(1)).assertionId, + "assertion 2"); + assertEquals(((lasso.SamlAssertion) response.assertion.getItem(2)).assertionId, + "assertion 3"); + assertions = response.assertion; + assertEquals(((lasso.SamlAssertion) assertions.getItem(0)).assertionId, + "assertion 1"); + assertEquals(((lasso.SamlAssertion) assertions.getItem(1)).assertionId, + "assertion 2"); + assertEquals(((lasso.SamlAssertion) assertions.getItem(2)).assertionId, + "assertion 3"); + assertions = null; + assertEquals(((lasso.SamlAssertion) response.assertion.getItem(0)).assertionId, + "assertion 1"); + assertEquals(((lasso.SamlAssertion) response.assertion.getItem(1)).assertionId, + "assertion 2"); + assertEquals(((lasso.SamlAssertion) response.assertion.getItem(2)).assertionId, + "assertion 3"); + response.assertion = null; + assertNull(response.assertion); + + response = null; + + Console.WriteLine(".. OK"); + } + + static void test05() { + Console.Write("Get & set attributes of nodes of type XML list."); + + lasso.LibAuthnRequest authnRequest = new lasso.LibAuthnRequest(); + + assertNull(authnRequest.extension); + + 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>"; + lasso.StringList extension = new lasso.StringList(); + assertEquals(extension.length(), 0); + extension.append(actionString1); + assertEquals(extension.length(), 1); + assertEquals(extension.getItem(0), actionString1); + assertEquals(extension.getItem(0), actionString1); + extension.append(actionString2); + assertEquals(extension.length(), 2); + assertEquals(extension.getItem(0), actionString1); + assertEquals(extension.getItem(1), actionString2); + extension.append(actionString3); + assertEquals(extension.length(), 3); + assertEquals(extension.getItem(0), actionString1); + assertEquals(extension.getItem(1), actionString2); + assertEquals(extension.getItem(2), actionString3); + authnRequest.extension = extension; + assertEquals(authnRequest.extension.getItem(0), actionString1); + assertEquals(authnRequest.extension.getItem(1), actionString2); + assertEquals(authnRequest.extension.getItem(2), actionString3); + assertEquals(extension.getItem(0), actionString1); + assertEquals(extension.getItem(1), actionString2); + assertEquals(extension.getItem(2), actionString3); + extension = null; + assertEquals(authnRequest.extension.getItem(0), actionString1); + assertEquals(authnRequest.extension.getItem(1), actionString2); + assertEquals(authnRequest.extension.getItem(2), actionString3); + extension = authnRequest.extension; + assertEquals(extension.getItem(0), actionString1); + assertEquals(extension.getItem(1), actionString2); + assertEquals(extension.getItem(2), actionString3); + extension = null; + assertEquals(authnRequest.extension.getItem(0), actionString1); + assertEquals(authnRequest.extension.getItem(1), actionString2); + assertEquals(authnRequest.extension.getItem(2), actionString3); + authnRequest.extension = null; + assertNull(authnRequest.extension); + + authnRequest = null; + + Console.WriteLine(".. OK"); + } + + static void test06() { + Console.Write("Get & set attributes of nodes of type node."); + + lasso.Login login = new lasso.Login(new lasso.Server(null, null, null, null)); + + assertNull(login.request); + login.request = (lasso.SamlpRequestAbstract) new lasso.LibAuthnRequest(); + ((lasso.LibAuthnRequest) login.request).consent = lasso.lasso.libConsentObtained; + assertEquals(((lasso.LibAuthnRequest) login.request).consent, + lasso.lasso.libConsentObtained); + login.request = null; + assertNull(login.request); + + login = null; + + Console.WriteLine(".. OK"); + } +} diff --git a/java/tests/BindingTests.java b/java/tests/BindingTests.java index c8202f95..0aa26659 100644 --- a/java/tests/BindingTests.java +++ b/java/tests/BindingTests.java @@ -254,9 +254,10 @@ public class BindingTests extends TestCase { Login login = new Login(new Server(null, null, null, null)); assertNull(login.getRequest()); - login.setRequest((SamlpRequestAbstract)new LibAuthnRequest()); - ((LibAuthnRequest)login.getRequest()).setConsent(lasso.libConsentObtained); - assertEquals(((LibAuthnRequest)login.getRequest()).getConsent(), lasso.libConsentObtained); + login.setRequest((SamlpRequestAbstract) new LibAuthnRequest()); + ((LibAuthnRequest) login.getRequest()).setConsent(lasso.libConsentObtained); + assertEquals(((LibAuthnRequest) login.getRequest()).getConsent(), + lasso.libConsentObtained); login.setRequest(null); assertNull(login.getRequest()); diff --git a/swig/Lasso.i b/swig/Lasso.i index 6c91d034..68c440a0 100644 --- a/swig/Lasso.i +++ b/swig/Lasso.i @@ -260,17 +260,109 @@ static void build_exception_msg(int errorCode, char *errorMsg) { ***********************************************************************/ -#if defined(SWIGCSHARP) || defined(SWIGJAVA) +/*********************************************************************** + * C# Dynamic Casting + ***********************************************************************/ + + +#ifdef SWIGCSHARP + +/* Accept LassoNode subclasses as input argument, when a LassoNode is expected. */ + +%typemap(csbody) DowncastableNode %{ + protected IntPtr swigCPtr; + protected bool swigCMemOwn; + + internal $csclassname(IntPtr cPtr, bool cMemoryOwn) { + swigCMemOwn = cMemoryOwn; + swigCPtr = cPtr; + } + + protected static IntPtr getCPtr($csclassname obj) { + return (obj == null) ? IntPtr.Zero : obj.swigCPtr; + } +%} + +%typemap(csbody) NODE_SUBCLASS %{ + internal $csclassname(IntPtr cPtr, bool cMemoryOwn) { + swigCMemOwn = cMemoryOwn; + swigCPtr = cPtr; + } + + protected static IntPtr getCPtr($csclassname obj) { + return (obj == null) ? IntPtr.Zero : obj.swigCPtr; + } +%} + +%typemap(csdestruct, methodname="Dispose") NODE_SUBCLASS { + base.Dispose(); +} + +/* Dynamically downcast to a LassoNode subclass, when a LassoNode is expected as a result. */ + +%typemap(out) DowncastableNode * { +/* FIXME */ +/* char classPath[256]; */ +/* jclass clazz; */ +/* char *name; */ + +/* name = (char *) G_OBJECT_TYPE_NAME($1); */ +/* name += 5; /\* Skip "Lasso" prefix. *\/ */ +/* sprintf(classPath, "com/entrouvert/lasso/%s", name); */ +/* clazz = (*jenv)->FindClass(jenv, classPath); */ +/* if (clazz) { */ +/* jmethodID mid = (*jenv)->GetMethodID(jenv, clazz, "<init>", "(JZ)V"); */ +/* if (mid) */ +/* *(void**)&$result = (*jenv)->NewObject(jenv, clazz, mid, $1, false); */ +/* } */ +} + +%typemap(csout) DowncastableNode * { + return $imcall; +} + +%typemap(ctype) DowncastableNode * "void *" +%typemap(imtype) DowncastableNode * "DowncastableNode" +%typemap(cstype) DowncastableNode * "DowncastableNode" + +%{ + +typedef struct { +} DowncastableNode; + +DowncastableNode *downcast_node(LassoNode *node) { + return (DowncastableNode *) node; +} + +%} + +%nodefault DowncastableNode; +typedef struct { +} DowncastableNode; + +DowncastableNode *downcast_node(LassoNode *node); // FIXME: Replace with LassoNode. + +%typemap(csout) NODE_SUPERCLASS * { + IntPtr cPtr = $imcall; + return (cPtr == IntPtr.Zero) ? null : ($csclassname) lassoPINVOKE.downcast_node(cPtr); +} + +%apply NODE_SUPERCLASS * {LassoNode *, LassoSamlpRequestAbstract *, + LassoSamlpResponseAbstract *}; + +#else /* ifndef SWIGCSHARP */ /*********************************************************************** - * C# & Java Dynamic Casting + * Java Dynamic Casting ***********************************************************************/ +#ifdef SWIGJAVA + /* Accept LassoNode subclasses as input argument, when a LassoNode is expected. */ -%typemap(javabody) LassoNode %{ +%typemap(javabody) DowncastableNode %{ protected long swigCPtr; protected boolean swigCMemOwn; @@ -294,6 +386,10 @@ static void build_exception_msg(int errorCode, char *errorMsg) { } %} +%typemap(javadestruct, methodname="delete") NODE_SUBCLASS { + super.delete(); +} + /* Dynamically downcast to a LassoNode subclass, when a LassoNode is expected as a result. */ %typemap(out) DowncastableNode * { @@ -345,14 +441,14 @@ DowncastableNode *downcast_node(LassoNode *node); // FIXME: Replace with LassoNo %apply NODE_SUPERCLASS * {LassoNode *, LassoSamlpRequestAbstract *, LassoSamlpResponseAbstract *}; -#else /* if !defined(SWIGCSHARP) && !defined(SWIGJAVA) */ - /*********************************************************************** * Perl, PHP & Python Dynamic Casting ***********************************************************************/ +#else /* ifndef SWIGJAVA */ + %{ typedef struct node_info { @@ -475,7 +571,8 @@ DYNAMIC_CAST(SWIGTYPE_p_LassoNode, dynamic_cast_node); DYNAMIC_CAST(SWIGTYPE_p_LassoSamlpRequestAbstract, dynamic_cast_node); DYNAMIC_CAST(SWIGTYPE_p_LassoSamlpResponseAbstract, dynamic_cast_node); -#endif /* if !defined(SWIGCSHARP) && !defined(SWIGJAVA) */ +#endif /* ifndef SWIGJAVA */ +#endif /* ifndef SWIGCSHARP */ /*********************************************************************** @@ -483,7 +580,21 @@ DYNAMIC_CAST(SWIGTYPE_p_LassoSamlpResponseAbstract, dynamic_cast_node); ***********************************************************************/ -#if defined(SWIGCSHARP) || defined(SWIGJAVA) +#ifdef SWIGCSHARP + +%define SET_NODE_INFO(className, superClassName) +%apply NODE_SUBCLASS {Lasso##className}; +%typemap(csbase) Lasso##className #superClassName; +%enddef + +%typemap(csbase) LassoNode "DowncastableNode"; + +SET_NODE_INFO(Node, DowncastableNode) +%include inheritance.h + +#else /* ifndef SWIGCSHARP */ + +#ifdef SWIGJAVA %define SET_NODE_INFO(className, superClassName) %apply NODE_SUBCLASS {Lasso##className}; @@ -492,9 +603,10 @@ DYNAMIC_CAST(SWIGTYPE_p_LassoSamlpResponseAbstract, dynamic_cast_node); %typemap(javabase) LassoNode "DowncastableNode"; +SET_NODE_INFO(Node, DowncastableNode) %include inheritance.h -#else /* if !defined(SWIGCSHARP) && !defined(SWIGJAVA) */ +#else /* ifndef SWIGJAVA */ %init %{ { /* Brace needed for pre-C99 compilers */ @@ -520,7 +632,8 @@ DYNAMIC_CAST(SWIGTYPE_p_LassoSamlpResponseAbstract, dynamic_cast_node); } %} -#endif /* if !defined(SWIGCSHARP) && !defined(SWIGJAVA) */ +#endif /* ifndef SWIGJAVA */ +#endif /* ifndef SWIGCSHARP */ /*********************************************************************** @@ -1060,6 +1173,15 @@ static void set_xml_list(GList **xmlListPointer, GPtrArray *xmlArray) { ***********************************************************************/ +#ifdef SWIGCSHARP +%pragma(csharp) imclasscode=%{ +/* FIXME: Doesn't work for C# */ +/* static { */ +/* // Initialize Lasso. */ +/* init(); */ +/* } */ +%} +#else /* ifndef SWIGCSHARP */ #ifdef SWIGJAVA #if SWIG_VERSION >= 0x010322 %include "enumsimple.swg" @@ -1091,7 +1213,8 @@ static void set_xml_list(GList **xmlListPointer, GPtrArray *xmlArray) { lasso_init(); %} #endif -#endif +#endif /* ifndef SWIGJAVA */ +#endif /* ifndef SWIGCSHARP */ /*********************************************************************** |