feat(routing): Support session based routing
[ric-plt/lib/rmr.git] / test / rmr_nng_api_static_test.c
index e9ecb72..cefc09f 100644 (file)
@@ -1,14 +1,14 @@
 // : vi ts=4 sw=4 noet :
 /*
 ==================================================================================
-        Copyright (c) 2019 Nokia 
-        Copyright (c) 2018-2019 AT&T Intellectual Property.
+           Copyright (c) 2019 Nokia
+           Copyright (c) 2018-2019 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
 
-       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,
@@ -23,7 +23,7 @@
        Abstract:       Specific tests related to the API functions in rmr_nng.c/rmr.c.
                                This should be included by a driver, but only the main RMr
                                driver and there likely not be a specific stand alone driver
-                               for just this small set of tests because of the depth of the 
+                               for just this small set of tests because of the depth of the
                                library needed to test at this level.
 
                                The message buffer specific API tests are in a different static
@@ -73,7 +73,7 @@ static void send_n_msgs( void* ctx, int n ) {
        }
 
        for( i = 0; i < n; i++ ) {
-fprintf( stderr, "mass send\n" );
+               //fprintf( stderr, "mass send\n" );
                msg->len = 100;
                msg->mtype = 1;
                msg->state = 999;
@@ -91,6 +91,7 @@ static int rmr_api_test( ) {
        int             v = 0;                                  // some value
        char    wbuf[128];
        int             i;
+       int             state;
 
        v = rmr_ready( NULL );
        errors += fail_if( v != 0, "rmr_ready returned true before initialisation" );
@@ -129,7 +130,7 @@ static int rmr_api_test( ) {
        } else {
                errors += fail_if( v < 0, "rmr_payload_size returned invalid size for good message" );
        }
-       
+
        v = rmr_get_rcvfd( NULL );
        errors += fail_if( v >= 0, "rmr_get_rcvfd returned a valid file descriptor when given nil context" );
        v = rmr_get_rcvfd( rmc );
@@ -138,6 +139,10 @@ static int rmr_api_test( ) {
        msg2 = rmr_send_msg( NULL, NULL );                      // drive for coverage
        errors += fail_not_nil( msg2, "send_msg returned msg pointer when given a nil message and context" );
 
+       msg->state = 0;
+       msg = rmr_send_msg( NULL, msg );
+       errors += fail_if( msg->state == 0, "rmr_send_msg did not set msg state when msg given with nil context" );
+
        // --- sends will fail with a no endpoint error until a dummy route table is set, so we test fail case first.
        msg->len = 100;
        msg->mtype = 1;
@@ -193,17 +198,28 @@ static int rmr_api_test( ) {
        rmr_rts_msg( rmc, NULL );
        errors += fail_if( errno == 0, "rmr_rts_msg did not set errno when given a nil message" );
 
+       msg->state = 0;
+       msg = rmr_rts_msg( NULL, msg );                 // should set state in msg
+       errors += fail_if_equal( msg->state, 0, "rmr_rts_msg did not set state when given valid message but no context" );
+       
+
        msg = rmr_rts_msg( rmc, msg );                  // return the buffer to the sender
        errors += fail_if_nil( msg, "rmr_rts_msg did not return a message pointer" );
        errors += fail_if( errno != 0, "rmr_rts_msg did not reset errno" );
 
 
        snprintf( msg->xaction, 17, "%015d", 16 );              // dummy transaction id (emulation generates, this should arrive after a few calls to recv)
+
+       msg->state = 0;
+       msg = rmr_call( NULL, msg );
+       errors += fail_if( msg->state == 0, "rmr_call did not set message state when given message with nil context" );
+
+       msg->mtype = 0;
        msg = rmr_call( rmc, msg );                                             // this call should return a message as we can anticipate a dummy message in
        errors += fail_if_nil( msg, "rmr_call returned a nil message on call expected to succeed" );
        if( msg ) {
                errors += fail_not_equal( msg->state, RMR_OK, "rmr_call did not properly set state on successful return" );
-               errors += fail_if( errno != 0, "rmr_call did not properly set errno on successful return" );
+               errors += fail_not_equal( errno, 0, "rmr_call did not properly set errno (a) on successful return" );
        }
 
        snprintf( wbuf, 17, "%015d", 14 );                              // if we call receive we should find this in the first 15 tries
@@ -220,7 +236,7 @@ static int rmr_api_test( ) {
        }
 
        errors += fail_if( i >= 16, "did not find expected message on queue" );
-               
+
        if( ! msg ) {
                msg = rmr_alloc_msg( rmc, 2048 );                               // something buggered above; get a new one
        }
@@ -239,6 +255,11 @@ static int rmr_api_test( ) {
        rmr_free_msg( msg2 );
 
 
+       msg2 = rmr_torcv_msg( NULL, NULL, 10 );
+       errors += fail_not_nil( msg2, "rmr_torcv_msg returned a pointer when given nil information" );
+       msg2 = rmr_torcv_msg( rmc, NULL, 10 );
+       errors += fail_if_nil( msg2, "rmr_torcv_msg did not return a message pointer when given a nil old msg" );
+
        // ---  test timeout receive; our dummy epoll function will return 1 ready on first call and 0 ready (timeout emulation) on second
        //              however we must drain the swamp (queue) first, so run until we get a timeout error, or 20 and report error if we get to 20.
        msg = NULL;
@@ -253,19 +274,31 @@ static int rmr_api_test( ) {
        }
        errors += fail_if( i >= 40, "torcv_msg never returned a timeout" );
 
-       
+
+       // ---- trace things that are not a part of the mbuf_api functions and thus must be tested here
+       state = rmr_init_trace( NULL, 37 );                                             // coverage test nil context
+       errors += fail_not_equal( state, 0, "attempt to initialise trace with nil context returned non-zero state (a)" );
+       errors += fail_if_equal( errno, 0, "attempt to initialise trace with nil context did not set errno as expected" );
+
+       state = rmr_init_trace( rmc, 37 );
+       errors += fail_if_equal( state, 0, "attempt to set trace len in context was not successful" );
+       errors += fail_not_equal( errno, 0, "attempt to set trace len in context did not clear errno" );
+
+       msg = rmr_tralloc_msg( rmc, 1024, 17, "1904308620110417" );
+       errors += fail_if_nil( msg, "attempt to allocate message with trace data returned nil message" );
+       state = rmr_get_trace( msg, wbuf, 17 );
+       errors += fail_not_equal( state, 17, "len of trace data (a) returned after msg allocation was not expected size (b)" );
+       state = strcmp( wbuf, "1904308620110417" );
+       errors += fail_not_equal( state, 0, "trace data returned after tralloc was not correct" );
+
        em_send_failures = 1;
        send_n_msgs( rmc, 30 );                 // send 30 messages with emulation failures
        em_send_failures = 0;
 
-       
+
        rmr_close( NULL );                      // drive for coverage
        rmr_close( rmc );                       // no return to check; drive for coverage
 
-//extern rmr_mbuf_t* rmr_mtosend_msg( void* vctx, rmr_mbuf_t* msg, int max_to ) {
-//extern rmr_mbuf_t* rmr_torcv_msg( void* vctx, rmr_mbuf_t* old_msg, int ms_to ) {
-
-
 
        if( ! errors ) {
                fprintf( stderr, "<INFO> all RMr API tests pass\n" );