• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include "src/core/ext/transport/chttp2/transport/varint.h"
20 
21 #include <grpc/grpc.h>
22 #include <grpc/slice.h>
23 #include <grpc/support/log.h>
24 
25 #include "test/core/util/test_config.h"
26 
test_varint(uint32_t value,uint32_t prefix_bits,uint8_t prefix_or,const char * expect_bytes,size_t expect_length)27 static void test_varint(uint32_t value, uint32_t prefix_bits, uint8_t prefix_or,
28                         const char* expect_bytes, size_t expect_length) {
29   uint32_t nbytes = GRPC_CHTTP2_VARINT_LENGTH(value, prefix_bits);
30   grpc_slice expect =
31       grpc_slice_from_copied_buffer(expect_bytes, expect_length);
32   grpc_slice slice;
33   gpr_log(GPR_DEBUG, "Test: 0x%08x", value);
34   GPR_ASSERT(nbytes == expect_length);
35   slice = grpc_slice_malloc(nbytes);
36   GRPC_CHTTP2_WRITE_VARINT(value, prefix_bits, prefix_or,
37                            GRPC_SLICE_START_PTR(slice), nbytes);
38   GPR_ASSERT(grpc_slice_eq(expect, slice));
39   grpc_slice_unref(expect);
40   grpc_slice_unref(slice);
41 }
42 
43 #define TEST_VARINT(value, prefix_bits, prefix_or, expect) \
44   test_varint(value, prefix_bits, prefix_or, expect, sizeof(expect) - 1)
45 
main(int argc,char ** argv)46 int main(int argc, char** argv) {
47   grpc_test_init(argc, argv);
48   grpc_init();
49   TEST_VARINT(0, 1, 0, "\x00");
50   TEST_VARINT(128, 1, 0, "\x7f\x01");
51   TEST_VARINT(16384, 1, 0, "\x7f\x81\x7f");
52   TEST_VARINT(2097152, 1, 0, "\x7f\x81\xff\x7f");
53   TEST_VARINT(268435456, 1, 0, "\x7f\x81\xff\xff\x7f");
54   TEST_VARINT(0xffffffff, 1, 0, "\x7f\x80\xff\xff\xff\x0f");
55   grpc_shutdown();
56   return 0;
57 }
58