simple_request.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "src/core/lib/surface/server.h"
  21. #include "test/core/bad_client/bad_client.h"
  22. #include "test/core/end2end/cq_verifier.h"
  23. #define PFX_STR \
  24. "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
  25. "\x00\x00\x00\x04\x00\x00\x00\x00\x00" /* settings frame */ \
  26. "\x00\x00\xc9\x01\x04\x00\x00\x00\x01" /* headers: generated from \
  27. simple_request.headers in this \
  28. directory */ \
  29. "\x10\x05:path\x08/foo/bar" \
  30. "\x10\x07:scheme\x04http" \
  31. "\x10\x07:method\x04POST" \
  32. "\x10\x0a:authority\x09localhost" \
  33. "\x10\x0c" \
  34. "content-type\x10" \
  35. "application/grpc" \
  36. "\x10\x14grpc-accept-encoding\x15" \
  37. "deflate,identity,gzip" \
  38. "\x10\x02te\x08trailers" \
  39. "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)"
  40. #define PFX_STR_UNUSUAL \
  41. "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
  42. "\x00\x00\x00\x04\x00\x00\x00\x00\x00" /* settings frame */ \
  43. "\x00\x00\xf4\x01\x04\x00\x00\x00\x01" /* headers: generated from \
  44. simple_request_unusual.headers \
  45. in this directory */ \
  46. "\x10\x05:path\x08/foo/bar" \
  47. "\x10\x07:scheme\x04http" \
  48. "\x10\x07:method\x04POST" \
  49. "\x10\x04host\x09localhost" \
  50. "\x10\x0c" \
  51. "content-type\x1e" \
  52. "application/grpc+this-is-valid" \
  53. "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip" \
  54. "\x10\x02te\x08trailers" \
  55. "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" \
  56. "\x10\x0cgrpc-timeout\x03" \
  57. "10S" \
  58. "\x10\x0cgrpc-timeout\x02" \
  59. "5S"
  60. #define PFX_STR_UNUSUAL2 \
  61. "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
  62. "\x00\x00\x00\x04\x00\x00\x00\x00\x00" /* settings frame */ \
  63. "\x00\x00\xf4\x01\x04\x00\x00\x00\x01" /* headers: generated from \
  64. simple_request_unusual2.headers \
  65. in this directory */ \
  66. "\x10\x05:path\x08/foo/bar" \
  67. "\x10\x07:scheme\x04http" \
  68. "\x10\x07:method\x04POST" \
  69. "\x10\x04host\x09localhost" \
  70. "\x10\x0c" \
  71. "content-type\x1e" \
  72. "application/grpc;this-is-valid" \
  73. "\x10\x14grpc-accept-encoding\x15identity,deflate,gzip" \
  74. "\x10\x02te\x08trailers" \
  75. "\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" \
  76. "\x10\x0cgrpc-timeout\x03" \
  77. "10S" \
  78. "\x10\x0cgrpc-timeout\x02" \
  79. "5S"
  80. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  81. static void verifier(grpc_server* server, grpc_completion_queue* cq,
  82. void* /*registered_method*/) {
  83. grpc_call_error error;
  84. grpc_call* s;
  85. grpc_call_details call_details;
  86. cq_verifier* cqv = cq_verifier_create(cq);
  87. grpc_metadata_array request_metadata_recv;
  88. grpc_call_details_init(&call_details);
  89. grpc_metadata_array_init(&request_metadata_recv);
  90. error = grpc_server_request_call(server, &s, &call_details,
  91. &request_metadata_recv, cq, cq, tag(101));
  92. GPR_ASSERT(GRPC_CALL_OK == error);
  93. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  94. cq_verify(cqv);
  95. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.host, "localhost"));
  96. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo/bar"));
  97. grpc_metadata_array_destroy(&request_metadata_recv);
  98. grpc_call_details_destroy(&call_details);
  99. grpc_call_unref(s);
  100. cq_verifier_destroy(cqv);
  101. }
  102. static void failure_verifier(grpc_server* server, grpc_completion_queue* cq,
  103. void* /*registered_method*/) {
  104. while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
  105. GPR_ASSERT(grpc_completion_queue_next(
  106. cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
  107. .type == GRPC_QUEUE_TIMEOUT);
  108. }
  109. }
  110. int main(int argc, char** argv) {
  111. grpc::testing::TestEnvironment env(argc, argv);
  112. grpc_init();
  113. /* basic request: check that things are working */
  114. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR, 0);
  115. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR_UNUSUAL, 0);
  116. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR_UNUSUAL2, 0);
  117. /* push an illegal data frame */
  118. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  119. PFX_STR
  120. "\x00\x00\x05\x00\x00\x00\x00\x00\x01"
  121. "\x34\x00\x00\x00\x00",
  122. 0);
  123. /* push a data frame with bad flags */
  124. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  125. PFX_STR "\x00\x00\x00\x00\x02\x00\x00\x00\x01", 0);
  126. /* push a window update with a bad length */
  127. GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
  128. PFX_STR "\x00\x00\x01\x08\x00\x00\x00\x00\x01", 0);
  129. /* push a window update with bad flags */
  130. GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
  131. PFX_STR "\x00\x00\x00\x08\x10\x00\x00\x00\x01", 0);
  132. /* push a window update with bad data (0 is not legal window size increment)
  133. */
  134. GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
  135. PFX_STR
  136. "\x00\x00\x04\x08\x00\x00\x00\x00\x01"
  137. "\x00\x00\x00\x00",
  138. 0);
  139. /* push a short goaway */
  140. GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
  141. PFX_STR "\x00\x00\x04\x07\x00\x00\x00\x00\x00", 0);
  142. /* disconnect before sending goaway */
  143. GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
  144. PFX_STR "\x00\x01\x12\x07\x00\x00\x00\x00\x00",
  145. GRPC_BAD_CLIENT_DISCONNECT);
  146. /* push a rst_stream with a bad length */
  147. GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
  148. PFX_STR "\x00\x00\x01\x03\x00\x00\x00\x00\x01", 0);
  149. /* push a rst_stream with bad flags */
  150. GRPC_RUN_BAD_CLIENT_TEST(failure_verifier, nullptr,
  151. PFX_STR "\x00\x00\x00\x03\x10\x00\x00\x00\x01", 0);
  152. grpc_shutdown();
  153. return 0;
  154. }