~ubuntu-branches/ubuntu/trusty/lasso/trusty

« back to all changes in this revision

Viewing changes to tests/basic_tests.c

  • Committer: Package Import Robot
  • Author(s): Frederic Peters
  • Date: 2014-01-07 13:22:21 UTC
  • mfrom: (1.1.11) (7.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20140107132221-htp0go0s9z5lqvj8
Tags: 2.4.0-1
* New upstream version.
* debian/control, debian/rules: use autoreconf
* debian/source/version: switch to 3.0 (quilt)

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 * GNU General Public License for more details.
18
18
 *
19
19
 * You should have received a copy of the GNU General Public License
20
 
 * along with this program; if not, write to the Free Software
21
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22
21
 */
23
22
 
24
23
#include <stdlib.h>
37
36
#include "../lasso/utils.h"
38
37
#include "../lasso/xml/private.h"
39
38
#include <libxml/tree.h>
40
 
#include "./tests.h"
 
39
#include "tests.h"
41
40
 
42
41
START_TEST(test01_server_load_dump_empty_string)
43
42
{
51
50
START_TEST(test02_server_load_dump_random_string)
52
51
{
53
52
        LassoServer *serverContext;
 
53
        begin_check_do_log(G_LOG_LEVEL_CRITICAL, "libxml2: Start tag expected, '<' not found\\n", FALSE);
54
54
        serverContext = lasso_server_new_from_dump("foo");
 
55
        end_check_do_log();
55
56
        fail_unless(serverContext == NULL,
56
57
                        "serverContext was created from a fake dump");
57
58
}
60
61
START_TEST(test03_server_load_dump_random_xml)
61
62
{
62
63
        LassoServer *serverContext;
 
64
        begin_check_do_log(G_LOG_LEVEL_CRITICAL, " Unable to build a LassoNode from a xmlNode", TRUE);
63
65
        serverContext = lasso_server_new_from_dump("<?xml version=\"1.0\"?><foo/>");
 
66
        end_check_do_log();
64
67
        fail_unless(serverContext == NULL,
65
68
                        "serverContext was created from fake (but valid XML) dump");
66
69
}
133
136
}
134
137
END_TEST
135
138
 
 
139
static struct XmlSnippet schema_snippets[] = {
 
140
        {NULL, 0, 0, NULL, NULL, NULL}
 
141
};
 
142
 
 
143
static void
 
144
class_init(LassoNodeClass *klass)
 
145
{
 
146
        LassoNodeClass *nclass = LASSO_NODE_CLASS(klass);
 
147
 
 
148
        nclass->node_data = g_new0(LassoNodeClassData, 1);
 
149
        lasso_node_class_set_nodename(nclass, "Assertion");
 
150
        lasso_node_class_set_ns(nclass,LASSO_SAML2_ASSERTION_HREF, LASSO_SAML2_ASSERTION_PREFIX);
 
151
        lasso_node_class_add_snippets(nclass, schema_snippets);
 
152
 
 
153
}
 
154
 
136
155
START_TEST(test08_test_new_from_xmlNode)
137
156
{
138
 
        static GType this_type = 0;
139
157
        gint r;
140
158
        LassoNode *node = NULL;
141
159
 
143
161
                sizeof (LassoNodeClass),
144
162
                NULL,
145
163
                NULL,
146
 
                NULL,
 
164
                (GClassInitFunc) class_init,
147
165
                NULL,
148
166
                NULL,
149
167
                sizeof(LassoNode),
152
170
                NULL
153
171
        };
154
172
 
155
 
        this_type = g_type_register_static(LASSO_TYPE_NODE,
 
173
        g_type_register_static(LASSO_TYPE_NODE,
156
174
                        "LassoTest", &this_info, 0);
157
175
        r = lasso_registry_default_add_direct_mapping("http://example.com", "Test1", LASSO_LASSO_HREF, "LassoTest");
158
176
        fail_unless(r == 0, "no mapping for http://example.com:Test1 should exist");
 
177
        begin_check_do_log(G_LOG_LEVEL_WARNING, "       Class LassoTest has no node_data so no initialization is possible", TRUE);
159
178
        node = lasso_node_new_from_dump("<Test1 xmlns=\"http://example.com\"></Test1>");
 
179
        end_check_do_log();
160
180
        fail_unless(node != NULL, "parsing <Test1/> should return an object");
161
181
        fail_unless(strcmp(G_OBJECT_TYPE_NAME(node), "LassoTest") == 0, "node classname should be LassoTest");
162
182
        g_object_unref(node);
166
186
START_TEST(test09_test_deserialization)
167
187
{
168
188
        char *content = NULL;
169
 
        unsigned int len = 0;
 
189
        size_t len = 0;
170
190
        LassoNode *node;
171
191
 
172
192
        g_file_get_contents(TESTSDATADIR "/response-1", &content, &len, NULL);
1489
1509
        lasso_release_string(node_dump);
1490
1510
        lasso_release_gobject(node2);
1491
1511
        lasso_release_gobject(node);
1492
 
        node = LASSO_NODE(lasso_samlp2_logout_request_new());
 
1512
/*      node = LASSO_NODE(lasso_samlp2_logout_request_new());
1493
1513
        node_dump = lasso_node_dump(node);
1494
1514
        fail_unless((node2 = lasso_node_new_from_dump(node_dump)) != NULL, "restoring dump failed after lasso_samlp2_logout_request_new");
1495
1515
        lasso_release_string(node_dump);
1496
1516
        lasso_release_gobject(node2);
1497
 
        lasso_release_gobject(node);
 
1517
        lasso_release_gobject(node); */
1498
1518
        node = LASSO_NODE(lasso_samlp2_logout_response_new());
1499
1519
        node_dump = lasso_node_dump(node);
1500
1520
        fail_unless((node2 = lasso_node_new_from_dump(node_dump)) != NULL, "restoring dump failed after lasso_samlp2_logout_response_new");
1501
1521
        lasso_release_string(node_dump);
1502
1522
        lasso_release_gobject(node2);
1503
1523
        lasso_release_gobject(node);
1504
 
        node = LASSO_NODE(lasso_samlp2_manage_name_id_request_new());
 
1524
/*      node = LASSO_NODE(lasso_samlp2_manage_name_id_request_new());
1505
1525
        node_dump = lasso_node_dump(node);
1506
1526
        fail_unless((node2 = lasso_node_new_from_dump(node_dump)) != NULL, "restoring dump failed after lasso_samlp2_manage_name_id_request_new");
1507
1527
        lasso_release_string(node_dump);
1508
1528
        lasso_release_gobject(node2);
1509
 
        lasso_release_gobject(node);
 
1529
        lasso_release_gobject(node); */
1510
1530
        node = LASSO_NODE(lasso_samlp2_manage_name_id_response_new());
1511
1531
        node_dump = lasso_node_dump(node);
1512
1532
        fail_unless((node2 = lasso_node_new_from_dump(node_dump)) != NULL, "restoring dump failed after lasso_samlp2_manage_name_id_response_new");
1840
1860
        lasso_release_string(node_dump);
1841
1861
        lasso_release_gobject(node2);
1842
1862
        lasso_release_gobject(node);
 
1863
        /* test serialization / deserialization of KeyInfoConfirmationDataType */
 
1864
        node = LASSO_NODE(lasso_saml2_key_info_confirmation_data_type_new());
 
1865
        node_dump = lasso_node_dump(node);
 
1866
        fail_unless((node2 = lasso_node_new_from_dump(node_dump)) != NULL, "restoring dump failed after lasso_saml2_key_info_confirmation_data_type_new");
 
1867
        lasso_release_string(node_dump);
 
1868
        lasso_release_gobject(node2);
 
1869
        lasso_release_gobject(node);
1843
1870
#endif
1844
1871
        /* test deserialization of saml2:EncryptedAssertion" */
1845
1872
        const char *encrypted_element_xml[] = {
1846
 
        "<EncryptedAssertion xmlns=\"urn:oasis:names:tc:SAML:2.0:assertion\">\n\
1847
 
                <EncryptedData/>\
1848
 
                <EncryptedKey/>\
 
1873
        "<EncryptedAssertion xmlns=\"urn:oasis:names:tc:SAML:2.0:assertion\" xmlns:xmlenc=\"http://www.w3.org/2001/04/xmlenc#\">\n\
 
1874
                <xmlenc:EncryptedData/>\
 
1875
                <xmlenc:EncryptedKey/>\
1849
1876
        </EncryptedAssertion>",
1850
 
        "<EncryptedID xmlns=\"urn:oasis:names:tc:SAML:2.0:assertion\">\n\
1851
 
                <EncryptedData/>\
1852
 
                <EncryptedKey/>\
 
1877
        "<EncryptedID xmlns=\"urn:oasis:names:tc:SAML:2.0:assertion\" xmlns:xmlenc=\"http://www.w3.org/2001/04/xmlenc#\">\n\
 
1878
                <xmlenc:EncryptedData/>\
 
1879
                <xmlenc:EncryptedKey/>\
1853
1880
        </EncryptedID>",
1854
 
        "<EncryptedAttribute xmlns=\"urn:oasis:names:tc:SAML:2.0:assertion\">\n\
1855
 
                <EncryptedData/>\
1856
 
                <EncryptedKey/>\
 
1881
        "<EncryptedAttribute xmlns=\"urn:oasis:names:tc:SAML:2.0:assertion\" xmlns:xmlenc=\"http://www.w3.org/2001/04/xmlenc#\">\n\
 
1882
                <xmlenc:EncryptedData/>\
 
1883
                <xmlenc:EncryptedKey/>\
1857
1884
        </EncryptedAttribute>",
1858
 
        "<NewEncryptedID xmlns=\"urn:oasis:names:tc:SAML:2.0:protocol\">\n\
1859
 
                <EncryptedData/>\
1860
 
                <EncryptedKey/>\
 
1885
        "<NewEncryptedID xmlns=\"urn:oasis:names:tc:SAML:2.0:protocol\" xmlns:xmlenc=\"http://www.w3.org/2001/04/xmlenc#\">\n\
 
1886
                <xmlenc:EncryptedData/>\
 
1887
                <xmlenc:EncryptedKey/>\
1861
1888
        </NewEncryptedID>", NULL };
1862
1889
        const char **iter = encrypted_element_xml;
1863
1890
        while (*iter) {
1939
1966
}
1940
1967
END_TEST
1941
1968
 
 
1969
#include <stdio.h>
 
1970
 
 
1971
/* test load federation */
 
1972
START_TEST(test13_test_lasso_server_load_metadata)
 
1973
{
 
1974
        LassoServer *server = NULL;
 
1975
        GList *loaded_entity_ids = NULL;
 
1976
        GList blacklisted_1 = { .data = "https://identities.univ-jfc.fr/idp/prod", .next = NULL };
 
1977
 
 
1978
        check_not_null(server = lasso_server_new(
 
1979
                        TESTSDATADIR "/idp5-saml2/metadata.xml",
 
1980
                        TESTSDATADIR "/idp5-saml2/private-key.pem",
 
1981
                        NULL, /* Secret key to unlock private key */
 
1982
                        NULL));
 
1983
        block_lasso_logs;
 
1984
        check_good_rc(lasso_server_load_metadata(server, LASSO_PROVIDER_ROLE_IDP,
 
1985
                                TESTSDATADIR "/metadata/renater-metadata.xml",
 
1986
                                TESTSDATADIR "/metadata/metadata-federation-renater.crt",
 
1987
                                &blacklisted_1, &loaded_entity_ids,
 
1988
                                LASSO_SERVER_LOAD_METADATA_FLAG_DEFAULT));
 
1989
        unblock_lasso_logs;
 
1990
        check_equals(g_hash_table_size(server->providers), 110);
 
1991
        check_equals(g_list_length(loaded_entity_ids), 110);
 
1992
 
 
1993
#if 0
 
1994
        /* UK federation file are too big to distribute (and I don't even known if it's right to do
 
1995
         * it, disable this test for now ) */
 
1996
        check_good_rc(lasso_server_load_metadata(server, LASSO_PROVIDER_ROLE_IDP,
 
1997
                                TESTSDATADIR "/ukfederation-metadata.xml",
 
1998
                                TESTSDATADIR "/ukfederation.pem",
 
1999
                                &blacklisted_1, &loaded_entity_ids,
 
2000
                                LASSO_SERVER_LOAD_METADATA_FLAG_DEFAULT));
 
2001
        check_equals(g_list_length(loaded_entity_ids), 283);
 
2002
        check_equals(g_hash_table_size(server->providers), 393);
 
2003
#endif
 
2004
        lasso_release_list_of_strings(loaded_entity_ids);
 
2005
 
 
2006
        lasso_release_gobject(server);
 
2007
}
 
2008
END_TEST
 
2009
 
 
2010
#include "../lasso/key.h"
 
2011
 
 
2012
/* test load federation */
 
2013
START_TEST(test14_lasso_key)
 
2014
{
 
2015
        LassoKey *key;
 
2016
        char *buffer;
 
2017
        gsize length;
 
2018
        char *base64_encoded;
 
2019
 
 
2020
        check_true(g_file_get_contents(TESTSDATADIR "sp1-la/private-key-raw.pem", &buffer, &length, NULL));
 
2021
        check_not_null(key = lasso_key_new_for_signature_from_memory(buffer,
 
2022
                                length, NULL, LASSO_SIGNATURE_METHOD_RSA_SHA1,
 
2023
                                NULL));
 
2024
        lasso_release_gobject(key);
 
2025
        check_not_null(key = lasso_key_new_for_signature_from_file(TESTSDATADIR
 
2026
                                "sp1-la/private-key-raw.pem", NULL, LASSO_SIGNATURE_METHOD_RSA_SHA1,
 
2027
                                NULL));
 
2028
        lasso_release_gobject(key);
 
2029
        base64_encoded = g_base64_encode(BAD_CAST buffer, length);
 
2030
        check_not_null(key = lasso_key_new_for_signature_from_base64_string(base64_encoded, NULL,
 
2031
                                LASSO_SIGNATURE_METHOD_RSA_SHA1, NULL));
 
2032
        lasso_release_string(base64_encoded);
 
2033
        lasso_release_string(buffer);
 
2034
}
 
2035
END_TEST
 
2036
 
 
2037
/* test load federation */
 
2038
START_TEST(test15_ds_key_info)
 
2039
{
 
2040
        LassoDsKeyInfo *ds_key_info = lasso_ds_key_info_new();
 
2041
        LassoDsKeyValue *ds_key_value = lasso_ds_key_value_new();
 
2042
        LassoDsX509Data *x509_data = lasso_ds_x509_data_new();
 
2043
        char *dump;
 
2044
        GList list;
 
2045
        LassoNode *node;
 
2046
 
 
2047
        lasso_ds_x509_data_set_certificate(x509_data, "coucou");
 
2048
        lasso_ds_key_value_set_x509_data(ds_key_value, x509_data);
 
2049
        ds_key_info->KeyValue = g_object_ref(ds_key_value);
 
2050
        dump = lasso_node_debug((LassoNode*)ds_key_info, 10);
 
2051
        lasso_release_gobject(ds_key_info);
 
2052
        lasso_release_gobject(ds_key_value);
 
2053
        lasso_release_gobject(x509_data);
 
2054
        ds_key_info = (LassoDsKeyInfo*)lasso_node_new_from_dump(dump);
 
2055
        lasso_release_string(dump);
 
2056
        check_not_null(ds_key_info);
 
2057
        check_true(LASSO_IS_DS_KEY_INFO(ds_key_info));
 
2058
        check_not_null(ds_key_info->KeyValue);
 
2059
        check_true(LASSO_IS_DS_KEY_VALUE(ds_key_info->KeyValue));
 
2060
        x509_data = lasso_ds_key_value_get_x509_data(ds_key_info->KeyValue);
 
2061
        check_not_null(x509_data);
 
2062
        check_true(LASSO_IS_DS_X509_DATA(x509_data));
 
2063
        check_str_equals(lasso_ds_x509_data_get_certificate(x509_data), "coucou");
 
2064
        /* LassoSaml2SubjectConfirmation */
 
2065
        LassoSaml2SubjectConfirmation *sc = (LassoSaml2SubjectConfirmation*) \
 
2066
                        lasso_saml2_subject_confirmation_new();
 
2067
        LassoSaml2KeyInfoConfirmationDataType *kicdt = (LassoSaml2KeyInfoConfirmationDataType*) \
 
2068
                        lasso_saml2_key_info_confirmation_data_type_new();
 
2069
        lasso_assign_string(sc->Method, LASSO_SAML2_CONFIRMATION_METHOD_HOLDER_OF_KEY);
 
2070
        lasso_assign_new_gobject(sc->SubjectConfirmationData, &kicdt->parent);
 
2071
        list = (GList){ .data = ds_key_info, .next = NULL, .prev = NULL };
 
2072
        lasso_saml2_key_info_confirmation_data_type_set_key_info(kicdt, &list);
 
2073
        dump = lasso_node_debug((LassoNode*)sc, 10);
 
2074
        lasso_release_gobject(sc);
 
2075
        lasso_release_gobject(ds_key_info);
 
2076
        node = lasso_node_new_from_dump(dump);
 
2077
        lasso_release_string(dump);
 
2078
        dump = lasso_node_debug(node, 10);
 
2079
        lasso_release_string(dump);
 
2080
}
 
2081
END_TEST
 
2082
 
1942
2083
Suite*
1943
2084
basic_suite()
1944
2085
{
1953
2094
        TCase *tc_registry_new_from_xmlNode = tcase_create("Test parsing a node that has a mapping to Lasso Object in the registry");
1954
2095
        TCase *tc_response_new_from_xmlNode = tcase_create("Test parsing a message from Ping Federate");
1955
2096
        TCase *tc_custom_namespace = tcase_create("Test custom namespace handling");
 
2097
        TCase *tc_load_metadata = tcase_create("Test loading a federation metadata file");
 
2098
        TCase *tc_key = tcase_create("Test loading and manipulating LassoKey objects");
 
2099
        TCase *tc_key_info = tcase_create("Test creating and dumping ds:KeyInfo nodes");
1956
2100
 
1957
2101
        suite_add_tcase(s, tc_server_load_dump_empty_string);
1958
2102
        suite_add_tcase(s, tc_server_load_dump_random_string);
1964
2108
        suite_add_tcase(s, tc_registry_new_from_xmlNode);
1965
2109
        suite_add_tcase(s, tc_response_new_from_xmlNode);
1966
2110
        suite_add_tcase(s, tc_custom_namespace);
 
2111
        suite_add_tcase(s, tc_load_metadata);
 
2112
        suite_add_tcase(s, tc_key);
 
2113
        suite_add_tcase(s, tc_key_info);
1967
2114
 
1968
2115
        tcase_add_test(tc_server_load_dump_empty_string, test01_server_load_dump_empty_string);
1969
2116
        tcase_add_test(tc_server_load_dump_random_string, test02_server_load_dump_random_string);
1977
2124
        tcase_add_test(tc_response_new_from_xmlNode, test10_test_alldumps);
1978
2125
        tcase_add_test(tc_response_new_from_xmlNode, test11_get_default_name_id_format);
1979
2126
        tcase_add_test(tc_custom_namespace, test12_custom_namespace);
 
2127
        tcase_add_test(tc_load_metadata, test13_test_lasso_server_load_metadata);
 
2128
        tcase_add_test(tc_key, test14_lasso_key);
 
2129
        tcase_add_test(tc_key_info, test15_ds_key_info);
 
2130
        tcase_set_timeout(tc_load_metadata, 10);
1980
2131
        return s;
1981
2132
}
1982
2133