large_metadata.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "absl/strings/str_format.h"
  20. #include "absl/strings/str_join.h"
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/string_util.h>
  23. #include "src/core/lib/gpr/string.h"
  24. #include "src/core/lib/surface/server.h"
  25. #include "test/core/bad_client/bad_client.h"
  26. #include "test/core/end2end/cq_verifier.h"
  27. // The large-metadata headers that we're adding for this test are not
  28. // actually appended to this in a single string, since the string would
  29. // be longer than the C99 string literal limit. Instead, we dynamically
  30. // construct it by adding the large headers one at a time.
  31. /* headers: generated from large_metadata.headers in this directory */
  32. #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST \
  33. "\x00\x00\x00\x04\x01\x00\x00\x00\x00" \
  34. "\x00" \
  35. "5{\x01\x05\x00\x00\x00\x01" \
  36. "\x10\x05:path\x08/foo/bar" \
  37. "\x10\x07:scheme\x04http" \
  38. "\x10\x07:method\x04POST" \
  39. "\x10\x0a:authority\x09localhost" \
  40. "\x10\x0c" \
  41. "content-type\x10" \
  42. "application/grpc" \
  43. "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip" \
  44. "\x10\x02te\x08trailers" \
  45. "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
  46. // Each large-metadata header is constructed from these start and end
  47. // strings, with a two-digit number in between.
  48. #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_START_STR "\x10\x0duser-header"
  49. #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_END_STR \
  50. "~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
  51. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  52. // The size of each large-metadata header string.
  53. #define PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_SIZE \
  54. ((sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_START_STR) - 1) + 2 + \
  55. (sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_END_STR) - 1))
  56. // The number of headers we're adding and the total size of the client
  57. // payload.
  58. #define NUM_HEADERS 46
  59. #define TOO_MUCH_METADATA_FROM_CLIENT_REQUEST_SIZE \
  60. ((sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST) - 1) + \
  61. (NUM_HEADERS * PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_SIZE) + 1)
  62. static void verifier_fails(grpc_server* server, grpc_completion_queue* cq,
  63. void* /*registered_method*/) {
  64. while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
  65. GPR_ASSERT(grpc_completion_queue_next(
  66. cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
  67. .type == GRPC_QUEUE_TIMEOUT);
  68. }
  69. }
  70. int main(int argc, char** argv) {
  71. int i;
  72. grpc_init();
  73. grpc::testing::TestEnvironment env(argc, argv);
  74. // Test sending more metadata than the server will accept.
  75. std::vector<std::string> headers;
  76. for (i = 0; i < NUM_HEADERS; ++i) {
  77. headers.push_back(absl::StrFormat(
  78. "%s%02d%s", PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_START_STR, i,
  79. PFX_TOO_MUCH_METADATA_FROM_CLIENT_HEADER_END_STR));
  80. }
  81. std::string client_headers = absl::StrJoin(headers, "");
  82. char client_payload[TOO_MUCH_METADATA_FROM_CLIENT_REQUEST_SIZE] =
  83. PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST;
  84. memcpy(client_payload + sizeof(PFX_TOO_MUCH_METADATA_FROM_CLIENT_REQUEST) - 1,
  85. client_headers.data(), client_headers.size());
  86. grpc_bad_client_arg args[2];
  87. args[0] = connection_preface_arg;
  88. args[1].client_validator = rst_stream_client_validator;
  89. args[1].client_payload = client_payload;
  90. args[1].client_payload_length = sizeof(client_payload) - 1;
  91. grpc_run_bad_client_test(verifier_fails, args, 2, 0);
  92. grpc_shutdown();
  93. return 0;
  94. }