summaryrefslogtreecommitdiff
path: root/testsuite/basic_engine_testsuite.c
blob: 65541a34058b5872c032c01fcbbd4d456da3bddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#undef NDEBUG
#include "config.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "basic_engine_testsuite.h"

struct test_harness test_harness;

/*
 * Make sure that get_info returns something and that repeated calls to it
 * return the same something.
 */
static enum test_result get_info_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    const engine_info *info = h1->get_info(h);
    assert(info != NULL);
    assert(info == h1->get_info(h));
    return SUCCESS;
}

/*
 * Make sure that the structure returned by get_info has a non-null description.
 */
static enum test_result get_info_description_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    const engine_info *info = h1->get_info(h);
    assert(info->description != NULL);
    return SUCCESS;
}

/*
 * Make sure that the structure returned by get_info has a valid number of
 * features and that the size of the feautes array equals that value
 */
static enum test_result get_info_features_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    const engine_info *info = h1->get_info(h);
    uint32_t nfeats = info->num_features;
    assert (nfeats > 0);
    const feature_info *fi = info->features;
    while (nfeats-- > 0) {
        assert(fi++ != NULL);
    }

    return SUCCESS;
}

/*
 * Make sure we can successfully allocate an item, allocate op returns success
 * and that item struct is populated
 */
static enum test_result allocate_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    void *key = "akey";
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1,1,1) == ENGINE_SUCCESS);
    assert(test_item != NULL);
    h1->release(h,NULL,test_item);
    return SUCCESS;
}

/*
 * Verify set behavior
 */
static enum test_result set_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *it;
    void *key = "key";
    assert(h1->allocate(h, NULL, &it, key,
                        strlen(key), 1, 1, 0) == ENGINE_SUCCESS);

    uint64_t prev_cas;
    uint64_t cas = 0;

    for (int ii = 0; ii < 10; ++ii) {
        prev_cas = cas;
        assert(h1->store(h, NULL, it, &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
        assert(cas != prev_cas);
    }
    h1->release(h, NULL, it);
    return SUCCESS;
}

/*
 * Verify add behavior
 */
static enum test_result add_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *it;
    void *key = "key";
    assert(h1->allocate(h, NULL, &it, key,
                        strlen(key), 1, 1, 0) == ENGINE_SUCCESS);
    uint64_t cas;

    for (int ii = 0; ii < 10; ++ii) {
        ENGINE_ERROR_CODE ret = h1->store(h, NULL, it, &cas, OPERATION_ADD, 0);
        if (ii == 0) {
            assert(ret == ENGINE_SUCCESS);
            assert(cas != 0);
        } else {
            assert(ret == ENGINE_NOT_STORED);
        }
    }
    h1->release(h, NULL, it);
    return SUCCESS;
}

/*
 * Verify replace behavior
 */
static enum test_result replace_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *it;
    void *key = "key";
    assert(set_test(h, h1) == SUCCESS);

    assert(h1->allocate(h, NULL, &it, key,
                        strlen(key), sizeof(int), 1, 0) == ENGINE_SUCCESS);
    item_info item_info = { .nvalue = 1 };
    assert(h1->get_item_info(h, NULL, it, &item_info) == true);

    uint64_t prev_cas;
    uint64_t cas = 0;

    for (int ii = 0; ii < 10; ++ii) {
        prev_cas = cas;
        *(int*)(item_info.value[0].iov_base) = ii;
        assert(h1->store(h, NULL, it, &cas, OPERATION_REPLACE,0) == ENGINE_SUCCESS);
        assert(cas != prev_cas);
    }
    h1->release(h, NULL, it);

    assert(h1->get(h, NULL, &it, key, strlen(key), 0) == ENGINE_SUCCESS);
    assert(h1->get_item_info(h, NULL, it, &item_info) == true);
    assert(item_info.value[0].iov_len == sizeof(int));
    assert(*(int*)(item_info.value[0].iov_base) == 9);
    h1->release(h, NULL, it);

    return SUCCESS;
}

/*
 * Verify append behavior
 */
