• 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/lib/compression/algorithm_metadata.h"
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/support/log.h>
26 
27 #include "src/core/lib/iomgr/exec_ctx.h"
28 #include "src/core/lib/slice/slice_internal.h"
29 #include "src/core/lib/transport/static_metadata.h"
30 #include "test/core/util/test_config.h"
31 
32 const uint32_t message_prefix_length = 0;
33 const uint32_t stream_prefix_length = 7;
test_algorithm_mesh(void)34 static void test_algorithm_mesh(void) {
35   int i;
36 
37   gpr_log(GPR_DEBUG, "test_algorithm_mesh");
38 
39   for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
40     const char* name;
41     grpc_compression_algorithm parsed;
42     grpc_slice mdstr;
43     grpc_mdelem mdelem;
44     grpc_core::ExecCtx exec_ctx;
45     GPR_ASSERT(
46         grpc_compression_algorithm_name((grpc_compression_algorithm)i, &name));
47     GPR_ASSERT(grpc_compression_algorithm_parse(
48         grpc_slice_from_static_string(name), &parsed));
49     GPR_ASSERT((int)parsed == i);
50     mdstr = grpc_slice_from_copied_string(name);
51     GPR_ASSERT(grpc_slice_eq(mdstr, grpc_compression_algorithm_slice(parsed)));
52     GPR_ASSERT(parsed == grpc_compression_algorithm_from_slice(mdstr));
53     if (parsed == 0) {
54       continue;
55     } else if (grpc_compression_algorithm_is_message(parsed)) {
56       mdelem = grpc_message_compression_encoding_mdelem(
57           grpc_compression_algorithm_to_message_compression_algorithm(parsed));
58       grpc_slice value = GRPC_MDVALUE(mdelem);
59       GPR_ASSERT(0 == memcmp(&name[message_prefix_length],
60                              GRPC_SLICE_START_PTR(value),
61                              GRPC_SLICE_LENGTH(value)));
62       GPR_ASSERT(grpc_slice_eq(GRPC_MDKEY(mdelem), GRPC_MDSTR_GRPC_ENCODING));
63     } else {
64       mdelem = grpc_stream_compression_encoding_mdelem(
65           grpc_compression_algorithm_to_stream_compression_algorithm(parsed));
66       grpc_slice value = GRPC_MDVALUE(mdelem);
67       GPR_ASSERT(0 == memcmp(&name[stream_prefix_length],
68                              GRPC_SLICE_START_PTR(value),
69                              GRPC_SLICE_LENGTH(value)));
70       GPR_ASSERT(
71           grpc_slice_eq(GRPC_MDKEY(mdelem), GRPC_MDSTR_CONTENT_ENCODING));
72     }
73     grpc_slice_unref_internal(mdstr);
74     GRPC_MDELEM_UNREF(mdelem);
75   }
76 
77   /* test failure */
78   GPR_ASSERT(GRPC_MDISNULL(
79       grpc_compression_encoding_mdelem(GRPC_COMPRESS_ALGORITHMS_COUNT)));
80 }
81 
test_algorithm_failure(void)82 static void test_algorithm_failure(void) {
83   grpc_core::ExecCtx exec_ctx;
84   grpc_slice mdstr;
85 
86   gpr_log(GPR_DEBUG, "test_algorithm_failure");
87 
88   GPR_ASSERT(grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT,
89                                              nullptr) == 0);
90   GPR_ASSERT(
91       grpc_compression_algorithm_name(static_cast<grpc_compression_algorithm>(
92                                           GRPC_COMPRESS_ALGORITHMS_COUNT + 1),
93                                       nullptr) == 0);
94   mdstr = grpc_slice_from_static_string("this-is-an-invalid-algorithm");
95   GPR_ASSERT(grpc_compression_algorithm_from_slice(mdstr) ==
96              GRPC_COMPRESS_ALGORITHMS_COUNT);
97   GPR_ASSERT(grpc_slice_eq(
98       grpc_compression_algorithm_slice(GRPC_COMPRESS_ALGORITHMS_COUNT),
99       grpc_empty_slice()));
100   GPR_ASSERT(grpc_slice_eq(
101       grpc_compression_algorithm_slice(static_cast<grpc_compression_algorithm>(
102           static_cast<int>(GRPC_COMPRESS_ALGORITHMS_COUNT) + 1)),
103       grpc_empty_slice()));
104   grpc_slice_unref_internal(mdstr);
105 }
106 
main(int argc,char ** argv)107 int main(int argc, char** argv) {
108   grpc_test_init(argc, argv);
109   grpc_init();
110 
111   test_algorithm_mesh();
112   test_algorithm_failure();
113 
114   grpc_shutdown();
115 
116   return 0;
117 }
118