Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 39 | #include "src/core/support/string.h" |
| 40 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 42 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <grpc/support/useful.h> |
| 44 | #include "test/core/util/test_config.h" |
| 45 | |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 46 | #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 48 | static void assert_encodes_as(gpr_timespec ts, const char *s) { |
Craig Tiller | b7b9c75 | 2015-01-23 13:52:43 -0800 | [diff] [blame] | 49 | char buffer[GRPC_CHTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 50 | grpc_chttp2_encode_timeout(ts, buffer); |
| 51 | gpr_log(GPR_INFO, "check '%s' == '%s'", buffer, s); |
| 52 | GPR_ASSERT(0 == strcmp(buffer, s)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 55 | void 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 88 | static void assert_decodes_as(const char *buffer, gpr_timespec expected) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | gpr_timespec got; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 90 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 95 | void 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" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 100 | unsigned i; |
Craig Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 101 | char *input; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 102 | 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 Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 106 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 107 | 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 Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 110 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 111 | 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 Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 114 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 115 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 121 | void 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 133 | void test_decoding_fails(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 134 | gpr_timespec x; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 135 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 147 | int main(int argc, char **argv) { |
| 148 | grpc_test_init(argc, argv); |
| 149 | test_encoding(); |
| 150 | test_decoding(); |
| 151 | test_decoding_fails(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 152 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 153 | } |