static enum test_result append_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *it;
    void *key = "key";
    uint64_t cas;

    assert(h1->allocate(h, NULL, &it, key,
                        strlen(key), 5, 1, 0) == ENGINE_SUCCESS);
    item_info item_info = { .nvalue = 1 };
    assert(h1->get_item_info(h, NULL, it, &item_info) == true);
    memcpy(item_info.value[0].iov_base, "HELLO", 5);
    assert(h1->store(h, NULL, it, &cas, OPERATION_SET, 0) == ENGINE_SUCCESS);
    h1->release(h, NULL, it);
    assert(h1->allocate(h, NULL, &it, key,
                        strlen(key), 6, 1, 0) == ENGINE_SUCCESS);
    item_info.nvalue = 1;
    assert(h1->get_item_info(h, NULL, it, &item_info) == true);
    memcpy(item_info.value[0].iov_base, " WORLD", 6);
    assert(h1->store(h, NULL, it, &cas, OPERATION_APPEND, 0) == ENGINE_SUCCESS);
    h1->release(h, NULL, it);

    assert(h1->get(h, NULL, &it, key, strlen(key), 0) == ENGINE_SUCCESS);
    assert(h1->get_item_info(h, NULL, it, &item_info) == true);
    assert(item_info.value[0].iov_len == 11);
    assert(memcmp(item_info.value[0].iov_base, "HELLO WORLD", 11) == 0);
    h1->release(h, NULL, it);

    return SUCCESS;
}

/*
 * Verify prepend behavior
 */
static enum test_result prepend_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *it;
    void *key = "key";
    uint64_t cas;

    assert(h1->allocate(h, NULL, &it, key,
                        strlen(key), 5, 1, 0) == ENGINE_SUCCESS);
    item_info item_info = { .nvalue = 1 };
    assert(h1->get_item_info(h, NULL, it, &item_info) == true);
    memcpy(item_info.value[0].iov_base, "HELLO", 5);
    assert(h1->store(h, NULL, it, &cas, OPERATION_SET, 0) == ENGINE_SUCCESS);
    h1->release(h, NULL, it);
    assert(h1->allocate(h, NULL, &it, key,
                        strlen(key), 6, 1, 0) == ENGINE_SUCCESS);
    item_info.nvalue = 1;
    assert(h1->get_item_info(h, NULL, it, &item_info) == true);
    memcpy(item_info.value[0].iov_base, " WORLD", 6);
    assert(h1->store(h, NULL, it, &cas, OPERATION_PREPEND, 0) == ENGINE_SUCCESS);
    h1->release(h, NULL, it);

    assert(h1->get(h, NULL, &it, key, strlen(key), 0) == ENGINE_SUCCESS);
    assert(h1->get_item_info(h, NULL, it, &item_info) == true);
    assert(item_info.value[0].iov_len == 11);
    assert(memcmp(item_info.value[0].iov_base, " WORLDHELLO", 11) == 0);
    h1->release(h, NULL, it);

    return SUCCESS;
}

/*
 * Make sure when we can successfully store an item after it has been allocated
 * and that the cas for the stored item has been generated.
 */
static enum test_result store_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    void *key = "bkey";
    uint64_t cas = 0;
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1,1,1) == ENGINE_SUCCESS);
    assert(h1->store(h, NULL, test_item, &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
    assert(cas != 0);
    h1->release(h,NULL,test_item);
    return SUCCESS;
}

/*
 * Make sure when we can successfully retrieve an item that has been stored in
 * the engine
 */
static enum test_result get_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    item *test_item_get = NULL;
    void *key = "get_test_key";
    uint64_t cas = 0;
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1,0, 0) == ENGINE_SUCCESS);
    assert(h1->store(h, NULL, test_item, &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
    assert(h1->get(h,NULL,&test_item_get,key,strlen(key),0) == ENGINE_SUCCESS);
    h1->release(h,NULL,test_item);
    h1->release(h,NULL,test_item_get);
    return SUCCESS;
}

static enum test_result expiry_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    item *test_item_get = NULL;
    void *key = "get_test_key";
    uint64_t cas = 0;
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1, 0, 10) == ENGINE_SUCCESS);
    assert(h1->store(h, NULL, test_item, &cas, OPERATION_SET, 0) == ENGINE_SUCCESS);
    test_harness.time_travel(11);
    assert(h1->get(h,NULL,&test_item_get,key,strlen(key),0) == ENGINE_KEY_ENOENT);
    h1->release(h,NULL,test_item);
    return SUCCESS;
}

