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 | |
| 39 | #include <grpc/support/log.h> |
| 40 | #include <grpc/support/useful.h> |
| 41 | #include "test/core/util/test_config.h" |
| 42 | |
| 43 | #define LOG_TEST() gpr_log(GPR_INFO, "%s", __FUNCTION__) |
| 44 | |
| 45 | static void assert_encodes_as(gpr_timespec ts, const char *s) { |
| 46 | char buffer[32]; |
| 47 | grpc_chttp2_encode_timeout(ts, buffer); |
| 48 | gpr_log(GPR_INFO, "check '%s' == '%s'", buffer, s); |
| 49 | GPR_ASSERT(0 == strcmp(buffer, s)); |
| 50 | } |
| 51 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame^] | 52 | void test_encoding(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | LOG_TEST(); |
| 54 | assert_encodes_as(gpr_time_from_micros(-1), "1n"); |
| 55 | assert_encodes_as(gpr_time_from_seconds(-10), "1n"); |
| 56 | assert_encodes_as(gpr_time_from_nanos(10), "10n"); |
| 57 | assert_encodes_as(gpr_time_from_nanos(999999999), "1S"); |
| 58 | assert_encodes_as(gpr_time_from_micros(1), "1u"); |
| 59 | assert_encodes_as(gpr_time_from_micros(10), "10u"); |
| 60 | assert_encodes_as(gpr_time_from_micros(100), "100u"); |
| 61 | assert_encodes_as(gpr_time_from_micros(890), "890u"); |
| 62 | assert_encodes_as(gpr_time_from_micros(900), "900u"); |
| 63 | assert_encodes_as(gpr_time_from_micros(901), "901u"); |
| 64 | assert_encodes_as(gpr_time_from_millis(1), "1m"); |
| 65 | assert_encodes_as(gpr_time_from_millis(2), "2m"); |
| 66 | assert_encodes_as(gpr_time_from_micros(10001), "10100u"); |
| 67 | assert_encodes_as(gpr_time_from_micros(999999), "1S"); |
| 68 | assert_encodes_as(gpr_time_from_millis(1000), "1S"); |
| 69 | assert_encodes_as(gpr_time_from_millis(2000), "2S"); |
| 70 | assert_encodes_as(gpr_time_from_millis(2500), "2500m"); |
| 71 | assert_encodes_as(gpr_time_from_millis(59900), "59900m"); |
| 72 | assert_encodes_as(gpr_time_from_seconds(50), "50S"); |
| 73 | assert_encodes_as(gpr_time_from_seconds(59), "59S"); |
| 74 | assert_encodes_as(gpr_time_from_seconds(60), "1M"); |
| 75 | assert_encodes_as(gpr_time_from_seconds(80), "80S"); |
| 76 | assert_encodes_as(gpr_time_from_seconds(90), "90S"); |
| 77 | assert_encodes_as(gpr_time_from_minutes(2), "2M"); |
| 78 | assert_encodes_as(gpr_time_from_minutes(20), "20M"); |
| 79 | assert_encodes_as(gpr_time_from_hours(1), "1H"); |
| 80 | assert_encodes_as(gpr_time_from_hours(10), "10H"); |
| 81 | assert_encodes_as(gpr_time_from_seconds(1000000000), "1000000000S"); |
| 82 | } |
| 83 | |
| 84 | static void assert_decodes_as(const char *buffer, gpr_timespec expected) { |
| 85 | gpr_timespec got; |
| 86 | gpr_log(GPR_INFO, "check decoding '%s'", buffer); |
| 87 | GPR_ASSERT(1 == grpc_chttp2_decode_timeout(buffer, &got)); |
| 88 | GPR_ASSERT(0 == gpr_time_cmp(got, expected)); |
| 89 | } |
| 90 | |
| 91 | void decode_suite(char ext, gpr_timespec (*answer)(long x)) { |
| 92 | long test_vals[] = {1, 12, 123, 1234, 12345, 123456, |
| 93 | 1234567, 12345678, 123456789, 98765432, 9876543, 987654, |
| 94 | 98765, 9876, 987, 98, 9}; |
| 95 | int i; |
| 96 | char input[32]; |
| 97 | for (i = 0; i < GPR_ARRAY_SIZE(test_vals); i++) { |
| 98 | sprintf(input, "%ld%c", test_vals[i], ext); |
| 99 | assert_decodes_as(input, answer(test_vals[i])); |
| 100 | sprintf(input, " %ld%c", test_vals[i], ext); |
| 101 | assert_decodes_as(input, answer(test_vals[i])); |
| 102 | sprintf(input, "%ld %c", test_vals[i], ext); |
| 103 | assert_decodes_as(input, answer(test_vals[i])); |
| 104 | sprintf(input, "%ld %c ", test_vals[i], ext); |
| 105 | assert_decodes_as(input, answer(test_vals[i])); |
| 106 | } |
| 107 | } |
| 108 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame^] | 109 | void test_decoding(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | LOG_TEST(); |
| 111 | decode_suite('n', gpr_time_from_nanos); |
| 112 | decode_suite('u', gpr_time_from_micros); |
| 113 | decode_suite('m', gpr_time_from_millis); |
| 114 | decode_suite('S', gpr_time_from_seconds); |
| 115 | decode_suite('M', gpr_time_from_minutes); |
| 116 | decode_suite('H', gpr_time_from_hours); |
| 117 | assert_decodes_as("1000000000000000000000u", gpr_inf_future); |
| 118 | } |
| 119 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame^] | 120 | void test_decoding_fails(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | gpr_timespec x; |
| 122 | LOG_TEST(); |
| 123 | GPR_ASSERT(0 == grpc_chttp2_decode_timeout("", &x)); |
| 124 | GPR_ASSERT(0 == grpc_chttp2_decode_timeout(" ", &x)); |
| 125 | GPR_ASSERT(0 == grpc_chttp2_decode_timeout("x", &x)); |
| 126 | GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1", &x)); |
| 127 | GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1x", &x)); |
| 128 | GPR_ASSERT(0 == grpc_chttp2_decode_timeout("1ux", &x)); |
| 129 | GPR_ASSERT(0 == grpc_chttp2_decode_timeout("!", &x)); |
| 130 | GPR_ASSERT(0 == grpc_chttp2_decode_timeout("n1", &x)); |
| 131 | GPR_ASSERT(0 == grpc_chttp2_decode_timeout("-1u", &x)); |
| 132 | } |
| 133 | |
| 134 | int main(int argc, char **argv) { |
| 135 | grpc_test_init(argc, argv); |
| 136 | test_encoding(); |
| 137 | test_decoding(); |
| 138 | test_decoding_fails(); |
| 139 | return 0; |
| 140 | } |