X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Fsi95_test.c;h=fb6e7fdaaa8de94651a3c2d145f44f6c2931a018;hb=e2c0690497a2db5659b3e138e4e106d70fee4aab;hp=70f13e86876802113537d45113458ad7c4a0723f;hpb=05850e0815095c029ecff43ac8e0983f2fba4fb6;p=ric-plt%2Flib%2Frmr.git diff --git a/test/si95_test.c b/test/si95_test.c index 70f13e8..fb6e7fd 100644 --- a/test/si95_test.c +++ b/test/si95_test.c @@ -1,8 +1,8 @@ // :vi sw=4 ts=4 noet: /* ================================================================================== - Copyright (c) 2020 Nokia - Copyright (c) 2020 AT&T Intellectual Property. + Copyright (c) 2020-2021 Nokia + Copyright (c) 2020-2021 AT&T Intellectual Property. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -32,29 +32,20 @@ #include #include #include -#include #include #include - -#include // these four needed for si address tests -#include -#include -#include - - - #include -#include -#include #include -#include -#include #include -#include #include -#include #include + +#include // these four needed for si address tests +#include +#include +#include + #define DEBUG 1 // specific test tools in this directory @@ -66,14 +57,39 @@ #include "test_support.c" // things like fail_if() #include "test_transport_em.c" // system/transport emulation (open, close, connect, etc) -/* -#include "rmr.h" // things the users see -#include "rmr_symtab.h" -#include "rmr_agnostic.h" // transport agnostic header -*/ #include #include + +// ------------- dummy functions to force edge cases when we can --------------------------------------- + +#define SYSTEM_UNDER_TEST 1 // for conditional code + +/* + These are global so they can be reset for individual tests. +*/ +static int good_mallocs = 0; // number of initial good malocs before failurs +static int bad_mallocs = 1; // number of failed mallocs (consecutive) + +static void* test_malloc( size_t n ) { + + fprintf( stderr, ">>>> test malloc: %d %d\n", good_mallocs, bad_mallocs ); + if( good_mallocs ) { + good_mallocs--; + return malloc( n ); + } + + if( bad_mallocs ) { + bad_mallocs--; + errno = ENOMEM; + return NULL; + } + + return malloc( n ); +} + +// ----------------------------------------------------------------------------------------------------- + #include //#include #include @@ -95,7 +111,9 @@ #include #include #include -//#include +#define malloc test_malloc +#include +#undef malloc // --------------------------------------------------------------------- @@ -145,6 +163,7 @@ static int memory( ) { ptr = SInew( GI_BLK ); errors += fail_if_nil( ptr, "memory: sinew returned nil when given giblk request" ); SItrash( GI_BLK, ptr ); // GI block cannot be trashed, ensure this (valgind will complain about a leak) + free( ptr ); // we can free GI block only in tests fprintf( stderr, " memory module finished with %d errors\n", errors ); return errors; @@ -181,10 +200,17 @@ static int cleanup() { SItp_stats( si_ctx ); // drive for coverage only SItp_stats( NULL ); - SIconnect( si_ctx, "localhost:43086" ); // ensure context has a tp block to free on shutdown + SIconnect( si_ctx, "127.0.0.1:43086" ); // ensure context has a tp block to free on shutdown SIshutdown( NULL ); SIabort( si_ctx ); + // cleaning up the remaining global resources + struct ginfo_blk *gptr = (struct ginfo_blk*)si_ctx; + SItrash( TP_BLK, gptr->tplist ); + free( gptr->tp_map ); + free( gptr->rbuf ); + free( gptr->cbtab ); + free( si_ctx ); fprintf( stderr, " cleanup module finished with %d errors\n", errors ); return errors; @@ -196,13 +222,14 @@ static int cleanup() { static int addr() { int errors = 0; int l; - struct sockaddr* addr; char buf1[4096]; // space to build buffers for xlation char* hr_addr; // human readable address returned void* net_addr; // a network address block of some type - addr = (struct sockaddr *) malloc( sizeof( struct sockaddr ) ); /* + struct sockaddr* addr; + addr = (struct sockaddr *) malloc( sizeof( struct sockaddr ) ); + l = SIgenaddr( " [ff02::4]:4567", PF_INET6, IPPROTO_TCP, SOCK_STREAM, &addr ); SIgenaddr( " [ff02::4]:4567", PF_INET6, IPPROTO_TCP, SOCK_STREAM, &addr ); @@ -212,7 +239,7 @@ static int addr() { errors += fail_if_true( l != 0, "SIaddress given two null pointers didn't return 0 len" ); l = SIaddress( buf1, NULL, 0 ); errors += fail_if_true( l != 0, "SIaddress given null dest pointer didn't return 0 len" ); - l = SIaddress( NULL, buf1, 0 ); + l = SIaddress( NULL, (void *) &buf1, 0 ); errors += fail_if_true( l != 0, "SIaddress given null src pointer didn't return 0 len" ); net_addr = NULL; @@ -224,20 +251,22 @@ static int addr() { snprintf( buf1, sizeof( buf1 ), "[ff02::5]:4002" ); // v6 might not be supported so failure is OK here; driving for coverage l = SIaddress( buf1, &net_addr, AC_TOADDR6 ); if( l > 0 ) { - l = SIaddress( net_addr, &hr_addr, AC_TODOT ); // convert the address back to hr string + l = SIaddress( net_addr, (void *) &hr_addr, AC_TODOT ); // convert the address back to hr string errors += fail_if_true( l < 1, "v6 to dot conversion failed" ); errors += fail_if_nil( hr_addr, "v6 to dot conversion yields a nil pointer" ); free( net_addr ); + free( hr_addr ); } snprintf( buf1, sizeof( buf1 ), "localhost:43086" ); l = SIaddress( buf1, (void **) &net_addr, AC_TOADDR ); errors += fail_if_true( l < 1, "v4 to addr conversion failed" ); - l = SIaddress( net_addr, &hr_addr, AC_TODOT ); // convert the address back to hr string + l = SIaddress( net_addr, (void *) &hr_addr, AC_TODOT ); // convert the address back to hr string errors += fail_if_true( l < 1, "to dot convdersion failed" ); errors += fail_if_nil( hr_addr, "v4 to dot conversion yields a nil pointer" ); free( net_addr ); + free( hr_addr ); fprintf( stderr, " addr module finished with %d errors\n", errors ); return errors; @@ -253,6 +282,7 @@ static int prep() { thing = SIlisten_prep( UDP_DEVICE, "localhost:1234", AF_INET ); errors += fail_if_nil( thing, "listen prep udp returned nil block" ); + SItrash( TP_BLK, thing ); thing = SIlisten_prep( UDP_DEVICE, "localhost:1234", 84306 ); // this should fail errors += fail_not_nil( thing, "listen prep udp returned valid block ptr for bogus family" ); @@ -270,6 +300,21 @@ static int prep() { static int poll() { int errors = 0; int status; + struct ginfo_blk* dummy; + + + dummy = SIinitialise( 0 ); // get one to fiddle to drive edge cases + dummy->flags |= GIF_SHUTDOWN; // shutdown edge condition + SIpoll( dummy, 1 ); + + free( dummy->tp_map ); + free( dummy->rbuf ); + free( dummy->cbtab ); + + memset( dummy, 0, sizeof( *dummy ) ); // force bad cookie check code to drive + SIpoll( dummy, 1 ); + + free (dummy ); status = SIpoll( si_ctx, 1 ); errors += fail_if_true( status != 0, "poll failed" ); @@ -398,6 +443,8 @@ static int new_sess( ) { status = SInewsession( si_ctx, tpptr ); errors += fail_if_true( status < 0, "newsession did failed when accept was good" ); + free( tpptr ); + fprintf( stderr, " new_sess module finished with %d errors\n", errors ); return errors; } @@ -436,6 +483,36 @@ static int send_tests( ) { } +/* + Wait testing. This is tricky because we don't have any sessions and thus it's difficult + to drive much of SIwait(). +*/ +static int wait_tests() { + int errors = 0; + struct ginfo_blk* dummy; + + + dummy = SIinitialise( 0 ); // get one to fiddle to drive edge cases + SIwait( dummy ); // malloc should "fail" + + dummy->flags |= GIF_SHUTDOWN; + SIwait( dummy ); + + free( dummy->tp_map ); + free( dummy->rbuf ); + free( dummy->cbtab ); + + memset( dummy, 0, sizeof( *dummy ) ); // force bad cookie check code to drive + SIwait( dummy ); + + free( dummy ); + + + SIwait( si_ctx ); // should drive once through the loop + + return errors; +} + // ---------------------------------------------------------------------------------------- /* @@ -459,6 +536,7 @@ int main() { errors += send_tests(); errors += poll(); + errors += wait_tests(); errors += cleanup();