/*
 * Make sure that we can release an item. For the most part all this test does
 * is ensure that thinds dont go splat when we call release. It does nothing to
 * ensure that release did much of anything.
 */
static enum test_result release_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    void *key = "release_test_key";
    uint64_t cas = 0;
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1, 0, 0) == ENGINE_SUCCESS);
    assert(h1->store(h, NULL, test_item, &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
    h1->release(h, NULL, test_item);
    return SUCCESS;
}

/*
 * Make sure that we can remove an item and that after the item has been
 * removed it can not be retrieved.
 */
static enum test_result remove_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    void *key = "remove_test_key";
    uint64_t cas = 0;
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1,0, 0) == ENGINE_SUCCESS);
    assert(h1->store(h, NULL, test_item, &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
    assert(h1->remove(h, NULL, key, strlen(key), cas, 0) == ENGINE_SUCCESS);
    item *check_item = test_item;
    assert(h1->get(h, NULL, &check_item, key, strlen(key), 0) ==  ENGINE_KEY_ENOENT);
    assert(check_item == NULL);
    h1->release(h, NULL, test_item);
    return SUCCESS;
}

/*
 * Make sure we can arithmetic operations to set the initial value of a key and
 * to then later increment that value
 */
static enum test_result incr_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    void *key = "incr_test_key";
    uint64_t cas = 0;
    uint64_t res = 0;
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1, 0, 0) == ENGINE_SUCCESS);
    assert(h1->arithmetic(h, NULL, key, strlen(key), true, true, 0, 1,
           0, &cas, &res, 0 ) == ENGINE_SUCCESS);
    assert(res == 1);
    assert(h1->arithmetic(h, NULL, key, strlen(key), true, false, 1, 0,
           0, &cas, &res, 0 ) == ENGINE_SUCCESS);
    assert(res == 2);
    h1->release(h, NULL, test_item);
    return SUCCESS;
}

static void *incr_test_main(void *arg) {
    ENGINE_HANDLE *h = arg;
    ENGINE_HANDLE_V1 *h1 = arg;
    void *key = "incr_test_key";
    uint64_t cas = 0;
    uint64_t res = 0;

    for (int ii = 0; ii < 1000; ++ii) {
        assert(h1->arithmetic(h, NULL, key, strlen(key), false, false, 1, 0,
                              0, &cas, &res, 0 ) == ENGINE_SUCCESS);

    }

    return NULL;
}


/*
 * Make sure we can arithmetic operations to set the initial value of a key and
 * to then later increment that value
 */
static enum test_result mt_incr_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
#ifdef __arm__
    const int max_threads = 1;
#else
    const int max_threads = 30;
#endif
    pthread_t tid[max_threads];

    if (max_threads < 2) {
        return SKIPPED;
    }

    item *test_item = NULL;
    void *key = "incr_test_key";
    uint64_t cas = 0;
    uint64_t res = 0;
    assert(h1->allocate(h, NULL, &test_item, key,
                        strlen(key), 1, 0, 0) == ENGINE_SUCCESS);
    assert(h1->arithmetic(h, NULL, key, strlen(key), true, true, 0, 1,
                          0, &cas, &res, 0 ) == ENGINE_SUCCESS);
    h1->release(h, NULL, test_item);

    for (int ii = 0; ii < max_threads; ++ii) {
        assert(pthread_create(&tid[ii], NULL, incr_test_main, h) == 0);
    }

    for (int ii = 0; ii < max_threads; ++ii) {
        void *ret;
        assert(pthread_join(tid[ii], &ret) == 0);
        assert(ret == NULL);
    }

    return SUCCESS;
}

/*
 * Make sure we can arithmetic operations to set the initial value of a key and
 * to then later decrement that value
 */
