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    if (handle_v1) {
992        handle_v1->destroy(handle, force);
993        handle_v1 = NULL;
994        handle = NULL;
995    }
996}
997
998static void reload_engine(ENGINE_HANDLE **h, ENGINE_HANDLE_V1 **h1,
999                          const char* engine, const char *cfg, bool init, bool force) {
1000    destroy_engine(force);
1001    handle_v1 = start_your_engines(engine, cfg, init);
1002    handle = (ENGINE_HANDLE*)(handle_v1);
1003    *h1 = handle_v1;
1004    *h = handle;
1005}
1006
1007static engine_test_t* current_testcase;
1008
1009static const engine_test_t* get_current_testcase(void)
1010{
1011    return current_testcase;
1012}
1013
1014static int execute_test(engine_test_t test,
1015                        const char *engine,
1016                        const char *default_cfg)
1017{
1018    enum test_result ret = PENDING;
1019    if (test.tfun != NULL) {
1020        current_testcase = &test;
1021        if (test.prepare != NULL) {
1022            if ((ret = test.prepare(&test)) == SUCCESS) {
1023                ret = PENDING;
1024            }
1025        }
1026
1027        if (ret == PENDING) {
1028            /* Start the engines and go */
1029            start_your_engines(engine, test.cfg ? test.cfg : default_cfg, true);
1030            if (test.test_setup != NULL) {
1031                if (!test.test_setup(handle, handle_v1)) {
1032                    fprintf(stderr, "Failed to run setup for test %s\n", test.name);
1033                    return FAIL;
1034                }
1035            }
1036            ret = test.tfun(handle, handle_v1);
1037            if (test.test_teardown != NULL) {
1038                if (!test.test_teardown(handle, handle_v1)) {
1039                    fprintf(stderr, "WARNING: Failed to run teardown for test %s\n", test.name);
1040                }
1041            }
1042            destroy_engine(false);
1043
1044            if (test.cleanup) {
1045                test.cleanup(&test, ret);
1046            }
1047        }
1048    }
1049
1050    return (int)ret;
1051}
1052
1053static void setup_alarm_handler() {
1054#ifndef WIN32
1055    struct sigaction sig_handler;
1056
1057    sig_handler.sa_handler = alarm_handler;
1058    sig_handler.sa_flags = 0;
1059
1060    sigaction(SIGALRM, &sig_handler, NULL);
1061#endif
1062}
1063
1064static void set_test_timeout(int timeout) {
1065#ifndef WIN32
1066    alarm(timeout);
1067#endif
1068}
1069
1070static void clear_test_timeout() {
1071#ifndef WIN32
1072    alarm(0);
1073    alarmed = 0;
1074#endif
1075}
1076
1077static int safe_append(char *buffer, const char *txt) {
1078    int len = 0;
1079
1080    /*
1081     * We should probably make this a bit safer (by
1082     * checking if its already escaped etc, but I'll
1083     * do that whenever it turns out to be a problem ;-)
1084     */
1085    while (*txt) {
1086        switch (*txt) {
1087#ifndef WIN32
1088        case ' ':
1089        case '\\':
1090        case ';':
1091        case '|':
1092        case '&':
1093            buffer[len++] = '\\';
1094#endif
1095        default:
1096            buffer[len++] = *txt;
1097        }
1098        ++txt;
1099    }
1100
1101    buffer[len++] = ' ';
1102
1103    return len;
1104}
1105
1106int main(int argc, char **argv) {
1107    int c, exitcode = 0, num_cases = 0, timeout = 0, loop_count = 0;
1108    bool verbose = false;
1109    bool quiet = false;
1110    bool dot = false;
1111    bool loop = false;
1112    bool terminate_on_error = false;
1113    const char *engine = NULL;
1114    const char *engine_args = NULL;
1115    const char *test_suite = NULL;
1116    const char *test_case = NULL;
1117    engine_test_t *testcases = NULL;
1118    cb_dlhandle_t handle;
1119    char *errmsg;
1120    void *symbol;
1121    struct test_harness harness;
1122    int test_case_id = -1;
1123    char *cmdline = malloc(64*1024); /* should be enough */
1124
1125    /* Hack to remove the warning from C99 */
1126    union {
1127        GET_TESTS get_tests;
1128        void* voidptr;
1129    } my_get_test;
1130
1131    /* Hack to remove the warning from C99 */
1132    union {
1133        SETUP_SUITE setup_suite;
1134        void* voidptr;
1135    } my_setup_suite;
1136
1137    /* Hack to remove the warning from C99 */
1138    union {
1139        TEARDOWN_SUITE teardown_suite;
1140        void* voidptr;
1141    } my_teardown_suite;
1142
1143    cb_initialize_sockets();
1144
1145    if (cmdline == NULL) {
1146        fprintf(stderr, "Failed to allocate memory");
1147        exit(EXIT_FAILURE);
1148    }
1149
1150    memset(&my_get_test, 0, sizeof(my_get_test));
1151    memset(&my_setup_suite, 0, sizeof(my_setup_suite));
1152    memset(&my_teardown_suite, 0, sizeof(my_teardown_suite));
1153
1154    logger_descriptor = get_null_logger();
1155    color_enabled = getenv("TESTAPP_ENABLE_COLOR") != NULL;
1156
1157    /* Use unbuffered stdio */
1158    setbuf(stdout, NULL);
1159    setbuf(stderr, NULL);
1160
1161    setup_alarm_handler();
1162
1163    /* process arguments */
1164    while ((c = getopt(argc, argv,
1165                       "h"  /* usage */
1166                       "E:" /* Engine to load */
1167                       "e:" /* Engine options */
1168                       "T:" /* Library with tests to load */
1169                       "t:" /* Timeout */
1170                       "L"  /* Loop until failure */
1171                       "q"  /* Be more quiet (only report failures) */
1172                       "."  /* dot mode. */
1173                       "n:"  /* test case to run */
1174                       "v" /* verbose output */
1175                       "Z"  /* Terminate on first error */
1176                       "C:" /* Test case id */
1177                       "s" /* spinlock the program */
1178                       )) != -1) {
1179        switch (c) {
1180        case 's' : {
1181            int spin = 1;
1182            while (spin) {
1183
1184            }
1185            break;
1186        }
1187        case 'C' :
1188            test_case_id = atoi(optarg);
1189            break;
1190        case 'E':
1191            engine = optarg;
1192            break;
1193        case 'e':
1194            engine_args = optarg;
1195            break;
1196        case 'h':
1197            usage();
1198            return 0;
1199        case 'T':
1200            test_suite = optarg;
1201            break;
1202        case 't':
1203            timeout = atoi(optarg);
1204            break;
1205        case 'L':
1206            loop = true;
1207            break;
1208        case 'n':
1209            test_case = optarg;
1210            break;
1211        case 'v' :
1212            verbose = true;
1213            break;
1214        case 'q':
1215            quiet = true;
1216            break;
1217        case '.':
1218            dot = true;
1219            break;
1220        case 'Z' :
1221            terminate_on_error = true;
1222            break;
1223        default:
1224            fprintf(stderr, "Illegal argument \"%c\"\n", c);
1225            return 1;
1226        }
1227    }
1228
1229    /* validate args */
1230    if (engine == NULL) {
1231        fprintf(stderr, "You must provide a path to the storage engine library.\n");
1232        return 1;
1233    }
1234
1235    if (test_suite == NULL) {
1236        fprintf(stderr, "You must provide a path to the testsuite library.\n");
1237        return 1;
1238    }
1239
1240    /* load test_suite */
1241    handle = cb_dlopen(test_suite, &errmsg);
1242    if (handle == NULL) {
1243        fprintf(stderr, "Failed to load testsuite %s: %s\n", test_suite,
1244                errmsg);
1245        free(errmsg);
1246        return 1;
1247    }
1248
1249    /* get the test cases */
1250    symbol = cb_dlsym(handle, "get_tests", &errmsg);
1251    if (symbol == NULL) {
1252        fprintf(stderr, "Could not find get_tests function in testsuite %s: %s\n", test_suite, errmsg);
1253        free(errmsg);
1254        return 1;
1255    }
1256    my_get_test.voidptr = symbol;
1257    testcases = (*my_get_test.get_tests)();
1258
1259    /* set up the suite if needed */
1260    memset(&harness, 0, sizeof(harness));
1261    harness.default_engine_cfg = engine_args;
1262    harness.engine_path = engine;
1263    harness.reload_engine = reload_engine;
1264    harness.start_engine = start_your_engines;
1265    harness.create_cookie = create_mock_cookie;
1266    harness.destroy_cookie = destroy_mock_cookie;
1267    harness.set_ewouldblock_handling = mock_set_ewouldblock_handling;
1268    harness.lock_cookie = lock_mock_cookie;
1269    harness.unlock_cookie = unlock_mock_cookie;
1270    harness.waitfor_cookie = waitfor_mock_cookie;
1271    harness.time_travel = mock_time_travel;
1272    harness.get_current_testcase = get_current_testcase;
1273
1274    for (num_cases = 0; testcases[num_cases].name; num_cases++) {
1275        /* Just counting */
1276    }
1277
1278    symbol = cb_dlsym(handle, "setup_suite", &errmsg);
1279    if (symbol == NULL) {
1280        free(errmsg);
1281    } else {
1282        my_setup_suite.voidptr = symbol;
1283        if (!(*my_setup_suite.setup_suite)(&harness)) {
1284            fprintf(stderr, "Failed to set up test suite %s \n", test_suite);
1285            return 1;
1286        }
1287    }
1288
1289    if (test_case_id != -1) {
1290        if (test_case_id >= num_cases) {
1291            fprintf(stderr, "Invalid test case id specified\n");
1292            exit(EXIT_FAILURE);
1293        }
1294        exit(execute_test(testcases[test_case_id], engine, engine_args));
1295    }
1296
1297    do {
1298        int i;
1299        bool need_newline = false;
1300        for (i = 0; testcases[i].name; i++) {
1301            int error;
1302            if (test_case != NULL && strcmp(test_case, testcases[i].name) != 0)
1303                continue;
1304            if (!quiet) {
1305                printf("Running [%04d/%04d]: %s...",
1306                       i + num_cases * loop_count,
1307                       num_cases * (loop_count + 1),
1308                       testcases[i].name);
1309                fflush(stdout);
1310            } else if(dot) {
1311                printf(".");
1312                need_newline = true;
1313                /* Add a newline every few tests */
1314                if ((i+1) % 70 == 0) {
1315                    printf("\n");
1316                    need_newline = false;
1317                }
1318            }
1319            set_test_timeout(timeout);
1320
1321            {
1322                int ii;
1323                int offset = 0;
1324                enum test_result ecode;
1325                time_t start;
1326                time_t stop;
1327                int rc;
1328                for (ii = 0; ii < argc; ++ii) {
1329                    offset += safe_append(cmdline + offset, argv[ii]);
1330                }
1331
1332                sprintf(cmdline + offset, "-C %d", i);
1333
1334                start = time(NULL);
1335                rc = system(cmdline);
1336                stop = time(NULL);
1337
1338#ifdef WIN32
1339                ecode = (enum test_result)rc;
1340#else
1341                if (WIFEXITED(rc)) {
1342                    ecode = (enum test_result)WEXITSTATUS(rc);
1343#ifdef WCOREDUMP
1344                } else if (WIFSIGNALED(rc) && WCOREDUMP(rc)) {
1345                    ecode = CORE;
1346#endif
1347                } else {
1348                    ecode = DIED;
1349                }
1350#endif
1351                error = report_test(testcases[i].name,
1352                                    stop - start,
1353                                    ecode, quiet,
1354                                    !verbose);
1355            }
1356            clear_test_timeout();
1357
1358            if (error != 0) {
1359                ++exitcode;
1360                if (terminate_on_error) {
1361                    exit(EXIT_FAILURE);
1362                }
1363            }
1364        }
1365
1366        if (need_newline) {
1367            printf("\n");
1368        }
1369        ++loop_count;
1370    } while (loop && exitcode == 0);
1371
1372    /* tear down the suite if needed */
1373    symbol = cb_dlsym(handle, "teardown_suite", &errmsg);
1374    if (symbol == NULL) {
1375        free(errmsg);
1376    } else {
1377        my_teardown_suite.voidptr = symbol;
1378        if (!(*my_teardown_suite.teardown_suite)()) {
1379            fprintf(stderr, "Failed to teardown up test suite %s \n", test_suite);
1380        }
1381    }
1382
1383    printf("# Passed %d of %d tests\n", num_cases - exitcode, num_cases);
1384    free(cmdline);
1385
1386    return exitcode;
1387}
1388