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/timeout_encoding.h"
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/lib/gpr/murmur_hash.h"
29 #include "src/core/lib/gpr/string.h"
30 #include "src/core/lib/gpr/useful.h"
31 #include "test/core/util/test_config.h"
32
33 #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
34
assert_encodes_as(grpc_millis ts,const char * s)35 static void assert_encodes_as(grpc_millis ts, const char* s) {
36 char buffer[GRPC_HTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE];
37 grpc_http2_encode_timeout(ts, buffer);
38 gpr_log(GPR_INFO, "check '%s' == '%s'", buffer, s);
39 GPR_ASSERT(0 == strcmp(buffer, s));
40 }
41
test_encoding(void)42 void test_encoding(void) {
43 LOG_TEST("test_encoding");
44 assert_encodes_as(-1, "1n");
45 assert_encodes_as(-10, "1n");
46 assert_encodes_as(1, "1m");
47 assert_encodes_as(10, "10m");
48 assert_encodes_as(100, "100m");
49 assert_encodes_as(890, "890m");
50 assert_encodes_as(900, "900m");
51 assert_encodes_as(901, "901m");
52 assert_encodes_as(1000, "1S");
53 assert_encodes_as(2000, "2S");
54 assert_encodes_as(2500, "2500m");
55 assert_encodes_as(59900, "59900m");
56 assert_encodes_as(50000, "50S");
57 assert_encodes_as(59000, "59S");
58 assert_encodes_as(60000, "1M");
59 assert_encodes_as(80000, "80S");
60 assert_encodes_as(90000, "90S");
61 assert_encodes_as(120000, "2M");
62 assert_encodes_as(20 * 60 * GPR_MS_PER_SEC, "20M");
63 assert_encodes_as(60 * 60 * GPR_MS_PER_SEC, "1H");
64 assert_encodes_as(10 * 60 * 60 * GPR_MS_PER_SEC, "10H");
65 }
66
assert_decodes_as(const char * buffer,grpc_millis expected)67 static void assert_decodes_as(const char* buffer, grpc_millis expected) {
68 grpc_millis got;
69 uint32_t hash = gpr_murmur_hash3(buffer, strlen(buffer), 0);
70 gpr_log(GPR_INFO, "check decoding '%s' (hash=0x%x)", buffer, hash);
71 GPR_ASSERT(1 == grpc_http2_decode_timeout(
72 grpc_slice_from_static_string(buffer), &got));
73 if (got != expected) {
74 gpr_log(GPR_ERROR, "got:'%" PRId64 "' != expected:'%" PRId64 "'", got,
75 expected);
76 abort();
77 }
78 }
79
decode_suite(char ext,grpc_millis (* answer)(int64_t x))80 void decode_suite(char ext, grpc_millis (*answer)(int64_t x)) {
81 long test_vals[] = {1, 12, 123, 1234, 12345, 123456,
82 1234567, 12345678, 123456789, 98765432, 9876543, 987654,
83 98765, 9876, 987, 98, 9};
84 unsigned i;
85 char* input;
86 for (i = 0; i < GPR_ARRAY_SIZE(test_vals); i++) {
87 gpr_asprintf(&input, "%ld%c", test_vals[i], ext);
88 assert_decodes_as(input, answer(test_vals[i]));
89 gpr_free(input);
90
91 gpr_asprintf(&input, " %ld%c", test_vals[i], ext);
92 assert_decodes_as(input, answer(test_vals[i]));
93 gpr_free(input);
94
95 gpr_asprintf(&input, "%ld %c", test_vals[i], ext);
96 assert_decodes_as(input, answer(test_vals[i]));
97 gpr_free(input);
98
99 gpr_asprintf(&input, "%ld %c ", test_vals[i], ext);
100 assert_decodes_as(input, answer(test_vals[i]));
101 gpr_free(input);
102 }
103 }
104
millis_from_nanos(int64_t x)105 static grpc_millis millis_from_nanos(int64_t x) {
106 return static_cast<grpc_millis>(x / GPR_NS_PER_MS + (x % GPR_NS_PER_MS != 0));
107 }
millis_from_micros(int64_t x)108 static grpc_millis millis_from_micros(int64_t x) {
109 return static_cast<grpc_millis>(x / GPR_US_PER_MS + (x % GPR_US_PER_MS != 0));
110 }
millis_from_millis(int64_t x)111 static grpc_millis millis_from_millis(int64_t x) {
112 return static_cast<grpc_millis>(x);
113 }
millis_from_seconds(int64_t x)114 static grpc_millis millis_from_seconds(int64_t x) {
115 return static_cast<grpc_millis>(x * GPR_MS_PER_SEC);
116 }
millis_from_minutes(int64_t x)117 static grpc_millis millis_from_minutes(int64_t x) {
118 return static_cast<grpc_millis>(x * 60 * GPR_MS_PER_SEC);
119 }
millis_from_hours(int64_t x)120 static grpc_millis millis_from_hours(int64_t x) {
121 return static_cast<grpc_millis>(x * 3600 * GPR_MS_PER_SEC);
122 }
123
test_decoding(void)124 void test_decoding(void) {
125 LOG_TEST("test_decoding");
126 decode_suite('n', millis_from_nanos);
127 decode_suite('u', millis_from_micros);
128 decode_suite('m', millis_from_millis);
129 decode_suite('S', millis_from_seconds);
130 decode_suite('M', millis_from_minutes);
131 decode_suite('H', millis_from_hours);
132 assert_decodes_as("1000000000S", millis_from_seconds(1000 * 1000 * 1000));
133 assert_decodes_as("1000000000000000000000u", GRPC_MILLIS_INF_FUTURE);
134 assert_decodes_as("1000000001S", GRPC_MILLIS_INF_FUTURE);
135 assert_decodes_as("2000000001S", GRPC_MILLIS_INF_FUTURE);
136 assert_decodes_as("9999999999S", GRPC_MILLIS_INF_FUTURE);
137 }
138
assert_decoding_fails(const char * s)139 static void assert_decoding_fails(const char* s) {
140 grpc_millis x;
141 GPR_ASSERT(0 ==
142 grpc_http2_decode_timeout(grpc_slice_from_static_string(s), &x));
143 }
144
test_decoding_fails(void)145 void test_decoding_fails(void) {
146 LOG_TEST("test_decoding_fails");
147 assert_decoding_fails("");
148 assert_decoding_fails(" ");
149 assert_decoding_fails("x");
150 assert_decoding_fails("1");
151 assert_decoding_fails("1x");
152 assert_decoding_fails("1ux");
153 assert_decoding_fails("!");
154 assert_decoding_fails("n1");
155 assert_decoding_fails("-1u");
156 }
157
main(int argc,char ** argv)158 int main(int argc, char** argv) {
159 grpc_test_init(argc, argv);
160 test_encoding();
161 test_decoding();
162 test_decoding_fails();
163 return 0;
164 }
165