static enum test_result decr_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    void *key = "decr_test_key";
    uint64_t cas = 0;
    uint64_t res = 0;
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1,0, 0) == ENGINE_SUCCESS);
    assert(h1->arithmetic(h, NULL, key, strlen(key), false, true, 0, 1,
           0, &cas, &res, 0 ) == ENGINE_SUCCESS);
    assert(res == 1);
    assert(h1->arithmetic(h, NULL, key, strlen(key), false, false, 1, 0,
           0, &cas, &res, 0 ) == ENGINE_SUCCESS);
    assert(res == 0);
    h1->release(h, NULL, test_item);
    return SUCCESS;
}

/*
 * Make sure we can successfully perform a flush operation and that any item
 * stored before the flush can not be retrieved
 */
static enum test_result flush_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    void *key = "flush_test_key";
    uint64_t cas = 0;
    test_harness.time_travel(3);
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1, 0, 0) == ENGINE_SUCCESS);
    assert(h1->store(h, NULL, test_item, &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
    assert(h1->flush(h, NULL, 0) == ENGINE_SUCCESS);
    item *check_item = test_item;
    assert(h1->get(h, NULL, &check_item, key, strlen(key), 0) ==  ENGINE_KEY_ENOENT);
    assert(check_item == NULL);
    h1->release(h, NULL, test_item);
    return SUCCESS;
}

/*
 * Make sure we can successfully retrieve the item info struct for an item and
 * that the contents of the item_info are as expected.
 */
static enum test_result get_item_info_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    char *key = "get_item_info_test_key";
    uint64_t cas = 0;
    const rel_time_t exp = 1;
    item_info ii = { .nvalue = 1 };
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1,0, exp) == ENGINE_SUCCESS);
    assert(h1->store(h, NULL, test_item, &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
    /* Had this been actual code, there'd be a connection here */
    assert(h1->get_item_info(h, NULL, test_item, &ii) == true);
    assert(ii.cas == cas);
    assert(ii.flags == 0);
    assert(strcmp(key,ii.key) == 0);
    assert(ii.nkey == strlen(key));
    assert(ii.nbytes == 1);
    assert(ii.exptime == exp);
    h1->release(h, NULL, test_item);
    return SUCCESS;
}

static enum test_result item_set_cas_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    item *test_item = NULL;
    char *key = "item_set_cas_test_key";
    uint64_t cas = 0;
    const rel_time_t exp = 1;
    item_info ii = { .nvalue = 1 };
    assert(h1->allocate(h, NULL, &test_item, key, strlen(key), 1,0, exp) == ENGINE_SUCCESS);
    assert(h1->store(h, NULL, test_item, &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
    uint64_t newcas = cas + 1;
    h1->item_set_cas(h, NULL, test_item, newcas);
    assert(h1->get_item_info(h, NULL, test_item, &ii) == true);
    assert(ii.cas == newcas);
    h1->release(h, NULL, test_item);
    return SUCCESS;
}

uint32_t evictions;
static void eviction_stats_handler(const char *key, const uint16_t klen,
                                   const char *val, const uint32_t vlen,
                                   const void *cookie) {
    if (memcmp(key, "evictions", klen) == 0) {
        char buffer[vlen + 1];
        memcpy(buffer, val, vlen);
        buffer[vlen] = '\0';
        evictions = atoi(buffer);
    }
}

static enum test_result lru_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    bool supported = false;
    const engine_info *info = h1->get_info(h);
    if (info != NULL) {
        uint32_t nfeats = info->num_features;
        assert (nfeats > 0);
        const feature_info *fi = info->features;
        while (nfeats-- > 0) {
            if (fi->feature == ENGINE_FEATURE_LRU) {
                supported = true;
                break;
            }
        }
    }

    if (!supported) {
        return SKIPPED;
    }

    item *test_item = NULL;
    const char *hot_key = "hot_key";
    uint64_t cas = 0;
    assert(h1->allocate(h, NULL, &test_item,
                        hot_key, strlen(hot_key), 4096, 0, 0) == ENGINE_SUCCESS);
    assert(h1->store(h, NULL, test_item,
                     &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
    h1->release(h, NULL, test_item);

    int ii;
    for (ii = 0; ii < 250; ++ii) {
        assert(h1->get(h, NULL, &test_item,
                       hot_key, strlen(hot_key), 0) ==  ENGINE_SUCCESS);
        h1->release(h, NULL, test_item);
        char key[1024];
        size_t keylen = snprintf(key, sizeof(key), "lru_test_key_%08d", ii);
        assert(h1->allocate(h, NULL, &test_item,
                            key, keylen, 4096, 0, 0) == ENGINE_SUCCESS);
        assert(h1->store(h, NULL, test_item,
                         &cas, OPERATION_SET,0) == ENGINE_SUCCESS);
        h1->release(h, NULL, test_item);
        assert(h1->get_stats(h, NULL, NULL, 0,
                             eviction_stats_handler) == ENGINE_SUCCESS);
        if (evictions == 2) {
            break;
        }
    }

    assert(ii < 250);
    for (int jj = 0; jj <= ii; ++jj) {
        char key[1024];
        size_t keylen = snprintf(key, sizeof(key), "lru_test_key_%08d", jj);
        if (jj == 0 || jj == 1) {
            assert(h1->get(h, NULL, &test_item,
                           key, keylen, 0) == ENGINE_KEY_ENOENT);
        } else {
            assert(h1->get(h, NULL, &test_item,
                           key, keylen, 0) == ENGINE_SUCCESS);
            assert(test_item != NULL);
            h1->release(h, NULL, test_item);
        }
    }
    return SUCCESS;
}

static enum test_result get_stats_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    return PENDING;
}

static enum test_result reset_stats_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    return PENDING;
}

static enum test_result get_stats_struct_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    return PENDING;
}

