Correct table clear bug in route table ready
[ric-plt/lib/rmr.git] / test / rt_static_test.c
index dbe8d78..79d0561 100644 (file)
@@ -123,9 +123,11 @@ static int rt_test( ) {
        int             enu = 0;
        int             state;
        char    *buf;
+       char    *buf2;
        char*   seed_fname;             // seed file
        SOCKET_TYPE     nn_sock;        // differnt in each transport (nng == struct, SI/Nano == int)
        rmr_mbuf_t*     mbuf;           // message for meid route testing
+       void*   p;                              // generic pointer
 
        #ifndef NNG_UNDER_TEST
                si_ctx_t* si_ctx = NULL;
@@ -192,7 +194,11 @@ static int rt_test( ) {
        entries[enu].group = 0; entries[enu].ep_name = "localhost:5512"; enu++;
 
 
-       rt = uta_rt_init( );                                                                            // get us a route table
+       rt = uta_rt_init( NULL );
+       errors += fail_if_false( rt == NULL, "rt_init given a nil context didn't return nil" );
+
+       ctx = mk_dummy_ctx();           // make a dummy with rtgate mutex
+       rt = uta_rt_init( ctx );                                                                                // get us a route table
        if( (errors += fail_if_nil( rt, "pointer to route table" )) ) {
                fprintf( stderr, "<FAIL> abort: cannot continue without a route table\n" );
                exit( 1 );
@@ -224,7 +230,7 @@ static int rt_test( ) {
        // ----- end hacking together a route table ---------------------------------------------------
 
 
-       crt = uta_rt_clone( rt );                                                               // clone only the endpoint entries
+       crt = uta_rt_clone( ctx, rt, NULL, 0 );                                                         // create a new rt and clone only the me entries
        errors += fail_if_nil( crt, "cloned route table" );
        if( crt ) {
                c1 = count_entries( rt, 1 );
@@ -233,12 +239,14 @@ static int rt_test( ) {
 
                c2 = count_entries( crt, 0 );
                errors += fail_not_equal( c2, 0, "cloned (endpoints) table entries space 0 count (a) was not zero as expected" );
+
+               errors += fail_if_false( crt->ephash == rt->ephash, "ephash pointer in cloned table is not right" );
                uta_rt_drop( crt );
        }
 
 
-       crt = uta_rt_clone_all( rt );                                                   // clone all entries
-       errors += fail_if_nil( crt, "cloned all route table" );
+       crt = uta_rt_clone( ctx, rt, NULL, 1 );                                                 // clone all entries (MT and ME)
+       errors += fail_if_nil( crt, "cloned (all) route table" );
 
        if( crt ) {
                c1 = count_entries( rt, 0 );
@@ -248,6 +256,8 @@ static int rt_test( ) {
                c1 = count_entries( rt, 1 );
                c2 = count_entries( crt, 1 );
                errors += fail_not_equal( c1, c2, "cloned (all) table entries space 1 count (b) did not match original table count (a)" );
+
+               errors += fail_if_false( crt->ephash == rt->ephash, "ephash pointer in cloned table (all) is not right" );
                uta_rt_drop( crt );
        }
 
@@ -274,7 +284,7 @@ static int rt_test( ) {
        #ifdef NNG_UNDER_TEST
                state = uta_epsock_byname( rt, "localhost:4561", &nn_sock, &ep );               // this should be found
        #else
-               state = uta_epsock_byname( ctx, "localhost:4561", &nn_sock, &ep );      // this should be found
+               state = uta_epsock_byname( ctx, "localhost:4561", &nn_sock, &ep );              // this should be found
        #endif
        errors += fail_if_equal( state, 0, "socket (by name)" );
        errors += fail_if_nil( ep, "epsock_byname did not populate endpoint pointer when expected to" );
@@ -285,16 +295,24 @@ static int rt_test( ) {
                state = uta_epsock_byname( NULL, "localhost:4561", &nn_sock, &ep );             // test coverage on nil checks
        #else
                state = uta_epsock_byname( NULL, "localhost:4561", &nn_sock, &ep );
+               errors += fail_not_equal( state, 0, "socket (by name) nil context check returned true" );
+
+               p = ctx->si_ctx;
+               ctx->si_ctx = NULL;             // set to drive second test
+               state = uta_epsock_byname( ctx, "localhost:4561", &nn_sock, &ep );
+               ctx->si_ctx = p;
        #endif
        errors += fail_not_equal( state, 0, "socket (by name) nil check returned true" );
 
-       ep->open = 1;
-       #if  NNG_UNDER_TEST
-               state = uta_epsock_byname( rt, "localhost:4561", &nn_sock, NULL );              // test coverage on nil checks
-       #else
-               state = uta_epsock_byname( ctx, "localhost:4561", &nn_sock, NULL );
-       #endif
-       errors += fail_if_equal( state, 0, "socket (by name) open ep check returned false" );
+       if( ep ) {                                      // if previous test fails, cant run this
+               ep->open = 1;
+               #if  NNG_UNDER_TEST
+                       state = uta_epsock_byname( rt, "localhost:4561", &nn_sock, NULL );              // test coverage on nil checks
+               #else
+                       state = uta_epsock_byname( ctx, "localhost:4561", &nn_sock, NULL );
+               #endif
+               errors += fail_if_equal( state, 0, "socket (by name) open ep check returned false" );
+       }
 
 
        // --- test that the get_rte function finds expected keys, and retries to find 'bad' sid attempts for valid mtypes with no sid
@@ -371,7 +389,7 @@ static int rt_test( ) {
        errors += fail_if_true( state, "uta_epsock_rr returned bad (non-zero) state when given nil socket pointer" );
 
 
-       uta_rt_clone( NULL );                                                           // verify null parms don't crash things
+       uta_rt_clone( ctx, NULL, NULL, 0 );                                                             // verify null parms don't crash things
        uta_rt_drop( NULL );
        #ifdef NNG_UNDER_TEST
                uta_epsock_rr( NULL, 0,  &more, &nn_sock, &ep );                        // drive null case for coverage
@@ -404,18 +422,57 @@ static int rt_test( ) {
                free( buf );
        }
 
+       fprintf( stderr, "<INFO> test is overtly dropping rt table at %p\n", rt );
+       ctx->rtable = NULL;
        uta_rt_drop( rt );
        rt = NULL;
 
 
+       // --- force the load of a RT which has some edge case forcing issues
        if( ctx ) {
-               if( (seed_fname = getenv( "RMR_SEED_RT" )) != NULL ) {
-                       read_static_rt( ctx, 0 );
-                       rt = ctx->rtable;
-                       errors += fail_if_nil( rt, "read seed table didn't generate a rtable pointer in context" );
-                       unsetenv( "RMR_SEED_RT" );                              // remove for next test
-               }
-
+               char*   rt_stuff =
+                               "newrt | start | dummy-seed\n"
+                               "mse | 1  | -1 | localhost:84306\n"
+                               "mse | 10  | -1 | localhost:84306\n"
+                               "mse | 10  | 1 | localhost:84306\n"
+                               "# should cause failure because there aren't 10 entries above\n"
+                               "newrt | end | 10\n"
+
+                               "# this table has no end\n"
+                               "newrt | start | dummy-seed\n"
+                               "mse | 1  | -1 | localhost:84306\n"
+                               "mse | 10  | -1 | localhost:84306\n"
+                               "mse | 10  | 1 | localhost:84306\n"
+
+                               "# this table should be ok\n"
+                               "newrt | start | dummy-seed\n"
+                               "mse | 1  | -1 | localhost:84306\n"
+                               "mse | 10  | -1 | localhost:84306\n"
+                               "mse | 10  | 1 | localhost:84306\n"
+                               "newrt | end | 3\n"
+
+                               "# for an update to the existing table\n"
+                               "# not in progress; drive that exception check\n"
+                               "update | end | 23\n"
+
+                               "update | start | dummy-seed\n"
+                               "mse | 3 | 2 | localhost:2222\n"
+                               "# short record to drive test\n"
+                               "del\n"
+                               "# no table end for exception handling\n"
+
+                               "update | start | dummy-seed\n"
+                               "mse | 2 | 2 | localhost:2222\n"
+                               "del | 10 | 1\n"
+                               "update | end | 2\n";
+
+               fprintf( stderr, "<INFO> loading RT from edge case static table\n" );
+               fprintf( stderr, "<INFO> %s\n", rt_stuff );
+               gen_custom_rt( ctx, rt_stuff );
+               fprintf( stderr, "<INFO> edge case load completed\n" );
+               errors += fail_if_nil( ctx->rtable, "edge case route table didn't generate a pointer into the context" );
+
+               unsetenv( "RMR_SEED_RT" );                      // remove for next read try
                read_static_rt( ctx, 0 );                       // drive for not there coverage
        }
 
@@ -427,9 +484,11 @@ static int rt_test( ) {
 
 
        ep = (endpoint_t *) malloc( sizeof( *ep ) );
+       memset( ep, 0, sizeof( ep ) );
        pthread_mutex_init( &ep->gate, NULL );
        ep->name = strdup( "worm" );
        ep->addr = NULL;
+       ep->notify = 1;
        #ifdef NNG_UNDER_TEST
                state = uta_link2( ep );
        #else
@@ -458,6 +517,7 @@ static int rt_test( ) {
 
        #ifndef NNG_UNDER_TEST
                ep->open = 0;                                                   // context is used only if ep not open, so to check this test close the ep
+               ep->notify = 1;
                state = rt_link2_ep( NULL, ep );
                errors += fail_if_true( state, "rt_link2_ep returned true when given bad context" );
 
@@ -527,6 +587,36 @@ static int rt_test( ) {
                rt_epcounts( ctx->rtable, "testing" );
                rt_epcounts( NULL, "testing" );
 
+               buf = ensure_nlterm( NULL );
+               errors += fail_if_nil( buf, "ensure nlterm returned null pointer when given nil ptr" );
+               if( buf ) {
+                       errors += fail_not_equal( strlen( buf ), 1, "ensure nlterm returned incorrect length string when given nil pointer" );
+                       free( buf );
+               }
+
+               buf = ensure_nlterm( strdup( "x" ) );                   // should return "x\n"
+               errors += fail_if_nil( buf, "ensure nlterm returned null pointer when given single char string" );
+               if( buf ) {
+                       errors += fail_not_equal( strlen( buf ), 2, "ensure nlterm returned incorrect length string when given single char string" );
+                       free( buf );
+               }
+
+               buf = strdup( "x\n" );
+               buf2 = ensure_nlterm( buf );                                    // buffer returned should be the same
+               if( fail_not_pequal( buf, buf2, "ensure nlterm returned new buffer for one char string with newline" ) ) {
+                       errors++;
+                       free( buf2 );
+               }
+               free( buf );
+
+               buf = strdup( "Missing our trips to Gloria's for papossas.\n" );
+               buf2 = ensure_nlterm( buf );                                                                                    // buffer returned should be the same
+               if( fail_not_pequal( buf, buf2, "ensure nlterm returned new buffer for string with newline" ) ) {
+                       errors++;
+                       free( buf2 );
+               }
+               free( buf );
+
                buf = ensure_nlterm( strdup( "Stand up and cheer!" ) );                                 // force addition of newline
                if( buf ) {
                        errors += fail_not_equal( strcmp( buf, "Stand up and cheer!\n" ), 0, "ensure nlterm didn't add newline" );
@@ -575,5 +665,10 @@ static int rt_test( ) {
                errors += fail_not_nil( ep, "fd2ep delete returned a pointer for unknown mapping" );
        #endif
 
+       // ---------------- misc coverage tests --------------------------------------------------------------------------
+               collect_things( NULL, NULL, NULL, NULL, NULL );                         // these both return null, these test NP checks
+               collect_things( NULL, NULL, NULL, NULL, (void *) 1234 );                // the last is an invalid pointer, but check needed to force check on previous param
+               del_rte( NULL, NULL, NULL, NULL, NULL );
+
        return !!errors;                        // 1 or 0 regardless of count
 }