1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#include "config.h"
3#include <errno.h>
4#include <stdarg.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9#include <signal.h>
10#include <getopt.h>
11#include <time.h>
12#include <platform/platform.h>
13#include "utilities/engine_loader.h"
14#include <memcached/engine_testapp.h>
15#include <memcached/extension_loggers.h>
16#include "mock_server.h"
17
18struct mock_engine {
19    ENGINE_HANDLE_V1 me;
20    ENGINE_HANDLE_V1 *the_engine;
21    TAP_ITERATOR iterator;
22};
23
24static bool color_enabled;
25
26#ifndef WIN32
27static sig_atomic_t alarmed;
28
29static void alarm_handler(int sig) {
30    alarmed = 1;
31}
32#endif
33
34static struct mock_engine* get_handle(ENGINE_HANDLE* handle) {
35    return (struct mock_engine*)handle;
36}
37
38static tap_event_t mock_tap_iterator(ENGINE_HANDLE* handle,
39                                     const void *cookie, item **itm,
40                                     void **es, uint16_t *nes, uint8_t *ttl,
41                                     uint16_t *flags, uint32_t *seqno,
42                                     uint16_t *vbucket) {
43   struct mock_engine *me = get_handle(handle);
44   return me->iterator((ENGINE_HANDLE*)me->the_engine, cookie, itm, es, nes,
45                       ttl, flags, seqno, vbucket);
46}
47
48static const engine_info* mock_get_info(ENGINE_HANDLE* handle) {
49    struct mock_engine *me = get_handle(handle);
50    return me->the_engine->get_info((ENGINE_HANDLE*)me->the_engine);
51}
52
53static ENGINE_ERROR_CODE mock_initialize(ENGINE_HANDLE* handle,
54                                         const char* config_str) {
55    struct mock_engine *me = get_handle(handle);
56    return me->the_engine->initialize((ENGINE_HANDLE*)me->the_engine, config_str);
57}
58
59static void mock_destroy(ENGINE_HANDLE* handle, const bool force) {
60    struct mock_engine *me = get_handle(handle);
61    me->the_engine->destroy((ENGINE_HANDLE*)me->the_engine, force);
62}
63
64static ENGINE_ERROR_CODE mock_allocate(ENGINE_HANDLE* handle,
65                                       const void* cookie,
66                                       item **item,
67                                       const void* key,
68                                       const size_t nkey,
69                                       const size_t nbytes,
70                                       const int flags,
71                                       const rel_time_t exptime,
72                                       uint8_t datatype) {
73    struct mock_engine *me = get_handle(handle);
74    struct mock_connstruct *c = (void*)cookie;
75    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
76
77    if (c == NULL) {
78        c = (void*)create_mock_cookie();
79    }
80
81    c->nblocks = 0;
82    cb_mutex_enter(&c->mutex);
83    while (ret == ENGINE_SUCCESS &&
84           (ret = me->the_engine->allocate((ENGINE_HANDLE*)me->the_engine, c,
85                                           item, key, nkey,
86                                           nbytes, flags,
87                                           exptime, datatype)) == ENGINE_EWOULDBLOCK &&
88           c->handle_ewouldblock)
89    {
90        ++c->nblocks;
91        cb_cond_wait(&c->cond, &c->mutex);
92        ret = c->status;
93    }
94    cb_mutex_exit(&c->mutex);
95
96    if (c != cookie) {
97        destroy_mock_cookie(c);
98    }
99
100    return ret;
101}
102
103static ENGINE_ERROR_CODE mock_remove(ENGINE_HANDLE* handle,
104                                     const void* cookie,
105                                     const void* key,
106                                     const size_t nkey,
107                                     uint64_t* cas,
108                                     uint16_t vbucket)
109{
110    struct mock_engine *me = get_handle(handle);
111    struct mock_connstruct *c = (void*)cookie;
112    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
113
114    if (c == NULL) {
115        c = (void*)create_mock_cookie();
116    }
117
118    c->nblocks = 0;
119    cb_mutex_enter(&c->mutex);
120    while (ret == ENGINE_SUCCESS &&
121           (ret = me->the_engine->remove((ENGINE_HANDLE*)me->the_engine, c, key,
122                                         nkey, cas, vbucket)) == ENGINE_EWOULDBLOCK &&
123           c->handle_ewouldblock)
124    {
125        ++c->nblocks;
126        cb_cond_wait(&c->cond, &c->mutex);
127        ret = c->status;
128    }
129    cb_mutex_exit(&c->mutex);
130
131    if (c != cookie) {
132        destroy_mock_cookie(c);
133    }
134
135    return ret;
136}
137
138static void mock_release(ENGINE_HANDLE* handle,
139                         const void *cookie,
140                         item* item) {
141    struct mock_engine *me = get_handle(handle);
142    me->the_engine->release((ENGINE_HANDLE*)me->the_engine, cookie, item);
143}
144
145static ENGINE_ERROR_CODE mock_get(ENGINE_HANDLE* handle,
146                                  const void* cookie,
147                                  item** item,
148                                  const void* key,
149                                  const int nkey,
150                                  uint16_t vbucket) {
151    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
152    struct mock_engine *me = get_handle(handle);
153    struct mock_connstruct *c = (void*)cookie;
154    if (c == NULL) {
155        c = (void*)create_mock_cookie();
156    }
157
158    c->nblocks = 0;
159    cb_mutex_enter(&c->mutex);
160    while (ret == ENGINE_SUCCESS &&
161           (ret = me->the_engine->get((ENGINE_HANDLE*)me->the_engine, c, item,
162                                      key, nkey, vbucket)) == ENGINE_EWOULDBLOCK &&
163           c->handle_ewouldblock)
164    {
165        ++c->nblocks;
166        cb_cond_wait(&c->cond, &c->mutex);
167        ret = c->status;
168    }
169    cb_mutex_exit(&c->mutex);
170
171    if (c != cookie) {
172        destroy_mock_cookie(c);
173    }
174
175    return ret;
176}
177
178static ENGINE_ERROR_CODE mock_get_stats(ENGINE_HANDLE* handle,
179                                        const void* cookie,
180                                        const char* stat_key,
181                                        int nkey,
182                                        ADD_STAT add_stat)
183{
184    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
185    struct mock_engine *me = get_handle(handle);
186    struct mock_connstruct *c = (void*)cookie;
187    if (c == NULL) {
188        c = (void*)create_mock_cookie();
189    }
190
191    c->nblocks = 0;
192    cb_mutex_enter(&c->mutex);
193    while (ret == ENGINE_SUCCESS &&
194           (ret = me->the_engine->get_stats((ENGINE_HANDLE*)me->the_engine, c, stat_key,
195                                            nkey, add_stat)) == ENGINE_EWOULDBLOCK &&
196           c->handle_ewouldblock)
197    {
198        ++c->nblocks;
199        cb_cond_wait(&c->cond, &c->mutex);
200        ret = c->status;
201    }
202    cb_mutex_exit(&c->mutex);
203
204    if (c != cookie) {
205        destroy_mock_cookie(c);
206    }
207
208    return ret;
209}
210
211static ENGINE_ERROR_CODE mock_store(ENGINE_HANDLE* handle,
212                                    const void *cookie,
213                                    item* item,
214                                    uint64_t *cas,
215                                    ENGINE_STORE_OPERATION operation,
216                                    uint16_t vbucket) {
217    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
218    struct mock_engine *me = get_handle(handle);
219    struct mock_connstruct *c = (void*)cookie;
220    if (c == NULL) {
221        c = (void*)create_mock_cookie();
222    }
223
224    c->nblocks = 0;
225    cb_mutex_enter(&c->mutex);
226    while (ret == ENGINE_SUCCESS &&
227           (ret = me->the_engine->store((ENGINE_HANDLE*)me->the_engine, c, item, cas,
228                                        operation, vbucket)) == ENGINE_EWOULDBLOCK &&
229           c->handle_ewouldblock)
230    {
231        ++c->nblocks;
232        cb_cond_wait(&c->cond, &c->mutex);
233        ret = c->status;
234    }
235    cb_mutex_exit(&c->mutex);
236
237    if (c != cookie) {
238        destroy_mock_cookie(c);
239    }
240
241    return ret;
242}
243
244static ENGINE_ERROR_CODE mock_arithmetic(ENGINE_HANDLE* handle,
245                                         const void* cookie,
246                                         const void* key,
247                                         const int nkey,
248                                         const bool increment,
249                                         const bool create,
250                                         const uint64_t delta,
251                                         const uint64_t initial,
252                                         const rel_time_t exptime,
253                                         uint64_t *cas,
254                                         uint8_t datatype,
255                                         uint64_t *result,
256                                         uint16_t vbucket) {
257    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
258    struct mock_engine *me = get_handle(handle);
259    struct mock_connstruct *c = (void*)cookie;
260    if (c == NULL) {
261        c = (void*)create_mock_cookie();
262    }
263
264    c->nblocks = 0;
265    cb_mutex_enter(&c->mutex);
266    while (ret == ENGINE_SUCCESS &&
267           (ret = me->the_engine->arithmetic((ENGINE_HANDLE*)me->the_engine, c, key,
268                                             nkey, increment, create,
269                                             delta, initial, exptime,
270                                             cas, datatype,
271                                             result, vbucket)) == ENGINE_EWOULDBLOCK &&
272           c->handle_ewouldblock)
273    {
274        ++c->nblocks;
275        cb_cond_wait(&c->cond, &c->mutex);
276        ret = c->status;
277    }
278    cb_mutex_exit(&c->mutex);
279
280    if (c != cookie) {
281        destroy_mock_cookie(c);
282    }
283
284    return ret;
285}
286
287static ENGINE_ERROR_CODE mock_flush(ENGINE_HANDLE* handle,
288                                    const void* cookie, time_t when) {
289    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
290    struct mock_engine *me = get_handle(handle);
291    struct mock_connstruct *c = (void*)cookie;
292    if (c == NULL) {
293        c = (void*)create_mock_cookie();
294    }
295
296    c->nblocks = 0;
297    cb_mutex_enter(&c->mutex);
298    while (ret == ENGINE_SUCCESS &&
299           (ret = me->the_engine->flush((ENGINE_HANDLE*)me->the_engine, c, when)) == ENGINE_EWOULDBLOCK &&
300           c->handle_ewouldblock)
301    {
302        ++c->nblocks;
303        cb_cond_wait(&c->cond, &c->mutex);
304        ret = c->status;
305    }
306    cb_mutex_exit(&c->mutex);
307
308    if (c != cookie) {
309        destroy_mock_cookie(c);
310    }
311
312    return ret;
313}
314
315static void mock_reset_stats(ENGINE_HANDLE* handle, const void *cookie) {
316    struct mock_engine *me = get_handle(handle);
317    me->the_engine->reset_stats((ENGINE_HANDLE*)me->the_engine, cookie);
318}
319
320static ENGINE_ERROR_CODE mock_unknown_command(ENGINE_HANDLE* handle,
321                                              const void* cookie,
322                                              protocol_binary_request_header *request,
323                                              ADD_RESPONSE response)
324{
325    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
326    struct mock_engine *me = get_handle(handle);
327    struct mock_connstruct *c = (void*)cookie;
328    if (c == NULL) {
329        c = (void*)create_mock_cookie();
330    }
331
332    c->nblocks = 0;
333    cb_mutex_enter(&c->mutex);
334    while (ret == ENGINE_SUCCESS &&
335           (ret = me->the_engine->unknown_command((ENGINE_HANDLE*)me->the_engine, c,
336                                                  request, response)) == ENGINE_EWOULDBLOCK &&
337           c->handle_ewouldblock)
338    {
339        ++c->nblocks;
340        cb_cond_wait(&c->cond, &c->mutex);
341        ret = c->status;
342    }
343    cb_mutex_exit(&c->mutex);
344
345    if (c != cookie) {
346        destroy_mock_cookie(c);
347    }
348
349    return ret;
350}
351
352static void mock_item_set_cas(ENGINE_HANDLE *handle, const void *cookie,
353                              item* item, uint64_t val)
354{
355    struct mock_engine *me = get_handle(handle);
356    me->the_engine->item_set_cas((ENGINE_HANDLE*)me->the_engine, cookie, item, val);
357}
358
359
360static bool mock_get_item_info(ENGINE_HANDLE *handle, const void *cookie,
361                               const item* item, item_info *item_info)
362{
363    struct mock_engine *me = get_handle(handle);
364    return me->the_engine->get_item_info((ENGINE_HANDLE*)me->the_engine,
365                                         cookie, item, item_info);
366}
367
368static void *mock_get_stats_struct(ENGINE_HANDLE* handle, const void* cookie)
369{
370    struct mock_engine *me = get_handle(handle);
371    return me->the_engine->get_stats_struct((ENGINE_HANDLE*)me->the_engine, cookie);
372}
373
374static ENGINE_ERROR_CODE mock_aggregate_stats(ENGINE_HANDLE* handle,
375                                              const void* cookie,
376                                              void (*callback)(void*, void*),
377                                              void *vptr)
378{
379    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
380    struct mock_engine *me = get_handle(handle);
381    struct mock_connstruct *c = (void*)cookie;
382    if (c == NULL) {
383        c = (void*)create_mock_cookie();
384    }
385
386    c->nblocks = 0;
387    cb_mutex_enter(&c->mutex);
388    while (ret == ENGINE_SUCCESS &&
389           (ret = me->the_engine->aggregate_stats((ENGINE_HANDLE*)me->the_engine, c,
390                                                  callback, vptr)) == ENGINE_EWOULDBLOCK &&
391           c->handle_ewouldblock)
392    {
393        ++c->nblocks;
394        cb_cond_wait(&c->cond, &c->mutex);
395        ret = c->status;
396    }
397    cb_mutex_exit(&c->mutex);
398
399    if (c != cookie) {
400        destroy_mock_cookie(c);
401    }
402
403    return ret;
404}
405
406static ENGINE_ERROR_CODE mock_tap_notify(ENGINE_HANDLE* handle,
407                                        const void *cookie,
408                                        void *engine_specific,
409                                        uint16_t nengine,
410                                        uint8_t ttl,
411                                        uint16_t tap_flags,
412                                        tap_event_t tap_event,
413                                        uint32_t tap_seqno,
414                                        const void *key,
415                                        size_t nkey,
416                                        uint32_t flags,
417                                        uint32_t exptime,
418                                        uint64_t cas,
419                                        uint8_t datatype,
420                                        const void *data,
421                                        size_t ndata,
422                                        uint16_t vbucket) {
423
424    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
425    struct mock_engine *me = get_handle(handle);
426    struct mock_connstruct *c = (void*)cookie;
427    if (c == NULL) {
428        c = (void*)create_mock_cookie();
429    }
430
431    c->nblocks = 0;
432    cb_mutex_enter(&c->mutex);
433    while (ret == ENGINE_SUCCESS &&
434           (ret = me->the_engine->tap_notify((ENGINE_HANDLE*)me->the_engine, c,
435                                             engine_specific, nengine, ttl, tap_flags,
436                                             tap_event, tap_seqno, key, nkey, flags,
437                                             exptime, cas, datatype,
438                                             data, ndata, vbucket)) == ENGINE_EWOULDBLOCK &&
439           c->handle_ewouldblock)
440    {
441        ++c->nblocks;
442        cb_cond_wait(&c->cond, &c->mutex);
443        ret = c->status;
444    }
445    cb_mutex_exit(&c->mutex);
446
447    if (c != cookie) {
448        destroy_mock_cookie(c);
449    }
450
451    return ret;
452}
453
454
455static TAP_ITERATOR mock_get_tap_iterator(ENGINE_HANDLE* handle, const void* cookie,
456                                           const void* client, size_t nclient,
457                                           uint32_t flags,
458                                           const void* userdata, size_t nuserdata) {
459    struct mock_engine *me = get_handle(handle);
460    me->iterator = me->the_engine->get_tap_iterator((ENGINE_HANDLE*)me->the_engine, cookie,
461                                                    client, nclient, flags, userdata, nuserdata);
462    return (me->iterator != NULL) ? mock_tap_iterator : NULL;
463}
464
465static size_t mock_errinfo(ENGINE_HANDLE *handle, const void* cookie,
466                           char *buffer, size_t buffsz) {
467    struct mock_engine *me = get_handle(handle);
468    return me->the_engine->errinfo((ENGINE_HANDLE*)me->the_engine, cookie,
469                                   buffer, buffsz);
470}
471
472static ENGINE_ERROR_CODE mock_upr_step(ENGINE_HANDLE* handle,
473                                       const void* cookie,
474                                       struct upr_message_producers *producers) {
475    struct mock_engine *me = get_handle(handle);
476    return me->the_engine->upr.step((ENGINE_HANDLE*)me->the_engine, cookie,
477                                    producers);
478}
479
480static ENGINE_ERROR_CODE mock_upr_open(ENGINE_HANDLE* handle,
481                                       const void* cookie,
482                                       uint32_t opaque,
483                                       uint32_t seqno,
484                                       uint32_t flags,
485                                       void *name,
486                                       uint16_t nname) {
487    struct mock_engine *me = get_handle(handle);
488    return me->the_engine->upr.open((ENGINE_HANDLE*)me->the_engine, cookie,
489                                    opaque, seqno, flags, name, nname);
490}
491
492static ENGINE_ERROR_CODE mock_upr_add_stream(ENGINE_HANDLE* handle,
493                                             const void* cookie,
494                                             uint32_t opaque,
495                                             uint16_t vbucket,
496                                             uint32_t flags) {
497
498    struct mock_engine *me = get_handle(handle);
499    struct mock_connstruct *c = (void*)cookie;
500    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
501
502    if (c == NULL) {
503        c = (void*)create_mock_cookie();
504    }
505
506    c->nblocks = 0;
507    cb_mutex_enter(&c->mutex);
508    while (ret == ENGINE_SUCCESS &&
509           (ret = me->the_engine->upr.add_stream((ENGINE_HANDLE*)me->the_engine, c,
510                                                 opaque, vbucket, flags))
511                                                == ENGINE_EWOULDBLOCK &&
512           c->handle_ewouldblock)
513        {
514            ++c->nblocks;
515            cb_cond_wait(&c->cond, &c->mutex);
516            ret = c->status;
517        }
518    cb_mutex_exit(&c->mutex);
519
520    if (c != cookie) {
521        destroy_mock_cookie(c);
522    }
523
524    return ret;
525}
526
527static ENGINE_ERROR_CODE mock_upr_close_stream(ENGINE_HANDLE* handle,
528                                               const void* cookie,
529                                               uint32_t opaque,
530                                               uint16_t vbucket) {
531    struct mock_engine *me = get_handle(handle);
532    return me->the_engine->upr.close_stream((ENGINE_HANDLE*)me->the_engine,
533                                            cookie, opaque, vbucket);
534}
535
536static ENGINE_ERROR_CODE mock_upr_stream_req(ENGINE_HANDLE* handle,
537                                             const void* cookie,
538                                             uint32_t flags,
539                                             uint32_t opaque,
540                                             uint16_t vbucket,
541                                             uint64_t start_seqno,
542                                             uint64_t end_seqno,
543                                             uint64_t vbucket_uuid,
544                                             uint64_t high_seqno,
545                                             uint64_t *rollback_seqno,
546                                             upr_add_failover_log callback) {
547    struct mock_engine *me = get_handle(handle);
548    return me->the_engine->upr.stream_req((ENGINE_HANDLE*)me->the_engine,
549                                          cookie, flags, opaque, vbucket,
550                                          start_seqno, end_seqno, vbucket_uuid,
551                                          high_seqno, rollback_seqno,
552                                          callback);
553}
554
555static ENGINE_ERROR_CODE mock_upr_get_failover_log(ENGINE_HANDLE* handle,
556                                                   const void* cookie,
557                                                   uint32_t opaque,
558                                                   uint16_t vbucket,
559                                                   upr_add_failover_log cb) {
560    struct mock_engine *me = get_handle(handle);
561    return me->the_engine->upr.get_failover_log((ENGINE_HANDLE*)me->the_engine,
562                                                cookie, opaque, vbucket, cb);
563}
564
565static ENGINE_ERROR_CODE mock_upr_stream_end(ENGINE_HANDLE* handle,
566                                             const void* cookie,
567                                             uint32_t opaque,
568                                             uint16_t vbucket,
569                                             uint32_t flags) {
570    struct mock_engine *me = get_handle(handle);
571    return me->the_engine->upr.stream_end((ENGINE_HANDLE*)me->the_engine,
572                                          cookie, opaque, vbucket, flags);
573}
574
575static ENGINE_ERROR_CODE mock_upr_snapshot_marker(ENGINE_HANDLE* handle,
576                                                  const void* cookie,
577                                                  uint32_t opaque,
578                                                  uint16_t vbucket) {
579    struct mock_engine *me = get_handle(handle);
580    return me->the_engine->upr.snapshot_marker((ENGINE_HANDLE*)me->the_engine,
581                                               cookie, opaque, vbucket);
582}
583
584static ENGINE_ERROR_CODE mock_upr_mutation(ENGINE_HANDLE* handle,
585                                           const void* cookie,
586                                           uint32_t opaque,
587                                           const void *key,
588                                           uint16_t nkey,
589                                           const void *value,
590                                           uint32_t nvalue,
591                                           uint64_t cas,
592                                           uint16_t vbucket,
593                                           uint32_t flags,
594                                           uint8_t datatype,
595                                           uint64_t bySeqno,
596                                           uint64_t revSeqno,
597                                           uint32_t expiration,
598                                           uint32_t lockTime,
599                                           const void *meta,
600                                           uint16_t nmeta,
601                                           uint8_t nru) {
602
603    struct mock_engine *me = get_handle(handle);
604    struct mock_connstruct *c = (void*)cookie;
605    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
606    if (c == NULL) {
607        c = (void*)create_mock_cookie();
608    }
609
610    c->nblocks = 0;
611    cb_mutex_enter(&c->mutex);
612    while (ret == ENGINE_SUCCESS &&
613           (ret = me->the_engine->upr.mutation((ENGINE_HANDLE*)me->the_engine, c, opaque, key,
614                                               nkey, value, nvalue, cas, vbucket, flags,
615                                               datatype, bySeqno, revSeqno, expiration,
616                                               lockTime, meta, nmeta, nru)) == ENGINE_EWOULDBLOCK &&
617           c->handle_ewouldblock)
618        {
619            ++c->nblocks;
620            cb_cond_wait(&c->cond, &c->mutex);
621            ret = c->status;
622        }
623    cb_mutex_exit(&c->mutex);
624
625    if (c != cookie) {
626        destroy_mock_cookie(c);
627    }
628
629    return ret;
630}
631
632static ENGINE_ERROR_CODE mock_upr_deletion(ENGINE_HANDLE* handle,
633                                           const void* cookie,
634                                           uint32_t opaque,
635                                           const void *key,
636                                           uint16_t nkey,
637                                           uint64_t cas,
638                                           uint16_t vbucket,
639                                           uint64_t bySeqno,
640                                           uint64_t revSeqno,
641                                           const void *meta,
642                                           uint16_t nmeta) {
643
644    struct mock_engine *me = get_handle(handle);
645    struct mock_connstruct *c = (void*)cookie;
646    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
647    if (c == NULL) {
648        c = (void*)create_mock_cookie();
649    }
650
651    c->nblocks = 0;
652    cb_mutex_enter(&c->mutex);
653    while (ret == ENGINE_SUCCESS &&
654           (ret = me->the_engine->upr.deletion((ENGINE_HANDLE*)me->the_engine, c,
655                                               opaque, key, nkey, cas, vbucket, bySeqno,
656                                               revSeqno, meta, nmeta)) == ENGINE_EWOULDBLOCK &&
657           c->handle_ewouldblock)
658        {
659            ++c->nblocks;
660            cb_cond_wait(&c->cond, &c->mutex);
661            ret = c->status;
662        }
663    cb_mutex_exit(&c->mutex);
664
665    if (c != cookie) {
666        destroy_mock_cookie(c);
667    }
668
669    return ret;
670}
671
672static ENGINE_ERROR_CODE mock_upr_expiration(ENGINE_HANDLE* handle,
673                                             const void* cookie,
674                                             uint32_t opaque,
675                                             const void *key,
676                                             uint16_t nkey,
677                                             uint64_t cas,
678                                             uint16_t vbucket,
679                                             uint64_t bySeqno,
680                                             uint64_t revSeqno,
681                                             const void *meta,
682                                             uint16_t nmeta) {
683
684    struct mock_engine *me = get_handle(handle);
685    struct mock_connstruct *c = (void*)cookie;
686    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
687    if (c == NULL) {
688        c = (void*)create_mock_cookie();
689    }
690
691    c->nblocks = 0;
692    cb_mutex_enter(&c->mutex);
693    while (ret == ENGINE_SUCCESS &&
694           (ret = me->the_engine->upr.expiration((ENGINE_HANDLE*)me->the_engine, c,
695                                                 opaque, key, nkey, cas, vbucket, bySeqno,
696                                                 revSeqno, meta, nmeta)) == ENGINE_EWOULDBLOCK &&
697           c->handle_ewouldblock)
698        {
699            ++c->nblocks;
700            cb_cond_wait(&c->cond, &c->mutex);
701            ret = c->status;
702        }
703    cb_mutex_exit(&c->mutex);
704
705    if (c != cookie) {
706        destroy_mock_cookie(c);
707    }
708
709    return ret;
710}
711
712static ENGINE_ERROR_CODE mock_upr_flush(ENGINE_HANDLE* handle,
713                                        const void* cookie,
714                                        uint32_t opaque,
715                                        uint16_t vbucket) {
716
717    struct mock_engine *me = get_handle(handle);
718    struct mock_connstruct *c = (void*)cookie;
719    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
720    if (c == NULL) {
721        c = (void*)create_mock_cookie();
722    }
723
724    c->nblocks = 0;
725    cb_mutex_enter(&c->mutex);
726    while (ret == ENGINE_SUCCESS &&
727           (ret = me->the_engine->upr.flush((ENGINE_HANDLE*)me->the_engine, c, opaque,
728                                            vbucket)) == ENGINE_EWOULDBLOCK &&
729           c->handle_ewouldblock)
730        {
731            ++c->nblocks;
732            cb_cond_wait(&c->cond, &c->mutex);
733            ret = c->status;
734        }
735    cb_mutex_exit(&c->mutex);
736
737    if (c != cookie) {
738        destroy_mock_cookie(c);
739    }
740
741    return ret;
742}
743
744static ENGINE_ERROR_CODE mock_upr_set_vbucket_state(ENGINE_HANDLE* handle,
745                                                    const void* cookie,
746                                                    uint32_t opaque,
747                                                    uint16_t vbucket,
748                                                    vbucket_state_t state) {
749    struct mock_engine *me = get_handle(handle);
750    return me->the_engine->upr.set_vbucket_state((ENGINE_HANDLE*)me->the_engine,
751                                                 cookie, opaque, vbucket,
752                                                 state);
753}
754
755static ENGINE_ERROR_CODE mock_upr_noop(ENGINE_HANDLE* handle,
756                                       const void* cookie,
757                                       uint32_t opaque) {
758    struct mock_engine *me = get_handle(handle);
759    return me->the_engine->upr.noop((ENGINE_HANDLE*)me->the_engine,
760                                    cookie, opaque);
761}
762
763static ENGINE_ERROR_CODE mock_upr_control(ENGINE_HANDLE* handle,
764                                          const void* cookie,
765                                          uint32_t opaque,
766                                          const void *key,
767                                          uint16_t nkey,
768                                          const void *value,
769                                          uint32_t nvalue) {
770
771    struct mock_engine *me = get_handle(handle);
772    return me->the_engine->upr.control((void*)me->the_engine,
773                                       cookie, opaque, key,
774                                       nkey, value, nvalue);
775}
776
777static ENGINE_ERROR_CODE mock_upr_buffer_acknowledgement(ENGINE_HANDLE* handle,
778                                                         const void* cookie,
779                                                         uint32_t opaque,
780                                                         uint16_t vbucket,
781                                                         uint32_t bb) {
782    struct mock_engine *me = get_handle(handle);
783    return me->the_engine->upr.buffer_acknowledgement((ENGINE_HANDLE*)me->the_engine,
784                                                      cookie, opaque, vbucket, bb);
785}
786
787static ENGINE_ERROR_CODE mock_upr_response_handler(ENGINE_HANDLE* handle,
788                                                   const void* cookie,
789                                                   protocol_binary_response_header *response) {
790    struct mock_engine *me = get_handle(handle);
791    return me->the_engine->upr.response_handler((ENGINE_HANDLE*)me->the_engine,
792                                                cookie, response);
793}
794
795struct mock_engine mock_engine;
796
797EXTENSION_LOGGER_DESCRIPTOR *logger_descriptor = NULL;
798static ENGINE_HANDLE *handle = NULL;
799static ENGINE_HANDLE_V1 *handle_v1 = NULL;
800
801static void usage(void) {
802    printf("\n");
803    printf("engine_testapp -E <path_to_engine_lib> -T <path_to_testlib>\n");
804    printf("               [-e <engine_config>] [-h]\n");
805    printf("\n");
806    printf("-E <path_to_engine_lib>      Path to the engine library file. The\n");
807    printf("                             engine library file is a library file\n");
808    printf("                             (.so or .dll) that the contains the \n");
809    printf("                             implementation of the engine being\n");
810    printf("                             tested.\n");
811    printf("\n");
812    printf("-T <path_to_testlib>         Path to the test library file. The test\n");
813    printf("                             library file is a library file (.so or\n");
814    printf("                             .dll) that contains the set of tests\n");
815    printf("                             to be executed.\n");
816    printf("\n");
817    printf("-t <timeout>                 Maximum time to run a test.\n");
818    printf("-e <engine_config>           Engine configuration string passed to\n");
819    printf("                             the engine.\n");
820    printf("-q                           Only print errors.");
821    printf("-.                           Print a . for each executed test.");
822    printf("\n");
823    printf("-h                           Prints this usage text.\n");
824    printf("-v                           verbose output\n");
825    printf("\n");
826}
827
828static int report_test(const char *name, time_t duration, enum test_result r, bool quiet, bool compact) {
829    int rc = 0;
830    char *msg = NULL;
831    int color = 0;
832    char color_str[8] = { 0 };
833    const char *reset_color = color_enabled ? "\033[m" : "";
834
835    switch (r) {
836    case SUCCESS:
837        msg="OK";
838        color = 32;
839        break;
840    case SKIPPED:
841        msg="SKIPPED";
842        color = 32;
843        break;
844    case FAIL:
845        color = 31;
846        msg="FAIL";
847        rc = 1;
848        break;
849    case DIED:
850        color = 31;
851        msg = "DIED";
852        rc = 1;
853        break;
854    case TIMEOUT:
855        color = 31;
856        msg = "TIMED OUT";
857        rc = 1;
858        break;
859    case CORE:
860        color = 31;
861        msg = "CORE DUMPED";
862        rc = 1;
863        break;
864    case PENDING:
865        color = 33;
866        msg = "PENDING";
867        break;
868    default:
869        color = 31;
870        msg = "UNKNOWN";
871        rc = 1;
872    }
873
874    cb_assert(msg);
875    if (color_enabled) {
876        snprintf(color_str, sizeof(color_str), "\033[%dm", color);
877    }
878
879    if (quiet) {
880        if (r != SUCCESS) {
881            printf("%s:  (%lu sec) %s%s%s\n", name, (long)duration,
882                   color_str, msg, reset_color);
883            fflush(stdout);
884        }
885    } else {
886        if (compact && (r == SUCCESS || r == SKIPPED || r == PENDING)) {
887            size_t len = strlen(name) + 27; /* for "Running [0/0] xxxx ..." etc */
888            size_t ii;
889
890            fprintf(stdout, "\r");
891            for (ii = 0; ii < len; ++ii) {
892                fprintf(stdout, " ");
893            }
894            fprintf(stdout, "\r");
895            fflush(stdout);
896        } else {
897            printf("(%lu sec) %s%s%s\n", (long)duration,
898                                         color_str, msg, reset_color);
899        }
900    }
901    return rc;
902}
903
904static ENGINE_HANDLE_V1 *start_your_engines(const char *engine, const char* cfg, bool engine_init) {
905
906    init_mock_server(handle);
907    if (!load_engine(engine, &get_mock_server_api, logger_descriptor, &handle)) {
908        fprintf(stderr, "Failed to load engine %s.\n", engine);
909        return NULL;
910    }
911
912    if (engine_init) {
913        if(!init_engine(handle, cfg, logger_descriptor)) {
914            fprintf(stderr, "Failed to init engine %s with config %s.\n", engine, cfg);
915            return NULL;
916        }
917    }
918
919    memset(&mock_engine, 0, sizeof(mock_engine));
920    mock_engine.me.interface.interface = 1;
921
922    mock_engine.me.get_info = mock_get_info;
923    mock_engine.me.initialize = mock_initialize;
924    mock_engine.me.destroy = mock_destroy;
925    mock_engine.me.allocate = mock_allocate;
926    mock_engine.me.remove = mock_remove;
927    mock_engine.me.release = mock_release;
928    mock_engine.me.get = mock_get;
929    mock_engine.me.store = mock_store;
930    mock_engine.me.arithmetic = mock_arithmetic;
931    mock_engine.me.flush = mock_flush;
932    mock_engine.me.get_stats = mock_get_stats;
933    mock_engine.me.reset_stats = mock_reset_stats;
934    mock_engine.me.get_stats_struct = mock_get_stats_struct;
935    mock_engine.me.aggregate_stats = mock_aggregate_stats;
936    mock_engine.me.unknown_command = mock_unknown_command;
937    mock_engine.me.tap_notify = mock_tap_notify;
938    mock_engine.me.get_tap_iterator = mock_get_tap_iterator;
939    mock_engine.me.item_set_cas = mock_item_set_cas;
940    mock_engine.me.get_item_info = mock_get_item_info;
941    mock_engine.me.errinfo = mock_errinfo;
942    mock_engine.me.upr.step = mock_upr_step;
943    mock_engine.me.upr.open = mock_upr_open;
944    mock_engine.me.upr.add_stream = mock_upr_add_stream;
945    mock_engine.me.upr.close_stream = mock_upr_close_stream;
946    mock_engine.me.upr.stream_req = mock_upr_stream_req;
947    mock_engine.me.upr.get_failover_log = mock_upr_get_failover_log;
948    mock_engine.me.upr.stream_end = mock_upr_stream_end;
949    mock_engine.me.upr.snapshot_marker = mock_upr_snapshot_marker;
950    mock_engine.me.upr.mutation = mock_upr_mutation;
951    mock_engine.me.upr.deletion = mock_upr_deletion;
952    mock_engine.me.upr.expiration = mock_upr_expiration;
953    mock_engine.me.upr.flush = mock_upr_flush;
954    mock_engine.me.upr.set_vbucket_state = mock_upr_set_vbucket_state;
955    mock_engine.me.upr.noop = mock_upr_noop;
956    mock_engine.me.upr.buffer_acknowledgement = mock_upr_buffer_acknowledgement;
957    mock_engine.me.upr.control = mock_upr_control;
958    mock_engine.me.upr.response_handler = mock_upr_response_handler;
959
960    handle_v1 = mock_engine.the_engine = (ENGINE_HANDLE_V1*)handle;
961    handle = (ENGINE_HANDLE*)&mock_engine.me;
962    handle_v1 = &mock_engine.me;
963
964    /* Reset all members that aren't set (to allow the users to write */
965    /* testcases to verify that they initialize them.. */
966    cb_assert(mock_engine.me.interface.interface == mock_engine.the_engine->interface.interface);
967
968    if (mock_engine.the_engine->get_stats_struct == NULL) {
969        mock_engine.me.get_stats_struct = NULL;
970    }
971    if (mock_engine.the_engine->aggregate_stats == NULL) {
972        mock_engine.me.aggregate_stats = NULL;
973    }
974    if (mock_engine.the_engine->unknown_command == NULL) {
975        mock_engine.me.unknown_command = NULL;
976    }
977    if (mock_engine.the_engine->tap_notify == NULL) {
978        mock_engine.me.tap_notify = NULL;
979    }
980    if (mock_engine.the_engine->get_tap_iterator == NULL) {
981        mock_engine.me.get_tap_iterator = NULL;
982    }
983    if (mock_engine.the_engine->errinfo == NULL) {
984        mock_engine.me.errinfo = NULL;
985    }
986
987    return &mock_engine.me;
988}
989
990static void destroy_engine(bool force) {
991    destroy_mock_event_callbacks();
992    if (handle_v1) {
993        handle_v1->destroy(handle, force);
994        handle_v1 = NULL;
995        handle = NULL;
996    }
997}
998
999static void reload_engine(ENGINE_HANDLE **h, ENGINE_HANDLE_V1 **h1,
1000                          const char* engine, const char *cfg, bool init, bool force) {
1001    destroy_engine(force);
1002    handle_v1 = start_your_engines(engine, cfg, init);
1003    handle = (ENGINE_HANDLE*)(handle_v1);
1004    *h1 = handle_v1;
1005    *h = handle;
1006}
1007
1008static engine_test_t* current_testcase;
1009
1010static const engine_test_t* get_current_testcase(void)
1011{
1012    return current_testcase;
1013}
1014
1015static int execute_test(engine_test_t test,
1016                        const char *engine,
1017                        const char *default_cfg)
1018{
1019    enum test_result ret = PENDING;
1020    if (test.tfun != NULL) {
1021        current_testcase = &test;
1022        if (test.prepare != NULL) {
1023            if ((ret = test.prepare(&test)) == SUCCESS) {
1024                ret = PENDING;
1025            }
1026        }
1027
1028        if (ret == PENDING) {
1029            /* Start the engines and go */
1030            start_your_engines(engine, test.cfg ? test.cfg : default_cfg, true);
1031            if (test.test_setup != NULL) {
1032                if (!test.test_setup(handle, handle_v1)) {
1033                    fprintf(stderr, "Failed to run setup for test %s\n", test.name);
1034                    return FAIL;
1035                }
1036            }
1037            ret = test.tfun(handle, handle_v1);
1038            if (test.test_teardown != NULL) {
1039                if (!test.test_teardown(handle, handle_v1)) {
1040                    fprintf(stderr, "WARNING: Failed to run teardown for test %s\n", test.name);
1041                }
1042            }
1043            destroy_engine(false);
1044
1045            if (test.cleanup) {
1046                test.cleanup(&test, ret);
1047            }
1048        }
1049    }
1050
1051    return (int)ret;
1052}
1053
1054static void setup_alarm_handler() {
1055#ifndef WIN32
1056    struct sigaction sig_handler;
1057
1058    sig_handler.sa_handler = alarm_handler;
1059    sig_handler.sa_flags = 0;
1060
1061    sigaction(SIGALRM, &sig_handler, NULL);
1062#endif
1063}
1064
1065static void set_test_timeout(int timeout) {
1066#ifndef WIN32
1067    alarm(timeout);
1068#endif
1069}
1070
1071static void clear_test_timeout() {
1072#ifndef WIN32
1073    alarm(0);
1074    alarmed = 0;
1075#endif
1076}
1077
1078static int safe_append(char *buffer, const char *txt) {
1079    int len = 0;
1080
1081    /*
1082     * We should probably make this a bit safer (by
1083     * checking if its already escaped etc, but I'll
1084     * do that whenever it turns out to be a problem ;-)
1085     */
1086    while (*txt) {
1087        switch (*txt) {
1088#ifndef WIN32
1089        case ' ':
1090        case '\\':
1091        case ';':
1092        case '|':
1093        case '&':
1094            buffer[len++] = '\\';
1095#endif
1096        default:
1097            buffer[len++] = *txt;
1098        }
1099        ++txt;
1100    }
1101
1102    buffer[len++] = ' ';
1103
1104    return len;
1105}
1106
1107int main(int argc, char **argv) {
1108    int c, exitcode = 0, num_cases = 0, timeout = 0, loop_count = 0;
1109    bool verbose = false;
1110    bool quiet = false;
1111    bool dot = false;
1112    bool loop = false;
1113    bool terminate_on_error = false;
1114    const char *engine = NULL;
1115    const char *engine_args = NULL;
1116    const char *test_suite = NULL;
1117    const char *test_case = NULL;
1118    engine_test_t *testcases = NULL;
1119    cb_dlhandle_t handle;
1120    char *errmsg;
1121    void *symbol;
1122    struct test_harness harness;
1123    int test_case_id = -1;
1124    char *cmdline = malloc(64*1024); /* should be enough */
1125
1126    /* Hack to remove the warning from C99 */
1127    union {
1128        GET_TESTS get_tests;
1129        void* voidptr;
1130    } my_get_test;
1131
1132    /* Hack to remove the warning from C99 */
1133    union {
1134        SETUP_SUITE setup_suite;
1135        void* voidptr;
1136    } my_setup_suite;
1137
1138    /* Hack to remove the warning from C99 */
1139    union {
1140        TEARDOWN_SUITE teardown_suite;
1141        void* voidptr;
1142    } my_teardown_suite;
1143
1144    cb_initialize_sockets();
1145
1146    if (cmdline == NULL) {
1147        fprintf(stderr, "Failed to allocate memory");
1148        exit(EXIT_FAILURE);
1149    }
1150
1151    memset(&my_get_test, 0, sizeof(my_get_test));
1152    memset(&my_setup_suite, 0, sizeof(my_setup_suite));
1153    memset(&my_teardown_suite, 0, sizeof(my_teardown_suite));
1154
1155    logger_descriptor = get_null_logger();
1156    color_enabled = getenv("TESTAPP_ENABLE_COLOR") != NULL;
1157
1158    /* Use unbuffered stdio */
1159    setbuf(stdout, NULL);
1160    setbuf(stderr, NULL);
1161
1162    setup_alarm_handler();
1163
1164    /* process arguments */
1165    while ((c = getopt(argc, argv,
1166                       "h"  /* usage */
1167                       "E:" /* Engine to load */
1168                       "e:" /* Engine options */
1169                       "T:" /* Library with tests to load */
1170                       "t:" /* Timeout */
1171                       "L"  /* Loop until failure */
1172                       "q"  /* Be more quiet (only report failures) */
1173                       "."  /* dot mode. */
1174                       "n:"  /* test case to run */
1175                       "v" /* verbose output */
1176                       "Z"  /* Terminate on first error */
1177                       "C:" /* Test case id */
1178                       "s" /* spinlock the program */
1179                       )) != -1) {
1180        switch (c) {
1181        case 's' : {
1182            int spin = 1;
1183            while (spin) {
1184
1185            }
1186            break;
1187        }
1188        case 'C' :
1189            test_case_id = atoi(optarg);
1190            break;
1191        case 'E':
1192            engine = optarg;
1193            break;
1194        case 'e':
1195            engine_args = optarg;
1196            break;
1197        case 'h':
1198            usage();
1199            return 0;
1200        case 'T':
1201            test_suite = optarg;
1202            break;
1203        case 't':
1204            timeout = atoi(optarg);
1205            break;
1206        case 'L':
1207            loop = true;
1208            break;
1209        case 'n':
1210            test_case = optarg;
1211            break;
1212        case 'v' :
1213            verbose = true;
1214            break;
1215        case 'q':
1216            quiet = true;
1217            break;
1218        case '.':
1219            dot = true;
1220            break;
1221        case 'Z' :
1222            terminate_on_error = true;
1223            break;
1224        default:
1225            fprintf(stderr, "Illegal argument \"%c\"\n", c);
1226            return 1;
1227        }
1228    }
1229
1230    /* validate args */
1231    if (engine == NULL) {
1232        fprintf(stderr, "You must provide a path to the storage engine library.\n");
1233        return 1;
1234    }
1235
1236    if (test_suite == NULL) {
1237        fprintf(stderr, "You must provide a path to the testsuite library.\n");
1238        return 1;
1239    }
1240
1241    /* load test_suite */
1242    handle = cb_dlopen(test_suite, &errmsg);
1243    if (handle == NULL) {
1244        fprintf(stderr, "Failed to load testsuite %s: %s\n", test_suite,
1245                errmsg);
1246        free(errmsg);
1247        return 1;
1248    }
1249
1250    /* get the test cases */
1251    symbol = cb_dlsym(handle, "get_tests", &errmsg);
1252    if (symbol == NULL) {
1253        fprintf(stderr, "Could not find get_tests function in testsuite %s: %s\n", test_suite, errmsg);
1254        free(errmsg);
1255        return 1;
1256    }
1257    my_get_test.voidptr = symbol;
1258    testcases = (*my_get_test.get_tests)();
1259
1260    /* set up the suite if needed */
1261    memset(&harness, 0, sizeof(harness));
1262    harness.default_engine_cfg = engine_args;
1263    harness.engine_path = engine;
1264    harness.reload_engine = reload_engine;
1265    harness.start_engine = start_your_engines;
1266    harness.create_cookie = create_mock_cookie;
1267    harness.destroy_cookie = destroy_mock_cookie;
1268    harness.set_ewouldblock_handling = mock_set_ewouldblock_handling;
1269    harness.lock_cookie = lock_mock_cookie;
1270    harness.unlock_cookie = unlock_mock_cookie;
1271    harness.waitfor_cookie = waitfor_mock_cookie;
1272    harness.time_travel = mock_time_travel;
1273    harness.get_current_testcase = get_current_testcase;
1274
1275    for (num_cases = 0; testcases[num_cases].name; num_cases++) {
1276        /* Just counting */
1277    }
1278
1279    symbol = cb_dlsym(handle, "setup_suite", &errmsg);
1280    if (symbol == NULL) {
1281        free(errmsg);
1282    } else {
1283        my_setup_suite.voidptr = symbol;
1284        if (!(*my_setup_suite.setup_suite)(&harness)) {
1285            fprintf(stderr, "Failed to set up test suite %s \n", test_suite);
1286            return 1;
1287        }
1288    }
1289
1290    if (test_case_id != -1) {
1291        if (test_case_id >= num_cases) {
1292            fprintf(stderr, "Invalid test case id specified\n");
1293            exit(EXIT_FAILURE);
1294        }
1295        exit(execute_test(testcases[test_case_id], engine, engine_args));
1296    }
1297
1298    do {
1299        int i;
1300        bool need_newline = false;
1301        for (i = 0; testcases[i].name; i++) {
1302            int error;
1303            if (test_case != NULL && strcmp(test_case, testcases[i].name) != 0)
1304                continue;
1305            if (!quiet) {
1306                printf("Running [%04d/%04d]: %s...",
1307                       i + num_cases * loop_count,
1308                       num_cases * (loop_count + 1),
1309                       testcases[i].name);
1310                fflush(stdout);
1311            } else if(dot) {
1312                printf(".");
1313                need_newline = true;
1314                /* Add a newline every few tests */
1315                if ((i+1) % 70 == 0) {
1316                    printf("\n");
1317                    need_newline = false;
1318                }
1319            }
1320            set_test_timeout(timeout);
1321
1322            {
1323                int ii;
1324                int offset = 0;
1325                enum test_result ecode;
1326                time_t start;
1327                time_t stop;
1328                int rc;
1329                for (ii = 0; ii < argc; ++ii) {
1330                    offset += safe_append(cmdline + offset, argv[ii]);
1331                }
1332
1333                sprintf(cmdline + offset, "-C %d", i);
1334
1335                start = time(NULL);
1336                rc = system(cmdline);
1337                stop = time(NULL);
1338
1339#ifdef WIN32
1340                ecode = (enum test_result)rc;
1341#else
1342                if (WIFEXITED(rc)) {
1343                    ecode = (enum test_result)WEXITSTATUS(rc);
1344#ifdef WCOREDUMP
1345                } else if (WIFSIGNALED(rc) && WCOREDUMP(rc)) {
1346                    ecode = CORE;
1347#endif
1348                } else {
1349                    ecode = DIED;
1350                }
1351#endif
1352                error = report_test(testcases[i].name,
1353                                    stop - start,
1354                                    ecode, quiet,
1355                                    !verbose);
1356            }
1357            clear_test_timeout();
1358
1359            if (error != 0) {
1360                ++exitcode;
1361                if (terminate_on_error) {
1362                    exit(EXIT_FAILURE);
1363                }
1364            }
1365        }
1366
1367        if (need_newline) {
1368            printf("\n");
1369        }
1370        ++loop_count;
1371    } while (loop && exitcode == 0);
1372
1373    /* tear down the suite if needed */
1374    symbol = cb_dlsym(handle, "teardown_suite", &errmsg);
1375    if (symbol == NULL) {
1376        free(errmsg);
1377    } else {
1378        my_teardown_suite.voidptr = symbol;
1379        if (!(*my_teardown_suite.teardown_suite)()) {
1380            fprintf(stderr, "Failed to teardown up test suite %s \n", test_suite);
1381        }
1382    }
1383
1384    printf("# Passed %d of %d tests\n", num_cases - exitcode, num_cases);
1385    free(cmdline);
1386
1387    return exitcode;
1388}
1389