static enum test_result aggregate_stats_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    return PENDING;
}

static protocol_binary_response_header *last_response;

static void release_last_response(void) {
    free(last_response);
    last_response = NULL;
}

static bool response_handler(const void *key, uint16_t keylen,
                             const void *ext, uint8_t extlen,
                             const void *body, uint32_t bodylen,
                             uint8_t datatype, uint16_t status,
                             uint64_t cas, const void *cookie)
{
    assert(last_response == NULL);
    last_response = malloc(sizeof(*last_response) + keylen + extlen + bodylen);
    if (last_response == NULL) {
        return false;
    }
    protocol_binary_response_header *r = last_response;
    r->response.magic = PROTOCOL_BINARY_RES;
    r->response.opcode = 0xff; // we don't know this!
    r->response.keylen = htons(keylen);
    r->response.extlen = extlen;
    r->response.datatype = PROTOCOL_BINARY_RAW_BYTES;
    r->response.status = htons(status);
    r->response.bodylen = htonl(keylen + extlen + bodylen);
    r->response.opaque = 0xffffff; // we don't know this
    r->response.cas = cas;
    char *ptr = (void*)(r + 1);
    memcpy(ptr, ext, extlen);
    ptr += extlen;
    memcpy(ptr, key, keylen);
    ptr += keylen;
    memcpy(ptr, body, bodylen);

    return true;
}

static enum test_result touch_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    union request {
        protocol_binary_request_touch touch;
        char buffer[512];
    };

    void *key = "get_test_key";
    size_t keylen = strlen(key);
    union request r = {
        .touch = {
            .message = {
                .header.request = {
                    .magic = PROTOCOL_BINARY_REQ,
                    .opcode = PROTOCOL_BINARY_CMD_TOUCH,
                    .keylen = htons((uint16_t)keylen),
                    .extlen = 4,
                    .datatype = PROTOCOL_BINARY_RAW_BYTES,
                    .vbucket = 0,
                    .bodylen = htonl(keylen + 4),
                    .opaque = 0xdeadbeef,
                    .cas = 0
                },
                .body = {
                    .expiration = htonl(10)
                }
            }
        }
    };

    memcpy(r.buffer + sizeof(r.touch.bytes), key, keylen);
    ENGINE_ERROR_CODE ret;
    ret = h1->unknown_command(h, NULL, &r.touch.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT);
    assert(last_response->response.keylen == 0);
    assert(last_response->response.extlen == 0);
    assert(last_response->response.bodylen == 0);
    release_last_response();

    // store and get a key
    assert(get_test(h, h1) == SUCCESS);

    // Set expiry time to 10 secs..
    ret = h1->unknown_command(h, NULL, &r.touch.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_SUCCESS);
    assert(last_response->response.keylen == 0);
    assert(last_response->response.extlen == 0);
    assert(last_response->response.bodylen == 0);
    release_last_response();

    // time-travel 11 secs..
    test_harness.time_travel(11);

    // The item should have expired now...
    item *item = NULL;
    assert(h1->get(h, NULL, &item, key, keylen, 0) == ENGINE_KEY_ENOENT);

    // Verify that it doesn't accept bogus packets. extlen is mandatory
    r.touch.message.header.request.extlen = 0;
    r.touch.message.header.request.bodylen = htonl(keylen);
    ret = h1->unknown_command(h, NULL, &r.touch.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_EINVAL);
    release_last_response();

    // key is mandatory!
    r.touch.message.header.request.extlen = 4;
    r.touch.message.header.request.keylen = 0;
    r.touch.message.header.request.bodylen = htonl(4);
    ret = h1->unknown_command(h, NULL, &r.touch.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_EINVAL);
    release_last_response();

    return SUCCESS;
}

