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
19 #include <grpc/grpc.h>
20 #include <grpc/support/time.h>
21 #include <grpcpp/channel.h>
22 #include <grpcpp/client_context.h>
23 #include <grpcpp/create_channel.h>
24 #include <grpcpp/server.h>
25 #include <grpcpp/server_builder.h>
26 #include <grpcpp/server_context.h>
27 #include <gtest/gtest.h>
28
29 #include "absl/log/check.h"
30 #include "absl/memory/memory.h"
31 #include "src/core/util/crash.h"
32 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
33 #include "src/proto/grpc/testing/echo.grpc.pb.h"
34 #include "test/core/test_util/port.h"
35 #include "test/core/test_util/test_config.h"
36 #include "test/cpp/util/subprocess.h"
37
38 static std::string g_root;
39
40 namespace grpc {
41 namespace testing {
42
43 namespace {
44
45 class CrashTest : public ::testing::Test {
46 protected:
CrashTest()47 CrashTest() {}
48
CreateServerAndStub()49 std::unique_ptr<grpc::testing::EchoTestService::Stub> CreateServerAndStub() {
50 auto port = grpc_pick_unused_port_or_die();
51 std::ostringstream addr_stream;
52 addr_stream << "localhost:" << port;
53 auto addr = addr_stream.str();
54 server_ = std::make_unique<SubProcess>(std::vector<std::string>({
55 g_root + "/client_crash_test_server",
56 "--address=" + addr,
57 }));
58 CHECK(server_);
59 return grpc::testing::EchoTestService::NewStub(
60 grpc::CreateChannel(addr, InsecureChannelCredentials()));
61 }
62
KillServer()63 void KillServer() { server_.reset(); }
64
65 private:
66 std::unique_ptr<SubProcess> server_;
67 };
68
TEST_F(CrashTest,KillBeforeWrite)69 TEST_F(CrashTest, KillBeforeWrite) {
70 auto stub = CreateServerAndStub();
71
72 EchoRequest request;
73 EchoResponse response;
74 ClientContext context;
75 context.set_wait_for_ready(true);
76
77 auto stream = stub->BidiStream(&context);
78
79 request.set_message("Hello");
80 EXPECT_TRUE(stream->Write(request));
81 EXPECT_TRUE(stream->Read(&response));
82 EXPECT_EQ(response.message(), request.message());
83
84 KillServer();
85
86 request.set_message("You should be dead");
87 // This may succeed or fail depending on the state of the TCP connection
88 stream->Write(request);
89 // But the read will definitely fail
90 EXPECT_FALSE(stream->Read(&response));
91
92 EXPECT_FALSE(stream->Finish().ok());
93 }
94
TEST_F(CrashTest,KillAfterWrite)95 TEST_F(CrashTest, KillAfterWrite) {
96 auto stub = CreateServerAndStub();
97
98 EchoRequest request;
99 EchoResponse response;
100 ClientContext context;
101 context.set_wait_for_ready(true);
102
103 auto stream = stub->BidiStream(&context);
104
105 request.set_message("Hello");
106 EXPECT_TRUE(stream->Write(request));
107 EXPECT_TRUE(stream->Read(&response));
108 EXPECT_EQ(response.message(), request.message());
109
110 request.set_message("I'm going to kill you");
111 EXPECT_TRUE(stream->Write(request));
112
113 KillServer();
114
115 // This may succeed or fail depending on how quick the server was
116 stream->Read(&response);
117
118 EXPECT_FALSE(stream->Finish().ok());
119 }
120
121 } // namespace
122
123 } // namespace testing
124 } // namespace grpc
125
main(int argc,char ** argv)126 int main(int argc, char** argv) {
127 std::string me = argv[0];
128 auto lslash = me.rfind('/');
129 if (lslash != std::string::npos) {
130 g_root = me.substr(0, lslash);
131 } else {
132 g_root = ".";
133 }
134
135 grpc::testing::TestEnvironment env(&argc, argv);
136 ::testing::InitGoogleTest(&argc, argv);
137 // Order seems to matter on these tests: run three times to eliminate that
138 for (int i = 0; i < 3; i++) {
139 if (RUN_ALL_TESTS() != 0) {
140 return 1;
141 }
142 }
143 return 0;
144 }
145