aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/transport/transport_end2end_tests.c
blob: ce6fbcf91dad04b586fb6ebcac7b7ec4a357e9ba (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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
/*
 *
 * Copyright 2014, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "test/core/transport/transport_end2end_tests.h"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "src/core/transport/transport.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/string.h>
#include <grpc/support/thd.h>
#include <grpc/support/useful.h>

enum { REQUEST_DEADLINE = 200000 }; /* valgrind need a large value */

static grpc_mdctx *g_metadata_context;

static gpr_once g_pending_ops_init = GPR_ONCE_INIT;
static gpr_mu g_mu;
static gpr_cv g_cv;
static int g_pending_ops;

/* Defines a suite of tests that all GRPC transports should be able to pass */

/******************************************************************************
 * Testing framework
 */

/* Forward declarations */
typedef struct test_fixture test_fixture;

/* User data passed to the transport and handed to each callback */
typedef struct test_user_data { test_fixture *fixture; } test_user_data;

/* A message we expect to receive (forms a singly linked list with next) */
typedef struct expected_message {
  /* The next message expected */
  struct expected_message *next;
  /* The (owned) data that we expect to receive */
  gpr_uint8 *data;
  /* The length of the expected message */
  size_t length;
  /* How many bytes of the expected message have we received? */
  size_t read_pos;
  /* Have we received the GRPC_OP_BEGIN for this message */
  int begun;
} expected_message;

/* Metadata we expect to receive */
typedef struct expected_metadata {
  struct expected_metadata *next;
  struct expected_metadata *prev;
  grpc_mdelem *metadata;
} expected_metadata;

/* Tracks a stream for a test. Forms a doubly-linked list with (prev, next) */
typedef struct test_stream {
  /* The owning fixture */
  test_fixture *fixture;
  /* The transport client stream */
  grpc_stream *client_stream;
  /* The transport server stream */
  grpc_stream *server_stream;
  /* Linked lists of messages expected on client and server */
  expected_message *client_expected_messages;
  expected_message *server_expected_messages;
  expected_metadata *client_expected_metadata;
  expected_metadata *server_expected_metadata;

  /* Test streams are linked in the fixture */
  struct test_stream *next;
  struct test_stream *prev;
} test_stream;

/* A test_fixture tracks all transport state and expectations for a test */
struct test_fixture {
  gpr_mu mu;
  gpr_cv cv; /* broadcast when expectation state has changed */

  /* The transport instances */
  grpc_transport *client_transport;
  grpc_transport *server_transport;
  /* User data for the transport instances - pointers to these are passed
     to the transport. */
  test_user_data client_ud;
  test_user_data server_ud;

  /* A pointer to the head of the tracked streams list, or NULL if no streams
     are open */
  test_stream *streams;
};

static void expect_metadata(test_stream *s, int from_client, const char *key,
                            const char *value);

/* Convert some number of seconds into a gpr_timespec that many seconds in the
   future */
static gpr_timespec deadline_from_seconds(double deadline_seconds) {
  return gpr_time_add(gpr_now(), gpr_time_from_micros(deadline_seconds * 1e6));
}

/* Init a test_user_data instance */
static void init_user_data(test_user_data *ud, test_fixture *f,
                           grpc_transport_test_config *config, int is_client) {
  ud->fixture = f;
}

/* Implements the alloc_recv_buffer transport callback */
static gpr_slice alloc_recv_buffer(void *user_data, grpc_transport *transport,
                                   grpc_stream *stream, size_t size_hint) {
  return gpr_slice_malloc(size_hint);
}

static void pending_ops_cleanup() {
  gpr_mu_destroy(&g_mu);
  gpr_cv_destroy(&g_cv);
}

static void pending_ops_init() {
  gpr_mu_init(&g_mu);
  gpr_cv_init(&g_cv);
  atexit(pending_ops_cleanup);
}

static void use_pending_ops() {
  gpr_once_init(&g_pending_ops_init, pending_ops_init);
}

static void add_pending_op() {
  use_pending_ops();
  gpr_mu_lock(&g_mu);
  g_pending_ops++;
  gpr_mu_unlock(&g_mu);
}

static void end_pending_op() {
  gpr_mu_lock(&g_mu);
  g_pending_ops--;
  gpr_cv_broadcast(&g_cv);
  gpr_mu_unlock(&g_mu);
}

static void wait_pending_ops() {
  use_pending_ops();
  gpr_mu_lock(&g_mu);
  while (g_pending_ops > 0) {
    gpr_cv_wait(&g_cv, &g_mu, gpr_inf_future);
  }
  gpr_mu_unlock(&g_mu);
}

/* Implements the create_stream transport callback */
static void create_stream(void *user_data, grpc_transport *transport,
                          const void *server_data) {
  test_user_data *ud = user_data;
  test_fixture *f = ud->fixture;
  test_stream *stream;

  GPR_ASSERT(ud == &f->server_ud);
  GPR_ASSERT(transport == f->server_transport);

  gpr_mu_lock(&f->mu);

  /* Search streams for the peer to this stream */
  if (!f->streams) goto done;
  /* found the expecting stream */
  stream = f->streams;
  stream->server_stream = gpr_malloc(grpc_transport_stream_size(transport));
  grpc_transport_init_stream(transport, stream->server_stream, server_data);

done:
  /* wakeup begin_stream, and maybe wait_and_verify */
  gpr_cv_broadcast(&f->cv);
  gpr_mu_unlock(&f->mu);
}

/* Search fixture streams for the test_stream instance holding a given transport
   stream */
static test_stream *find_test_stream(test_fixture *f, grpc_stream *stream) {
  test_stream *s;

  GPR_ASSERT(f->streams);
  s = f->streams;
  do {
    if (s->client_stream == stream || s->server_stream == stream) {
      return s;
    }
  } while (s != f->streams);

  GPR_ASSERT(0 && "found");
  return NULL;
}

/* Stringify a grpc_stream_state for debugging */
static const char *state_name(grpc_stream_state state) {
  switch (state) {
    case GRPC_STREAM_OPEN:
      return "GRPC_STREAM_OPEN";
    case GRPC_STREAM_RECV_CLOSED:
      return "GRPC_STREAM_RECV_CLOSED";
    case GRPC_STREAM_SEND_CLOSED:
      return "GRPC_STREAM_SEND_CLOSED";
    case GRPC_STREAM_CLOSED:
      return "GRPC_STREAM_CLOSED";
  }
  GPR_ASSERT(0 && "reachable");
  return NULL;
}

typedef struct {
  grpc_transport *transport;
  grpc_stream *stream;
} destroy_stream_args;

static void destroy_stream(void *p) {
  destroy_stream_args *a = p;
  grpc_transport_destroy_stream(a->transport, a->stream);
  gpr_free(a->stream);
  gpr_free(a);
  end_pending_op();
}

static void recv_batch(void *user_data, grpc_transport *transport,
                       grpc_stream *stream, grpc_stream_op *ops,
                       size_t ops_count, grpc_stream_state final_state) {
  test_user_data *ud = user_data;
  test_fixture *f = ud->fixture;
  test_stream *s;
  /* Pointer to the root pointer of either client or server expected messages;
     not a simple pointer as we may need to manipulate the list (on receipt
     of messages */
  expected_message **expect_root_message;
  expected_metadata **expect_root_metadata;
  expected_metadata *emd;
  size_t i, j;
  char *hexstr1, *hexstr2;
  int repeats = 0;

  gpr_mu_lock(&f->mu);

  s = find_test_stream(f, stream);
  expect_root_message = s->client_stream == stream
                            ? &s->client_expected_messages
                            : &s->server_expected_messages;
  expect_root_metadata = s->client_stream == stream
                             ? &s->client_expected_metadata
                             : &s->server_expected_metadata;

  /* Debug log */
  gpr_log(GPR_DEBUG, "recv_batch: %d ops on %s final_state=%s", ops_count,
          s->client_stream == stream ? "client" : "server",
          state_name(final_state));
#define CLEAR_REPEATS                           \
  if (repeats) {                                \
    gpr_log(GPR_DEBUG, "  + %d more", repeats); \
    repeats = 0;                                \
  }
  for (i = 0; i < ops_count; i++) {
    switch (ops[i].type) {
      case GRPC_NO_OP:
        CLEAR_REPEATS;
        gpr_log(GPR_DEBUG, "  [%02d] GRPC_NO_OP", i);
        break;
      case GRPC_OP_METADATA_BOUNDARY:
        CLEAR_REPEATS;
        gpr_log(GPR_DEBUG, "  [%02d] GRPC_OP_METADATA_BOUNDARY", i);
        break;
      case GRPC_OP_METADATA:
        CLEAR_REPEATS;
        hexstr1 =
            gpr_hexdump(grpc_mdstr_as_c_string(ops[i].data.metadata->key),
                        GPR_SLICE_LENGTH(ops[i].data.metadata->key->slice),
                        GPR_HEXDUMP_PLAINTEXT);
        hexstr2 =
            gpr_hexdump(grpc_mdstr_as_c_string(ops[i].data.metadata->value),
                        GPR_SLICE_LENGTH(ops[i].data.metadata->value->slice),
                        GPR_HEXDUMP_PLAINTEXT);
        gpr_log(GPR_DEBUG, "  [%02d] GRPC_OP_METADATA key=%s value=%s", i,
                hexstr1, hexstr2);
        gpr_free(hexstr1);
        gpr_free(hexstr2);
        break;
      case GRPC_OP_BEGIN_MESSAGE:
        CLEAR_REPEATS;
        gpr_log(GPR_DEBUG, "  [%02d] GRPC_OP_BEGIN_MESSAGE len=%d", i,
                ops[i].data.begin_message.length);
        break;
      case GRPC_OP_DEADLINE:
        CLEAR_REPEATS;
        gpr_log(GPR_DEBUG, "  [%02d] GRPC_OP_DEADLINE value=%d.%09d", i,
                ops[i].data.deadline.tv_sec, ops[i].data.deadline.tv_nsec);
        break;
      case GRPC_OP_SLICE:
        if (i && ops[i - 1].type == GRPC_OP_SLICE &&
            GPR_SLICE_LENGTH(ops[i - 1].data.slice) ==
                GPR_SLICE_LENGTH(ops[i].data.slice)) {
          repeats++;
        } else {
          CLEAR_REPEATS;
          gpr_log(GPR_DEBUG, "  [%02d] GRPC_OP_SLICE len=%d", i,
                  GPR_SLICE_LENGTH(ops[i].data.slice));
        }
        break;
      case GRPC_OP_FLOW_CTL_CB:
        CLEAR_REPEATS;
        gpr_log(GPR_DEBUG, "  [%02d] GRPC_OP_FLOW_CTL_CB", i);
        break;
    }
  }
  CLEAR_REPEATS;

  /* Iterate over operations, and verify them against expectations */
  for (i = 0; i < ops_count; i++) {
    switch (ops[i].type) {
      case GRPC_NO_OP:
        break;
      case GRPC_OP_METADATA_BOUNDARY:
        break;
      case GRPC_OP_METADATA:
        GPR_ASSERT(*expect_root_metadata && "must be expecting metadata");
        emd = *expect_root_metadata;
        if (emd == NULL) {
          gpr_log(GPR_ERROR, "metadata not found");
          abort();
        }
        do {
          if (emd->metadata == ops[i].data.metadata) {
            if (emd == *expect_root_metadata) {
              if (emd->next == emd) {
                *expect_root_metadata = NULL;
              } else {
                *expect_root_metadata = emd->next;
              }
            }
            emd->next->prev = emd->prev;
            emd->prev->next = emd->next;
            grpc_mdelem_unref(emd->metadata);
            grpc_mdelem_unref(ops[i].data.metadata);
            gpr_free(emd);
            emd = NULL;
            break;
          }
          emd = emd->next;
        } while (emd != *expect_root_metadata);
        if (emd) {
          gpr_log(GPR_ERROR, "metadata not found");
          abort();
        }
        break;
      case GRPC_OP_BEGIN_MESSAGE:
        GPR_ASSERT(*expect_root_message && "must be expecting a message");
        GPR_ASSERT((*expect_root_message)->read_pos == 0 &&
                   "must be at the start of a message");
        GPR_ASSERT((*expect_root_message)->begun == 0 &&
                   "can only BEGIN a message once");
        GPR_ASSERT((*expect_root_message)->length ==
                       ops[i].data.begin_message.length &&
                   "message lengths must match");
        (*expect_root_message)->begun = 1;
        break;
      case GRPC_OP_SLICE:
        GPR_ASSERT(*expect_root_message && "must be expecting a message");
        GPR_ASSERT((*expect_root_message)->begun == 1 &&
                   "must have begun a message");
        GPR_ASSERT((*expect_root_message)->read_pos +
                           GPR_SLICE_LENGTH(ops[i].data.slice) <=
                       (*expect_root_message)->length &&
                   "must not send more data than expected");
        for (j = 0; j < GPR_SLICE_LENGTH(ops[i].data.slice); j++) {
          GPR_ASSERT((*expect_root_message)
                             ->data[(*expect_root_message)->read_pos + j] ==
                         GPR_SLICE_START_PTR(ops[i].data.slice)[j] &&
                     "must send the correct message");
        }
        (*expect_root_message)->read_pos += GPR_SLICE_LENGTH(ops[i].data.slice);
        if ((*expect_root_message)->read_pos ==
            (*expect_root_message)->length) {
          expected_message *great_success = *expect_root_message;
          *expect_root_message = great_success->next;
          gpr_free(great_success->data);
          gpr_free(great_success);
        }
        gpr_slice_unref(ops[i].data.slice);
        break;
      case GRPC_OP_FLOW_CTL_CB:
        GPR_ASSERT(0 && "allowed");
        break;
      case GRPC_OP_DEADLINE:
        GPR_ASSERT(0 && "implemented");
        break;
    }
  }

  /* If the stream has become fully closed then we must destroy the transport
     part of the stream */
  if (final_state == GRPC_STREAM_CLOSED) {
    destroy_stream_args *dsa = gpr_malloc(sizeof(destroy_stream_args));
    gpr_thd_id id;
    dsa->transport = transport;
    dsa->stream = stream;
    /* start a thread after incrementing a pending op counter (so we can wait
       at test completion */
    add_pending_op();
    gpr_thd_new(&id, destroy_stream, dsa, NULL);
    if (stream == s->client_stream) {
      GPR_ASSERT(s->client_expected_messages == NULL &&
                 "must receive all expected messages");
      s->client_stream = NULL;
    } else {
      GPR_ASSERT(s->server_expected_messages == NULL &&
                 "must receive all expected messages");
      s->server_stream = NULL;
    }
    /* And if both the client and the server report fully closed, we can
       unlink the stream object entirely */
    if (s->client_stream == NULL && s->server_stream == NULL) {
      s->next->prev = s->prev;
      s->prev->next = s->next;
      if (s == f->streams) {
        if (s->next == f->streams) {
          f->streams = NULL;
        } else {
          f->streams = s->next;
        }
      }
    }
  }

  /* wakeup wait_and_verify */
  gpr_cv_broadcast(&f->cv);
  gpr_mu_unlock(&f->mu);
}

static void close_transport(void *user_data, grpc_transport *transport) {}

static grpc_transport_callbacks transport_callbacks = {
    alloc_recv_buffer, create_stream, recv_batch, close_transport};

/* Helper for tests to create a stream.
   Arguments:
     s - uninitialized test_stream struct to begin
     f - test fixture to associate this stream with
     method, host, deadline_seconds - header fields for the stream */
static void begin_stream(test_stream *s, test_fixture *f, const char *method,
                         const char *host, double deadline_seconds) {
  /* Deadline to initiate the stream (prevents the tests from hanging
     forever) */
  gpr_timespec deadline = deadline_from_seconds(10.0);
  grpc_stream_op_buffer sopb;

  grpc_sopb_init(&sopb);

  gpr_mu_lock(&f->mu);

  s->fixture = f;
  s->client_stream =
      gpr_malloc(grpc_transport_stream_size(f->client_transport));
  /* server stream will be set once it's received by the peer transport */
  s->server_stream = NULL;
  s->client_expected_messages = NULL;
  s->server_expected_messages = NULL;
  s->client_expected_metadata = NULL;
  s->server_expected_metadata = NULL;

  if (f->streams) {
    s->next = f->streams;
    s->prev = s->next->prev;
    s->next->prev = s->prev->next = s;
  } else {
    s->next = s->prev = s;
  }
  f->streams = s;

  gpr_mu_unlock(&f->mu);

  GPR_ASSERT(0 == grpc_transport_init_stream(f->client_transport,
                                             s->client_stream, NULL));

#define ADDMD(k, v)                                                           \
  do {                                                                        \
    grpc_mdelem *md = grpc_mdelem_from_strings(g_metadata_context, (k), (v)); \
    grpc_sopb_add_metadata(&sopb, md);                                        \
    expect_metadata(s, 1, (k), (v));                                          \
  } while (0)

  ADDMD(":path", method);
  ADDMD(":authority", host);
  ADDMD(":method", "POST");
  grpc_transport_send_batch(f->client_transport, s->client_stream, sopb.ops,
                            sopb.nops, 0);
  sopb.nops = 0;

  grpc_sopb_destroy(&sopb);

  /* wait for the server side stream to be created */
  gpr_mu_lock(&f->mu);
  while (s->server_stream == NULL) {
    GPR_ASSERT(0 == gpr_cv_wait(&f->cv, &f->mu, deadline));
  }
  gpr_mu_unlock(&f->mu);
}

static grpc_transport_setup_result setup_transport(
    test_fixture *f, grpc_transport **set_transport, void *user_data,
    grpc_transport *transport) {
  grpc_transport_setup_result result;

  gpr_mu_lock(&f->mu);
  *set_transport = transport;
  gpr_cv_broadcast(&f->cv);
  gpr_mu_unlock(&f->mu);

  result.callbacks = &transport_callbacks;
  result.user_data = user_data;
  return result;
}

static grpc_transport_setup_result setup_server_transport(
    void *arg, grpc_transport *transport, grpc_mdctx *mdctx) {
  test_fixture *f = arg;
  return setup_transport(f, &f->server_transport, &f->server_ud, transport);
}

static grpc_transport_setup_result setup_client_transport(
    void *arg, grpc_transport *transport, grpc_mdctx *mdctx) {
  test_fixture *f = arg;
  return setup_transport(f, &f->client_transport, &f->client_ud, transport);
}

/* Begin a test

   Arguments:
     f      - uninitialized test_fixture struct
     config - test configuration for this test
     name   - the name of this test */
static void begin_test(test_fixture *f, grpc_transport_test_config *config,
                       const char *name) {
  gpr_timespec timeout = gpr_time_add(gpr_now(), gpr_time_from_micros(100e6));

  gpr_log(GPR_INFO, "BEGIN: %s/%s", name, config->name);

  gpr_mu_init(&f->mu);
  gpr_cv_init(&f->cv);

  f->streams = NULL;

  init_user_data(&f->client_ud, f, config, 1);
  init_user_data(&f->server_ud, f, config, 0);

  f->client_transport = NULL;
  f->server_transport = NULL;

  GPR_ASSERT(0 ==
             config->create_transport(setup_client_transport, f,
                                      setup_server_transport, f,
                                      g_metadata_context));

  gpr_mu_lock(&f->mu);
  while (!f->client_transport || !f->server_transport) {
    GPR_ASSERT(gpr_cv_wait(&f->cv, &f->mu, timeout));
  }
  gpr_mu_unlock(&f->mu);
}

/* Enumerate expected messages on a stream */
static void enumerate_expected_messages(
    test_stream *s, expected_message *root, const char *stream_tag,
    void (*cb)(void *user, const char *fmt, ...), void *user) {
  expected_message *msg;

  for (msg = root; msg; msg = msg->next) {
    cb(user,
       "Waiting for message to finish: "
       "length=%zu read_pos=%zu begun=%d",
       msg->length, msg->read_pos);
  }
}

/* Walk through everything that is still waiting to happen, and call 'cb' with
   userdata 'user' for that expectation. */
static void enumerate_expectations(test_fixture *f,
                                   void (*cb)(void *user, const char *fmt, ...),
                                   void *user) {
  test_stream *stream;

  if (f->streams) {
    stream = f->streams;
    do {
      cb(user,
         "Waiting for request to close: "
         "client=%p, server=%p",
         stream->client_stream, stream->server_stream);
      enumerate_expected_messages(stream, stream->client_expected_messages,
                                  "client", cb, user);
      enumerate_expected_messages(stream, stream->server_expected_messages,
                                  "server", cb, user);
      stream = stream->next;
    } while (stream != f->streams);
  }
}

/* Callback for enumerate_expectations, that increments an integer each time
   an expectation is seen */
static void increment_expectation_count(void *p, const char *fmt, ...) {
  ++*(int *)p;
}

/* Returns the count of pending expectations in a fixture. Requires mu taken */
static int count_expectations(test_fixture *f) {
  int n = 0;
  enumerate_expectations(f, increment_expectation_count, &n);
  return n;
}

/* Callback for enumerate_expectations that adds an expectation to the log */
static void dump_expectation(void *p, const char *fmt, ...) {
  char buffer[256];
  va_list args;
  va_start(args, fmt);

  vsprintf(buffer, fmt, args);
  gpr_log(GPR_INFO, "EXPECTED: %s", buffer);

  va_end(args);
}

/* Add all pending expectations to the log */
static void dump_expectations(test_fixture *f) {
  enumerate_expectations(f, dump_expectation, NULL);
}

/* Wait until all expectations are completed, or crash */
static void wait_and_verify(test_fixture *f) {
  gpr_timespec deadline = deadline_from_seconds(10.0);

  gpr_mu_lock(&f->mu);
  while (count_expectations(f) > 0) {
    gpr_log(GPR_INFO, "waiting for expectations to complete");
    if (gpr_cv_wait(&f->cv, &f->mu, deadline)) {
      gpr_log(GPR_ERROR, "Timeout waiting for expectation completion");
      dump_expectations(f);
      gpr_mu_unlock(&f->mu);
      abort();
    }
  }
  gpr_mu_unlock(&f->mu);
}

/* Finish a test */
static void end_test(test_fixture *f) {
  wait_and_verify(f);

  grpc_transport_close(f->client_transport);
  grpc_transport_close(f->server_transport);
  grpc_transport_destroy(f->client_transport);
  grpc_transport_destroy(f->server_transport);

  wait_pending_ops();
}

/* Generate a test slice filled with {0,1,2,3,...,255,0,1,2,3,4,...} */
static gpr_slice generate_test_data(size_t length) {
  gpr_slice slice = gpr_slice_malloc(length);
  int i;
  for (i = 0; i < length; i++) {
    GPR_SLICE_START_PTR(slice)[i] = i;
  }
  return slice;
}

/* Add an expected message to the end of a list with root root */
static void append_expected_message(expected_message **root,
                                    expected_message *message) {
  expected_message *end;

  if (!*root) {
    *root = message;
    return;
  }

  for (end = *root; end->next; end = end->next)
    ;
  end->next = message;
}

/* Add an expected message on stream 's''.
   If from_client==1, expect it on the server, otherwise expect it on the client
   Variadic parameters are a NULL-terminated list of pointers to slices that
   should be expected as payload */
static void expect_message(test_stream *s, int from_client,
                           /* gpr_slice* */...) {
  va_list args;
  gpr_slice *slice;
  size_t capacity = 32;
  size_t length = 0;
  gpr_uint8 *buffer = gpr_malloc(capacity);
  expected_message *e;

  va_start(args, from_client);
  while ((slice = va_arg(args, gpr_slice *))) {
    while (GPR_SLICE_LENGTH(*slice) + length > capacity) {
      capacity *= 2;
      buffer = gpr_realloc(buffer, capacity);
    }
    memcpy(buffer + length, GPR_SLICE_START_PTR(*slice),
           GPR_SLICE_LENGTH(*slice));
    length += GPR_SLICE_LENGTH(*slice);
  }
  va_end(args);

  e = gpr_malloc(sizeof(expected_message));
  e->data = buffer;
  e->length = length;
  e->read_pos = 0;
  e->begun = 0;
  e->next = NULL;

  gpr_mu_lock(&s->fixture->mu);
  append_expected_message(
      from_client ? &s->server_expected_messages : &s->client_expected_messages,
      e);
  gpr_mu_unlock(&s->fixture->mu);
}

static void expect_metadata(test_stream *s, int from_client, const char *key,
                            const char *value) {
  expected_metadata *e = gpr_malloc(sizeof(expected_metadata));
  expected_metadata **root =
      from_client ? &s->server_expected_metadata : &s->client_expected_metadata;
  e->metadata = grpc_mdelem_from_strings(g_metadata_context, key, value);
  gpr_mu_lock(&s->fixture->mu);
  if (!*root) {
    *root = e;
    e->next = e->prev = e;
  } else {
    e->next = *root;
    e->prev = e->next->prev;
    e->next->prev = e->prev->next = e;
  }
  gpr_mu_unlock(&s->fixture->mu);
}

/******************************************************************************
 * Actual unit tests
 */

/* Test that we can create, begin, and end a test */
static void test_no_op(grpc_transport_test_config *config) {
  test_fixture f;
  begin_test(&f, config, __FUNCTION__);
  end_test(&f);
}

/* Test that a request can be initiated and terminated normally */
static void test_simple_request(grpc_transport_test_config *config) {
  test_fixture f;
  test_stream s;

  begin_test(&f, config, __FUNCTION__);
  begin_stream(&s, &f, "/Test", "foo.google.com", 10);
  grpc_transport_send_batch(f.client_transport, s.client_stream, NULL, 0, 1);
  grpc_transport_send_batch(f.server_transport, s.server_stream, NULL, 0, 1);
  end_test(&f);
}

/* Test that a request can be aborted by the client */
static void test_can_abort_client(grpc_transport_test_config *config) {
  test_fixture f;
  test_stream s;

  begin_test(&f, config, __FUNCTION__);
  begin_stream(&s, &f, "/Test", "foo.google.com", 10);
  expect_metadata(&s, 0, "grpc-status", "1");
  expect_metadata(&s, 1, "grpc-status", "1");
  grpc_transport_abort_stream(f.client_transport, s.client_stream,
                              GRPC_STATUS_CANCELLED);
  end_test(&f);
}

/* Test that a request can be aborted by the server */
static void test_can_abort_server(grpc_transport_test_config *config) {
  test_fixture f;
  test_stream s;

  begin_test(&f, config, __FUNCTION__);
  begin_stream(&s, &f, "/Test", "foo.google.com", 10);
  expect_metadata(&s, 0, "grpc-status", "1");
  expect_metadata(&s, 1, "grpc-status", "1");
  grpc_transport_abort_stream(f.server_transport, s.server_stream,
                              GRPC_STATUS_CANCELLED);
  end_test(&f);
}

/* Test that a request can be sent with payload */
static void test_request_with_data(grpc_transport_test_config *config,
                                   size_t message_length) {
  test_fixture f;
  test_stream s;
  gpr_slice data = generate_test_data(message_length);
  grpc_stream_op_buffer sopb;

  grpc_sopb_init(&sopb);
  begin_test(&f, config, __FUNCTION__);
  gpr_log(GPR_INFO, "message_length = %d", message_length);
  begin_stream(&s, &f, "/Test", "foo.google.com", 10);
  expect_message(&s, 1, &data, NULL);
  grpc_sopb_add_begin_message(&sopb, message_length, 0);
  grpc_sopb_add_slice(&sopb, data);
  grpc_transport_set_allow_window_updates(f.server_transport, s.server_stream,
                                          1);
  grpc_transport_send_batch(f.client_transport, s.client_stream, sopb.ops,
                            sopb.nops, 1);
  sopb.nops = 0;
  grpc_transport_send_batch(f.server_transport, s.server_stream, NULL, 0, 1);
  end_test(&f);
  grpc_sopb_destroy(&sopb);
}

/* Increment an integer pointed to by x - used for verifying flow control */
static void increment_int(void *x, grpc_op_error error) { ++*(int *)x; }

/* Test that flow control callbacks are made at appropriate times */
static void test_request_with_flow_ctl_cb(grpc_transport_test_config *config,
                                          size_t message_length) {
  test_fixture f;
  test_stream s;
  int flow_ctl_called = 0;
  gpr_slice data = generate_test_data(message_length);
  grpc_stream_op_buffer sopb;

  grpc_sopb_init(&sopb);
  begin_test(&f, config, __FUNCTION__);
  gpr_log(GPR_INFO, "length=%d", message_length);
  begin_stream(&s, &f, "/Test", "foo.google.com", 10);
  expect_message(&s, 1, &data, NULL);
  grpc_sopb_add_begin_message(&sopb, message_length, 0);
  grpc_sopb_add_slice(&sopb, data);
  grpc_sopb_add_flow_ctl_cb(&sopb, increment_int, &flow_ctl_called);
  grpc_transport_set_allow_window_updates(f.server_transport, s.server_stream,
                                          1);
  grpc_transport_send_batch(f.client_transport, s.client_stream, sopb.ops,
                            sopb.nops, 1);
  sopb.nops = 0;
  grpc_transport_send_batch(f.server_transport, s.server_stream, NULL, 0, 1);
  end_test(&f);
  GPR_ASSERT(flow_ctl_called == 1);
  grpc_sopb_destroy(&sopb);
}

/* Set an event on ping response */
static void ping_cb(void *p) { gpr_event_set(p, (void *)1); }

/* Test that pinging gets a response */
static void test_ping(grpc_transport_test_config *config) {
  test_fixture f;
  gpr_event ev;

  begin_test(&f, config, __FUNCTION__);
  gpr_event_init(&ev);

  grpc_transport_ping(f.client_transport, ping_cb, &ev);
  GPR_ASSERT(gpr_event_wait(&ev, deadline_from_seconds(10)));

  end_test(&f);
}

/******************************************************************************
 * Test driver
 */

static const size_t interesting_message_lengths[] = {
    1, 100, 10000, 100000, 1000000,
};

void grpc_transport_end2end_tests(grpc_transport_test_config *config) {
  int i;

  g_metadata_context = grpc_mdctx_create();

  test_no_op(config);
  test_simple_request(config);
  test_can_abort_client(config);
  test_can_abort_server(config);
  test_ping(config);
  for (i = 0; i < GPR_ARRAY_SIZE(interesting_message_lengths); i++) {
    test_request_with_data(config, interesting_message_lengths[i]);
    test_request_with_flow_ctl_cb(config, interesting_message_lengths[i]);
  }

  grpc_mdctx_orphan(g_metadata_context);

  gpr_log(GPR_INFO, "tests completed ok");
}