diff options
Diffstat (limited to 'dispatcher/elapi_dispatcher_ut.c')
-rw-r--r-- | dispatcher/elapi_dispatcher_ut.c | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/dispatcher/elapi_dispatcher_ut.c b/dispatcher/elapi_dispatcher_ut.c new file mode 100644 index 0000000..119d40a --- /dev/null +++ b/dispatcher/elapi_dispatcher_ut.c @@ -0,0 +1,201 @@ +/* Copyright */ + +#include <stdio.h> +#include <malloc.h> +#include "elapi_dispatcher.h" +#include "elapi_collection.h" +#include "elapi_debug.h" +#include "elapi_util.h" +#include "elapi_tools.h" + +struct collection_item *event; +struct collection_item *peer; +struct collection_item *socket; +struct collection_item *host; + + +int construct_event() +{ + char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; + int found = 0; + + int error = EOK; + + DEBUG_STRING("construct_event","Entry."); + + if((error=create_collection(&peer,"peer")) || + (error=add_str_property(peer,NULL,"hostname","peerhost.mytest.com",0)) || + (error=add_str_property(peer,NULL,"IPv4","10.10.10.10",12)) || /* Expect trailing zero to be truncated */ + (error=add_str_property(peer,NULL,"IPv6","bla:bla:bla:bla:bla:bla",0))) { + printf("Failed to add property. Error %d",error); + destroy_collection(peer); + return error; + } + + if((error=create_collection(&host,"host")) || + (error=add_str_property(host,NULL,"hostname","myhost.mytest.com",0)) || + (error=add_str_property(host,NULL,"IPv4","20.20.20.20",13)) || + (error=add_str_property(host,NULL,"IPv6","bla:bla:bla:bla:bla:bla",0))) { + printf("Failed to add property. Error %d",error); + destroy_collection(peer); + destroy_collection(host); + return error; + } + + + if((error=create_collection(&socket,"socket")) || + (error=add_int_property(socket,NULL,"id",1)) || + (error=add_long_property(socket,NULL,"packets",100000000L)) || + (error=add_binary_property(socket,NULL,"stack",binary_dump,sizeof(binary_dump)))) { + destroy_collection(peer); + destroy_collection(host); + destroy_collection(socket); + printf("Failed to add property. Error %d\n",error); + return error; + } + + /* Embed peer host into the socket2 as reference */ + error = add_collection_to_collection(socket,NULL,"peer",peer,ELAPI_ADD_MODE_REFERENCE); + if(error) { + destroy_collection(peer); + destroy_collection(host); + destroy_collection(socket); + printf("Failed to create collection. Error %d\n",error); + return error; + } + + /* Construct event */ + if((error=create_collection(&event,"event")) || + (error=add_str_property(event,NULL,"escape1","ds\\sd=ewrw===sada",0)) || + (error=add_str_property(event,NULL,"escape2","dss,d,=,ewrw===sada",0)) || + (error=add_str_property(event,NULL,"escape3","ds\\sd=ewrw,()===sada",0)) || + (error=add_str_property(event,NULL,"escape4","dssd=ewrw===))))sada",0)) || + (error=add_str_property(event,NULL,"escape5","d\\ss(((((d=ew(()()()(),(,,\\\\\\\\rw===sada",0)) || + (error=add_int_property(event,NULL,"something",100))) { + destroy_collection(peer); + destroy_collection(host); + destroy_collection(socket); + destroy_collection(event); + printf("Failed to create collection. Error %d\n",error); + return error; + } + + /* Add host to event */ + error = add_collection_to_collection(event,NULL,NULL,host,ELAPI_ADD_MODE_REFERENCE); + if(error) { + destroy_collection(peer); + destroy_collection(host); + destroy_collection(socket); + destroy_collection(event); + printf("Failed to add collections. Error %d\n",error); + return error; + } + + error = add_collection_to_collection(event,NULL,NULL,socket,ELAPI_ADD_MODE_REFERENCE); + if(error) { + destroy_collection(peer); + destroy_collection(host); + destroy_collection(socket); + destroy_collection(event); + printf("Failed to add collections. Error %d\n",error); + return error; + } + + debug_collection(event,ELAPI_TRAVERSE_DEFAULT); + + return EOK; +} + + +int main() +{ + int error = EOK; + struct dispatcher_handle *dispatcher; + char *sinks[]= { "foo", "stderr", "bar", NULL }; + + printf("Test start\n"); + + + error = create_audit_dispatcher(&dispatcher,"my_app",sinks,NULL,NULL); + printf("create_audit_dispatcher returned %d\n", error); + + /* Try to log with invalid parameters */ + printf("%s","=================\nNegative test1\n"); + log_audit_event(NULL, NULL); + printf("%s","=================\nNegative test2\n"); + log_audit_event(dispatcher, NULL); + + printf("%s","=================\nCreating collection\n"); + error = construct_event(); + if(error) { + printf("Error creating event %d\n",error); + return error; + } + + + printf("%s","=================\nLogging peer - expect success\n"); + print_collection(peer); + log_audit_event(dispatcher, peer); + printf("%s","=================\nLogging host - expect success\n"); + print_collection(host); + log_audit_event(dispatcher, host); + printf("%s","=================\nLogging socket - expect success\n"); + print_collection(socket); + log_audit_event(dispatcher, socket); + printf("%s","=================\nLogging event - expect success\n"); + print_collection(event); + log_audit_event(dispatcher, event); + + /* Try to alter list of sinks */ + printf("%s","=================\nSeries of negative test cases.\n"); + /* Deleting non exisintg sink */ + error = alter_audit_dispatcher(dispatcher,"far2",ELAPI_SINK_ACTION_DELETE); + if(error == 0) printf("%s","Expected failure got success\n"); + else printf("Expected failure. Error : %d\n",error); + + /* Editing non exisintg sink */ + error = alter_audit_dispatcher(dispatcher,"far2",ELAPI_SINK_ACTION_ENABLE); + if(error == 0) printf("%s","Expected failure got success\n"); + else printf("Expected failure. Error : %d\n",error); + + /* Adding duplicate */ + error = alter_audit_dispatcher(dispatcher,"bar",ELAPI_SINK_ACTION_ADD); + if(error == 0) printf("%s","Expected failure got success\n"); + else printf("Expected failure. Error : %d\n",error); + + /* Adding new */ + error = alter_audit_dispatcher(dispatcher,"far2",ELAPI_SINK_ACTION_ADD); + if(error != 0) printf("Expected success got failure %d\n",error); + else printf("Success : %d\n",error); + + /* Enable */ + error = alter_audit_dispatcher(dispatcher,"far2",ELAPI_SINK_ACTION_ENABLE); + if(error != 0) printf("Expected success got failure %d\n",error); + else printf("Success : %d\n",error); + + /* Pulse */ + error = alter_audit_dispatcher(dispatcher,"far2",ELAPI_SINK_ACTION_PULSE); + if(error != 0) printf("Expected success got failure %d\n",error); + else printf("Success : %d\n",error); + + /* Disable */ + error = alter_audit_dispatcher(dispatcher,"far2",ELAPI_SINK_ACTION_DISABLE); + if(error != 0) printf("Expected success got failure %d\n",error); + else printf("Success : %d\n",error); + + /* Delete */ + error = alter_audit_dispatcher(dispatcher,"far2",ELAPI_SINK_ACTION_DELETE); + if(error != 0) printf("Expected success got failure %d\n",error); + else printf("Success : %d\n",error); + + destroy_audit_dispatcher(dispatcher); + destroy_collection(peer); + destroy_collection(host); + destroy_collection(socket); + destroy_collection(event); + printf("Test end\n"); + return error; +} + + + |