blob: ba6c3191f1edbd50f707afee949ed7dc27ce416c [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/transport/chttp2/timeout_encoding.h"
35
36#include <stdio.h>
37#include <string.h>
38
Craig Tiller03f75252015-01-23 15:22:42 -080039#include "src/core/support/string.h"
40#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070042#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#include <grpc/support/useful.h>
44#include "test/core/util/test_config.h"
45
Craig Tiller35696192015-05-24 15:00:37 -070046#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047
Craig Tillera82950e2015-09-22 12:33:20 -070048static void assert_encodes_as(gpr_timespec ts, const char *s) {
Craig Tillerb7b9c752015-01-23 13:52:43 -080049 char buffer[GRPC_CHTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE];
Craig Tillera82950e2015-09-22 12:33:20 -070050 grpc_chttp2_encode_timeout(ts, buffer);
51 gpr_log(GPR_INFO, "check '%s' == '%s'", buffer, s);
52 GPR_ASSERT(0 == strcmp(buffer, s));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053}
54
Craig Tillera82950e2015-09-22 12:33:20 -070055void test_encoding(void) {
56 LOG_TEST("test_encoding");
57 assert_encodes_as(gpr_time_from_micros(-1, GPR_TIMESPAN), "1n");
58 assert_encodes_as(gpr_time_from_seconds(-10, GPR_TIMESPAN), "1n");
59 assert_encodes_as(gpr_time_from_nanos(10, GPR_TIMESPAN), "10n");
60 assert_encodes_as(gpr_time_from_nanos(999999999, GPR_TIMESPAN), "1S");
61 assert_encodes_as(gpr_time_from_micros(1, GPR_TIMESPAN), "1u");
62 assert_encodes_as(gpr_time_from_micros(10, GPR_TIMESPAN), "10u");
63 assert_encodes_as(gpr_time_from_micros(100, GPR_TIMESPAN), "100u");
64 assert_encodes_as(gpr_time_from_micros(890, GPR_TIMESPAN), "890u");
65 assert_encodes_as(gpr_time_from_micros(900, GPR_TIMESPAN), "900u");
66 assert_encodes_as(gpr_time_from_micros(901, GPR_TIMESPAN), "901u");
67 assert_encodes_as(gpr_time_from_millis(1, GPR_TIMESPAN), "1m");
68 assert_encodes_as(gpr_time_from_millis(2, GPR_TIMESPAN), "2m");
69 assert_encodes_as(gpr_time_from_micros(10001, GPR_TIMESPAN), "10100u");
70 assert_encodes_as(gpr_time_from_micros(999999, GPR_TIMESPAN), "1S");
71 assert_encodes_as(gpr_time_from_millis(1000, GPR_TIMESPAN), "1S");
72 assert_encodes_as(gpr_time_from_millis(2000, GPR_TIMESPAN), "2S");
73 assert_encodes_as(gpr_time_from_millis(2500, GPR_TIMESPAN), "2500m");
74 assert_encodes_as(gpr_time_from_millis(59900, GPR_TIMESPAN), "59900m");
75 assert_encodes_as(gpr_time_from_seconds(50, GPR_TIMESPAN), "50S");
76 assert_encodes_as(gpr_time_from_seconds(59, GPR_TIMESPAN), "59S");
77 assert_encodes_as(gpr_time_from_seconds(60, GPR_TIMESPAN), "1M");
78 assert_encodes_as(gpr_time_from_seconds(80, GPR_TIMESPAN), "80S");
79 assert_encodes_as(gpr_time_from_seconds(90, GPR_TIMESPAN), "90S");
80 assert_encodes_as(gpr_time_from_minutes(2, GPR_TIMESPAN), "2M");
81 assert_encodes_as(gpr_time_from_minutes(20, GPR_TIMESPAN), "20M");
82 assert_encodes_as(gpr_time_from_hours(1, GPR_TIMESPAN), "1H");
83 assert_encodes_as(gpr_time_from_hours(10, GPR_TIMESPAN), "10H");
84 assert_encodes_as(gpr_time_from_seconds(1000000000, GPR_TIMESPAN),
85 "1000000000S");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086}
87
Craig Tillera82950e2015-09-22 12:33:20 -070088static void assert_decodes_as(const char *buffer, gpr_timespec expected) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089 gpr_timespec got;
Craig Tillera82950e2015-09-22 12:33:20 -070090 gpr_log(GPR_INFO, "check decoding '%s'", buffer);
91 GPR_ASSERT(1 == grpc_chttp2_decode_timeout(buffer, &got));
92 GPR_ASSERT(0 == gpr_time_cmp(got, expected));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093}
94
Craig Tillera82950e2015-09-22 12:33:20 -070095void decode_suite(char ext,
96 gpr_timespec (*answer)(long x, gpr_clock_type clock)) {
97 long test_vals[] = {1, 12, 123, 1234, 12345, 123456,
98 1234567, 12345678, 123456789, 98765432, 9876543, 987654,
99 98765, 9876, 987, 98, 9};
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100100 unsigned i;
Craig Tiller03f75252015-01-23 15:22:42 -0800101 char *input;
Craig Tillera82950e2015-09-22 12:33:20 -0700102 for (i = 0; i < GPR_ARRAY_SIZE(test_vals); i++) {
103 gpr_asprintf(&input, "%ld%c", test_vals[i], ext);
104 assert_decodes_as(input, answer(test_vals[i], GPR_TIMESPAN));
105 gpr_free(input);
Craig Tiller03f75252015-01-23 15:22:42 -0800106
Craig Tillera82950e2015-09-22 12:33:20 -0700107 gpr_asprintf(&input, " %ld%c", test_vals[i], ext);
108 assert_decodes_as(input, answer(test_vals[i], GPR_TIMESPAN));
109 gpr_free(input);
Craig Tiller03f75252015-01-23 15:22:42 -0800110
Craig Tillera82950e2015-09-22 12:33:20 -0700111 gpr_asprintf(&input, "%ld %c", test_vals[i], ext);
112 assert_decodes_as(input, answer(test_vals[i], GPR_TIMESPAN));
113 gpr_free(input);
Craig Tiller03f75252015-01-23 15:22:42 -0800114
Craig Tillera82950e2015-09-22 12:33:20 -0700115 gpr_asprintf(&input, "%ld %c ", test_vals[i], ext);
116 assert_decodes_as(input, answer(test_vals[i], GPR_TIMESPAN));
117 gpr_free(input);
118 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119}
120
Craig Tillera82950e2015-09-22 12:33:20 -0700121void test_decoding(void) {
122 LOG_TEST("test_decoding");
123 decode_suite('n', gpr_time_from_nanos);
124 decode_suite('u', gpr_time_from_micros);
125 decode_suite('m', gpr_time_from_millis);
126 decode_suite('S', gpr_time_from_seconds);
127 decode_suite('M', gpr_time_from_minutes);
128 decode_suite('H', gpr_time_from_hours);
129 assert_decodes_as("1000000000000000000000u",
130 gpr_inf_future(GPR_CLOCK_REALTIME));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131}
132
Craig Tillera82950e2015-09-22 12:33:20 -0700133void test_decoding_fails(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134 gpr_timespec x;
Craig Tillera82950e2015-09-22 12:33:20 -0700135 LOG_TEST("test_decoding_fails");
136 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("", &x));
137 GPR_ASSERT(0 == grpc_chttp2_decode_timeout(" ", &x));
138 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("x", &x));
139 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1", &x));
140 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1x", &x));
141 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1ux", &x));
142 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("!", &x));
143 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("n1", &x));
144 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("-1u", &x));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800145}
146
Craig Tillera82950e2015-09-22 12:33:20 -0700147int main(int argc, char **argv) {
148 grpc_test_init(argc, argv);
149 test_encoding();
150 test_decoding();
151 test_decoding_fails();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800153}