Release RMR version 4.8.2
[ric-plt/lib/rmr.git] / test / wormhole_static_test.c
index 7a4246f..cbca81d 100644 (file)
@@ -1,14 +1,14 @@
 // : vi ts=4 sw=4 noet :
 /*
 ==================================================================================
 // : vi ts=4 sw=4 noet :
 /*
 ==================================================================================
-        Copyright (c) 2019 Nokia 
-        Copyright (c) 2018-2019 AT&T Intellectual Property.
+           Copyright (c) 2019-2021 Nokia
+           Copyright (c) 2018-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.
    You may obtain a copy of the License at
 
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
-       http://www.apache.org/licenses/LICENSE-2.0
+          http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
 #include <errno.h>
 #include <string.h>
 #include <stdint.h>
 #include <errno.h>
 #include <string.h>
 #include <stdint.h>
+#include <pthread.h>
+#include <semaphore.h>
 
 
-#include "../src/common/include/rmr.h"
-#include "../src/common/include/rmr_agnostic.h"
+#include "rmr.h"
+#include "rmr_agnostic.h"
 
 
 /*
        Note that the last tests in this function destroy the context and message so
 
 
 /*
        Note that the last tests in this function destroy the context and message so
-       any tests added MUST be ahead of those tests. 
+       any tests added MUST be ahead of those tests.
 */
 static int worm_test( ) {
        uta_ctx_t* ctx;                 // context needed to test load static rt
        char    wbuf[1024];
        int errors = 0;                 // number errors found
 */
 static int worm_test( ) {
        uta_ctx_t* ctx;                 // context needed to test load static rt
        char    wbuf[1024];
        int errors = 0;                 // number errors found
+       int             state = 0;
        int     i;
        int     i;
+       void* p;
 
        rmr_mbuf_t*     mbuf;           // mbuf to send to peer
 
        rmr_mbuf_t*     mbuf;           // mbuf to send to peer
+       rmr_mbuf_t*     resp;           // response message from a wormhole call
        int             whid = -1;
        int             last_whid;
 
        int             whid = -1;
        int             last_whid;
 
-       ctx = (uta_ctx_t *) malloc( sizeof( uta_ctx_t ) );
-       if( ctx == NULL ) {
-               fail_if_nil( ctx, "could not allocate dummy context" );
-               return 1;
-       }
-       memset( ctx, 0, sizeof( *ctx ) );
+       ctx = mk_dummy_ctx();
        ctx->my_name = strdup( "tester" );
        ctx->my_name = strdup( "tester" );
+       ctx->my_ip = strdup( "30.4.19.86:1111" );
 
        gen_rt( ctx );
 
 
        gen_rt( ctx );
 
+       // ---- must run these checks before wormhole(s) are opened --------------------------
+       mbuf = rmr_alloc_msg( ctx, 2048 );                      // get an muf to pass round, no need to init for first test
+       resp = rmr_wh_call( ctx, 0, mbuf, 1, 10 );
+       errors += fail_if_nil( resp, "wormhole call given valid context and msg, before wormholes open  didn't return message pointer" );
+       if( resp ) {
+       errors += fail_if_equal( resp->state, RMR_OK, "wormhole call given valid context and msg, before wormholes open returned ok state" );
+       }
+
+       rmr_wh_close( NULL, 0 );                        // drive for coverage; nothing to vet
+       rmr_wh_close( ctx, 0 );
+       wh_init( NULL );
+
+       // ----- end must run before opening wormholes ---------------------------------------
+
        whid = rmr_wh_open( NULL, NULL );
        errors += fail_not_equal( whid, -1, "call to wh_open with invalid values did not return bad whid" );
 
        whid = rmr_wh_open( NULL, NULL );
        errors += fail_not_equal( whid, -1, "call to wh_open with invalid values did not return bad whid" );
 
@@ -76,6 +91,9 @@ static int worm_test( ) {
        whid = rmr_wh_open( ctx, "localhost:89219" );
        errors += fail_if_equal( whid, -1, "call to wh_open with valid target failed" );
 
        whid = rmr_wh_open( ctx, "localhost:89219" );
        errors += fail_if_equal( whid, -1, "call to wh_open with valid target failed" );
 
+       i = wh_init( ctx );                                     // already initialised, ensure no harm
+       errors += fail_not_equal( i, 1, "second call to wh_init() didn't return true" );
+
        rmr_wh_close( ctx, 4 );                                 // test for coverage only; [5] should have nil pointer
        rmr_wh_close( ctx, 50 );                                        // test for coverage only; more than allocated reference
 
        rmr_wh_close( ctx, 4 );                                 // test for coverage only; [5] should have nil pointer
        rmr_wh_close( ctx, 50 );                                        // test for coverage only; more than allocated reference
 
@@ -100,11 +118,23 @@ static int worm_test( ) {
        whid = rmr_wh_open( ctx, "localhost:21961" );
        errors += fail_not_equal( whid, 3, "attempt to fill in a hole didn't return expected" );
 
        whid = rmr_wh_open( ctx, "localhost:21961" );
        errors += fail_not_equal( whid, 3, "attempt to fill in a hole didn't return expected" );
 
-       rmr_wh_send_msg( NULL, 0, NULL );                       // tests for coverage
-       rmr_wh_send_msg( ctx, 0, NULL );
+       p = rmr_wh_send_msg( NULL, 0, NULL );                   // tests for coverage
+       fail_not_nil( p, "wh_send_msg returned a pointer when given nil context and message" );
+
+       p = rmr_wh_send_msg( ctx, 0, NULL );
+       fail_not_nil( p, "wh_send_msg returned a pointer when given nil message with valid context" );
 
        mbuf = rmr_alloc_msg( ctx, 2048 );                      // get an muf to pass round
        errors += fail_if_nil( mbuf, "unable to allocate mbuf for send tests (giving up on send tests)" );
 
        mbuf = rmr_alloc_msg( ctx, 2048 );                      // get an muf to pass round
        errors += fail_if_nil( mbuf, "unable to allocate mbuf for send tests (giving up on send tests)" );
+
+       mbuf->state = 0;
+       mbuf = rmr_wh_send_msg( NULL, 0, mbuf );
+       if( mbuf ) {
+               fail_if_equal( mbuf->state, 0, "wh_send_msg returned a zero state when given a nil context" );
+       }
+       fail_if_nil( mbuf, "wh_send_msg returned a nil message buffer when given a nil context"  );
+
+
        while( mbuf ) {
                if( !(mbuf = rmr_wh_send_msg( ctx, 50, mbuf )) ) {              // test for coverage
                        errors += fail_if_nil( mbuf, "send didn't return an mbuf (skip rest of send tests)" );
        while( mbuf ) {
                if( !(mbuf = rmr_wh_send_msg( ctx, 50, mbuf )) ) {              // test for coverage
                        errors += fail_if_nil( mbuf, "send didn't return an mbuf (skip rest of send tests)" );
@@ -123,7 +153,47 @@ static int worm_test( ) {
                break;
        }
 
                break;
        }
 
+       // ---- wormhole state -----------
+       state = rmr_wh_state( NULL, 0 );
+       errors += fail_if_equal( state, RMR_OK, "wh_state with bad context did not return error" );
+
+       state = rmr_wh_state( ctx, -1 );
+       errors += fail_if_equal( state, RMR_OK, "wh_state with bad whid did not return error" );
+
+       whid = rmr_wh_open( ctx, "localhost:9219" );
+       if( ! fail_if_equal( whid, -1, "skipping some wh_state tests: no whid returned" ) ) {
+               state = rmr_wh_state( ctx, whid );
+               errors += fail_not_equal( state, RMR_OK, "wh_state on an open wh did not return OK" );
+
+               rmr_wh_close( ctx, whid );
+               state = rmr_wh_state( ctx, whid );
+               errors += fail_if_equal( state, RMR_OK, "wh_state on a closed wh returned OK" );
+               whid = -1;
+       }
+
+       // ----- wormhole call --------------------------------------------------
+       mbuf = rmr_alloc_msg( ctx, 2048 );                              // ensure we have a buffer to pass
+       resp = rmr_wh_call( NULL, 0, NULL, 1, 10 );     // ensure no msg when msg is nil
+       errors += fail_not_nil( resp, "wormhole call given nil context and msg didn't return nil message" );
+
+       resp = rmr_wh_call( NULL, 0, mbuf, 1, 10 );      // ensure invalid state when context is bad
+       errors += fail_if_nil( resp, "wormhole call given nil context and valid msg didn't return message pointer" );
+       if( resp != NULL ) {
+               errors += fail_if_equal( resp->state, RMR_OK, "wormhole call given nil context and valid message returned OK state" );
+       }
+
+       resp = rmr_wh_call( ctx, 2000, mbuf, 1, 10 );        // invalid wormhole id
+       errors += fail_if_nil( resp, "wormhole call given bad whid returned no msg" );
+       if( resp ) {
+               errors += fail_if_equal( resp->state, RMR_OK, "wormhole call given bad whid returned good state" );
+       }   
+
+       whid = rmr_wh_open( ctx, "localhost:9219" );    // ensure one is open
+       resp = rmr_wh_call( ctx, whid, mbuf, 1, 1 );
+       errors += fail_if_nil( resp, "wormhole call given valid info returned nil response" );
+
 
 
+       // -----------------------------------------------------------------------
        // WARNING:  these tests destroy the context, so they MUST be last
        if( mbuf ) {                    // only if we got an mbuf
                errno = 0;
        // WARNING:  these tests destroy the context, so they MUST be last
        if( mbuf ) {                    // only if we got an mbuf
                errno = 0;
@@ -133,6 +203,7 @@ static int worm_test( ) {
                errors += fail_not_equal( mbuf->state, RMR_ERR_NOHDR, "send with bad header did now set msg state correctly" );
 
                errno = 0;
                errors += fail_not_equal( mbuf->state, RMR_ERR_NOHDR, "send with bad header did now set msg state correctly" );
 
                errno = 0;
+               wh_nuke( NULL );                                // coverage only
                wh_nuke( ctx );
                ctx->wormholes = NULL;
                mbuf = rmr_wh_send_msg( ctx, 4, mbuf );         // coverage test on mbuf header check
                wh_nuke( ctx );
                ctx->wormholes = NULL;
                mbuf = rmr_wh_send_msg( ctx, 4, mbuf );         // coverage test on mbuf header check
@@ -147,5 +218,5 @@ static int worm_test( ) {
                free( ctx );
        }
 
                free( ctx );
        }
 
-       return !!errors;                        // 1 or 0 regardless of count
+       return errors;
 }
 }