CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / test / hdr_static_test.c
1 // : vi ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4             Copyright (c) 2019-2021 Nokia
5             Copyright (c) 2018-2021 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:       hdr_static_test.c
23         Abstract:       This tests specific properties of the message header
24
25         Author:         E. Scott Daniels
26         Date:           12 April 2019
27 */
28
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <strings.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <netdb.h>
37 #include <pthread.h>
38 #include <semaphore.h>
39
40 #include <nng/nng.h>
41 #include <nng/protocol/pubsub0/pub.h>
42 #include <nng/protocol/pubsub0/sub.h>
43 #include <nng/protocol/pipeline0/push.h>
44 #include <nng/protocol/pipeline0/pull.h>
45
46 #include "rmr.h"
47 #include "rmr_agnostic.h"
48
49 #define EMULATE_NNG
50 #define NNG_UNDER_TEST
51 #include "sr_nng_static.c"
52
53 #include "test_support.c"
54
55 /*
56         Dummy for testing here
57 */
58 extern void rmr_free_msg( rmr_mbuf_t* mbuf ) {
59 }
60
61 static int hdr_test( ) {
62         int errors = 0;
63         uta_ctx_t*      ctx;
64         rmr_mbuf_t*     msg;
65         uta_mhdr_t*     hdr;
66         int hlen;
67         int len;
68         int     payload_len = 2049;
69         int trace_len = 37;
70
71         ctx = (uta_ctx_t *) malloc( sizeof( *ctx ) );
72         ctx->trace_data_len = 0;
73         ctx->my_name = strdup( "my-dummy-host-name-and-port:xxxx" );
74
75         msg = alloc_zcmsg( ctx, NULL, payload_len, 0 );                         // header len here should just be len of our struct
76         hdr = (uta_mhdr_t *) msg->header;
77         hlen = RMR_HDR_LEN( hdr );
78
79         fprintf( stderr, "<INFO> struct len= %d  msg len= %d %d\n", (int) sizeof( uta_mhdr_t ), hlen, htonl( hlen ) );
80         errors += fail_not_equal( hlen, (int) sizeof( uta_mhdr_t ), "header len (a) not size of struct when no trace data is present" );
81
82         len = (int) sizeof( uta_mhdr_t ) + payload_len;                                 // expected size of transport buffer allocated
83         errors += fail_not_equal( len, msg->alloc_len, "alloc len (a)  not expected size" );
84
85
86         ctx->trace_data_len = trace_len;                // alloc messages with tracing buffer in place
87         msg = alloc_zcmsg( ctx, NULL, payload_len, 0 );                         // header len here should just be len of our struct
88         hdr = (uta_mhdr_t *) msg->header;
89         hlen = RMR_HDR_LEN( hdr );
90         fprintf( stderr, "<INFO> with trace data: struct+trace len= %d  msg len= %d %d\n", (int) sizeof( uta_mhdr_t )+trace_len, hlen, htonl( hlen ) );
91         errors += fail_not_equal( hlen, (int) sizeof( uta_mhdr_t ) + trace_len, "header len (a) was not header + trace data size (b)" );
92
93         len = RMR_TR_LEN( hdr );
94         errors += fail_not_equal( len, trace_len, "trace len in header (a) not expected value (b)" );
95
96         len = RMR_D1_LEN( hdr );
97         errors += fail_not_equal( len, 0, "d1 len in header (a) not expected value (b)" );
98
99         len = RMR_D2_LEN( hdr );
100         errors += fail_not_equal( len, 0, "d2 len in header (a) not expected value (b)" );
101
102
103         // -------------------------------------------------------------------------------------------
104
105         if( ! errors ) {
106                 fprintf( stderr, "<INFO> all msg header tests pass\n" );
107         }
108         return errors;
109 }
110
111 int main() {
112         int errors = 0;
113
114         errors +=  hdr_test();
115
116         return !! errors;
117 }