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