David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 19 | #include "src/core/lib/compression/algorithm_metadata.h" |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 20 | |
| 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 Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 28 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 29 | #include "src/core/lib/transport/static_metadata.h" |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 30 | #include "test/core/util/test_config.h" |
| 31 | |
Muxi Yan | 4c7bc85 | 2017-10-11 16:23:17 -0700 | [diff] [blame] | 32 | const uint32_t message_prefix_length = 8; |
Muxi Yan | e6d888d | 2017-09-14 09:42:56 -0700 | [diff] [blame] | 33 | const uint32_t stream_prefix_length = 7; |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 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++) { |
Muxi Yan | 38fcd0c | 2017-12-06 18:52:18 -0800 | [diff] [blame] | 40 | const char* name; |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 41 | grpc_compression_algorithm parsed; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 42 | grpc_slice mdstr; |
| 43 | grpc_mdelem mdelem; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 44 | grpc_core::ExecCtx exec_ctx; |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 45 | GPR_ASSERT( |
| 46 | grpc_compression_algorithm_name((grpc_compression_algorithm)i, &name)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 47 | GPR_ASSERT(grpc_compression_algorithm_parse( |
| 48 | grpc_slice_from_static_string(name), &parsed)); |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 49 | GPR_ASSERT((int)parsed == i); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 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)); |
Muxi Yan | e6d888d | 2017-09-14 09:42:56 -0700 | [diff] [blame] | 53 | if (parsed == 0) { |
| 54 | continue; |
| 55 | } else if (grpc_compression_algorithm_is_message(parsed)) { |
Muxi Yan | 68ee7ce | 2017-09-25 12:38:35 -0700 | [diff] [blame] | 56 | mdelem = grpc_message_compression_encoding_mdelem( |
| 57 | grpc_compression_algorithm_to_message_compression_algorithm(parsed)); |
Muxi Yan | e6d888d | 2017-09-14 09:42:56 -0700 | [diff] [blame] | 58 | grpc_slice value = GRPC_MDVALUE(mdelem); |
Muxi Yan | 4c7bc85 | 2017-10-11 16:23:17 -0700 | [diff] [blame] | 59 | GPR_ASSERT(0 == memcmp(&name[message_prefix_length], |
Muxi Yan | 68ee7ce | 2017-09-25 12:38:35 -0700 | [diff] [blame] | 60 | GRPC_SLICE_START_PTR(value), |
| 61 | GRPC_SLICE_LENGTH(value))); |
Muxi Yan | e6d888d | 2017-09-14 09:42:56 -0700 | [diff] [blame] | 62 | GPR_ASSERT(grpc_slice_eq(GRPC_MDKEY(mdelem), GRPC_MDSTR_GRPC_ENCODING)); |
| 63 | } else { |
Muxi Yan | 68ee7ce | 2017-09-25 12:38:35 -0700 | [diff] [blame] | 64 | mdelem = grpc_stream_compression_encoding_mdelem( |
| 65 | grpc_compression_algorithm_to_stream_compression_algorithm(parsed)); |
Muxi Yan | e6d888d | 2017-09-14 09:42:56 -0700 | [diff] [blame] | 66 | grpc_slice value = GRPC_MDVALUE(mdelem); |
Muxi Yan | 68ee7ce | 2017-09-25 12:38:35 -0700 | [diff] [blame] | 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)); |
Muxi Yan | e6d888d | 2017-09-14 09:42:56 -0700 | [diff] [blame] | 72 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 73 | grpc_slice_unref_internal(mdstr); |
| 74 | GRPC_MDELEM_UNREF(mdelem); |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* test failure */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 78 | GPR_ASSERT(GRPC_MDISNULL( |
| 79 | grpc_compression_encoding_mdelem(GRPC_COMPRESS_ALGORITHMS_COUNT))); |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void test_algorithm_failure(void) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 83 | grpc_core::ExecCtx exec_ctx; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 84 | grpc_slice mdstr; |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 85 | |
| 86 | gpr_log(GPR_DEBUG, "test_algorithm_failure"); |
| 87 | |
| 88 | GPR_ASSERT(grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT, |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 89 | nullptr) == 0); |
Yash Tibrewal | 34a57d0 | 2017-10-23 15:33:21 -0700 | [diff] [blame] | 90 | GPR_ASSERT( |
| 91 | grpc_compression_algorithm_name(static_cast<grpc_compression_algorithm>( |
| 92 | GRPC_COMPRESS_ALGORITHMS_COUNT + 1), |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 93 | nullptr) == 0); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 94 | mdstr = grpc_slice_from_static_string("this-is-an-invalid-algorithm"); |
| 95 | GPR_ASSERT(grpc_compression_algorithm_from_slice(mdstr) == |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 96 | GRPC_COMPRESS_ALGORITHMS_COUNT); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 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( |
Yash Tibrewal | 34a57d0 | 2017-10-23 15:33:21 -0700 | [diff] [blame] | 101 | grpc_compression_algorithm_slice(static_cast<grpc_compression_algorithm>( |
| 102 | static_cast<int>(GRPC_COMPRESS_ALGORITHMS_COUNT) + 1)), |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 103 | grpc_empty_slice())); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 104 | grpc_slice_unref_internal(mdstr); |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Muxi Yan | 38fcd0c | 2017-12-06 18:52:18 -0800 | [diff] [blame] | 107 | int main(int argc, char** argv) { |
David Garcia Quintas | 48ec806 | 2015-12-11 14:04:59 -0800 | [diff] [blame] | 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 | } |