blob: 39c993ae595bf4f851586a703770bc25cb645a49 [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>
42#include <grpc/support/useful.h>
43#include "test/core/util/test_config.h"
44
45#define LOG_TEST() gpr_log(GPR_INFO, "%s", __FUNCTION__)
46
47static void assert_encodes_as(gpr_timespec ts, const char *s) {
Craig Tillerb7b9c752015-01-23 13:52:43 -080048 char buffer[GRPC_CHTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049 grpc_chttp2_encode_timeout(ts, buffer);
50 gpr_log(GPR_INFO, "check '%s' == '%s'", buffer, s);
51 GPR_ASSERT(0 == strcmp(buffer, s));
52}
53
Craig Tiller32946d32015-01-15 11:37:30 -080054void test_encoding(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055 LOG_TEST();
56 assert_encodes_as(gpr_time_from_micros(-1), "1n");
57 assert_encodes_as(gpr_time_from_seconds(-10), "1n");
58 assert_encodes_as(gpr_time_from_nanos(10), "10n");
59 assert_encodes_as(gpr_time_from_nanos(999999999), "1S");
60 assert_encodes_as(gpr_time_from_micros(1), "1u");
61 assert_encodes_as(gpr_time_from_micros(10), "10u");
62 assert_encodes_as(gpr_time_from_micros(100), "100u");
63 assert_encodes_as(gpr_time_from_micros(890), "890u");
64 assert_encodes_as(gpr_time_from_micros(900), "900u");
65 assert_encodes_as(gpr_time_from_micros(901), "901u");
66 assert_encodes_as(gpr_time_from_millis(1), "1m");
67 assert_encodes_as(gpr_time_from_millis(2), "2m");
68 assert_encodes_as(gpr_time_from_micros(10001), "10100u");
69 assert_encodes_as(gpr_time_from_micros(999999), "1S");
70 assert_encodes_as(gpr_time_from_millis(1000), "1S");
71 assert_encodes_as(gpr_time_from_millis(2000), "2S");
72 assert_encodes_as(gpr_time_from_millis(2500), "2500m");
73 assert_encodes_as(gpr_time_from_millis(59900), "59900m");
74 assert_encodes_as(gpr_time_from_seconds(50), "50S");
75 assert_encodes_as(gpr_time_from_seconds(59), "59S");
76 assert_encodes_as(gpr_time_from_seconds(60), "1M");
77 assert_encodes_as(gpr_time_from_seconds(80), "80S");
78 assert_encodes_as(gpr_time_from_seconds(90), "90S");
79 assert_encodes_as(gpr_time_from_minutes(2), "2M");
80 assert_encodes_as(gpr_time_from_minutes(20), "20M");
81 assert_encodes_as(gpr_time_from_hours(1), "1H");
82 assert_encodes_as(gpr_time_from_hours(10), "10H");
83 assert_encodes_as(gpr_time_from_seconds(1000000000), "1000000000S");
84}
85
86static void assert_decodes_as(const char *buffer, gpr_timespec expected) {
87 gpr_timespec got;
88 gpr_log(GPR_INFO, "check decoding '%s'", buffer);
89 GPR_ASSERT(1 == grpc_chttp2_decode_timeout(buffer, &got));
90 GPR_ASSERT(0 == gpr_time_cmp(got, expected));
91}
92
93void decode_suite(char ext, gpr_timespec (*answer)(long x)) {
94 long test_vals[] = {1, 12, 123, 1234, 12345, 123456,
95 1234567, 12345678, 123456789, 98765432, 9876543, 987654,
96 98765, 9876, 987, 98, 9};
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +010097 unsigned i;
Craig Tiller03f75252015-01-23 15:22:42 -080098 char *input;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080099 for (i = 0; i < GPR_ARRAY_SIZE(test_vals); i++) {
Craig Tiller03f75252015-01-23 15:22:42 -0800100 gpr_asprintf(&input, "%ld%c", test_vals[i], ext);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101 assert_decodes_as(input, answer(test_vals[i]));
Craig Tiller03f75252015-01-23 15:22:42 -0800102 gpr_free(input);
103
104 gpr_asprintf(&input, " %ld%c", test_vals[i], ext);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105 assert_decodes_as(input, answer(test_vals[i]));
Craig Tiller03f75252015-01-23 15:22:42 -0800106 gpr_free(input);
107
108 gpr_asprintf(&input, "%ld %c", test_vals[i], ext);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 assert_decodes_as(input, answer(test_vals[i]));
Craig Tiller03f75252015-01-23 15:22:42 -0800110 gpr_free(input);
111
112 gpr_asprintf(&input, "%ld %c ", test_vals[i], ext);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113 assert_decodes_as(input, answer(test_vals[i]));
Craig Tiller03f75252015-01-23 15:22:42 -0800114 gpr_free(input);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115 }
116}
117
Craig Tiller32946d32015-01-15 11:37:30 -0800118void test_decoding(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119 LOG_TEST();
120 decode_suite('n', gpr_time_from_nanos);
121 decode_suite('u', gpr_time_from_micros);
122 decode_suite('m', gpr_time_from_millis);
123 decode_suite('S', gpr_time_from_seconds);
124 decode_suite('M', gpr_time_from_minutes);
125 decode_suite('H', gpr_time_from_hours);
126 assert_decodes_as("1000000000000000000000u", gpr_inf_future);
127}
128
Craig Tiller32946d32015-01-15 11:37:30 -0800129void test_decoding_fails(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130 gpr_timespec x;
131 LOG_TEST();
132 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("", &x));
133 GPR_ASSERT(0 == grpc_chttp2_decode_timeout(" ", &x));
134 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("x", &x));
135 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1", &x));
136 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1x", &x));
137 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1ux", &x));
138 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("!", &x));
139 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("n1", &x));
140 GPR_ASSERT(0 == grpc_chttp2_decode_timeout("-1u", &x));
141}
142
143int main(int argc, char **argv) {
144 grpc_test_init(argc, argv);
145 test_encoding();
146 test_decoding();
147 test_decoding_fails();
148 return 0;
Craig Tiller06059952015-02-18 08:34:56 -0800149}