Mercurial > hg > waaad
view waaad/tests/testsess.c @ 429:afa2b5ffe44c
Allow unaligned strings in uti_hash (as received inside RADIUS attributes)
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Wed, 24 Jun 2009 16:40:10 +0900 |
parents | 860f41038ea2 |
children |
line wrap: on
line source
/********************************************************************************************************* * Software License Agreement (BSD License) * * Author: Sebastien Decugis <sdecugis@nict.go.jp> * * * * Copyright (c) 2009, WIDE Project and NICT * * All rights reserved. * * * * Redistribution and use of this software in source and binary forms, with or without modification, are * * permitted provided that the following conditions are met: * * * * * Redistributions of source code must retain the above * * copyright notice, this list of conditions and the * * following disclaimer. * * * * * Redistributions in binary form must reproduce the above * * copyright notice, this list of conditions and the * * following disclaimer in the documentation and/or other * * materials provided with the distribution. * * * * * Neither the name of the WIDE Project or NICT nor the * * names of its contributors may be used to endorse or * * promote products derived from this software without * * specific prior written permission of WIDE Project and * * NICT. * * * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * *********************************************************************************************************/ #include "tests.h" /* Test for the sessions module */ /* Structure that is passed to the module */ typedef struct { int hasbeenfreed; int id; } td_t; /* Function that "cleanup" the data */ void cleanup(void * data) { td_t * d = (td_t *)data; d->hasbeenfreed += 1; } #define TEST_DIAM_ID "testsess.myid" /* Main test routine */ int main(int argc, char *argv[]) { sess_reg_t * client1 = NULL; sess_reg_t * client2 = NULL; sess_id_t * session1 = NULL; sess_id_t * session2 = NULL; sess_id_t * session3 = NULL; td_t td[4] = { {0,1}, {0,2}, {0,3}, {0,4}}; td_t *ptd; char * str = NULL; int new; /* First, initialize the daemon modules */ INIT_WAAAD(); g_pconf->diameter_identity = TEST_DIAM_ID; g_conf->diamid_len = strlen(TEST_DIAM_ID); /* Check that the hash function works properly with unaligned data */ { char * unalign = "a" TEST_DIAM_ID; CHECK( uti_hash ( g_pconf->diameter_identity, g_conf->diamid_len ), uti_hash ( unalign + 1, g_conf->diamid_len )); } /* Tests creation and destruction of sessions */ { sess_id_t * session = NULL; /* Create one session */ CHECK( 0, sess_new ( &session1, SESSION_NEW_DEFAULT, NULL ) ); /* Check it begins as expected */ CHECK( 0, sess_getsid ( session1, &str) ); CHECK( 0, strncmp( TEST_DIAM_ID, str, strlen(TEST_DIAM_ID)) ); /* Check we can not create another session with the same name */ CHECK( EALREADY, sess_new ( &session, SESSION_NEW_FULL, str ) ); CHECK( EALREADY, sess_new ( &session, SESSION_NEW_SEQUENCE, str + strlen(TEST_DIAM_ID) + 1 ) ); /* Now create a session using the opt part */ CHECK( 0, sess_new ( &session2, SESSION_NEW_DEFAULT, "user@testsess" ) ); CHECK( 0, sess_getsid ( session2, &str) ); CHECK( 0, strcmp( ";user@testsess", str + strlen(str) - strlen(";user@testsess")) ); /* verify the end of the string */ /* Test the link and unlink */ CHECK( 0, sess_link ( session2 ) ); session = session2; CHECK( 0, sess_unlink ( &session ) ); session = session2; CHECK( 0, sess_unlink ( &session ) ); CHECK( EINVAL, sess_unlink ( &session2 ) ); CHECK( 0, sess_new ( &session2, SESSION_NEW_DEFAULT, "user@testsess" ) ); /* Create a session for other peer */ #define ANOTHER_ID "another.diameter.id" #define ANOTHER_OPT ";with@opt.str" CHECK( 0, sess_new ( &session3, SESSION_NEW_OTHER, ANOTHER_ID ) ); CHECK( 0, sess_getsid ( session3, &str) ); CHECK( 0, strncmp( ANOTHER_ID, str, strlen(ANOTHER_ID)) ); /* verify the beginning of the string */ CHECK( 0, sess_unlink ( &session3 ) ); CHECK( 0, sess_new ( &session3, SESSION_NEW_OTHER, ANOTHER_ID ANOTHER_OPT ) ); CHECK( 0, sess_getsid ( session3, &str) ); CHECK( 0, strncmp( ANOTHER_ID, str, strlen(ANOTHER_ID)) ); /* verify the beginning of the string */ CHECK( 0, strcmp( ANOTHER_OPT, str + strlen(str) - strlen(ANOTHER_OPT)) ); /* verify the end of the string */ CHECK( 0, sess_unlink ( &session3 ) ); /* Create a third session */ CHECK( 0, sess_new ( &session3, SESSION_NEW_SEQUENCE, "1;1;another_test_session" ) ); /* Check we find it again */ CHECK( 0, sess_fromsid ( TEST_DIAM_ID ";" "1;1;another_test_session" "and some trash data follow", strlen(TEST_DIAM_ID ";" "1;1;another_test_session"), &session, &new) ); CHECK( 0, new); CHECK( 0, sess_unlink( &session ) ); #if 0 sess_dump(); #endif } /* Tests the registration of client functions */ { /* Create the two module clients */ CHECK( 0, sess_regext ( &client1 ) ); CHECK( 0, sess_regext ( &client2 ) ); /* Test that we can delete the clients */ CHECK( 0, sess_deregext ( client1 ) ); CHECK( 0, sess_deregext ( client2 ) ); /* Register it again */ CHECK( 0, sess_regext ( &client1 ) ); CHECK( 0, sess_regext ( &client2 ) ); } /* Now associate data with the sessions */ { /* Associate data with all sessions */ CHECK( 0, sess_data_reg( session1, client1, &td[0], cleanup) ); CHECK( 0, sess_data_reg( session2, client1, &td[1], cleanup) ); CHECK( 0, sess_data_reg( session3, client1, &td[2], cleanup) ); /* Destroy the client, the cleanup function must be called */ CHECK( 0, sess_deregext ( client1 ) ); CHECK( 1, td[0].hasbeenfreed ); td[0].hasbeenfreed = 0; CHECK( 1, td[1].hasbeenfreed ); td[1].hasbeenfreed = 0; CHECK( 1, td[2].hasbeenfreed ); td[2].hasbeenfreed = 0; /* Recreate the client */ CHECK( 0, sess_regext ( &client1 ) ); /* Associate data */ CHECK( 0, sess_data_reg( session1, client1, &td[0], cleanup) ); CHECK( 0, sess_data_reg( session2, client1, &td[1], cleanup) ); CHECK( 0, sess_data_reg( session2, client2, &td[2], cleanup) ); CHECK( 0, sess_data_reg( session3, client2, &td[3], cleanup) ); /* Check we cannot associate more data with the same tuplet */ CHECK( EALREADY, sess_data_reg( session2, client1, &td[3], cleanup) ); /* Get the data */ CHECK( 0, sess_data_get( session2, client1, (void *)&ptd ) ); CHECK( 2, ptd->id ); CHECK( 0, sess_data_get( session2, client2, (void *)&ptd ) ); CHECK( 3, ptd->id ); /* Check case where there is no data */ CHECK( ENOENT, sess_data_get( session1, client2, (void *)&ptd ) ); /* Now remove the data */ CHECK( 0, sess_data_dereg( session3, client2, (void *)&ptd ) ); CHECK( 4, ptd->id ); CHECK( 0, ptd->hasbeenfreed ); CHECK( 0, sess_data_dereg( session2, client2, NULL ) ); CHECK( 1, td[2].hasbeenfreed ); #if 0 sess_dump(); #endif } CHECK( 0, sess_unlink( &session1 ) ); CHECK( 0, sess_unlink( &session2 ) ); CHECK( 0, sess_unlink( &session3 ) ); CHECK( 0, sess_deregext ( client1 ) ); CHECK( 0, sess_deregext ( client2 ) ); /* Simulate a "real life" situation */ { /* Register a client at extension's initialization */ CHECK( 0, sess_regext ( &client1 ) ); /* The extension initiates a session */ CHECK( 0, sess_new ( &session1, SESSION_NEW_DEFAULT, "more_test" ) ); /* It associates some data with this session */ td[0].hasbeenfreed = 0; td[0].id = 0xabcd; CHECK( 0, sess_data_reg( session1, client1, &td[0], cleanup) ); /* -- for the purpose of the test, we need to save the name of the session -- */ CHECK( 0, sess_getsid ( session1, &str) ); /* In "real life" situation, the session would then be used to set the Session-Id AVP in a message */ /* Then we do not use the session object until we receive a message, or a timer expires, or ... */ CHECK( 0, sess_unlink( &session1 ) ); /* Ok, now let's assume we received a message with a Session-Id AVP set to the same string. retrieve the session */ CHECK( 0, sess_fromsid ( str, strlen(str), &session3, NULL) ); /* Now retrieve any data we would have stored along, like a state machine state */ CHECK( 0, sess_data_get( session3, client1, (void *)&ptd ) ); /* -- for the test: check we retrieved the correct data -- */ CHECK( 0xabcd, ptd->id ); CHECK( 0, ptd->hasbeenfreed ); /* We're done for this message */ CHECK( 0, sess_unlink( &session3 ) ); } CHECK( 0, sess_fini() ); /* That's all for the tests yet */ PASSTEST(); }