changeset 103:5c9a4c18d414

Tests for the security
author Sebastien Decugis <sdecugis@nict.go.jp>
date Fri, 25 Jul 2008 11:14:01 +0900
parents b9085fed408e
children a6fb0680654e
files waaad/tests/testsec.c
diffstat 1 files changed, 180 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/waaad/tests/testsec.c	Thu Jul 24 18:29:32 2008 +0900
+++ b/waaad/tests/testsec.c	Fri Jul 25 11:14:01 2008 +0900
@@ -38,13 +38,193 @@
 /* Test for the security module */
 
 
+/* Module's functions */
+static int test_state_change (sec_pss_t newstate, sec_pss_t oldstate, sec_session_t * session, void ** ext_session)
+{
+	return 0;
+}
+
+static int test_send_protect (sec_session_t * session, void ** ext_session, void * data, size_t length)
+{
+	return 0;
+}
+
+static int test_recv_unprotect (sec_session_t * session, void ** ext_session, void ** data, size_t *length)
+{
+	return 0;
+}
+
+static int test_is_supported_peer_a (char * diamid, struct sockaddr * sa, int * priority)
+{
+	if (!diamid)
+		return EINVAL;
+	
+	if (*diamid == 'y') {
+		*priority = atoi(diamid+1);
+		return 0;
+	}
+	
+	if (*diamid == 'a') {
+		*priority = 10;
+		return 0;
+	}
+	
+	*priority = -1;
+	
+	return 0;
+}
+static int test_is_supported_peer_b (char * diamid, struct sockaddr * sa, int * priority)
+{
+	if (!diamid)
+		return EINVAL;
+	
+	if (*diamid == 'y') {
+		*priority = atoi(diamid+1) + 1;
+		return 0;
+	}
+	
+	if (*diamid == 'b') {
+		*priority = 10;
+		return 0;
+	}
+	
+	*priority = -1;
+	
+	return 0;
+}
+
+
+
+
 /* Main test routine */
 int main(int argc, char *argv[])
 {
+	int ret = 0;
+	
+	sec_module_t mod_a0 = {
+		0,
+		test_is_supported_peer_a,
+		test_state_change,
+		test_send_protect,
+		test_recv_unprotect
+	};
+	
+	sec_module_t mod_a1 = {
+		0,
+		test_is_supported_peer_a,
+		test_state_change,
+		test_send_protect,
+		test_recv_unprotect
+	};
+	
+	sec_module_t mod_b = {
+		1,
+		test_is_supported_peer_b,
+		test_state_change,
+		test_send_protect,
+		test_recv_unprotect
+	};
+		
+	sec_mod_hdl_t * modhdl_a;
+	sec_mod_hdl_t * modhdl_b;
 	
 	/* First, initialize the daemon modules */
 	INIT_WAAAD();
 	
+	/* Test modules registration and deregistration */
+	{
+		sec_mod_hdl_t * modhdl;
+		
+		CHECK( 0, sec_register( &mod_a0, &modhdl_a) );
+		CHECK( 0, sec_register( &mod_b,  &modhdl_b) );
+		
+		/* Check another module with same insecid cannot register */
+		CHECK( EALREADY, sec_register( &mod_a1, &modhdl) );
+		
+		/* Now test deregistration */
+		CHECK( 0, sec_unregister( modhdl_a ) );
+		CHECK( 0, sec_unregister( modhdl_b ) );
+		
+		/* And register again */
+		CHECK( 0, sec_register( &mod_a0, &modhdl_a) );
+		CHECK( 0, sec_register( &mod_b,  &modhdl_b) );
+	}
+	
+	/* Test the module election process (daemon internals) */
+	{
+		_sec_item_t * list;
+		struct sockaddr sa;
+		
+		char * diamid_a = "a";
+		char * diamid_b = "b";
+		char * diamid_n = "n";
+		char * diamid_y3= "y3";
+		
+		/* A peer handled only by module A */
+		CHECK( 0, sec_getmodules( diamid_a, &sa, &list ) );
+		CHECK( 1, (list != NULL) ? 1 : 0 );
+		CHECK( &mod_a0, list->sm );
+		CHECK( NULL, list->next );
+		CHECK( 0, sec_freemodules( list ) );
+		
+		/* A peer handled only by module B */
+		CHECK( 0, sec_getmodules( diamid_b, &sa, &list ) );
+		CHECK( 1, (list != NULL) ? 1 : 0 );
+		CHECK( &mod_b, list->sm );
+		CHECK( NULL, list->next );
+		CHECK( 0, sec_freemodules( list ) );
+		
+		/* A peer handled by no module */
+		CHECK( 0, sec_getmodules( diamid_n, &sa, &list ) );
+		CHECK( 1, (list == NULL) ? 1 : 0 );
+		CHECK( 0, sec_freemodules( list ) );
+		
+		/* A peer handled by both modules, B is prioritary */
+		CHECK( 0, sec_getmodules( diamid_y3, &sa, &list ) );
+		CHECK( 1, (list != NULL) ? 1 : 0 );
+		CHECK( &mod_b, list->sm );
+		CHECK( 1, (list->next != NULL) ? 1 : 0 );
+		CHECK( &mod_a0, list->next->sm );
+		CHECK( NULL, list->next->next );
+		CHECK( 0, sec_freemodules( list ) );
+	}
+	
+	/* Test the refcount */
+	{
+		_sec_item_t * list;
+		struct sockaddr sa;
+		char * diamid= "y5";
+		
+		/* Get the list */
+		CHECK( 0, sec_getmodules( diamid, &sa, &list ) );
+		CHECK( 1, (list != NULL) ? 1 : 0 );
+		CHECK( &mod_b, list->sm );
+		CHECK( 1, (list->next != NULL) ? 1 : 0 );
+		CHECK( &mod_a0, list->next->sm );
+		CHECK( NULL, list->next->next );
+		
+		/* Now suppose we will be using module b */
+		CHECK( 0, sec_modlink( list->hdl ) );
+		
+		CHECK( 0, sec_freemodules( list ) );
+		
+		/* Now the module can not be unregistered since it is in use */
+		CHECK( EBUSY, sec_unregister( modhdl_b ) );
+		
+		/* Then stop using it in the peer */
+		CHECK( 0, sec_modunlink( list->hdl ) );
+		
+		/* The module is no longer in use */
+		CHECK( 0, sec_unregister( modhdl_b ) );
+		
+		/* restore */
+		CHECK( 0, sec_register( &mod_b,  &modhdl_b) );
+	}
+
+	/* Cleanup */
+	CHECK( 0, sec_unregister( modhdl_a ) );
+	CHECK( 0, sec_unregister( modhdl_b ) );
+	
 	/* That's all for the tests yet */
 	PASSTEST();
 } 
"Welcome to our mercurial repository"