Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 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> |
| 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 | |
| 47 | static void assert_encodes_as(gpr_timespec ts, const char *s) { |
Craig Tiller | b7b9c75 | 2015-01-23 13:52:43 -0800 | [diff] [blame] | 48 | char buffer[GRPC_CHTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | 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 Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 54 | void test_encoding(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | 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 | |
| 86 | static 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 | |
| 93 | void 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" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 97 | unsigned i; |
Craig Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 98 | char *input; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | for (i = 0; i < GPR_ARRAY_SIZE(test_vals); i++) { |
Craig Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 100 | gpr_asprintf(&input, "%ld%c", test_vals[i], ext); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | assert_decodes_as(input, answer(test_vals[i])); |
Craig Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 102 | gpr_free(input); |
| 103 | |
| 104 | gpr_asprintf(&input, " %ld%c", test_vals[i], ext); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | assert_decodes_as(input, answer(test_vals[i])); |
Craig Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 106 | gpr_free(input); |
| 107 | |
| 108 | gpr_asprintf(&input, "%ld %c", test_vals[i], ext); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | assert_decodes_as(input, answer(test_vals[i])); |
Craig Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 110 | gpr_free(input); |
| 111 | |
| 112 | gpr_asprintf(&input, "%ld %c ", test_vals[i], ext); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | assert_decodes_as(input, answer(test_vals[i])); |
Craig Tiller | 03f7525 | 2015-01-23 15:22:42 -0800 | [diff] [blame] | 114 | gpr_free(input); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 118 | void test_decoding(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 119 | 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 Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 129 | void test_decoding_fails(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 130 | 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 | |
| 143 | int main(int argc, char **argv) { |
| 144 | grpc_test_init(argc, argv); |
| 145 | test_encoding(); |
| 146 | test_decoding(); |
| 147 | test_decoding_fails(); |
| 148 | return 0; |
| 149 | } |