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 "src/core/lib/transport/status_conversion.h"
20
21 #include <grpc/support/time.h>
22
23 #include <memory>
24
25 #include "gtest/gtest.h"
26 #include "src/core/lib/iomgr/exec_ctx.h"
27 #include "test/core/test_util/test_config.h"
28
29 #define GRPC_STATUS_TO_HTTP2_ERROR(a, b) \
30 ASSERT_EQ(grpc_status_to_http2_error(a), (b))
31 #define HTTP2_ERROR_TO_GRPC_STATUS(a, deadline, b) \
32 do { \
33 grpc_core::ExecCtx exec_ctx; \
34 ASSERT_EQ(grpc_http2_error_to_grpc_status(a, deadline), (b)); \
35 \
36 } while (0)
37 #define GRPC_STATUS_TO_HTTP2_STATUS(a, b) \
38 ASSERT_EQ(grpc_status_to_http2_status(a), (b))
39 #define HTTP2_STATUS_TO_GRPC_STATUS(a, b) \
40 ASSERT_EQ(grpc_http2_status_to_grpc_status(a), (b))
41
TEST(StatusConversionTest,TestGrpcStatusToHttp2Error)42 TEST(StatusConversionTest, TestGrpcStatusToHttp2Error) {
43 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_OK, GRPC_HTTP2_NO_ERROR);
44 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_CANCELLED, GRPC_HTTP2_CANCEL);
45 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_UNKNOWN, GRPC_HTTP2_INTERNAL_ERROR);
46 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_INVALID_ARGUMENT,
47 GRPC_HTTP2_INTERNAL_ERROR);
48 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_DEADLINE_EXCEEDED, GRPC_HTTP2_CANCEL);
49 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_NOT_FOUND, GRPC_HTTP2_INTERNAL_ERROR);
50 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_ALREADY_EXISTS,
51 GRPC_HTTP2_INTERNAL_ERROR);
52 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_PERMISSION_DENIED,
53 GRPC_HTTP2_INADEQUATE_SECURITY);
54 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_UNAUTHENTICATED,
55 GRPC_HTTP2_INTERNAL_ERROR);
56 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_RESOURCE_EXHAUSTED,
57 GRPC_HTTP2_ENHANCE_YOUR_CALM);
58 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_FAILED_PRECONDITION,
59 GRPC_HTTP2_INTERNAL_ERROR);
60 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_ABORTED, GRPC_HTTP2_INTERNAL_ERROR);
61 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_OUT_OF_RANGE,
62 GRPC_HTTP2_INTERNAL_ERROR);
63 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_UNIMPLEMENTED,
64 GRPC_HTTP2_INTERNAL_ERROR);
65 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_INTERNAL, GRPC_HTTP2_INTERNAL_ERROR);
66 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_UNAVAILABLE,
67 GRPC_HTTP2_REFUSED_STREAM);
68 GRPC_STATUS_TO_HTTP2_ERROR(GRPC_STATUS_DATA_LOSS, GRPC_HTTP2_INTERNAL_ERROR);
69 }
70
TEST(StatusConversionTest,TestGrpcStatusToHttp2Status)71 TEST(StatusConversionTest, TestGrpcStatusToHttp2Status) {
72 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_OK, 200);
73 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_CANCELLED, 200);
74 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_UNKNOWN, 200);
75 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_INVALID_ARGUMENT, 200);
76 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_DEADLINE_EXCEEDED, 200);
77 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_NOT_FOUND, 200);
78 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_ALREADY_EXISTS, 200);
79 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_PERMISSION_DENIED, 200);
80 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_UNAUTHENTICATED, 200);
81 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_RESOURCE_EXHAUSTED, 200);
82 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_FAILED_PRECONDITION, 200);
83 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_ABORTED, 200);
84 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_OUT_OF_RANGE, 200);
85 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_UNIMPLEMENTED, 200);
86 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_INTERNAL, 200);
87 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_UNAVAILABLE, 200);
88 GRPC_STATUS_TO_HTTP2_STATUS(GRPC_STATUS_DATA_LOSS, 200);
89 }
90
TEST(StatusConversionTest,TestHttp2ErrorToGrpcStatus)91 TEST(StatusConversionTest, TestHttp2ErrorToGrpcStatus) {
92 const grpc_core::Timestamp before_deadline =
93 grpc_core::Timestamp::InfFuture();
94 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_NO_ERROR, before_deadline,
95 GRPC_STATUS_INTERNAL);
96 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_PROTOCOL_ERROR, before_deadline,
97 GRPC_STATUS_INTERNAL);
98 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_INTERNAL_ERROR, before_deadline,
99 GRPC_STATUS_INTERNAL);
100 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_FLOW_CONTROL_ERROR, before_deadline,
101 GRPC_STATUS_INTERNAL);
102 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_SETTINGS_TIMEOUT, before_deadline,
103 GRPC_STATUS_INTERNAL);
104 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_STREAM_CLOSED, before_deadline,
105 GRPC_STATUS_INTERNAL);
106 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_FRAME_SIZE_ERROR, before_deadline,
107 GRPC_STATUS_INTERNAL);
108 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_REFUSED_STREAM, before_deadline,
109 GRPC_STATUS_UNAVAILABLE);
110 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_CANCEL, before_deadline,
111 GRPC_STATUS_CANCELLED);
112 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_COMPRESSION_ERROR, before_deadline,
113 GRPC_STATUS_INTERNAL);
114 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_CONNECT_ERROR, before_deadline,
115 GRPC_STATUS_INTERNAL);
116 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_ENHANCE_YOUR_CALM, before_deadline,
117 GRPC_STATUS_RESOURCE_EXHAUSTED);
118 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_INADEQUATE_SECURITY, before_deadline,
119 GRPC_STATUS_PERMISSION_DENIED);
120
121 const grpc_core::Timestamp after_deadline;
122 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_NO_ERROR, after_deadline,
123 GRPC_STATUS_INTERNAL);
124 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_PROTOCOL_ERROR, after_deadline,
125 GRPC_STATUS_INTERNAL);
126 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_INTERNAL_ERROR, after_deadline,
127 GRPC_STATUS_INTERNAL);
128 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_FLOW_CONTROL_ERROR, after_deadline,
129 GRPC_STATUS_INTERNAL);
130 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_SETTINGS_TIMEOUT, after_deadline,
131 GRPC_STATUS_INTERNAL);
132 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_STREAM_CLOSED, after_deadline,
133 GRPC_STATUS_INTERNAL);
134 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_FRAME_SIZE_ERROR, after_deadline,
135 GRPC_STATUS_INTERNAL);
136 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_REFUSED_STREAM, after_deadline,
137 GRPC_STATUS_UNAVAILABLE);
138 // We only have millisecond granularity in our timing code. This sleeps for 5
139 // millis to ensure that the status conversion code will pick up the fact
140 // that the deadline has expired.
141 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
142 gpr_time_from_millis(5, GPR_TIMESPAN)));
143 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_CANCEL, after_deadline,
144 GRPC_STATUS_DEADLINE_EXCEEDED);
145 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_COMPRESSION_ERROR, after_deadline,
146 GRPC_STATUS_INTERNAL);
147 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_CONNECT_ERROR, after_deadline,
148 GRPC_STATUS_INTERNAL);
149 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_ENHANCE_YOUR_CALM, after_deadline,
150 GRPC_STATUS_RESOURCE_EXHAUSTED);
151 HTTP2_ERROR_TO_GRPC_STATUS(GRPC_HTTP2_INADEQUATE_SECURITY, after_deadline,
152 GRPC_STATUS_PERMISSION_DENIED);
153 }
154
TEST(StatusConversionTest,TestHttp2StatusToGrpcStatus)155 TEST(StatusConversionTest, TestHttp2StatusToGrpcStatus) {
156 HTTP2_STATUS_TO_GRPC_STATUS(200, GRPC_STATUS_OK);
157 HTTP2_STATUS_TO_GRPC_STATUS(400, GRPC_STATUS_INTERNAL);
158 HTTP2_STATUS_TO_GRPC_STATUS(401, GRPC_STATUS_UNAUTHENTICATED);
159 HTTP2_STATUS_TO_GRPC_STATUS(403, GRPC_STATUS_PERMISSION_DENIED);
160 HTTP2_STATUS_TO_GRPC_STATUS(404, GRPC_STATUS_UNIMPLEMENTED);
161 HTTP2_STATUS_TO_GRPC_STATUS(409, GRPC_STATUS_UNKNOWN);
162 HTTP2_STATUS_TO_GRPC_STATUS(412, GRPC_STATUS_UNKNOWN);
163 HTTP2_STATUS_TO_GRPC_STATUS(429, GRPC_STATUS_UNAVAILABLE);
164 HTTP2_STATUS_TO_GRPC_STATUS(499, GRPC_STATUS_UNKNOWN);
165 HTTP2_STATUS_TO_GRPC_STATUS(500, GRPC_STATUS_UNKNOWN);
166 HTTP2_STATUS_TO_GRPC_STATUS(502, GRPC_STATUS_UNAVAILABLE);
167 HTTP2_STATUS_TO_GRPC_STATUS(503, GRPC_STATUS_UNAVAILABLE);
168 HTTP2_STATUS_TO_GRPC_STATUS(504, GRPC_STATUS_UNAVAILABLE);
169 }
170
TEST(StatusConversionTest,TestGrpcHttp2StatusToGrpcStatusAll)171 TEST(StatusConversionTest, TestGrpcHttp2StatusToGrpcStatusAll) {
172 // check all status values can be converted
173 for (int i = 0; i <= 999; i++) {
174 grpc_http2_status_to_grpc_status(i);
175 }
176 }
177
main(int argc,char ** argv)178 int main(int argc, char** argv) {
179 grpc::testing::TestEnvironment env(&argc, argv);
180 ::testing::InitGoogleTest(&argc, argv);
181 grpc::testing::TestGrpcScope grpc_scope;
182 return RUN_ALL_TESTS();
183 }
184