• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 
25 #include "src/core/lib/gpr/string.h"
26 #include "src/core/lib/slice/slice_string_helpers.h"
27 #include "test/core/util/test_config.h"
28 
29 #define TEST_VECTOR(raw, encoded, dict) \
30   test_vector(raw, sizeof(raw) - 1, encoded, sizeof(encoded) - 1, dict)
31 
32 #define TEST_NONCONFORMANT_VECTOR(encoded, permissive_unencoded, dict) \
33   test_nonconformant_vector(encoded, sizeof(encoded) - 1,              \
34                             permissive_unencoded,                      \
35                             sizeof(permissive_unencoded) - 1, dict)
36 
test_vector(const char * raw,size_t raw_length,const char * encoded,size_t encoded_length,const uint8_t * dict)37 static void test_vector(const char* raw, size_t raw_length, const char* encoded,
38                         size_t encoded_length, const uint8_t* dict) {
39   char* raw_msg = gpr_dump(raw, raw_length, GPR_DUMP_HEX | GPR_DUMP_ASCII);
40   char* encoded_msg =
41       gpr_dump(encoded, encoded_length, GPR_DUMP_HEX | GPR_DUMP_ASCII);
42   gpr_log(GPR_DEBUG, "Trial:\nraw = %s\nencoded = %s", raw_msg, encoded_msg);
43   gpr_free(raw_msg);
44   gpr_free(encoded_msg);
45 
46   grpc_slice raw_slice = grpc_slice_from_copied_buffer(raw, raw_length);
47   grpc_slice encoded_slice =
48       grpc_slice_from_copied_buffer(encoded, encoded_length);
49   grpc_slice raw2encoded_slice = grpc_percent_encode_slice(raw_slice, dict);
50   grpc_slice encoded2raw_slice;
51   GPR_ASSERT(grpc_strict_percent_decode_slice(encoded_slice, dict,
52                                               &encoded2raw_slice));
53   grpc_slice encoded2raw_permissive_slice =
54       grpc_permissive_percent_decode_slice(encoded_slice);
55 
56   char* raw2encoded_msg =
57       grpc_dump_slice(raw2encoded_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
58   char* encoded2raw_msg =
59       grpc_dump_slice(encoded2raw_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
60   char* encoded2raw_permissive_msg = grpc_dump_slice(
61       encoded2raw_permissive_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
62   gpr_log(GPR_DEBUG,
63           "Result:\nraw2encoded = %s\nencoded2raw = %s\nencoded2raw_permissive "
64           "= %s",
65           raw2encoded_msg, encoded2raw_msg, encoded2raw_permissive_msg);
66   gpr_free(raw2encoded_msg);
67   gpr_free(encoded2raw_msg);
68   gpr_free(encoded2raw_permissive_msg);
69 
70   GPR_ASSERT(grpc_slice_eq(raw_slice, encoded2raw_slice));
71   GPR_ASSERT(grpc_slice_eq(raw_slice, encoded2raw_permissive_slice));
72   GPR_ASSERT(grpc_slice_eq(encoded_slice, raw2encoded_slice));
73 
74   grpc_slice_unref(encoded2raw_slice);
75   grpc_slice_unref(encoded2raw_permissive_slice);
76   grpc_slice_unref(raw2encoded_slice);
77   grpc_slice_unref(raw_slice);
78   grpc_slice_unref(encoded_slice);
79 }
80 
test_nonconformant_vector(const char * encoded,size_t encoded_length,const char * permissive_unencoded,size_t permissive_unencoded_length,const uint8_t * dict)81 static void test_nonconformant_vector(const char* encoded,
82                                       size_t encoded_length,
83                                       const char* permissive_unencoded,
84                                       size_t permissive_unencoded_length,
85                                       const uint8_t* dict) {
86   char* permissive_unencoded_msg =
87       gpr_dump(permissive_unencoded, permissive_unencoded_length,
88                GPR_DUMP_HEX | GPR_DUMP_ASCII);
89   char* encoded_msg =
90       gpr_dump(encoded, encoded_length, GPR_DUMP_HEX | GPR_DUMP_ASCII);
91   gpr_log(GPR_DEBUG, "Trial:\nraw = %s\nencoded = %s", permissive_unencoded_msg,
92           encoded_msg);
93   gpr_free(permissive_unencoded_msg);
94   gpr_free(encoded_msg);
95 
96   grpc_slice permissive_unencoded_slice = grpc_slice_from_copied_buffer(
97       permissive_unencoded, permissive_unencoded_length);
98   grpc_slice encoded_slice =
99       grpc_slice_from_copied_buffer(encoded, encoded_length);
100   grpc_slice encoded2raw_slice;
101   GPR_ASSERT(!grpc_strict_percent_decode_slice(encoded_slice, dict,
102                                                &encoded2raw_slice));
103   grpc_slice encoded2raw_permissive_slice =
104       grpc_permissive_percent_decode_slice(encoded_slice);
105 
106   char* encoded2raw_permissive_msg = grpc_dump_slice(
107       encoded2raw_permissive_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
108   gpr_log(GPR_DEBUG, "Result:\nencoded2raw_permissive = %s",
109           encoded2raw_permissive_msg);
110   gpr_free(encoded2raw_permissive_msg);
111 
112   GPR_ASSERT(
113       grpc_slice_eq(permissive_unencoded_slice, encoded2raw_permissive_slice));
114 
115   grpc_slice_unref(permissive_unencoded_slice);
116   grpc_slice_unref(encoded2raw_permissive_slice);
117   grpc_slice_unref(encoded_slice);
118 }
119 
main(int argc,char ** argv)120 int main(int argc, char** argv) {
121   grpc_test_init(argc, argv);
122   grpc_init();
123   TEST_VECTOR(
124       "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~",
125       "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~",
126       grpc_url_percent_encoding_unreserved_bytes);
127   TEST_VECTOR("\x00", "%00", grpc_url_percent_encoding_unreserved_bytes);
128   TEST_VECTOR("\x01", "%01", grpc_url_percent_encoding_unreserved_bytes);
129   TEST_VECTOR("a b", "a%20b", grpc_url_percent_encoding_unreserved_bytes);
130   TEST_VECTOR(" b", "%20b", grpc_url_percent_encoding_unreserved_bytes);
131   TEST_VECTOR("a b", "a b", grpc_compatible_percent_encoding_unreserved_bytes);
132   TEST_VECTOR(" b", " b", grpc_compatible_percent_encoding_unreserved_bytes);
133   TEST_VECTOR("\x0f", "%0F", grpc_url_percent_encoding_unreserved_bytes);
134   TEST_VECTOR("\xff", "%FF", grpc_url_percent_encoding_unreserved_bytes);
135   TEST_VECTOR("\xee", "%EE", grpc_url_percent_encoding_unreserved_bytes);
136   TEST_VECTOR("%2", "%252", grpc_url_percent_encoding_unreserved_bytes);
137   TEST_NONCONFORMANT_VECTOR("%", "%",
138                             grpc_url_percent_encoding_unreserved_bytes);
139   TEST_NONCONFORMANT_VECTOR("%A", "%A",
140                             grpc_url_percent_encoding_unreserved_bytes);
141   TEST_NONCONFORMANT_VECTOR("%AG", "%AG",
142                             grpc_url_percent_encoding_unreserved_bytes);
143   TEST_NONCONFORMANT_VECTOR("\0", "\0",
144                             grpc_url_percent_encoding_unreserved_bytes);
145   grpc_shutdown();
146   return 0;
147 }
148