static enum test_result gat_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    union request {
        protocol_binary_request_gat gat;
        char buffer[512];
    };

    void *key = "get_test_key";
    size_t keylen = strlen(key);
    union request r = {
        .gat = {
            .message = {
                .header.request = {
                    .magic = PROTOCOL_BINARY_REQ,
                    .opcode = PROTOCOL_BINARY_CMD_GAT,
                    .keylen = htons((uint16_t)keylen),
                    .extlen = 4,
                    .datatype = PROTOCOL_BINARY_RAW_BYTES,
                    .vbucket = 0,
                    .bodylen = htonl(keylen + 4),
                    .opaque = 0xdeadbeef,
                    .cas = 0
                },
                .body = {
                    .expiration = htonl(10)
                }
            }
        }
    };

    memcpy(r.buffer + sizeof(r.gat.bytes), key, keylen);
    ENGINE_ERROR_CODE ret;
    ret = h1->unknown_command(h, NULL, &r.gat.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_KEY_ENOENT);
    assert(last_response->response.keylen == 0);
    assert(last_response->response.extlen == 0);
    assert(last_response->response.bodylen == 0);
    release_last_response();

    // store and get a key
    assert(get_test(h, h1) == SUCCESS);

    // Set expiry time to 10 secs..
    ret = h1->unknown_command(h, NULL, &r.gat.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_SUCCESS);
    assert(last_response->response.keylen == 0);
    assert(last_response->response.extlen == 4);
    assert(ntohl(last_response->response.bodylen) == 5); // get_test sets 1 byte datalen
    release_last_response();

    // time-travel 11 secs..
    test_harness.time_travel(11);

    // The item should have expired now...
    item *item = NULL;
    assert(h1->get(h, NULL, &item, key, keylen, 0) == ENGINE_KEY_ENOENT);

    // Verify that it doesn't accept bogus packets. extlen is mandatory
    r.gat.message.header.request.extlen = 0;
    r.gat.message.header.request.bodylen = htonl(keylen);
    ret = h1->unknown_command(h, NULL, &r.gat.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_EINVAL);
    release_last_response();

    // key is mandatory!
    r.gat.message.header.request.extlen = 4;
    r.gat.message.header.request.keylen = 0;
    r.gat.message.header.request.bodylen = htonl(4);
    ret = h1->unknown_command(h, NULL, &r.gat.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_EINVAL);
    release_last_response();

    return SUCCESS;
}

static enum test_result gatq_test(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1) {
    union request {
        protocol_binary_request_gat gat;
        char buffer[512];
    };

    void *key = "get_test_key";
    size_t keylen = strlen(key);
    union request r = {
        .gat = {
            .message = {
                .header.request = {
                    .magic = PROTOCOL_BINARY_REQ,
                    .opcode = PROTOCOL_BINARY_CMD_GATQ,
                    .keylen = htons((uint16_t)keylen),
                    .extlen = 4,
                    .datatype = PROTOCOL_BINARY_RAW_BYTES,
                    .vbucket = 0,
                    .bodylen = htonl(keylen + 4),
                    .opaque = 0xdeadbeef,
                    .cas = 0
                },
                .body = {
                    .expiration = htonl(10)
                }
            }
        }
    };

