26b1b21a38d12cc2ec34918abefed32f6a9bfbd0
[ric-plt/lib/rmr.git] / test / si95_test.c
1 // :vi sw=4 ts=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2020 Nokia
5         Copyright (c) 2020 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mmemonic:       si95_test.c
23         Abstract:       This is the main driver to test the si95 core functions
24                                 (within rmr/src/si/src/si95). 
25
26         Author:         E. Scott Daniels
27         Date:           6 March 2018
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <netdb.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <pthread.h>
37 #include <ctype.h>
38
39 #include <netdb.h>              // these four needed for si address tests
40 #include <stdio.h>
41 #include <ctype.h>
42 #include <netinet/in.h>
43
44
45
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <strings.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <stdint.h>
53 #include <ctype.h>
54 #include <sys/epoll.h>
55 #include <pthread.h>
56 #include <semaphore.h>
57
58 #define DEBUG 1
59
60                                                                                         // specific test tools in this directory
61 #undef NNG_UNDER_TEST                                           // NNG is NOT under test so undefine if set
62 #define NO_EMULATION 1                                          // no emulation of transport functions
63 #define NO_PRIVATE_HEADERS 1                            // no rmr_si or rmr_nng headers
64 #define NO_DUMMY_RMR 1                                          // no msg things
65 #include "test_support.c"                                       // things like fail_if()
66
67
68 /*
69 #include "rmr.h"                                        // things the users see
70 #include "rmr_symtab.h"
71 #include "rmr_agnostic.h"                       // transport agnostic header
72 */
73 #include <rmr_logging.h>
74 #include <logging.c>
75
76 #include <si95/siaddress.c>
77 //#include <si95/sialloc.c>
78 //#include <si95/sibldpoll.c>
79 //#include <si95/sicbreg.c>
80 //#include <si95/sicbstat.c>
81 //#include <si95/siclose.c>
82 #include <si95/siconnect.c>
83 #include <si95/siestablish.c>
84 //#include <si95/sigetadd.c>
85 //#include <si95/sigetname.c>
86 #include <si95/siinit.c>
87 //#include <si95/silisten.c>
88 #include <si95/sinew.c>
89 //#include <si95/sinewses.c>
90 //#include <si95/sipoll.c>
91 //#include <si95/sircv.c>
92 //#include <si95/sisend.c>
93 //#include <si95/sisendt.c>
94 #include <si95/sishutdown.c>
95 #include <si95/siterm.c>
96 #include <si95/sitrash.c>
97 //#include <si95/siwait.c>
98
99 // ---------------------------------------------------------------------
100
101 void*   si_ctx = NULL;                  // a global context might be useful
102
103 // ---------------------------------------------------------------------
104
105 /*
106         Memory allocation/free related tests
107 */
108 static int memory( ) {
109         int             errors = 0;
110         void*   ptr;
111         void*   iptr;
112
113         // ---- SInew ----------------
114         ptr = SInew( 100 );                             // invalid block type should return nil
115         errors += fail_not_nil( ptr, "memory: sinew did not return nil when given a valid struct type" );
116         SItrash( 100, NULL );                           // drive trash for coverage
117
118         iptr = SInew( IOQ_BLK );
119         errors += fail_if_nil( iptr, "memory: sinew returned nil when given ioq request" );
120         SItrash(  IOQ_BLK, iptr );
121
122         ptr = SInew( TP_BLK );
123         errors += fail_if_nil( ptr, "memory: sinew returned nil when given tpblk request" );
124         if( ptr ) {
125                 iptr = SInew( IOQ_BLK );
126                 ((struct tp_blk *)ptr)->squeue = iptr;
127                 SItrash(  TP_BLK, ptr );
128         }
129         
130         ptr = SInew( GI_BLK );
131         errors += fail_if_nil( ptr, "memory: sinew returned nil when given giblk request" );
132         SItrash(  GI_BLK, ptr );                // GI block cannot be trashed, ensure this (valgind will complain about a leak)
133
134
135         return errors;
136 }
137
138
139 /*
140         Test initialisation related things
141 */
142 static int init() {
143         int             errors = 0;
144
145         si_ctx = SIinitialise( 0 );
146         errors += fail_if_nil( si_ctx, "init: siinit returned a nil pointer" );
147
148         SIclr_tflags( si_ctx, 0x00 );           // drive for coverage; no return value from these
149         SIset_tflags( si_ctx, 0x03 );
150
151         return errors;
152 }
153
154 static int cleanup() {
155         int errors = 0;
156         
157         if( ! si_ctx ) {
158                 return 0;
159         }
160
161         SIconnect( si_ctx, "localhost:43086" );         // ensure context has a tp block to free on shutdown
162         SIshutdown( si_ctx );
163
164         return errors;
165 }
166
167 /*
168         Address related tests.
169 */
170 static int addr() {
171         int errors = 0;
172         int l;
173         struct sockaddr* addr;
174         char buf1[4096];
175         char buf2[4096];
176         char* dest;
177
178         addr = (struct sockaddr *) malloc( sizeof( struct sockaddr ) );
179 /*
180         l = SIgenaddr( "    [ff02::4]:4567", PF_INET6, IPPROTO_TCP, SOCK_STREAM, &addr );
181
182         SIgenaddr( "    [ff02::4]:4567", PF_INET6, IPPROTO_TCP, SOCK_STREAM, &addr );
183 */
184
185         dest = NULL;
186         snprintf( buf1, sizeof( buf1 ), "   [ff02::5:4001" );           // invalid address, drive leading space eater too
187         l = SIaddress( buf1, &dest, AC_TOADDR6 );
188         errors += fail_if_true( l > 0, "to addr6 with bad addr convdersion returned valid len" );
189
190         snprintf( buf1, sizeof( buf1 ), "[ff02::5]:4002" );             // v6 might not be supported so failure is OK here
191         l=SIaddress( buf1, &dest, AC_TOADDR6 );
192         errors += fail_if_true( l < 1, "to addr convdersion failed" );
193
194         snprintf( buf1, sizeof( buf1 ), "localhost:43086" );
195         l = SIaddress( buf1, (void **) &dest, AC_TOADDR );
196         errors += fail_if_true( l < 1, "to addr convdersion failed" );
197
198         snprintf( buf1, sizeof( buf1 ), "localhost:4004" );
199         l = SIaddress( buf1, &dest, AC_TODOT );
200         errors += fail_if_true( l < 1, "to dot convdersion failed" );
201
202         return errors;
203
204 }
205
206 /*
207         Drive tests...
208 */
209 int main() {
210         int errors = 0;
211
212         rmr_set_vlevel( 5 );                    // enable all debugging
213
214         fprintf( stderr, "\n<INFO> starting SI95 tests\n" );
215
216         errors += init();
217         errors += memory();
218         errors += addr();
219 fprintf( stderr, ">>> cleaning\n" );
220         errors += cleanup();
221
222         fprintf( stderr, "<INFO> testing finished\n" );
223         if( errors == 0 ) {
224                 fprintf( stderr, "<PASS> all tests were OK\n\n" );
225         } else {
226                 fprintf( stderr, "<FAIL> %d errors in SI95 core code\n\n", errors );
227         }
228
229         return !!errors;
230 }