1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#include "config.h"
3#include <assert.h>
4#include <errno.h>
5#include <stdarg.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10#include <signal.h>
11#include <getopt.h>
12#include <time.h>
13#include <platform/platform.h>
14#include "utilities/engine_loader.h"
15#include <memcached/engine_testapp.h>
16#include <memcached/extension_loggers.h>
17#include "mock_server.h"
18
19struct mock_engine {
20    ENGINE_HANDLE_V1 me;
21    ENGINE_HANDLE_V1 *the_engine;
22    TAP_ITERATOR iterator;
23};
24
25static bool color_enabled;
26
27#ifndef WIN32
28static sig_atomic_t alarmed;
29
30static void alarm_handler(int sig) {
31    alarmed = 1;
32}
33#endif
34
35static struct mock_engine* get_handle(ENGINE_HANDLE* handle) {
36    return (struct mock_engine*)handle;
37}
38
39static tap_event_t mock_tap_iterator(ENGINE_HANDLE* handle,
40                                     const void *cookie, item **itm,
41                                     void **es, uint16_t *nes, uint8_t *ttl,
42                                     uint16_t *flags, uint32_t *seqno,
43                                     uint16_t *vbucket) {
44   struct mock_engine *me = get_handle(handle);
45   return me->iterator((ENGINE_HANDLE*)me->the_engine, cookie, itm, es, nes,
46                       ttl, flags, seqno, vbucket);
47}
48
49static const engine_info* mock_get_info(ENGINE_HANDLE* handle) {
50    struct mock_engine *me = get_handle(handle);
51    return me->the_engine->get_info((ENGINE_HANDLE*)me->the_engine);
52}
53
54static ENGINE_ERROR_CODE mock_initialize(ENGINE_HANDLE* handle,
55                                         const char* config_str) {
56    struct mock_engine *me = get_handle(handle);
57    return me->the_engine->initialize((ENGINE_HANDLE*)me->the_engine, config_str);
58}
59
60static void mock_destroy(ENGINE_HANDLE* handle, const bool force) {
61    struct mock_engine *me = get_handle(handle);
62    me->the_engine->destroy((ENGINE_HANDLE*)me->the_engine, force);
63}
64
65static ENGINE_ERROR_CODE mock_allocate(ENGINE_HANDLE* handle,
66                                       const void* cookie,
67                                       item **item,
68                                       const void* key,
69                                       const size_t nkey,
70                                       const size_t nbytes,
71                                       const int flags,
72                                       const rel_time_t exptime,
73                                       uint8_t datatype) {
74    struct mock_engine *me = get_handle(handle);
75    struct mock_connstruct *c = (void*)cookie;
76    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
77
78    if (c == NULL) {
79        c = (void*)create_mock_cookie();
80    }
81
82    c->nblocks = 0;
83    cb_mutex_enter(&c->mutex);
84    while (ret == ENGINE_SUCCESS &&
85           (ret = me->the_engine->allocate((ENGINE_HANDLE*)me->the_engine, c,
86                                           item, key, nkey,
87                                           nbytes, flags,
88                                           exptime, datatype)) == ENGINE_EWOULDBLOCK &&
89           c->handle_ewouldblock)
90    {
91        ++c->nblocks;
92        cb_cond_wait(&c->cond, &c->mutex);
93        ret = c->status;
94    }
95    cb_mutex_exit(&c->mutex);
96
97    if (c != cookie) {
98        destroy_mock_cookie(c);
99    }
100
101    return ret;
102}
103
104static ENGINE_ERROR_CODE mock_remove(ENGINE_HANDLE* handle,
105                                     const void* cookie,
106                                     const void* key,
107                                     const size_t nkey,
108                                     uint64_t* cas,
109                                     uint16_t vbucket)
110{
111    struct mock_engine *me = get_handle(handle);
112    struct mock_connstruct *c = (void*)cookie;
113    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
114
115    if (c == NULL) {
116        c = (void*)create_mock_cookie();
117    }
118
119    c->nblocks = 0;
120    cb_mutex_enter(&c->mutex);
121    while (ret == ENGINE_SUCCESS &&
122           (ret = me->the_engine->remove((ENGINE_HANDLE*)me->the_engine, c, key,
123                                         nkey, cas, vbucket)) == ENGINE_EWOULDBLOCK &&
124           c->handle_ewouldblock)
125    {
126        ++c->nblocks;
127        cb_cond_wait(&c->cond, &c->mutex);
128        ret = c->status;
129    }
130    cb_mutex_exit(&c->mutex);
131
132    if (c != cookie) {
133        destroy_mock_cookie(c);
134    }
135
136    return ret;
137}
138
139static void mock_release(ENGINE_HANDLE* handle,
140                         const void *cookie,
141                         item* item) {
142    struct mock_engine *me = get_handle(handle);
143    me->the_engine->release((ENGINE_HANDLE*)me->the_engine, cookie, item);
144}
145
146static ENGINE_ERROR_CODE mock_get(ENGINE_HANDLE* handle,
147                                  const void* cookie,
148                                  item** item,
149                                  const void* key,
150                                  const int nkey,
151                                  uint16_t vbucket) {
152    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
153    struct mock_engine *me = get_handle(handle);
154    struct mock_connstruct *c = (void*)cookie;
155    if (c == NULL) {
156        c = (void*)create_mock_cookie();
157    }
158
159    c->nblocks = 0;
160    cb_mutex_enter(&c->mutex);
161    while (ret == ENGINE_SUCCESS &&
162           (ret = me->the_engine->get((ENGINE_HANDLE*)me->the_engine, c, item,
163                                      key, nkey, vbucket)) == ENGINE_EWOULDBLOCK &&
164           c->handle_ewouldblock)
165    {
166        ++c->nblocks;
167        cb_cond_wait(&c->cond, &c->mutex);
168        ret = c->status;
169    }
170    cb_mutex_exit(&c->mutex);
171
172    if (c != cookie) {
173        destroy_mock_cookie(c);
174    }
175
176    return ret;
177}
178
179static ENGINE_ERROR_CODE mock_get_stats(ENGINE_HANDLE* handle,
180                                        const void* cookie,
181                                        const char* stat_key,
182                                        int nkey,
183                                        ADD_STAT add_stat)
184{
185    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
186    struct mock_engine *me = get_handle(handle);
187    struct mock_connstruct *c = (void*)cookie;
188    if (c == NULL) {
189        c = (void*)create_mock_cookie();
190    }
191
192    c->nblocks = 0;
193    cb_mutex_enter(&c->mutex);
194    while (ret == ENGINE_SUCCESS &&
195           (ret = me->the_engine->get_stats((ENGINE_HANDLE*)me->the_engine, c, stat_key,
196                                            nkey, add_stat)) == ENGINE_EWOULDBLOCK &&
197           c->handle_ewouldblock)
198    {
199        ++c->nblocks;
200        cb_cond_wait(&c->cond, &c->mutex);
201        ret = c->status;
202    }
203    cb_mutex_exit(&c->mutex);
204
205    if (c != cookie) {
206        destroy_mock_cookie(c);
207    }
208
209    return ret;
210}
211
212static ENGINE_ERROR_CODE mock_store(ENGINE_HANDLE* handle,
213                                    const void *cookie,
214                                    item* item,
215                                    uint64_t *cas,
216                                    ENGINE_STORE_OPERATION operation,
217                                    uint16_t vbucket) {
218    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
219    struct mock_engine *me = get_handle(handle);
220    struct mock_connstruct *c = (void*)cookie;
221    if (c == NULL) {
222        c = (void*)create_mock_cookie();
223    }
224
225    c->nblocks = 0;
226    cb_mutex_enter(&c->mutex);
227    while (ret == ENGINE_SUCCESS &&
228           (ret = me->the_engine->store((ENGINE_HANDLE*)me->the_engine, c, item, cas,
229                                        operation, vbucket)) == ENGINE_EWOULDBLOCK &&
230           c->handle_ewouldblock)
231    {
232        ++c->nblocks;
233        cb_cond_wait(&c->cond, &c->mutex);
234        ret = c->status;
235    }
236    cb_mutex_exit(&c->mutex);
237
238    if (c != cookie) {
239        destroy_mock_cookie(c);
240    }
241
242    return ret;
243}
244
245static ENGINE_ERROR_CODE mock_arithmetic(ENGINE_HANDLE* handle,
246                                         const void* cookie,
247                                         const void* key,
248                                         const int nkey,
249                                         const bool increment,
250                                         const bool create,
251                                         const uint64_t delta,
252                                         const uint64_t initial,
253                                         const rel_time_t exptime,
254                                         uint64_t *cas,
255                                         uint8_t datatype,
256                                         uint64_t *result,
257                                         uint16_t vbucket) {
258    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
259    struct mock_engine *me = get_handle(handle);
260    struct mock_connstruct *c = (void*)cookie;
261    if (c == NULL) {
262        c = (void*)create_mock_cookie();
263    }
264
265    c->nblocks = 0;
266    cb_mutex_enter(&c->mutex);
267    while (ret == ENGINE_SUCCESS &&
268           (ret = me->the_engine->arithmetic((ENGINE_HANDLE*)me->the_engine, c, key,
269                                             nkey, increment, create,
270                                             delta, initial, exptime,
271                                             cas, datatype,
272                                             result, vbucket)) == ENGINE_EWOULDBLOCK &&
273           c->handle_ewouldblock)
274    {
275        ++c->nblocks;
276        cb_cond_wait(&c->cond, &c->mutex);
277        ret = c->status;
278    }
279    cb_mutex_exit(&c->mutex);
280
281    if (c != cookie) {
282        destroy_mock_cookie(c);
283    }
284
285    return ret;
286}
287
288static ENGINE_ERROR_CODE mock_flush(ENGINE_HANDLE* handle,
289                                    const void* cookie, time_t when) {
290    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
291    struct mock_engine *me = get_handle(handle);
292    struct mock_connstruct *c = (void*)cookie;
293    if (c == NULL) {
294        c = (void*)create_mock_cookie();
295    }
296
297    c->nblocks = 0;
298    cb_mutex_enter(&c->mutex);
299    while (ret == ENGINE_SUCCESS &&
300           (ret = me->the_engine->flush((ENGINE_HANDLE*)me->the_engine, c, when)) == ENGINE_EWOULDBLOCK &&
301           c->handle_ewouldblock)
302    {
303        ++c->nblocks;
304        cb_cond_wait(&c->cond, &c->mutex);
305        ret = c->status;
306    }
307    cb_mutex_exit(&c->mutex);
308
309    if (c != cookie) {
310        destroy_mock_cookie(c);
311    }
312
313    return ret;
314}
315
316static void mock_reset_stats(ENGINE_HANDLE* handle, const void *cookie) {
317    struct mock_engine *me = get_handle(handle);
318    me->the_engine->reset_stats((ENGINE_HANDLE*)me->the_engine, cookie);
319}
320
321static ENGINE_ERROR_CODE mock_unknown_command(ENGINE_HANDLE* handle,
322                                              const void* cookie,
323                                              protocol_binary_request_header *request,
324                                              ADD_RESPONSE response)
325{
326    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
327    struct mock_engine *me = get_handle(handle);
328    struct mock_connstruct *c = (void*)cookie;
329    if (c == NULL) {
330        c = (void*)create_mock_cookie();
331    }
332
333    c->nblocks = 0;
334    cb_mutex_enter(&c->mutex);
335    while (ret == ENGINE_SUCCESS &&
336           (ret = me->the_engine->unknown_command((ENGINE_HANDLE*)me->the_engine, c,
337                                                  request, response)) == ENGINE_EWOULDBLOCK &&
338           c->handle_ewouldblock)
339    {
340        ++c->nblocks;
341        cb_cond_wait(&c->cond, &c->mutex);
342        ret = c->status;
343    }
344    cb_mutex_exit(&c->mutex);
345
346    if (c != cookie) {
347        destroy_mock_cookie(c);
348    }
349
350    return ret;
351}
352
353static void mock_item_set_cas(ENGINE_HANDLE *handle, const void *cookie,
354                              item* item, uint64_t val)
355{
356    struct mock_engine *me = get_handle(handle);
357    me->the_engine->item_set_cas((ENGINE_HANDLE*)me->the_engine, cookie, item, val);
358}
359
360
361static bool mock_get_item_info(ENGINE_HANDLE *handle, const void *cookie,
362                               const item* item, item_info *item_info)
363{
364    struct mock_engine *me = get_handle(handle);
365    return me->the_engine->get_item_info((ENGINE_HANDLE*)me->the_engine,
366                                         cookie, item, item_info);
367}
368
369static void *mock_get_stats_struct(ENGINE_HANDLE* handle, const void* cookie)
370{
371    struct mock_engine *me = get_handle(handle);
372    return me->the_engine->get_stats_struct((ENGINE_HANDLE*)me->the_engine, cookie);
373}
374
375static ENGINE_ERROR_CODE mock_aggregate_stats(ENGINE_HANDLE* handle,
376                                              const void* cookie,
377                                              void (*callback)(void*, void*),
378                                              void *vptr)
379{
380    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
381    struct mock_engine *me = get_handle(handle);
382    struct mock_connstruct *c = (void*)cookie;
383    if (c == NULL) {
384        c = (void*)create_mock_cookie();
385    }
386
387    c->nblocks = 0;
388    cb_mutex_enter(&c->mutex);
389    while (ret == ENGINE_SUCCESS &&
390           (ret = me->the_engine->aggregate_stats((ENGINE_HANDLE*)me->the_engine, c,
391                                                  callback, vptr)) == ENGINE_EWOULDBLOCK &&
392           c->handle_ewouldblock)
393    {
394        ++c->nblocks;
395        cb_cond_wait(&c->cond, &c->mutex);
396        ret = c->status;
397    }
398    cb_mutex_exit(&c->mutex);
399
400    if (c != cookie) {
401        destroy_mock_cookie(c);
402    }
403
404    return ret;
405}
406
407static ENGINE_ERROR_CODE mock_tap_notify(ENGINE_HANDLE* handle,
408                                        const void *cookie,
409                                        void *engine_specific,
410                                        uint16_t nengine,
411                                        uint8_t ttl,
412                                        uint16_t tap_flags,
413                                        tap_event_t tap_event,
414                                        uint32_t tap_seqno,
415                                        const void *key,
416                                        size_t nkey,
417                                        uint32_t flags,
418                                        uint32_t exptime,
419                                        uint64_t cas,
420                                        uint8_t datatype,
421                                        const void *data,
422                                        size_t ndata,
423                                        uint16_t vbucket) {
424
425    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
426    struct mock_engine *me = get_handle(handle);
427    struct mock_connstruct *c = (void*)cookie;
428    if (c == NULL) {
429        c = (void*)create_mock_cookie();
430    }
431
432    c->nblocks = 0;
433    cb_mutex_enter(&c->mutex);
434    while (ret == ENGINE_SUCCESS &&
435           (ret = me->the_engine->tap_notify((ENGINE_HANDLE*)me->the_engine, c,
436                                             engine_specific, nengine, ttl, tap_flags,
437                                             tap_event, tap_seqno, key, nkey, flags,
438                                             exptime, cas, datatype,
439                                             data, ndata, vbucket)) == ENGINE_EWOULDBLOCK &&
440           c->handle_ewouldblock)
441    {
442        ++c->nblocks;
443        cb_cond_wait(&c->cond, &c->mutex);
444        ret = c->status;
445    }
446    cb_mutex_exit(&c->mutex);
447
448    if (c != cookie) {
449        destroy_mock_cookie(c);
450    }
451
452    return ret;
453}
454
455
456static TAP_ITERATOR mock_get_tap_iterator(ENGINE_HANDLE* handle, const void* cookie,
457                                           const void* client, size_t nclient,
458                                           uint32_t flags,
459                                           const void* userdata, size_t nuserdata) {
460    struct mock_engine *me = get_handle(handle);
461    me->iterator = me->the_engine->get_tap_iterator((ENGINE_HANDLE*)me->the_engine, cookie,
462                                                    client, nclient, flags, userdata, nuserdata);
463    return (me->iterator != NULL) ? mock_tap_iterator : NULL;
464}
465
466static size_t mock_errinfo(ENGINE_HANDLE *handle, const void* cookie,
467                           char *buffer, size_t buffsz) {
468    struct mock_engine *me = get_handle(handle);
469    return me->the_engine->errinfo((ENGINE_HANDLE*)me->the_engine, cookie,
470                                   buffer, buffsz);
471}
472
473static ENGINE_ERROR_CODE mock_upr_step(ENGINE_HANDLE* handle,
474                                       const void* cookie,
475                                       struct upr_message_producers *producers) {
476    struct mock_engine *me = get_handle(handle);
477    return me->the_engine->upr.step((ENGINE_HANDLE*)me->the_engine, cookie,
478                                    producers);
479}
480
481static ENGINE_ERROR_CODE mock_upr_open(ENGINE_HANDLE* handle,
482                                       const void* cookie,
483                                       uint32_t opaque,
484                                       uint32_t seqno,
485                                       uint32_t flags,
486                                       void *name,
487                                       uint16_t nname) {
488    struct mock_engine *me = get_handle(handle);
489    return me->the_engine->upr.open((ENGINE_HANDLE*)me->the_engine, cookie,
490                                    opaque, seqno, flags, name, nname);
491}
492
493static ENGINE_ERROR_CODE mock_upr_add_stream(ENGINE_HANDLE* handle,
494                                             const void* cookie,
495                                             uint32_t opaque,
496                                             uint16_t vbucket,
497                                             uint32_t flags) {
498
499    struct mock_engine *me = get_handle(handle);
500    struct mock_connstruct *c = (void*)cookie;
501    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
502
503    if (c == NULL) {
504        c = (void*)create_mock_cookie();
505    }
506
507    c->nblocks = 0;
508    cb_mutex_enter(&c->mutex);
509    while (ret == ENGINE_SUCCESS &&
510           (ret = me->the_engine->upr.add_stream((ENGINE_HANDLE*)me->the_engine, c,
511                                                 opaque, vbucket, flags))
512                                                == ENGINE_EWOULDBLOCK &&
513           c->handle_ewouldblock)
514        {
515            ++c->nblocks;
516            cb_cond_wait(&c->cond, &c->mutex);
517            ret = c->status;
518        }
519    cb_mutex_exit(&c->mutex);
520
521    if (c != cookie) {
522        destroy_mock_cookie(c);
523    }
524
525    return ret;
526}
527
528static ENGINE_ERROR_CODE mock_upr_close_stream(ENGINE_HANDLE* handle,
529                                               const void* cookie,
530                                               uint32_t opaque,
531                                               uint16_t vbucket) {
532    struct mock_engine *me = get_handle(handle);
533    return me->the_engine->upr.close_stream((ENGINE_HANDLE*)me->the_engine,
534                                            cookie, opaque, vbucket);
535}
536
537static ENGINE_ERROR_CODE mock_upr_stream_req(ENGINE_HANDLE* handle,
538                                             const void* cookie,
539                                             uint32_t flags,
540                                             uint32_t opaque,
541                                             uint16_t vbucket,
542                                             uint64_t start_seqno,
543                                             uint64_t end_seqno,
544                                             uint64_t vbucket_uuid,
545                                             uint64_t high_seqno,
546                                             uint64_t *rollback_seqno,
547                                             upr_add_failover_log callback) {
548    struct mock_engine *me = get_handle(handle);
549    return me->the_engine->upr.stream_req((ENGINE_HANDLE*)me->the_engine,
550                                          cookie, flags, opaque, vbucket,
551                                          start_seqno, end_seqno, vbucket_uuid,
552                                          high_seqno, rollback_seqno,
553                                          callback);
554}
555
556static ENGINE_ERROR_CODE mock_upr_get_failover_log(ENGINE_HANDLE* handle,
557                                                   const void* cookie,
558                                                   uint32_t opaque,
559                                                   uint16_t vbucket,
560                                                   upr_add_failover_log cb) {
561    struct mock_engine *me = get_handle(handle);
562    return me->the_engine->upr.get_failover_log((ENGINE_HANDLE*)me->the_engine,
563                                                cookie, opaque, vbucket, cb);
564}
565
566static ENGINE_ERROR_CODE mock_upr_stream_end(ENGINE_HANDLE* handle,
567                                             const void* cookie,
568                                             uint32_t opaque,
569                                             uint16_t vbucket,
570                                             uint32_t flags) {
571    struct mock_engine *me = get_handle(handle);
572    return me->the_engine->upr.stream_end((ENGINE_HANDLE*)me->the_engine,
573                                          cookie, opaque, vbucket, flags);
574}
575
576static ENGINE_ERROR_CODE mock_upr_snapshot_marker(ENGINE_HANDLE* handle,
577                                                  const void* cookie,
578                                                  uint32_t opaque,
579                                                  uint16_t vbucket) {
580    struct mock_engine *me = get_handle(handle);
581    return me->the_engine->upr.snapshot_marker((ENGINE_HANDLE*)me->the_engine,
582                                               cookie, opaque, vbucket);
583}
584
585static ENGINE_ERROR_CODE mock_upr_mutation(ENGINE_HANDLE* handle,
586                                           const void* cookie,
587                                           uint32_t opaque,
588                                           const void *key,
589                                           uint16_t nkey,
590                                           const void *value,
591                                           uint32_t nvalue,
592                                           uint64_t cas,
593                                           uint16_t vbucket,
594                                           uint32_t flags,
595                                           uint8_t datatype,
596                                           uint64_t bySeqno,
597                                           uint64_t revSeqno,
598                                           uint32_t expiration,
599                                           uint32_t lockTime,
600                                           const void *meta,
601                                           uint16_t nmeta,
602                                           uint8_t nru) {
603
604    struct mock_engine *me = get_handle(handle);
605    struct mock_connstruct *c = (void*)cookie;
606    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
607    if (c == NULL) {
608        c = (void*)create_mock_cookie();
609    }
610
611    c->nblocks = 0;
612    cb_mutex_enter(&c->mutex);
613    while (ret == ENGINE_SUCCESS &&
614           (ret = me->the_engine->upr.mutation((ENGINE_HANDLE*)me->the_engine, c, opaque, key,
615                                               nkey, value, nvalue, cas, vbucket, flags,
616                                               datatype, bySeqno, revSeqno, expiration,
617                                               lockTime, meta, nmeta, nru)) == ENGINE_EWOULDBLOCK &&
618           c->handle_ewouldblock)
619        {
620            ++c->nblocks;
621            cb_cond_wait(&c->cond, &c->mutex);
622            ret = c->status;
623        }
624    cb_mutex_exit(&c->mutex);
625
626    if (c != cookie) {
627        destroy_mock_cookie(c);
628    }
629
630    return ret;
631}
632
633static ENGINE_ERROR_CODE mock_upr_deletion(ENGINE_HANDLE* handle,
634                                           const void* cookie,
635                                           uint32_t opaque,
636                                           const void *key,
637                                           uint16_t nkey,
638                                           uint64_t cas,
639                                           uint16_t vbucket,
640                                           uint64_t bySeqno,
641                                           uint64_t revSeqno,
642                                           const void *meta,
643                                           uint16_t nmeta) {
644
645    struct mock_engine *me = get_handle(handle);
646    struct mock_connstruct *c = (void*)cookie;
647    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
648    if (c == NULL) {
649        c = (void*)create_mock_cookie();
650    }
651
652    c->nblocks = 0;
653    cb_mutex_enter(&c->mutex);
654    while (ret == ENGINE_SUCCESS &&
655           (ret = me->the_engine->upr.deletion((ENGINE_HANDLE*)me->the_engine, c,
656                                               opaque, key, nkey, cas, vbucket, bySeqno,
657                                               revSeqno, meta, nmeta)) == ENGINE_EWOULDBLOCK &&
658           c->handle_ewouldblock)
659        {
660            ++c->nblocks;
661            cb_cond_wait(&c->cond, &c->mutex);
662            ret = c->status;
663        }
664    cb_mutex_exit(&c->mutex);
665
666    if (c != cookie) {
667        destroy_mock_cookie(c);
668    }
669
670    return ret;
671}
672
673static ENGINE_ERROR_CODE mock_upr_expiration(ENGINE_HANDLE* handle,
674                                             const void* cookie,
675                                             uint32_t opaque,
676                                             const void *key,
677                                             uint16_t nkey,
678                                             uint64_t cas,
679                                             uint16_t vbucket,
680                                             uint64_t bySeqno,
681                                             uint64_t revSeqno,
682                                             const void *meta,
683                                             uint16_t nmeta) {
684
685    struct mock_engine *me = get_handle(handle);
686    struct mock_connstruct *c = (void*)cookie;
687    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
688    if (c == NULL) {
689        c = (void*)create_mock_cookie();
690    }
691
692    c->nblocks = 0;
693    cb_mutex_enter(&c->mutex);
694    while (ret == ENGINE_SUCCESS &&
695           (ret = me->the_engine->upr.expiration((ENGINE_HANDLE*)me->the_engine, c,
696                                                 opaque, key, nkey, cas, vbucket, bySeqno,
697                                                 revSeqno, meta, nmeta)) == ENGINE_EWOULDBLOCK &&
698           c->handle_ewouldblock)
699        {
700            ++c->nblocks;
701            cb_cond_wait(&c->cond, &c->mutex);
702            ret = c->status;
703        }
704    cb_mutex_exit(&c->mutex);
705
706    if (c != cookie) {
707        destroy_mock_cookie(c);
708    }
709
710    return ret;
711}
712
713static ENGINE_ERROR_CODE mock_upr_flush(ENGINE_HANDLE* handle,
714                                        const void* cookie,
715                                        uint32_t opaque,
716                                        uint16_t vbucket) {
717
718    struct mock_engine *me = get_handle(handle);
719    struct mock_connstruct *c = (void*)cookie;
720    ENGINE_ERROR_CODE ret = ENGINE_SUCCESS;
721    if (c == NULL) {
722        c = (void*)create_mock_cookie();
723    }
724
725    c->nblocks = 0;
726    cb_mutex_enter(&c->mutex);
727    while (ret == ENGINE_SUCCESS &&
728           (ret = me->the_engine->upr.flush((ENGINE_HANDLE*)me->the_engine, c, opaque,
729                                            vbucket)) == ENGINE_EWOULDBLOCK &&
730           c->handle_ewouldblock)
731        {
732            ++c->nblocks;
733            cb_cond_wait(&c->cond, &c->mutex);
734            ret = c->status;
735        }
736    cb_mutex_exit(&c->mutex);
737
738    if (c != cookie) {
739        destroy_mock_cookie(c);
740    }
741
742    return ret;
743}
744
745static ENGINE_ERROR_CODE mock_upr_set_vbucket_state(ENGINE_HANDLE* handle,
746                                                    const void* cookie,
747                                                    uint32_t opaque,
748                                                    uint16_t vbucket,
749                                                    vbucket_state_t state) {
750    struct mock_engine *me = get_handle(handle);
751    return me->the_engine->upr.set_vbucket_state((ENGINE_HANDLE*)me->the_engine,
752                                                 cookie, opaque, vbucket,
753                                                 state);
754}
755
756static ENGINE_ERROR_CODE mock_upr_noop(ENGINE_HANDLE* handle,
757                                       const void* cookie,
758                                       uint32_t opaque) {
759    struct mock_engine *me = get_handle(handle);
760    return me->the_engine->upr.noop((ENGINE_HANDLE*)me->the_engine,
761                                    cookie, opaque);
762}
763
764static ENGINE_ERROR_CODE mock_upr_control(ENGINE_HANDLE* handle,
765                                          const void* cookie,
766                                          uint32_t opaque,
767                                          const void *key,
768                                          uint16_t nkey,
769                                          const void *value,
770                                          uint32_t nvalue) {
771
772    struct mock_engine *me = get_handle(handle);
773    return me->the_engine->upr.control((void*)me->the_engine,
774                                       cookie, opaque, key,
775                                       nkey, value, nvalue);
776}
777
778static ENGINE_ERROR_CODE mock_upr_buffer_acknowledgement(ENGINE_HANDLE* handle,
779                                                         const void* cookie,
780                                                         uint32_t opaque,
781                                                         uint16_t vbucket,
782                                                         uint32_t bb) {
783    struct mock_engine *me = get_handle(handle);
784    return me->the_engine->upr.buffer_acknowledgement((ENGINE_HANDLE*)me->the_engine,
785                                                      cookie, opaque, vbucket, bb);
786}
787
788static ENGINE_ERROR_CODE mock_upr_response_handler(ENGINE_HANDLE* handle,
789                                                   const void* cookie,
790                                                   protocol_binary_response_header *response) {
791    struct mock_engine *me = get_handle(handle);
792    return me->the_engine->upr.response_handler((ENGINE_HANDLE*)me->the_engine,
793                                                cookie, response);
794}
795
796struct mock_engine mock_engine;
797
798EXTENSION_LOGGER_DESCRIPTOR *logger_descriptor = NULL;
799static ENGINE_HANDLE *handle = NULL;
800static ENGINE_HANDLE_V1 *handle_v1 = NULL;
801
802static void usage(void) {
803    printf("\n");
804    printf("engine_testapp -E <path_to_engine_lib> -T <path_to_testlib>\n");
805    printf("               [-e <engine_config>] [-h]\n");
806    printf("\n");
807    printf("-E <path_to_engine_lib>      Path to the engine library file. The\n");
808    printf("                             engine library file is a library file\n");
809    printf("                             (.so or .dll) that the contains the \n");
810    printf("                             implementation of the engine being\n");
811    printf("                             tested.\n");
812    printf("\n");
813    printf("-T <path_to_testlib>         Path to the test library file. The test\n");
814    printf("                             library file is a library file (.so or\n");
815    printf("                             .dll) that contains the set of tests\n");
816    printf("                             to be executed.\n");
817    printf("\n");
818    printf("-t <timeout>                 Maximum time to run a test.\n");
819    printf("-e <engine_config>           Engine configuration string passed to\n");
820    printf("                             the engine.\n");
821    printf("-q                           Only print errors.");
822    printf("-.                           Print a . for each executed test.");
823    printf("\n");
824    printf("-h                           Prints this usage text.\n");
825    printf("-v                           verbose output\n");
826    printf("\n");
827}
828
829static int report_test(const char *name, time_t duration, enum test_result r, bool quiet, bool compact) {
830    int rc = 0;
831    char *msg = NULL;
832    int color = 0;
833    char color_str[8] = { 0 };
834    const char *reset_color = color_enabled ? "\033[m" : "";
835
836    switch (r) {
837    case SUCCESS:
838        msg="OK";
839        color = 32;
840        break;
841    case SKIPPED:
842        msg="SKIPPED";
843        color = 32;
844        break;
845    case FAIL:
846        color = 31;
847        msg="FAIL";
848        rc = 1;
849        break;
850    case DIED:
851        color = 31;
852        msg = "DIED";
853        rc = 1;
854        break;
855    case TIMEOUT:
856        color = 31;
857        msg = "TIMED OUT";
858        rc = 1;
859        break;
860    case CORE:
861        color = 31;
862        msg = "CORE DUMPED";
863        rc = 1;
864        break;
865    case PENDING:
866        color = 33;
867        msg = "PENDING";
868        break;
869    default:
870        color = 31;
871        msg = "UNKNOWN";
872        rc = 1;
873    }
874
875    assert(msg);
876    if (color_enabled) {
877        snprintf(color_str, sizeof(color_str), "\033[%dm", color);
878    }
879
880    if (quiet) {
881        if (r != SUCCESS) {
882            printf("%s:  (%lu sec) %s%s%s\n", name, (long)duration,
883                   color_str, msg, reset_color);
884            fflush(stdout);
885        }
886    } else {
887        if (compact && (r == SUCCESS || r == SKIPPED || r == PENDING)) {
888            size_t len = strlen(name) + 27; /* for "Running [0/0] xxxx ..." etc */
889            size_t ii;
890
891            fprintf(stdout, "\r");
892            for (ii = 0; ii < len; ++ii) {
893                fprintf(stdout, " ");
894            }
895            fprintf(stdout, "\r");
896            fflush(stdout);
897        } else {
898            printf("(%lu sec) %s%s%s\n", (long)duration,
899                                         color_str, msg, reset_color);
900        }
901    }
902    return rc;
903}
904
905static ENGINE_HANDLE_V1 *start_your_engines(const char *engine, const char* cfg, bool engine_init) {
906
907    init_mock_server(handle);
908    if (!load_engine(engine, &get_mock_server_api, logger_descriptor, &handle)) {
909        fprintf(stderr, "Failed to load engine %s.\n", engine);
910        return NULL;
911    }
912
913    if (engine_init) {
914        if(!init_engine(handle, cfg, logger_descriptor)) {
915            fprintf(stderr, "Failed to init engine %s with config %s.\n", engine, cfg);
916            return NULL;
917        }
918    }
919
920    memset(&mock_engine, 0, sizeof(mock_engine));
921    mock_engine.me.interface.interface = 1;
922
923    mock_engine.me.get_info = mock_get_info;
924    mock_engine.me.initialize = mock_initialize;
925    mock_engine.me.destroy = mock_destroy;
926    mock_engine.me.allocate = mock_allocate;
927    mock_engine.me.remove = mock_remove;
928    mock_engine.me.release = mock_release;
929    mock_engine.me.get = mock_get;
930    mock_engine.me.store = mock_store;
931    mock_engine.me.arithmetic = mock_arithmetic;
932    mock_engine.me.flush = mock_flush;
933    mock_engine.me.get_stats = mock_get_stats;
934    mock_engine.me.reset_stats = mock_reset_stats;
935    mock_engine.me.get_stats_struct = mock_get_stats_struct;
936    mock_engine.me.aggregate_stats = mock_aggregate_stats;
937    mock_engine.me.unknown_command = mock_unknown_command;
938    mock_engine.me.tap_notify = mock_tap_notify;
939    mock_engine.me.get_tap_iterator = mock_get_tap_iterator;
940    mock_engine.me.item_set_cas = mock_item_set_cas;
941    mock_engine.me.get_item_info = mock_get_item_info;
942    mock_engine.me.errinfo = mock_errinfo;
943    mock_engine.me.upr.step = mock_upr_step;
944    mock_engine.me.upr.open = mock_upr_open;
945    mock_engine.me.upr.add_stream = mock_upr_add_stream;
946    mock_engine.me.upr.close_stream = mock_upr_close_stream;
947    mock_engine.me.upr.stream_req = mock_upr_stream_req;
948    mock_engine.me.upr.get_failover_log = mock_upr_get_failover_log;
949    mock_engine.me.upr.stream_end = mock_upr_stream_end;
950    mock_engine.me.upr.snapshot_marker = mock_upr_snapshot_marker;
951    mock_engine.me.upr.mutation = mock_upr_mutation;
952    mock_engine.me.upr.deletion = mock_upr_deletion;
953    mock_engine.me.upr.expiration = mock_upr_expiration;
954    mock_engine.me.upr.flush = mock_upr_flush;
955    mock_engine.me.upr.set_vbucket_state = mock_upr_set_vbucket_state;
956    mock_engine.me.upr.noop = mock_upr_noop;
957    mock_engine.me.upr.buffer_acknowledgement = mock_upr_buffer_acknowledgement;
958    mock_engine.me.upr.control = mock_upr_control;
959    mock_engine.me.upr.response_handler = mock_upr_response_handler;
960
961    handle_v1 = mock_engine.the_engine = (ENGINE_HANDLE_V1*)handle;
962    handle = (ENGINE_HANDLE*)&mock_engine.me;
963    handle_v1 = &mock_engine.me;
964
965    /* Reset all members that aren't set (to allow the users to write */
966    /* testcases to verify that they initialize them.. */
967    assert(mock_engine.me.interface.interface == mock_engine.the_engine->interface.interface);
968
969    if (mock_engine.the_engine->get_stats_struct == NULL) {
970        mock_engine.me.get_stats_struct = NULL;
971    }
972    if (mock_engine.the_engine->aggregate_stats == NULL) {
973        mock_engine.me.aggregate_stats = NULL;
974    }
975    if (mock_engine.the_engine->unknown_command == NULL) {
976        mock_engine.me.unknown_command = NULL;
977    }
978    if (mock_engine.the_engine->tap_notify == NULL) {
979        mock_engine.me.tap_notify = NULL;
980    }
981    if (mock_engine.the_engine->get_tap_iterator == NULL) {
982        mock_engine.me.get_tap_iterator = NULL;
983    }
984    if (mock_engine.the_engine->errinfo == NULL) {
985        mock_engine.me.errinfo = NULL;
986    }
987
988    return &mock_engine.me;
989}
990
991static void destroy_engine(bool force) {
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