    memcpy(r.buffer + sizeof(r.gat.bytes), key, keylen);
    ENGINE_ERROR_CODE ret;
    ret = h1->unknown_command(h, NULL, &r.gat.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);

    // GATQ is quiet and should not produce any result
    assert(last_response == NULL);

    // store and get a key
    assert(get_test(h, h1) == SUCCESS);

    // Set expiry time to 10 secs..
    ret = h1->unknown_command(h, NULL, &r.gat.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_SUCCESS);
    assert(last_response->response.keylen == 0);
    assert(last_response->response.extlen == 4);
    assert(ntohl(last_response->response.bodylen) == 5); // get_test sets 1 byte datalen
    release_last_response();

    // time-travel 11 secs..
    test_harness.time_travel(11);

    // The item should have expired now...
    item *item = NULL;
    assert(h1->get(h, NULL, &item, key, keylen, 0) == ENGINE_KEY_ENOENT);

    // Verify that it doesn't accept bogus packets. extlen is mandatory
    r.gat.message.header.request.extlen = 0;
    r.gat.message.header.request.bodylen = htonl(keylen);
    ret = h1->unknown_command(h, NULL, &r.gat.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_EINVAL);
    release_last_response();

    // key is mandatory!
    r.gat.message.header.request.extlen = 4;
    r.gat.message.header.request.keylen = 0;
    r.gat.message.header.request.bodylen = htonl(4);
    ret = h1->unknown_command(h, NULL, &r.gat.message.header, response_handler);
    assert(ret == ENGINE_SUCCESS);
    assert(last_response != NULL);
    assert(ntohs(last_response->response.status) == PROTOCOL_BINARY_RESPONSE_EINVAL);
    release_last_response();

    return SUCCESS;
}

MEMCACHED_PUBLIC_API
engine_test_t* get_tests(void) {
    static engine_test_t tests[]  = {
        {"get info test", get_info_test, NULL, NULL, NULL},
        {"get info description test", get_info_description_test, NULL, NULL, NULL},
        {"get info features test", get_info_features_test, NULL, NULL, NULL},
        {"allocate test", allocate_test, NULL, NULL, NULL},
        {"set test", set_test, NULL, NULL, NULL},
        {"add test", add_test, NULL, NULL, NULL},
        {"replace test", replace_test, NULL, NULL, NULL},
        {"append test", append_test, NULL, NULL, NULL},
        {"prepend test", prepend_test, NULL, NULL, NULL},
        {"store test", store_test, NULL, NULL, NULL},
        {"get test", get_test, NULL, NULL, NULL},
        {"expiry test", expiry_test, NULL, NULL, NULL},
        {"remove test", remove_test, NULL, NULL, NULL},
        {"release test", release_test, NULL, NULL, NULL},
        {"incr test", incr_test, NULL, NULL, NULL},
        {"mt incr test", mt_incr_test, NULL, NULL, NULL},
        {"decr test", decr_test, NULL, NULL, NULL},
        {"flush test", flush_test, NULL, NULL, NULL},
        {"get item info test", get_item_info_test, NULL, NULL, NULL},
        {"set cas test", item_set_cas_test, NULL, NULL, NULL},
        {"LRU test", lru_test, NULL, NULL, "cache_size=48"},
        {"get stats test", get_stats_test, NULL, NULL, NULL},
        {"reset stats test", reset_stats_test, NULL, NULL, NULL},
        {"get stats struct test", get_stats_struct_test, NULL, NULL, NULL},
        {"aggregate stats test", aggregate_stats_test, NULL, NULL, NULL},
        {"touch", touch_test, NULL, NULL, NULL},
        {"Get And Touch", gat_test, NULL, NULL, NULL},
        {"Get And Touch Quiet", gatq_test, NULL, NULL, NULL},
        {NULL, NULL, NULL, NULL, NULL}
    };
    return tests;
}

MEMCACHED_PUBLIC_API
bool setup_suite(struct test_harness *th) {
    test_harness = *th;
    return true;
}