window_overflow.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <string.h>
  19. #include <grpc/grpc.h>
  20. #include <grpc/support/alloc.h>
  21. #include "src/core/lib/surface/server.h"
  22. #include "test/core/bad_client/bad_client.h"
  23. #define PFX_STR \
  24. "\x00\x00\x00\x04\x01\x00\x00\x00\x00" \
  25. "\x00\x00\xc9\x01\x04\x00\x00\x00\x01" /* headers: generated from \
  26. simple_request.headers in this \
  27. directory */ \
  28. "\x10\x05:path\x08/foo/bar" \
  29. "\x10\x07:scheme\x04http" \
  30. "\x10\x07:method\x04POST" \
  31. "\x10\x0a:authority\x09localhost" \
  32. "\x10\x0c" \
  33. "content-type\x10" \
  34. "application/grpc" \
  35. "\x10\x14grpc-accept-encoding\x15" \
  36. "deflate,identity,gzip" \
  37. "\x10\x02te\x08trailers" \
  38. "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
  39. static void verifier(grpc_server* server, grpc_completion_queue* cq,
  40. void* /*registered_method*/) {
  41. while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
  42. GPR_ASSERT(grpc_completion_queue_next(
  43. cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
  44. .type == GRPC_QUEUE_TIMEOUT);
  45. }
  46. }
  47. char* g_buffer;
  48. size_t g_cap = 0;
  49. size_t g_count = 0;
  50. static void addbuf(const void* data, size_t len) {
  51. if (g_count + len > g_cap) {
  52. g_cap = std::max(g_count + len, g_cap * 2);
  53. g_buffer = static_cast<char*>(gpr_realloc(g_buffer, g_cap));
  54. }
  55. memcpy(g_buffer + g_count, data, len);
  56. g_count += len;
  57. }
  58. int main(int argc, char** argv) {
  59. int i, j;
  60. #define MAX_FRAME_SIZE 16384
  61. #define MESSAGES_PER_FRAME (MAX_FRAME_SIZE / 5)
  62. #define FRAME_SIZE (MESSAGES_PER_FRAME * 5)
  63. #define SEND_SIZE (4 * 1024 * 1024)
  64. #define NUM_FRAMES (SEND_SIZE / FRAME_SIZE + 1)
  65. grpc::testing::TestEnvironment env(argc, argv);
  66. grpc_init();
  67. addbuf(PFX_STR, sizeof(PFX_STR) - 1);
  68. for (i = 0; i < NUM_FRAMES; i++) {
  69. uint8_t hdr[9] = {static_cast<uint8_t>(FRAME_SIZE >> 16),
  70. static_cast<uint8_t>(FRAME_SIZE >> 8),
  71. static_cast<uint8_t>
  72. FRAME_SIZE,
  73. 0,
  74. 0,
  75. 0,
  76. 0,
  77. 0,
  78. 1};
  79. addbuf(hdr, sizeof(hdr));
  80. for (j = 0; j < MESSAGES_PER_FRAME; j++) {
  81. uint8_t message[5] = {0, 0, 0, 0, 0};
  82. addbuf(message, sizeof(message));
  83. }
  84. }
  85. grpc_bad_client_arg bca[2];
  86. bca[0] = connection_preface_arg;
  87. bca[1] = {rst_stream_client_validator, nullptr, g_buffer, g_count};
  88. grpc_run_bad_client_test(verifier, bca, 2, GRPC_BAD_CLIENT_LARGE_REQUEST);
  89. gpr_free(g_buffer);
  90. grpc_shutdown();
  91. return 0;
  92. }