unknown_frame.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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>
  19. #include <gtest/gtest.h>
  20. #include <grpc/support/string_util.h>
  21. #include "src/core/lib/surface/server.h"
  22. #include "test/core/bad_client/bad_client.h"
  23. static void verifier(grpc_server* server, grpc_completion_queue* cq,
  24. void* /*registered_method*/) {
  25. while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
  26. GPR_ASSERT(grpc_completion_queue_next(
  27. cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
  28. .type == GRPC_QUEUE_TIMEOUT);
  29. }
  30. }
  31. #define APPEND_BUFFER(string, to_append) \
  32. ((string).append((to_append), sizeof(to_append) - 1))
  33. namespace {
  34. TEST(UnknownFrameType, Test) {
  35. /* test that all invalid/unknown frame types are handled */
  36. for (int i = 10; i <= 255; i++) {
  37. std::string unknown_frame_string;
  38. APPEND_BUFFER(unknown_frame_string, "\x00\x00\x00");
  39. char frame_type = static_cast<char>(i);
  40. unknown_frame_string.append(&frame_type, 1);
  41. APPEND_BUFFER(unknown_frame_string, "\x00\x00\x00\x00\x01");
  42. grpc_bad_client_arg args[2];
  43. args[0] = connection_preface_arg;
  44. args[1].client_validator = nullptr;
  45. args[1].client_payload = unknown_frame_string.c_str();
  46. args[1].client_payload_length = unknown_frame_string.size();
  47. grpc_run_bad_client_test(verifier, args, 2, GRPC_BAD_CLIENT_DISCONNECT);
  48. }
  49. }
  50. } // namespace
  51. int main(int argc, char** argv) {
  52. grpc::testing::TestEnvironment env(argc, argv);
  53. ::testing::InitGoogleTest(&argc, argv);
  54. grpc_init();
  55. int retval = RUN_ALL_TESTS();
  56. grpc_shutdown();
  57. return retval;
  58. }