blob: d2977f68256caca9dd136d118d37332da9e6deca [file] [log] [blame]
E. Scott Danielsfc5c77b2020-02-21 13:24:29 -05001/*
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_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.
25
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
29 during the test.
30
31 Date: 21 February 2020
32 Author: E. Scott Daniels
33*/
34
35#ifndef _em_common_c
36#define _em_common_c
37
38
39static int em_send_failures = 0; // test programme can set this to emulate eagain send failures
40static int em_timeout = -1; // set by set socket option
41static int em_mtc_msgs = 0; // set to generate 'received' messages with mt-call header data
42static int return_value = 0; // functions should return this value
43static int rcv_count = 0; // receive counter for transaction id to allow test to rest
44static int rcv_delay = 0; // forced delay before call to rcvmsg starts to work
45
46static int gates_ok = 0;
47static pthread_mutex_t rcv_gate;
48static int em_gen_long_hostname = 0; // if set the emulated hostname generates a longer name (>40 char)
49
50/*
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.
53#define MSG_VER 1
54struct em_msg {
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 ???
63};
64*/
65
66/*
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.
69*/
70#define ALT_MSG_VER 1 // alternate every so often
71#define MSG_VER 3 // default version to insert
72struct em_msg {
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 ???
81
82 // V2 extension
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)
89
90 // V3 stuff
91 unsigned char srcip[64]; // sender ID for return to sender needs
92};
93
94// -- emulation control functions ------------------------------------------------------
95
96/*
97 Test app can call this to have all emulated functions return failure instead
98 of success.
99*/
100static void en_set_return( int rv ) {
101 return_value = rv;
102}
103
104static int em_nng_foo() {
105 fprintf( stderr, "emulated functions in play" );
106}
107
108/*
109 Turns on/off the generation of multi-threaded call messages
110*/
111static int em_set_mtc_msgs( int state ) {
112 em_mtc_msgs = state;
113}
114
115/*
116 Returns the size of the header we inserted
117*/
118static int em_hdr_size() {
119 if( em_mtc_msgs ) {
120 return (int) sizeof( struct em_msg ) + 4;
121 }
122
123 return (int) sizeof( struct em_msg );
124}
125
126static void em_set_rcvcount( int v ) {
127 rcv_count = v;
128}
129
130static void em_set_rcvdelay( int v ) {
131 if( v < 0 ) {
132 fprintf( stderr, "<EM> ##ERR## attempt to set receive delay with invalid value was ignored: %d seconds\n", v );
133 return;
134 }
135 fprintf( stderr, "<EM> receive delay is now %d seconds\n", v );
136 rcv_delay = v;
137}
138
139static void em_start() {
140 if( ! gates_ok ) {
141 pthread_mutex_init( &rcv_gate, NULL );
142 gates_ok = 1;
143 }
144}
145
146
147// ----------- gethostname emulation ---------------------------------------
148#define gethostname em_gethostname
149static int em_gethostname( char* buf, size_t len ) {
150 if( len < 1 ) {
151 errno = EINVAL;
152 return 1;
153 }
154
155 if( em_gen_long_hostname ) {
156 snprintf( buf, len, "hostname-which-is-long-a860430b890219-dfw82" );
157 } else {
158 snprintf( buf, len, "em-hostname" );
159 }
160
161 return 0;
162}
163
164static int em_set_long_hostname( int v ) {
165 em_gen_long_hostname = !!v;
166}
167
168
169// ----------- epoll emulation ---------------------------------------------
170
171// CAUTION: sys/epoll.h must be included before these define for functions
172// to properly compile.
173//
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
178
179/*
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
183 drive both cases.
184*/
185static int em_wait( int fd, void* events, int n, int to ) {
186 static int ready = 0;
187
188 ready = !ready;
189 return ready;
190}
191
192static int em_ep_ctl( int epfd, int op, int fd, struct epoll_event *event ) {
193 return 0;
194}
195
196static int em_ep_create( int size ) {
197 return 0;
198}
199
200
201
202#endif