| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2019 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 |
|
| 19 |
#include <gtest/gtest.h> |
| 20 |
|
| 21 |
#include <grpc/grpc.h> |
| 22 |
#include <grpc/support/log.h> |
| 23 |
|
| 24 |
#include "src/core/lib/surface/server.h" |
| 25 |
#include "test/core/bad_client/bad_client.h" |
| 26 |
#include "test/core/util/test_config.h" |
| 27 |
|
| 28 |
#define HEADER_FRAME_ID_1 \ |
| 29 |
"\x00\x00\xc9\x01\x05\x00\x00\x00\x01" /* headers: generated from \ |
| 30 |
simple_request.headers in this \ |
| 31 |
directory */ \ |
| 32 |
"\x10\x05:path\x08/foo/bar" \ |
| 33 |
"\x10\x07:scheme\x04http" \ |
| 34 |
"\x10\x07:method\x04POST" \ |
| 35 |
"\x10\x0a:authority\x09localhost" \ |
| 36 |
"\x10\x0c" \ |
| 37 |
"content-type\x10" \ |
| 38 |
"application/grpc" \ |
| 39 |
"\x10\x14grpc-accept-encoding\x15" \ |
| 40 |
"deflate,identity,gzip" \ |
| 41 |
"\x10\x02te\x08trailers" \ |
| 42 |
"\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" |
| 43 |
|
| 44 |
#define HEADER_FRAME_ID_2 \ |
| 45 |
"\x00\x00\xc9\x01\x05\x00\x00\x00\x02" /* headers: generated from \ |
| 46 |
simple_request.headers in this \ |
| 47 |
directory */ \ |
| 48 |
"\x10\x05:path\x08/foo/bar" \ |
| 49 |
"\x10\x07:scheme\x04http" \ |
| 50 |
"\x10\x07:method\x04POST" \ |
| 51 |
"\x10\x0a:authority\x09localhost" \ |
| 52 |
"\x10\x0c" \ |
| 53 |
"content-type\x10" \ |
| 54 |
"application/grpc" \ |
| 55 |
"\x10\x14grpc-accept-encoding\x15" \ |
| 56 |
"deflate,identity,gzip" \ |
| 57 |
"\x10\x02te\x08trailers" \ |
| 58 |
"\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" |
| 59 |
|
| 60 |
#define HEADER_FRAME_ID_3 \ |
| 61 |
"\x00\x00\xc9\x01\x05\x00\x00\x00\x03" /* headers: generated from \ |
| 62 |
simple_request.headers in this \ |
| 63 |
directory */ \ |
| 64 |
"\x10\x05:path\x08/foo/bar" \ |
| 65 |
"\x10\x07:scheme\x04http" \ |
| 66 |
"\x10\x07:method\x04POST" \ |
| 67 |
"\x10\x0a:authority\x09localhost" \ |
| 68 |
"\x10\x0c" \ |
| 69 |
"content-type\x10" \ |
| 70 |
"application/grpc" \ |
| 71 |
"\x10\x14grpc-accept-encoding\x15" \ |
| 72 |
"deflate,identity,gzip" \ |
| 73 |
"\x10\x02te\x08trailers" \ |
| 74 |
"\x10\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)" |
| 75 |
|
| 76 |
namespace { |
| 77 |
|
| 78 |
void verifier(grpc_server* server, grpc_completion_queue* cq, |
| 79 |
void* /*registered_method*/) { |
| 80 |
while (grpc_core::Server::FromC(server)->HasOpenConnections()) { |
| 81 |
GPR_ASSERT(grpc_completion_queue_next( |
| 82 |
cq, grpc_timeout_milliseconds_to_deadline(20), nullptr) |
| 83 |
.type == GRPC_QUEUE_TIMEOUT); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
TEST(BadStreamingId, RegularHeader) { |
| 88 |
grpc_bad_client_arg args[2]; |
| 89 |
args[0] = connection_preface_arg; |
| 90 |
args[1].client_validator = nullptr; |
| 91 |
args[1].client_payload = HEADER_FRAME_ID_1; |
| 92 |
args[1].client_payload_length = sizeof(HEADER_FRAME_ID_1) - 1; |
| 93 |
grpc_run_bad_client_test(verifier, args, 2, GRPC_BAD_CLIENT_DISCONNECT); |
| 94 |
} |
| 95 |
|
| 96 |
TEST(BadStreamingId, NonClientStreamId) { |
| 97 |
grpc_bad_client_arg args[2]; |
| 98 |
args[0] = connection_preface_arg; |
| 99 |
// send a header frame with non-client stream id 2 |
| 100 |
args[1].client_validator = nullptr; |
| 101 |
args[1].client_payload = HEADER_FRAME_ID_2; |
| 102 |
args[1].client_payload_length = sizeof(HEADER_FRAME_ID_2) - 1; |
| 103 |
grpc_run_bad_client_test(verifier, args, 2, GRPC_BAD_CLIENT_DISCONNECT); |
| 104 |
} |
| 105 |
|
| 106 |
TEST(BadStreamingId, ClosedStreamId) { |
| 107 |
grpc_bad_client_arg args[4]; |
| 108 |
args[0] = connection_preface_arg; |
| 109 |
// send a header frame with stream id 1 |
| 110 |
args[1].client_validator = nullptr; |
| 111 |
args[1].client_payload = HEADER_FRAME_ID_1; |
| 112 |
args[1].client_payload_length = sizeof(HEADER_FRAME_ID_1) - 1; |
| 113 |
// send a header frame with stream id 3 |
| 114 |
args[2].client_validator = nullptr; |
| 115 |
args[2].client_payload = HEADER_FRAME_ID_3; |
| 116 |
args[2].client_payload_length = sizeof(HEADER_FRAME_ID_3) - 1; |
| 117 |
// send a header frame with closed stream id 1 again |
| 118 |
args[3].client_validator = nullptr; |
| 119 |
args[3].client_payload = HEADER_FRAME_ID_1; |
| 120 |
args[3].client_payload_length = sizeof(HEADER_FRAME_ID_1) - 1; |
| 121 |
grpc_run_bad_client_test(verifier, args, 4, GRPC_BAD_CLIENT_DISCONNECT); |
| 122 |
} |
| 123 |
|
| 124 |
} // namespace |
| 125 |
|
| 126 |
int main(int argc, char** argv) { |
| 127 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 128 |
::testing::InitGoogleTest(&argc, argv); |
| 129 |
grpc_init(); |
| 130 |
int retval = RUN_ALL_TESTS(); |
| 131 |
grpc_shutdown(); |
| 132 |
return retval; |
| 133 |
} |