b64_test.cc
Go to the documentation of this file.
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/slice/b64.h"
20 
21 #include <string.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/slice.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 
31 
32 static int buffers_are_equal(const unsigned char* buf1,
33  const unsigned char* buf2, size_t size) {
34  size_t i;
35  for (i = 0; i < size; i++) {
36  if (buf1[i] != buf2[i]) {
37  gpr_log(GPR_ERROR, "buf1 and buf2 differ: buf1[%d] = %x vs buf2[%d] = %x",
38  static_cast<int>(i), buf1[i], static_cast<int>(i), buf2[i]);
39  return 0;
40  }
41  }
42  return 1;
43 }
44 
45 static void test_simple_encode_decode_b64(int url_safe, int multiline) {
46  const char* hello = "hello";
47  char* hello_b64 =
48  grpc_base64_encode(hello, strlen(hello), url_safe, multiline);
50  grpc_slice hello_slice = grpc_base64_decode(hello_b64, url_safe);
51  GPR_ASSERT(GRPC_SLICE_LENGTH(hello_slice) == strlen(hello));
52  GPR_ASSERT(strncmp((const char*)GRPC_SLICE_START_PTR(hello_slice), hello,
53  GRPC_SLICE_LENGTH(hello_slice)) == 0);
54 
55  grpc_slice_unref_internal(hello_slice);
56 
57  gpr_free(hello_b64);
58 }
59 
60 static void test_full_range_encode_decode_b64(int url_safe, int multiline) {
61  unsigned char orig[256];
62  size_t i;
63  char* b64;
64  grpc_slice orig_decoded;
65  for (i = 0; i < sizeof(orig); i++) orig[i] = static_cast<uint8_t>(i);
66 
67  /* Try all the different paddings. */
68  for (i = 0; i < 3; i++) {
70  b64 = grpc_base64_encode(orig, sizeof(orig) - i, url_safe, multiline);
71  orig_decoded = grpc_base64_decode(b64, url_safe);
72  GPR_ASSERT(GRPC_SLICE_LENGTH(orig_decoded) == (sizeof(orig) - i));
74  sizeof(orig) - i));
75  grpc_slice_unref_internal(orig_decoded);
76  gpr_free(b64);
77  }
78 }
79 
82 }
83 
86 }
87 
90 }
91 
94 }
95 
98 }
99 
102 }
103 
106 }
107 
110 }
111 
113  unsigned char orig[256];
114  size_t i;
115  char* b64;
116  grpc_slice orig_decoded;
117  int url_safe = 1;
118  for (i = 0; i < sizeof(orig); i++) orig[i] = static_cast<uint8_t>(i);
119 
121  b64 = grpc_base64_encode(orig, sizeof(orig), url_safe, 0);
122  orig_decoded = grpc_base64_decode(b64, !url_safe);
123  GPR_ASSERT(GRPC_SLICE_IS_EMPTY(orig_decoded));
124  gpr_free(b64);
125  grpc_slice_unref_internal(orig_decoded);
126 
127  b64 = grpc_base64_encode(orig, sizeof(orig), !url_safe, 0);
128  orig_decoded = grpc_base64_decode(b64, url_safe);
129  GPR_ASSERT(GRPC_SLICE_IS_EMPTY(orig_decoded));
130  gpr_free(b64);
131  grpc_slice_unref_internal(orig_decoded);
132 }
133 
134 static void test_rfc4648_test_vectors(void) {
135  char* b64;
136 
137  b64 = grpc_base64_encode("", 0, 0, 0);
138  GPR_ASSERT(strcmp("", b64) == 0);
139  gpr_free(b64);
140 
141  b64 = grpc_base64_encode("f", 1, 0, 0);
142  GPR_ASSERT(strcmp("Zg==", b64) == 0);
143  gpr_free(b64);
144 
145  b64 = grpc_base64_encode("fo", 2, 0, 0);
146  GPR_ASSERT(strcmp("Zm8=", b64) == 0);
147  gpr_free(b64);
148 
149  b64 = grpc_base64_encode("foo", 3, 0, 0);
150  GPR_ASSERT(strcmp("Zm9v", b64) == 0);
151  gpr_free(b64);
152 
153  b64 = grpc_base64_encode("foob", 4, 0, 0);
154  GPR_ASSERT(strcmp("Zm9vYg==", b64) == 0);
155  gpr_free(b64);
156 
157  b64 = grpc_base64_encode("fooba", 5, 0, 0);
158  GPR_ASSERT(strcmp("Zm9vYmE=", b64) == 0);
159  gpr_free(b64);
160 
161  b64 = grpc_base64_encode("foobar", 6, 0, 0);
162  GPR_ASSERT(strcmp("Zm9vYmFy", b64) == 0);
163  gpr_free(b64);
164 }
165 
166 static void test_unpadded_decode(void) {
167  grpc_slice decoded;
168 
170  decoded = grpc_base64_decode("Zm9vYmFy", 0);
171  GPR_ASSERT(!GRPC_SLICE_IS_EMPTY(decoded));
172  GPR_ASSERT(grpc_slice_str_cmp(decoded, "foobar") == 0);
173  grpc_slice_unref(decoded);
174 
175  decoded = grpc_base64_decode("Zm9vYmE", 0);
176  GPR_ASSERT(!GRPC_SLICE_IS_EMPTY(decoded));
177  GPR_ASSERT(grpc_slice_str_cmp(decoded, "fooba") == 0);
178  grpc_slice_unref(decoded);
179 
180  decoded = grpc_base64_decode("Zm9vYg", 0);
181  GPR_ASSERT(!GRPC_SLICE_IS_EMPTY(decoded));
182  GPR_ASSERT(grpc_slice_str_cmp(decoded, "foob") == 0);
183  grpc_slice_unref(decoded);
184 
185  decoded = grpc_base64_decode("Zm9v", 0);
186  GPR_ASSERT(!GRPC_SLICE_IS_EMPTY(decoded));
187  GPR_ASSERT(grpc_slice_str_cmp(decoded, "foo") == 0);
188  grpc_slice_unref(decoded);
189 
190  decoded = grpc_base64_decode("Zm8", 0);
191  GPR_ASSERT(!GRPC_SLICE_IS_EMPTY(decoded));
192  GPR_ASSERT(grpc_slice_str_cmp(decoded, "fo") == 0);
193  grpc_slice_unref(decoded);
194 
195  decoded = grpc_base64_decode("Zg", 0);
196  GPR_ASSERT(!GRPC_SLICE_IS_EMPTY(decoded));
197  GPR_ASSERT(grpc_slice_str_cmp(decoded, "f") == 0);
198  grpc_slice_unref(decoded);
199 
200  decoded = grpc_base64_decode("", 0);
202 }
203 
204 int main(int argc, char** argv) {
205  grpc::testing::TestEnvironment env(&argc, argv);
206  grpc_init();
218  grpc_shutdown();
219  return 0;
220 }
test_unpadded_decode
static void test_unpadded_decode(void)
Definition: b64_test.cc:166
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
test_simple_encode_decode_b64_urlsafe_multiline
static void test_simple_encode_decode_b64_urlsafe_multiline(void)
Definition: b64_test.cc:92
log.h
generate.env
env
Definition: generate.py:37
slice.h
test_simple_encode_decode_b64_urlsafe_no_multiline
static void test_simple_encode_decode_b64_urlsafe_no_multiline(void)
Definition: b64_test.cc:88
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
test_simple_encode_decode_b64
static void test_simple_encode_decode_b64(int url_safe, int multiline)
Definition: b64_test.cc:45
test_full_range_encode_decode_b64_no_multiline
static void test_full_range_encode_decode_b64_no_multiline(void)
Definition: b64_test.cc:96
test_simple_encode_decode_b64_no_multiline
static void test_simple_encode_decode_b64_no_multiline(void)
Definition: b64_test.cc:80
test_simple_encode_decode_b64_multiline
static void test_simple_encode_decode_b64_multiline(void)
Definition: b64_test.cc:84
hello
static z_const char hello[]
Definition: bloaty/third_party/zlib/test/example.c:29
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
GRPC_SLICE_IS_EMPTY
#define GRPC_SLICE_IS_EMPTY(slice)
Definition: include/grpc/impl/codegen/slice.h:112
grpc_base64_decode
grpc_slice grpc_base64_decode(const char *b64, int url_safe)
Definition: b64.cc:126
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
buffers_are_equal
static int buffers_are_equal(const unsigned char *buf1, const unsigned char *buf2, size_t size)
Definition: b64_test.cc:32
grpc.h
main
int main(int argc, char **argv)
Definition: b64_test.cc:204
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
b64.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
test_full_range_encode_decode_b64
static void test_full_range_encode_decode_b64(int url_safe, int multiline)
Definition: b64_test.cc:60
test_url_safe_unsafe_mismatch_failure
static void test_url_safe_unsafe_mismatch_failure(void)
Definition: b64_test.cc:112
slice_internal.h
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_base64_encode
char * grpc_base64_encode(const void *vdata, size_t data_size, int url_safe, int multiline)
Definition: b64.cc:59
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
test_config.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
exec_ctx.h
buf2
static char buf2[32]
Definition: test-fs.c:127
test_full_range_encode_decode_b64_urlsafe_no_multiline
static void test_full_range_encode_decode_b64_urlsafe_no_multiline(void)
Definition: b64_test.cc:104
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
grpc_slice_str_cmp
GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b)
Definition: slice/slice.cc:426
test_full_range_encode_decode_b64_urlsafe_multiline
static void test_full_range_encode_decode_b64_urlsafe_multiline(void)
Definition: b64_test.cc:108
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
test_full_range_encode_decode_b64_multiline
static void test_full_range_encode_decode_b64_multiline(void)
Definition: b64_test.cc:100
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_slice_unref_internal
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:39
test_rfc4648_test_vectors
static void test_rfc4648_test_vectors(void)
Definition: b64_test.cc:134


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:35