CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / test / test_transport_em.c
1 /*
2 ==================================================================================
3         Copyright (c) 2020 Nokia
4         Copyright (c) 2020 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10            http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 /*
21         Mnemonic:       test_transport.c
22         Abstract:       This supplies a bunch of dummy system functions which emulate
23                                 things like connect() bind() etc.
24
25                                 This module must be directly included to be used.
26         Date:           17 April 2020
27         Author:         E. Scott Daniels
28 */
29
30 #ifndef _test_transport_c
31 #define _sitransport_h                  // prevent the transport defs when including SI95
32
33
34 char    tpem_last_addr[1024];           // last address to simulate connection to ourself
35 int             tpem_last_len = 0;
36
37 int tpem_addr_dup = 0;                  // getsockname duplicates last addr if true
38 int tpem_conn_state = 0;                // states returned by emulated functions allowing failures to be driven
39 int tpem_sock_state = 0;                // if sock state 0, then socket call returns good fd
40 int tpem_listen_state = 0;
41 int tpem_bind_state = 0;
42 int tpem_accept_fd = 5;                 // file desc returned by accept
43 int tpem_sel_ef = -1;                   // select sets this fd's error if >= 0
44 int tpem_sel_block = 0;                 // set if select call inidcates would block
45 int     tpem_send_err = 0;                      // set to cause send to return error
46
47 // ------------ emulation control -------------------------------------------
48
49 /*
50         Allow test prog to set various things
51 */
52 static void tpem_set_conn_state( int s ) {
53         tpem_conn_state = s;
54 }
55
56 static void tpem_set_addr_dup_state( int s ) {
57         tpem_addr_dup = s;
58 }
59
60 static void tpem_set_sock_state( int s ) {
61         tpem_sock_state = s;
62 }
63
64 static void tpem_set_bind_state( int s ) {
65         tpem_bind_state = s;
66 }
67
68 static void tpem_set_accept_fd( int s ) {
69         tpem_accept_fd = s;
70 }
71
72 static void tpem_set_selef_fd( int s ) {
73         tpem_sel_ef = s;
74 }
75
76 static void tpem_set_sel_blk( int s ) {
77         tpem_sel_block = s;
78 }
79
80 static void tpem_set_send_err( int s ) {
81         tpem_send_err = s;
82 }
83
84 // ---- emulated functions ---------------------------------------------------
85
86 static int tpem_bind( int socket, struct sockaddr* addr, socklen_t alen ) {
87         return tpem_bind_state;
88 }
89
90 static int tpem_connect( int socket, struct sockaddr* addr, socklen_t alen ) {
91         memcpy( tpem_last_addr, addr, alen );
92         tpem_last_len = alen;
93         fprintf( stderr, "<SYSEM> connection simulated rc=%d\n", tpem_conn_state );
94         return tpem_conn_state;
95 }
96
97 /*
98         This gets the last address connected to if dup is true; else returns 0s
99         which should be enough to test that connection didn't loop back to us.
100 */
101 static int tpem_getsockname( int socket, struct sockaddr* address, socklen_t* alen ) {
102         int clen;               // copy len
103
104         if( tpem_last_len > 0 ) {
105                 clen = tpem_last_len > *alen ? *alen : tpem_last_len;
106                 if( tpem_addr_dup ) {
107                         memcpy( address, tpem_last_addr, clen );
108                 } else {
109                         memset( address, 0, clen );
110                 }
111                 *alen = clen;
112         } else {
113                 memset( address, 0, *alen );
114         }
115
116         return 0;
117 }
118
119 static int tpem_listen( int socket, int backlog ) {
120         return tpem_listen_state;
121 }
122
123 static int tpem_socket( int domain, int type, int protocol ) {
124         static int fd = 1;
125
126         if( tpem_sock_state == 0 ) {
127                 if( ++fd > 10 ) {
128                         fd = 3;                         // ensure we don't stomp on std* descriptors
129                 }
130
131                 return fd;
132         }
133
134         return -1;
135 }
136
137 static int tpem_accept( int socket, struct sockaddr *restrict address, socklen_t *restrict address_len) {
138         return tpem_accept_fd;
139 }
140
141 /*
142         Emulate a select. If tpem_sel_ef is set, then the error fd set for the fd is set to true.
143         If sel_woudl_block is set, then the select returns blocking
144 */
145 static int tpem_select( int fd_count, fd_set* rf, fd_set* wf, fd_set* ef, void* time ) {
146         fprintf( stderr, "<SYSTEM> select returns %d (1==no-block)\n", tpem_sel_block ? -1 : 1  );
147
148         if( tpem_sel_block ) {
149                 return -1;
150         }
151
152         if( tpem_sel_ef >= 0 ) {
153                 FD_SET( tpem_sel_ef, ef );
154         } else {
155                 FD_ZERO( ef );
156         }
157
158         return 1;
159 }
160
161 /*
162         If tpem_send_err is set, we return less than count;
163 */
164 static int tpem_send( int fd, void* buf, int count, int flags ) {
165         errno = tpem_send_err;
166
167         fprintf( stderr, "<SYSTEM> send on fd=%d for %d bytes ret=%d\n", fd, count, tpem_send_err ? -1 : count );
168         return tpem_send_err ? -1 : count;
169 }
170
171
172 // ---------------------------------------------------------------------------------------
173
174 /*
175         redefine all system calls to reference functions here. There are two defs
176         SI functions should use the capitalised verision so that sliding ff under
177         it is possible. There might be instances wehre the actual system call is
178         needed, so we also define the lowercase value.
179 */
180 #define BIND tpem_bind
181 #define bind tpem_bind
182 #define CONNECT tpem_connect
183 #define connect tpem_connect
184 #define getsockname tpem_getsockname
185 #define SOCKET tpem_socket
186 #define socket tpem_socket
187 #define LISTEN tpem_listen
188 #define listen tpem_listen
189 #define accept tpem_accept
190 #define ACCEPT tpem_accept
191 #define SEND    tpem_send
192 #define SELECT  tpem_select
193 #define select  tpem_select
194
195 /*
196         these are defined in SI so that we can use the system stack or FFstack
197         they must exist and reference system calls if not defined above.
198 */
199 #define CLOSE           close
200 #define SHUTDOWN        shutdown
201 #define GETSOCKOPT      getsockopt
202 #define SETSOCKOPT      setsockopt
203 #define READ            read
204 #define WRITE           write
205 #define SENDTO          sendto
206 #define RECV            recv
207 #define RECVFROM        recvfrom
208 #define RECVMSG         recvmsg
209
210
211
212 #endif
213