2 ==================================================================================
3 Copyright (c) 2020 Nokia
4 Copyright (c) 2020 AT&T Intellectual Property.
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
21 Mnemonic: test_common_em.c
22 Abstract: This supplies some dummy emulation functions that are common
23 to any of the emulation code. This file should be inlucded
24 by the emulation code, and not the test programme.
26 This also includes dummy struct defs so that we can simulate
27 returning messages and message buffers and some control
28 functions which can be used to taylor the emulation behavour
31 Date: 21 February 2020
32 Author: E. Scott Daniels
39 static int em_send_failures = 0; // test programme can set this to emulate eagain send failures
40 static int em_timeout = -1; // set by set socket option
41 static int em_mtc_msgs = 0; // set to generate 'received' messages with mt-call header data
42 static int return_value = 0; // functions should return this value
43 static int rcv_count = 0; // receive counter for transaction id to allow test to rest
44 static int rcv_delay = 0; // forced delay before call to rcvmsg starts to work
46 static int gates_ok = 0;
47 static pthread_mutex_t rcv_gate;
48 static int em_gen_long_hostname = 0; // if set the emulated hostname generates a longer name (>40 char)
51 Simulated v1 message for receive to return. This needs to match the RMr header
52 so that we can fill in length, type and xaction id things.
55 int32_t mtype; // message type ("long" network integer)
56 int32_t plen; // payload length
57 int32_t rmr_ver; // our internal message version number
58 unsigned char xid[32]; // space for user transaction id or somesuch
59 unsigned char sid[32]; // sender ID for return to sender needs
60 unsigned char src[16]; // name of the sender (source)
61 unsigned char meid[32]; // managed element id.
62 struct timespec ts; // timestamp ???
67 v2 message; should be able to use it for everything that is set up here as
68 we don't add a payload even if setting a v1 type.
70 #define ALT_MSG_VER 1 // alternate every so often
71 #define MSG_VER 3 // default version to insert
73 int32_t mtype; // message type ("long" network integer)
74 int32_t plen; // payload length
75 int32_t rmr_ver; // our internal message version number
76 unsigned char xid[32]; // space for user transaction id or somesuch
77 unsigned char sid[32]; // sender ID for return to sender needs
78 unsigned char src[64]; // name of the sender (source)
79 unsigned char meid[32]; // managed element id.
80 struct timespec ts; // timestamp ???
83 int32_t flags; // HFL_* constants
84 int32_t len0; // length of the RMr header data
85 int32_t len1; // length of the tracing data
86 int32_t len2; // length of data 1 (d1)
87 int32_t len3; // length of data 2 (d2)
88 int32_t sub_id; // subscription id (-1 invalid)
91 unsigned char srcip[64]; // sender ID for return to sender needs
94 // -- emulation control functions ------------------------------------------------------
97 Test app can call this to have all emulated functions return failure instead
100 static void en_set_return( int rv ) {
104 static int em_nng_foo() {
105 fprintf( stderr, "emulated functions in play" );
109 Turns on/off the generation of multi-threaded call messages
111 static int em_set_mtc_msgs( int state ) {
116 Returns the size of the header we inserted
118 static int em_hdr_size() {
120 return (int) sizeof( struct em_msg ) + 4;
123 return (int) sizeof( struct em_msg );
126 static void em_set_rcvcount( int v ) {
130 static void em_set_rcvdelay( int v ) {
132 fprintf( stderr, "<EM> ##ERR## attempt to set receive delay with invalid value was ignored: %d seconds\n", v );
135 fprintf( stderr, "<EM> receive delay is now %d seconds\n", v );
139 static void em_start() {
141 pthread_mutex_init( &rcv_gate, NULL );
147 // ----------- gethostname emulation ---------------------------------------
148 #define gethostname em_gethostname
149 static int em_gethostname( char* buf, size_t len ) {
155 if( em_gen_long_hostname ) {
156 snprintf( buf, len, "hostname-which-is-long-a860430b890219-dfw82" );
158 snprintf( buf, len, "em-hostname" );
164 static int em_set_long_hostname( int v ) {
165 em_gen_long_hostname = !!v;
169 // ----------- epoll emulation ---------------------------------------------
171 // CAUTION: sys/epoll.h must be included before these define for functions
172 // to properly compile.
174 #include <sys/epoll.h>
175 #define epoll_wait em_wait
176 #define epoll_ctl em_ep_ctl
177 #define epoll_create em_ep_create
180 Every other call returns 1 ready; alternate calls return 0 ready.
181 Mostly for testing the timeout receive call. First call should return
182 something ready and the second should return nothing ready so we can
185 static int em_wait( int fd, void* events, int n, int to ) {
186 static int ready = 0;
192 static int em_ep_ctl( int epfd, int op, int fd, struct epoll_event *event ) {
196 static int em_ep_create( int size ) {