1 // vim: ts=4 sw=4 noet:
3 --------------------------------------------------------------------------------
4 Copyright (c) 2018-2019 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 --------------------------------------------------------------------------------
22 Abstract: Basic unit tests for the mc listener.
24 Author: E. Scott Daniels
27 #define FOREVER 0 // allows forever loops in mcl code to escape after one loop
38 #include <sys/types.h>
40 #define TEST_MTYPE 1000 // message type for testing
41 #include "test_rmr_em.c" // emulated rmr functions (for receives)
43 // this/these are what we are testing; include them directly (must be after forever def)
48 Set up env things for the rdc setup call.
50 static void set_env() {
51 setenv( "MCL_RDC_ENABLE", "1", 1 ); // cause 'raw' capture to be setup
52 setenv( "MCL_RDC_STAGE", "/tmp/mc_listener_test/stage", 1 ); // unit test script should create and will purge after
53 setenv( "MCL_RDC_FINAL", "/tmp/mc_listener_test/final", 1 );
54 setenv( "MCL_RDC_SUFFIX", ".xxx", 1 );
55 setenv( "MCL_RDC_DONE", ".done", 1 );
56 setenv( "MCL_RDC_FREQ", "10", 1 );
60 Parms: [fifo-dir-name]
62 int main( int argc, char** argv ) {
64 void* bad_ctx; // context with a bad directory path for coverage/error testing
66 char* dname = "/tmp/fifos";
70 int rfd; // a reader file des so we can read what we write
71 fifo_t* fref = NULL; // fifo type reference; we just verify that suss sets it
77 char timestamp[1024]; // read will put a timestamp here
83 set_env(); // set env that setup_rdc() looks for
85 ctx = mcl_mk_context( dname ); // allocate the context
87 fprintf( stderr, "[FAIL] couldn't make context\n" );
91 mcl_set_sigh(); // prevent colobber from broken pipe
93 open_fifo( ctx, TEST_MTYPE, WRITER ); // open dummy to prevent blocking reader
94 rfd = open_fifo( ctx, TEST_MTYPE, READER ); // open a reader to check fanout output
96 fprintf( stderr, "[FAIL] unable to open a pipe reader for type == 100\n" );
100 fd = suss_fifo( ctx, TEST_MTYPE, WRITER, &fref ); // should open the file for writing and return the fdes
102 fprintf( stderr, "[FAIL] suss_fifo did not return a valid fd\n" );
107 fprintf( stderr, "[FAIL] suss_fifo did not set the fifo reference pointer\n" );
114 fd2= suss_fifo( ctx, TEST_MTYPE, 0, NULL ); // should open the file file for reading and return a different fd
116 fprintf( stderr, "[FAIL] suss_fifo did not return a valid fd\n" );
120 fprintf( stderr, "[FAIL] reading and writing fifo file descriptors expected to differ; both were %d\n", fd );
124 mcl_start_listening( ctx, port, 0 ); // start the listener
126 // under test, the FOREVER = 0 keeps fanout from blocking; drive several times to cover all cases
127 mcl_fifo_fanout( ctx, 5, 1 ); // first rmr receive call will simulate a timeout
128 mcl_fifo_fanout( ctx, 5, 1 ); // second receive simulates a health check
129 mcl_fifo_fanout( ctx, 5, 1 ); // 3-n return alternating timeout messages; drive so that
130 mcl_fifo_fanout( ctx, 5, 1 ); // we will have several land in the FIFO
131 mcl_fifo_fanout( ctx, 5, 1 );
132 mcl_fifo_fanout( ctx, 5, 1 );
133 mcl_fifo_fanout( ctx, 5, 1 );
136 state = mcl_fifo_read1( ctx, TEST_MTYPE, payload, sizeof( payload ), TRUE );
138 fprintf( stderr, "[FAIL] fifo_read return positive value when expected to\n" );
141 state = mcl_fifo_tsread1( ctx, TEST_MTYPE, payload, sizeof( payload ), TRUE, timestamp );
143 fprintf( stderr, "[FAIL] fifo_read with timestamp return positive value when expected to\n" );
147 state = fifo_read1( NULL, TEST_MTYPE, payload, sizeof( payload ), 1, timestamp ); // coverage error check
149 fprintf( stderr, "[FAIL] fifo_read didn't return 0 when given a nil context to\n" );
153 mcl_fifo_fanout( ctx, 5, 0 ); // test with writing short header
154 mcl_fifo_fanout( ctx, 5, 0 );
156 // ------ some error/coverage testing ---------------------------
157 logit( LOG_CRIT, "critical message" );
158 logit( LOG_ERR, "error message" );
159 logit( LOG_WARN, "warning message" );
160 logit( LOG_STAT, "stats message" );
162 bad_ctx = mcl_mk_context( "/nosuchdirectoryinthesystem" ); // create a context where fifo opens should fail
163 if( bad_ctx == NULL ) {
164 fprintf( stderr, "[FAIL] couldn't make 'bad' context" );
169 fd = suss_fifo( bad_ctx, TEST_MTYPE, 1, &fref ); // should fail to open the file for writing beacuse directory is bad
171 fprintf( stderr, "[FAIL] suss_fifo returned a valid fd when given a context with a bad directory path\n" );
175 fprintf( stderr, "[FAIL] suss_fifo returned an fref pointer when given a bad context\n" );
179 fd = suss_fifo( NULL, TEST_MTYPE, 1, &fref ); // coverage nil pointer check
181 fprintf( stderr, "[FAIL] suss_fifo returned a valid fd when given a nil context a bad directory path\n" );
185 fd = suss_fifo( ctx, -1, 1, &fref ); // mad message type check
187 fprintf( stderr, "[FAIL] suss_fifo returned a valid fd when given a bad message type\n" );
191 // -- buffer testing ------------------------------------------------------
192 bp = build_hdr( 1024, wbuf, 0 );
193 bp = build_hdr( 1024, NULL, 0 );
195 fprintf( stderr, "[FAIL] build_hdr didn't return a buffer pointer when given a nil buffer\n" );
200 bp = build_hdr( 1024, wbuf, sizeof( wbuf ) );
202 fprintf( stderr, "[FAIL] build_hdr didn't return a buffer pointer\n" );
207 // ----- msg receive testing ----------------------------------------------------
208 buf = mcl_get_msg( NULL, NULL, 1 ); // drive nil pointer checks
211 fprintf( stderr, "[FAIL], get_msg call with nil context returned a buffer pointer\n" );
214 buf = mcl_get_msg( ctx, NULL, 1 ); // drive to force coverage; nothing is sent, so we can't validate buffer
217 mcl_fifo_one( NULL, NULL, 1, 1 );
218 mcl_fifo_one( ctx, NULL, 1, 1 );
219 mcl_fifo_one( ctx, wbuf, 0, 1 );
220 mcl_fifo_one( ctx, wbuf, 10, 100 );
223 // --- some rdc testing as best as we can without message generators --------
224 rdc_init( NULL, NULL, ".foo", ".bar" ); // coverage testing
226 ctx = setup_rdc(); // coverage test to ensure that it generates a context
228 fprintf( stderr, "[FAIL] setup_rdc did not return a context pointer\n" );
232 rdc_set_freq( NULL, 0 ); // error/nil test
233 rdc_set_freq( ctx, 0 ); // error/nil test
234 rdc_set_freq( ctx, 10 ); // roll after 10seconds to test that
236 build_hdr( 1024, wbuf, sizeof( wbuf ) );
238 bp = rdc_init_buf( TEST_MTYPE, wbuf, 10, bp ); // set up for write
239 rdc_write( ctx, bp, payload, sizeof( payload ) ); // write the raw data
241 fprintf( stderr, "[INFO] pausing to test rdc file rolling\n" );
243 build_hdr( 1024, wbuf, sizeof( wbuf ) );
245 bp = rdc_init_buf( TEST_MTYPE, wbuf, 10, bp );
246 rdc_write( ctx, bp, payload, sizeof( payload ) );
249 // CAUTION: filenames need to match those expected in the run script as it creates src, and will validate, destination files
250 state = copy_unlink( "/tmp/mc_listener_test/no-such-copy_src", "/tmp/mc_listener_test/copy_dest", 0664 ); // first couple drive for error and coverage
252 fprintf( stderr, "[FAIL] copy-unlink of bad file didn't return bad state\n" );
255 state = copy_unlink( "/tmp/mc_listener_test/copy_src", "/tmp/mc_listener_test-nodir/copy_dest", 0664 );
257 fprintf( stderr, "[FAIL] copy-unlink of bad target didn't return bad state\n" );
260 state = copy_unlink( "/tmp/mc_listener_test/copy_src", "/tmp/mc_listener_test/copy_dest", 0664 ); // drive for coverage; setup script can check contents
262 fprintf( stderr, "[FAIL] copy-unlink expected success but failed\n" );
265 state = mvocp( "/tmp/mc_listener_test/bad-src-mv_src", "/tmp/mc_listener_test/mv_dest" );
267 fprintf( stderr, "[FAIL] mv or copy expected failure didn't set bad state\n" );
270 state = mvocp( "/tmp/mc_listener_test/mv_src", "/tmp/mc_listener_test/mv_dest" );
272 fprintf( stderr, "[FAIL] mv or copy expected to succeed didn't set good state\n" );
277 // ---- finally, check error count, write nice cheerful message and exit ----
279 fprintf( stderr, "[PASS] unit_test: everything looks peachy\n" );
281 fprintf( stderr, "[FAIL] unit_test: there were %d errors\n", errors );