| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 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/slice/percent_encoding.h" |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
|
| 23 |
#include <utility> |
| 24 |
|
| 25 |
#include "gtest/gtest.h" |
| 26 |
|
| 27 |
#include <grpc/support/alloc.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
|
| 30 |
#include "src/core/lib/gpr/string.h" |
| 31 |
#include "src/core/lib/slice/slice_string_helpers.h" |
| 32 |
#include "test/core/util/test_config.h" |
| 33 |
|
| 34 |
#define TEST_VECTOR(raw, encoded, dict) \ |
| 35 |
test_vector(raw, sizeof(raw) - 1, encoded, sizeof(encoded) - 1, dict) |
| 36 |
|
| 37 |
#define TEST_NONCONFORMANT_VECTOR(encoded, permissive_unencoded) \ |
| 38 |
test_nonconformant_vector(encoded, sizeof(encoded) - 1, \ |
| 39 |
permissive_unencoded, \ |
| 40 |
sizeof(permissive_unencoded) - 1) |
| 41 |
|
| 42 |
static void test_vector(const char* raw, size_t raw_length, const char* encoded, |
| 43 |
size_t encoded_length, |
| 44 |
grpc_core::PercentEncodingType type) { |
| 45 |
char* raw_msg = gpr_dump(raw, raw_length, GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 46 |
char* encoded_msg = |
| 47 |
gpr_dump(encoded, encoded_length, GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 48 |
gpr_log(GPR_DEBUG, "Trial:\nraw = %s\nencoded = %s", raw_msg, encoded_msg); |
| 49 |
gpr_free(raw_msg); |
| 50 |
gpr_free(encoded_msg); |
| 51 |
|
| 52 |
auto raw_slice = grpc_core::Slice::FromCopiedBuffer(raw, raw_length); |
| 53 |
auto encoded_slice = |
| 54 |
grpc_core::Slice::FromCopiedBuffer(encoded, encoded_length); |
| 55 |
auto raw2encoded_slice = grpc_core::PercentEncodeSlice(raw_slice.Ref(), type); |
| 56 |
auto encoded2raw_permissive_slice = |
| 57 |
grpc_core::PermissivePercentDecodeSlice(encoded_slice.Ref()); |
| 58 |
|
| 59 |
char* raw2encoded_msg = grpc_dump_slice(raw2encoded_slice.c_slice(), |
| 60 |
GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 61 |
char* encoded2raw_permissive_msg = grpc_dump_slice( |
| 62 |
encoded2raw_permissive_slice.c_slice(), GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 63 |
gpr_log(GPR_DEBUG, |
| 64 |
"Result:\nraw2encoded = %s\nencoded2raw_permissive " |
| 65 |
"= %s", |
| 66 |
raw2encoded_msg, encoded2raw_permissive_msg); |
| 67 |
gpr_free(raw2encoded_msg); |
| 68 |
gpr_free(encoded2raw_permissive_msg); |
| 69 |
|
| 70 |
ASSERT_EQ(raw_slice, encoded2raw_permissive_slice); |
| 71 |
ASSERT_EQ(encoded_slice, raw2encoded_slice); |
| 72 |
} |
| 73 |
|
| 74 |
static void test_nonconformant_vector(const char* encoded, |
| 75 |
size_t encoded_length, |
| 76 |
const char* permissive_unencoded, |
| 77 |
size_t permissive_unencoded_length) { |
| 78 |
char* permissive_unencoded_msg = |
| 79 |
gpr_dump(permissive_unencoded, permissive_unencoded_length, |
| 80 |
GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 81 |
char* encoded_msg = |
| 82 |
gpr_dump(encoded, encoded_length, GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 83 |
gpr_log(GPR_DEBUG, "Trial:\nraw = %s\nencoded = %s", permissive_unencoded_msg, |
| 84 |
encoded_msg); |
| 85 |
gpr_free(permissive_unencoded_msg); |
| 86 |
gpr_free(encoded_msg); |
| 87 |
|
| 88 |
auto permissive_unencoded_slice = grpc_core::Slice::FromCopiedBuffer( |
| 89 |
permissive_unencoded, permissive_unencoded_length); |
| 90 |
auto encoded_slice = |
| 91 |
grpc_core::Slice::FromCopiedBuffer(encoded, encoded_length); |
| 92 |
auto encoded2raw_permissive_slice = |
| 93 |
grpc_core::PermissivePercentDecodeSlice(std::move(encoded_slice)); |
| 94 |
|
| 95 |
char* encoded2raw_permissive_msg = grpc_dump_slice( |
| 96 |
encoded2raw_permissive_slice.c_slice(), GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 97 |
gpr_log(GPR_DEBUG, "Result:\nencoded2raw_permissive = %s", |
| 98 |
encoded2raw_permissive_msg); |
| 99 |
gpr_free(encoded2raw_permissive_msg); |
| 100 |
|
| 101 |
ASSERT_EQ(permissive_unencoded_slice, encoded2raw_permissive_slice); |
| 102 |
} |
| 103 |
|
| 104 |
TEST(PercentEncodingTest, MainTest) { |
| 105 |
TEST_VECTOR( |
| 106 |
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~", |
| 107 |
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~", |
| 108 |
grpc_core::PercentEncodingType::URL); |
| 109 |
TEST_VECTOR("\x00", "%00", grpc_core::PercentEncodingType::URL); |
| 110 |
TEST_VECTOR("\x01", "%01", grpc_core::PercentEncodingType::URL); |
| 111 |
TEST_VECTOR("a b", "a%20b", grpc_core::PercentEncodingType::URL); |
| 112 |
TEST_VECTOR(" b", "%20b", grpc_core::PercentEncodingType::URL); |
| 113 |
TEST_VECTOR("a b", "a b", grpc_core::PercentEncodingType::Compatible); |
| 114 |
TEST_VECTOR(" b", " b", grpc_core::PercentEncodingType::Compatible); |
| 115 |
TEST_VECTOR("\x0f", "%0F", grpc_core::PercentEncodingType::URL); |
| 116 |
TEST_VECTOR("\xff", "%FF", grpc_core::PercentEncodingType::URL); |
| 117 |
TEST_VECTOR("\xee", "%EE", grpc_core::PercentEncodingType::URL); |
| 118 |
TEST_VECTOR("%2", "%252", grpc_core::PercentEncodingType::URL); |
| 119 |
TEST_NONCONFORMANT_VECTOR("%", "%"); |
| 120 |
TEST_NONCONFORMANT_VECTOR("%A", "%A"); |
| 121 |
TEST_NONCONFORMANT_VECTOR("%AG", "%AG"); |
| 122 |
TEST_NONCONFORMANT_VECTOR("\0", "\0"); |
| 123 |
} |
| 124 |
|
| 125 |
int main(int argc, char** argv) { |
| 126 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 127 |
::testing::InitGoogleTest(&argc, argv); |
| 128 |
grpc::testing::TestGrpcScope grpc_scope; |
| 129 |
return RUN_ALL_TESTS(); |
| 130 |
} |