blob: 1699d271427c1ed8092f3d32273854d108738f37 [file] [log] [blame]
David Garcia Quintas48ec8062015-12-11 14:04:59 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
David Garcia Quintas48ec8062015-12-11 14:04:59 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
David Garcia Quintas48ec8062015-12-11 14:04:59 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
David Garcia Quintas48ec8062015-12-11 14:04:59 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
David Garcia Quintas48ec8062015-12-11 14:04:59 -080016 *
17 */
18
Craig Tiller9533d042016-03-25 17:11:06 -070019#include "src/core/lib/compression/algorithm_metadata.h"
David Garcia Quintas48ec8062015-12-11 14:04:59 -080020
21#include <stdlib.h>
22#include <string.h>
23
24#include <grpc/grpc.h>
25#include <grpc/support/log.h>
26#include <grpc/support/useful.h>
27
Craig Tiller7c70b6c2017-01-23 07:48:42 -080028#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070029#include "src/core/lib/transport/static_metadata.h"
David Garcia Quintas48ec8062015-12-11 14:04:59 -080030#include "test/core/util/test_config.h"
31
Muxi Yan4c7bc852017-10-11 16:23:17 -070032const uint32_t message_prefix_length = 8;
Muxi Yane6d888d2017-09-14 09:42:56 -070033const uint32_t stream_prefix_length = 7;
David Garcia Quintas48ec8062015-12-11 14:04:59 -080034static 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++) {
Muxi Yan38fcd0c2017-12-06 18:52:18 -080040 const char* name;
David Garcia Quintas48ec8062015-12-11 14:04:59 -080041 grpc_compression_algorithm parsed;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080042 grpc_slice mdstr;
43 grpc_mdelem mdelem;
Yash Tibrewal8cf14702017-12-06 09:47:54 -080044 grpc_core::ExecCtx exec_ctx;
David Garcia Quintas48ec8062015-12-11 14:04:59 -080045 GPR_ASSERT(
46 grpc_compression_algorithm_name((grpc_compression_algorithm)i, &name));
Craig Tiller7c70b6c2017-01-23 07:48:42 -080047 GPR_ASSERT(grpc_compression_algorithm_parse(
48 grpc_slice_from_static_string(name), &parsed));
David Garcia Quintas48ec8062015-12-11 14:04:59 -080049 GPR_ASSERT((int)parsed == i);
Craig Tiller7c70b6c2017-01-23 07:48:42 -080050 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));
Muxi Yane6d888d2017-09-14 09:42:56 -070053 if (parsed == 0) {
54 continue;
55 } else if (grpc_compression_algorithm_is_message(parsed)) {
Muxi Yan68ee7ce2017-09-25 12:38:35 -070056 mdelem = grpc_message_compression_encoding_mdelem(
57 grpc_compression_algorithm_to_message_compression_algorithm(parsed));
Muxi Yane6d888d2017-09-14 09:42:56 -070058 grpc_slice value = GRPC_MDVALUE(mdelem);
Muxi Yan4c7bc852017-10-11 16:23:17 -070059 GPR_ASSERT(0 == memcmp(&name[message_prefix_length],
Muxi Yan68ee7ce2017-09-25 12:38:35 -070060 GRPC_SLICE_START_PTR(value),
61 GRPC_SLICE_LENGTH(value)));
Muxi Yane6d888d2017-09-14 09:42:56 -070062 GPR_ASSERT(grpc_slice_eq(GRPC_MDKEY(mdelem), GRPC_MDSTR_GRPC_ENCODING));
63 } else {
Muxi Yan68ee7ce2017-09-25 12:38:35 -070064 mdelem = grpc_stream_compression_encoding_mdelem(
65 grpc_compression_algorithm_to_stream_compression_algorithm(parsed));
Muxi Yane6d888d2017-09-14 09:42:56 -070066 grpc_slice value = GRPC_MDVALUE(mdelem);
Muxi Yan68ee7ce2017-09-25 12:38:35 -070067 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));
Muxi Yane6d888d2017-09-14 09:42:56 -070072 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -080073 grpc_slice_unref_internal(mdstr);
74 GRPC_MDELEM_UNREF(mdelem);
David Garcia Quintas48ec8062015-12-11 14:04:59 -080075 }
76
77 /* test failure */
Craig Tiller7c70b6c2017-01-23 07:48:42 -080078 GPR_ASSERT(GRPC_MDISNULL(
79 grpc_compression_encoding_mdelem(GRPC_COMPRESS_ALGORITHMS_COUNT)));
David Garcia Quintas48ec8062015-12-11 14:04:59 -080080}
81
82static void test_algorithm_failure(void) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -080083 grpc_core::ExecCtx exec_ctx;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080084 grpc_slice mdstr;
David Garcia Quintas48ec8062015-12-11 14:04:59 -080085
86 gpr_log(GPR_DEBUG, "test_algorithm_failure");
87
88 GPR_ASSERT(grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080089 nullptr) == 0);
Yash Tibrewal34a57d02017-10-23 15:33:21 -070090 GPR_ASSERT(
91 grpc_compression_algorithm_name(static_cast<grpc_compression_algorithm>(
92 GRPC_COMPRESS_ALGORITHMS_COUNT + 1),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080093 nullptr) == 0);
Craig Tiller7c70b6c2017-01-23 07:48:42 -080094 mdstr = grpc_slice_from_static_string("this-is-an-invalid-algorithm");
95 GPR_ASSERT(grpc_compression_algorithm_from_slice(mdstr) ==
David Garcia Quintas48ec8062015-12-11 14:04:59 -080096 GRPC_COMPRESS_ALGORITHMS_COUNT);
Craig Tiller7c70b6c2017-01-23 07:48:42 -080097 GPR_ASSERT(grpc_slice_eq(
98 grpc_compression_algorithm_slice(GRPC_COMPRESS_ALGORITHMS_COUNT),
99 grpc_empty_slice()));
100 GPR_ASSERT(grpc_slice_eq(
Yash Tibrewal34a57d02017-10-23 15:33:21 -0700101 grpc_compression_algorithm_slice(static_cast<grpc_compression_algorithm>(
102 static_cast<int>(GRPC_COMPRESS_ALGORITHMS_COUNT) + 1)),
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800103 grpc_empty_slice()));
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800104 grpc_slice_unref_internal(mdstr);
David Garcia Quintas48ec8062015-12-11 14:04:59 -0800105}
106
Muxi Yan38fcd0c2017-12-06 18:52:18 -0800107int main(int argc, char** argv) {
David Garcia Quintas48ec8062015-12-11 14:04:59 -0800108 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}