mirror of https://github.com/grpc/grpc.git
[slice] remove absl_base64 experiment (#36178)
Closes #36178
COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36178 from markdroth:b64_remove 0139e3bce8
PiperOrigin-RevId: 619288082
pull/36163/head
parent
ca478bd429
commit
e715261ac6
31 changed files with 11 additions and 814 deletions
@ -1,239 +0,0 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2015 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/slice/b64.h" |
||||
|
||||
#include <stdint.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/slice/slice.h" |
||||
|
||||
// --- Constants. ---
|
||||
|
||||
static const int8_t base64_bytes[] = { |
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||||
-1, -1, -1, -1, -1, -1, -1, 0x3E, -1, -1, -1, 0x3F, |
||||
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, -1, -1, |
||||
-1, 0x7F, -1, -1, -1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
||||
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, |
||||
0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, -1, -1, -1, -1, -1, |
||||
-1, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, |
||||
0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, |
||||
0x31, 0x32, 0x33, -1, -1, -1, -1, -1}; |
||||
|
||||
static const char base64_url_unsafe_chars[] = |
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
||||
static const char base64_url_safe_chars[] = |
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
||||
|
||||
#define GRPC_BASE64_PAD_CHAR '=' |
||||
#define GRPC_BASE64_PAD_BYTE 0x7F |
||||
#define GRPC_BASE64_MULTILINE_LINE_LEN 76 |
||||
#define GRPC_BASE64_MULTILINE_NUM_BLOCKS (GRPC_BASE64_MULTILINE_LINE_LEN / 4) |
||||
|
||||
// --- base64 functions. ---
|
||||
|
||||
char* grpc_base64_encode(const void* vdata, size_t data_size, int url_safe, |
||||
int multiline) { |
||||
size_t result_projected_size = |
||||
grpc_base64_estimate_encoded_size(data_size, multiline); |
||||
char* result = static_cast<char*>(gpr_malloc(result_projected_size)); |
||||
grpc_base64_encode_core(result, vdata, data_size, url_safe, multiline); |
||||
return result; |
||||
} |
||||
|
||||
size_t grpc_base64_estimate_encoded_size(size_t data_size, int multiline) { |
||||
size_t result_projected_size = |
||||
4 * ((data_size + 3) / 3) + |
||||
2 * (multiline ? (data_size / (3 * GRPC_BASE64_MULTILINE_NUM_BLOCKS)) |
||||
: 0) + |
||||
1; |
||||
return result_projected_size; |
||||
} |
||||
|
||||
void grpc_base64_encode_core(char* result, const void* vdata, size_t data_size, |
||||
int url_safe, int multiline) { |
||||
const unsigned char* data = static_cast<const unsigned char*>(vdata); |
||||
const char* base64_chars = |
||||
url_safe ? base64_url_safe_chars : base64_url_unsafe_chars; |
||||
const size_t result_projected_size = |
||||
grpc_base64_estimate_encoded_size(data_size, multiline); |
||||
|
||||
char* current = result; |
||||
size_t num_blocks = 0; |
||||
size_t i = 0; |
||||
|
||||
// Encode each block.
|
||||
while (data_size >= 3) { |
||||
*current++ = base64_chars[(data[i] >> 2) & 0x3F]; |
||||
*current++ = |
||||
base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)]; |
||||
*current++ = |
||||
base64_chars[((data[i + 1] & 0x0F) << 2) | ((data[i + 2] >> 6) & 0x03)]; |
||||
*current++ = base64_chars[data[i + 2] & 0x3F]; |
||||
|
||||
data_size -= 3; |
||||
i += 3; |
||||
if (multiline && (++num_blocks == GRPC_BASE64_MULTILINE_NUM_BLOCKS)) { |
||||
*current++ = '\r'; |
||||
*current++ = '\n'; |
||||
num_blocks = 0; |
||||
} |
||||
} |
||||
|
||||
// Take care of the tail.
|
||||
if (data_size == 2) { |
||||
*current++ = base64_chars[(data[i] >> 2) & 0x3F]; |
||||
*current++ = |
||||
base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)]; |
||||
*current++ = base64_chars[(data[i + 1] & 0x0F) << 2]; |
||||
*current++ = GRPC_BASE64_PAD_CHAR; |
||||
} else if (data_size == 1) { |
||||
*current++ = base64_chars[(data[i] >> 2) & 0x3F]; |
||||
*current++ = base64_chars[(data[i] & 0x03) << 4]; |
||||
*current++ = GRPC_BASE64_PAD_CHAR; |
||||
*current++ = GRPC_BASE64_PAD_CHAR; |
||||
} |
||||
|
||||
GPR_ASSERT(current >= result); |
||||
GPR_ASSERT((uintptr_t)(current - result) < result_projected_size); |
||||
result[current - result] = '\0'; |
||||
} |
||||
|
||||
grpc_slice grpc_base64_decode(const char* b64, int url_safe) { |
||||
return grpc_base64_decode_with_len(b64, strlen(b64), url_safe); |
||||
} |
||||
|
||||
static void decode_one_char(const unsigned char* codes, unsigned char* result, |
||||
size_t* result_offset) { |
||||
uint32_t packed = (static_cast<uint32_t>(codes[0]) << 2) | |
||||
(static_cast<uint32_t>(codes[1]) >> 4); |
||||
result[(*result_offset)++] = static_cast<unsigned char>(packed); |
||||
} |
||||
|
||||
static void decode_two_chars(const unsigned char* codes, unsigned char* result, |
||||
size_t* result_offset) { |
||||
uint32_t packed = (static_cast<uint32_t>(codes[0]) << 10) | |
||||
(static_cast<uint32_t>(codes[1]) << 4) | |
||||
(static_cast<uint32_t>(codes[2]) >> 2); |
||||
result[(*result_offset)++] = static_cast<unsigned char>(packed >> 8); |
||||
result[(*result_offset)++] = static_cast<unsigned char>(packed); |
||||
} |
||||
|
||||
static int decode_group(const unsigned char* codes, size_t num_codes, |
||||
unsigned char* result, size_t* result_offset) { |
||||
GPR_ASSERT(num_codes <= 4); |
||||
|
||||
// Short end groups that may not have padding.
|
||||
if (num_codes == 1) { |
||||
gpr_log(GPR_ERROR, "Invalid group. Must be at least 2 bytes."); |
||||
return 0; |
||||
} |
||||
if (num_codes == 2) { |
||||
decode_one_char(codes, result, result_offset); |
||||
return 1; |
||||
} |
||||
if (num_codes == 3) { |
||||
decode_two_chars(codes, result, result_offset); |
||||
return 1; |
||||
} |
||||
|
||||
// Regular 4 byte groups with padding or not.
|
||||
GPR_ASSERT(num_codes == 4); |
||||
if (codes[0] == GRPC_BASE64_PAD_BYTE || codes[1] == GRPC_BASE64_PAD_BYTE) { |
||||
gpr_log(GPR_ERROR, "Invalid padding detected."); |
||||
return 0; |
||||
} |
||||
if (codes[2] == GRPC_BASE64_PAD_BYTE) { |
||||
if (codes[3] == GRPC_BASE64_PAD_BYTE) { |
||||
decode_one_char(codes, result, result_offset); |
||||
} else { |
||||
gpr_log(GPR_ERROR, "Invalid padding detected."); |
||||
return 0; |
||||
} |
||||
} else if (codes[3] == GRPC_BASE64_PAD_BYTE) { |
||||
decode_two_chars(codes, result, result_offset); |
||||
} else { |
||||
// No padding.
|
||||
uint32_t packed = (static_cast<uint32_t>(codes[0]) << 18) | |
||||
(static_cast<uint32_t>(codes[1]) << 12) | |
||||
(static_cast<uint32_t>(codes[2]) << 6) | codes[3]; |
||||
result[(*result_offset)++] = static_cast<unsigned char>(packed >> 16); |
||||
result[(*result_offset)++] = static_cast<unsigned char>(packed >> 8); |
||||
result[(*result_offset)++] = static_cast<unsigned char>(packed); |
||||
} |
||||
return 1; |
||||
} |
||||
|
||||
grpc_slice grpc_base64_decode_with_len(const char* b64, size_t b64_len, |
||||
int url_safe) { |
||||
grpc_slice result = GRPC_SLICE_MALLOC(b64_len); |
||||
unsigned char* current = GRPC_SLICE_START_PTR(result); |
||||
size_t result_size = 0; |
||||
unsigned char codes[4]; |
||||
size_t num_codes = 0; |
||||
|
||||
while (b64_len--) { |
||||
unsigned char c = static_cast<unsigned char>(*b64++); |
||||
signed char code; |
||||
if (c >= GPR_ARRAY_SIZE(base64_bytes)) continue; |
||||
if (url_safe) { |
||||
if (c == '+' || c == '/') { |
||||
gpr_log(GPR_ERROR, "Invalid character for url safe base64 %c", c); |
||||
goto fail; |
||||
} |
||||
if (c == '-') { |
||||
c = '+'; |
||||
} else if (c == '_') { |
||||
c = '/'; |
||||
} |
||||
} |
||||
code = base64_bytes[c]; |
||||
if (code == -1) { |
||||
if (c != '\r' && c != '\n') { |
||||
gpr_log(GPR_ERROR, "Invalid character %c", c); |
||||
goto fail; |
||||
} |
||||
} else { |
||||
codes[num_codes++] = static_cast<unsigned char>(code); |
||||
if (num_codes == 4) { |
||||
if (!decode_group(codes, num_codes, current, &result_size)) goto fail; |
||||
num_codes = 0; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (num_codes != 0 && |
||||
!decode_group(codes, num_codes, current, &result_size)) { |
||||
goto fail; |
||||
} |
||||
GRPC_SLICE_SET_LENGTH(result, result_size); |
||||
return result; |
||||
|
||||
fail: |
||||
grpc_core::CSliceUnref(result); |
||||
return grpc_empty_slice(); |
||||
} |
@ -1,52 +0,0 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2015 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#ifndef GRPC_SRC_CORE_LIB_SLICE_B64_H |
||||
#define GRPC_SRC_CORE_LIB_SLICE_B64_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <stddef.h> |
||||
|
||||
#include <grpc/slice.h> |
||||
|
||||
// Encodes data using base64. It is the caller's responsibility to free
|
||||
// the returned char * using gpr_free. Returns NULL on NULL input.
|
||||
// TODO(makdharma) : change the flags to bool from int
|
||||
char* grpc_base64_encode(const void* data, size_t data_size, int url_safe, |
||||
int multiline); |
||||
|
||||
// estimate the upper bound on size of base64 encoded data. The actual size
|
||||
// is guaranteed to be less than or equal to the size returned here.
|
||||
size_t grpc_base64_estimate_encoded_size(size_t data_size, int multiline); |
||||
|
||||
// Encodes data using base64 and write it to memory pointed to by result. It is
|
||||
// the caller's responsibility to allocate enough memory in |result| to fit the
|
||||
// encoded data.
|
||||
void grpc_base64_encode_core(char* result, const void* vdata, size_t data_size, |
||||
int url_safe, int multiline); |
||||
|
||||
// Decodes data according to the base64 specification. Returns an empty
|
||||
// slice in case of failure.
|
||||
grpc_slice grpc_base64_decode(const char* b64, int url_safe); |
||||
|
||||
// Same as above except that the length is provided by the caller.
|
||||
grpc_slice grpc_base64_decode_with_len(const char* b64, size_t b64_len, |
||||
int url_safe); |
||||
|
||||
#endif // GRPC_SRC_CORE_LIB_SLICE_B64_H
|
@ -1,39 +0,0 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2019 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#include <stddef.h> |
||||
#include <stdint.h> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc/slice.h> |
||||
|
||||
#include "src/core/lib/slice/b64.h" |
||||
|
||||
bool squelch = true; |
||||
bool leak_check = true; |
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
||||
if (size < 1) return 0; |
||||
grpc_init(); |
||||
const bool url_safe = static_cast<uint8_t>(0x100) < data[0]; |
||||
grpc_slice res = grpc_base64_decode_with_len( |
||||
reinterpret_cast<const char*>(data + 1), size - 1, url_safe); |
||||
grpc_slice_unref(res); |
||||
grpc_shutdown(); |
||||
return 0; |
||||
} |
@ -1 +0,0 @@ |
||||
~ |
@ -1,37 +0,0 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2019 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#include <stddef.h> |
||||
#include <stdint.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
|
||||
#include "src/core/lib/slice/b64.h" |
||||
|
||||
bool squelch = true; |
||||
bool leak_check = true; |
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
||||
if (size < 2) return 0; |
||||
const bool url_safe = static_cast<uint8_t>(0x100) < data[0]; |
||||
const bool multiline = static_cast<uint8_t>(0x100) < data[1]; |
||||
char* res = grpc_base64_encode(reinterpret_cast<const char*>(data + 2), |
||||
size - 2, url_safe, multiline); |
||||
gpr_free(res); |
||||
return 0; |
||||
} |
@ -1,193 +0,0 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2015 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#include "src/core/lib/slice/b64.h" |
||||
|
||||
#include <stdint.h> |
||||
#include <string.h> |
||||
|
||||
#include <memory> |
||||
|
||||
#include "absl/strings/string_view.h" |
||||
#include "gtest/gtest.h" |
||||
|
||||
#include <grpc/slice.h> |
||||
#include <grpc/support/alloc.h> |
||||
|
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/slice/slice_internal.h" |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
static void test_simple_encode_decode_b64(int url_safe, int multiline) { |
||||
const char* hello = "hello"; |
||||
char* hello_b64 = |
||||
grpc_base64_encode(hello, strlen(hello), url_safe, multiline); |
||||
grpc_core::ExecCtx exec_ctx; |
||||
grpc_slice hello_slice = grpc_base64_decode(hello_b64, url_safe); |
||||
ASSERT_EQ(grpc_core::StringViewFromSlice(hello_slice), |
||||
absl::string_view(hello)); |
||||
grpc_slice_unref(hello_slice); |
||||
|
||||
gpr_free(hello_b64); |
||||
} |
||||
|
||||
static void test_full_range_encode_decode_b64(int url_safe, int multiline) { |
||||
unsigned char orig[256]; |
||||
size_t i; |
||||
char* b64; |
||||
grpc_slice orig_decoded; |
||||
for (i = 0; i < sizeof(orig); i++) orig[i] = static_cast<uint8_t>(i); |
||||
|
||||
// Try all the different paddings.
|
||||
for (i = 0; i < 3; i++) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
b64 = grpc_base64_encode(orig, sizeof(orig) - i, url_safe, multiline); |
||||
orig_decoded = grpc_base64_decode(b64, url_safe); |
||||
ASSERT_EQ( |
||||
grpc_core::StringViewFromSlice(orig_decoded), |
||||
absl::string_view(reinterpret_cast<char*>(orig), sizeof(orig) - i)); |
||||
grpc_slice_unref(orig_decoded); |
||||
gpr_free(b64); |
||||
} |
||||
} |
||||
|
||||
TEST(B64Test, SimpleEncodeDecodeB64NoMultiline) { |
||||
test_simple_encode_decode_b64(0, 0); |
||||
} |
||||
|
||||
TEST(B64Test, SimpleEncodeDecodeB64Multiline) { |
||||
test_simple_encode_decode_b64(0, 1); |
||||
} |
||||
|
||||
TEST(B64Test, SimpleEncodeDecodeB64UrlsafeNoMultiline) { |
||||
test_simple_encode_decode_b64(1, 0); |
||||
} |
||||
|
||||
TEST(B64Test, SimpleEncodeDecodeB64UrlsafeMultiline) { |
||||
test_simple_encode_decode_b64(1, 1); |
||||
} |
||||
|
||||
TEST(B64Test, FullRangeEncodeDecodeB64NoMultiline) { |
||||
test_full_range_encode_decode_b64(0, 0); |
||||
} |
||||
|
||||
TEST(B64Test, FullRangeEncodeDecodeB64Multiline) { |
||||
test_full_range_encode_decode_b64(0, 1); |
||||
} |
||||
|
||||
TEST(B64Test, FullRangeEncodeDecodeB64UrlsafeNoMultiline) { |
||||
test_full_range_encode_decode_b64(1, 0); |
||||
} |
||||
|
||||
TEST(B64Test, FullRangeEncodeDecodeB64UrlsafeMultiline) { |
||||
test_full_range_encode_decode_b64(1, 1); |
||||
} |
||||
|
||||
TEST(B64Test, UrlSafeUnsafeMismatchFailure) { |
||||
unsigned char orig[256]; |
||||
size_t i; |
||||
char* b64; |
||||
grpc_slice orig_decoded; |
||||
int url_safe = 1; |
||||
for (i = 0; i < sizeof(orig); i++) orig[i] = static_cast<uint8_t>(i); |
||||
|
||||
grpc_core::ExecCtx exec_ctx; |
||||
b64 = grpc_base64_encode(orig, sizeof(orig), url_safe, 0); |
||||
orig_decoded = grpc_base64_decode(b64, !url_safe); |
||||
ASSERT_TRUE(GRPC_SLICE_IS_EMPTY(orig_decoded)); |
||||
gpr_free(b64); |
||||
grpc_slice_unref(orig_decoded); |
||||
|
||||
b64 = grpc_base64_encode(orig, sizeof(orig), !url_safe, 0); |
||||
orig_decoded = grpc_base64_decode(b64, url_safe); |
||||
ASSERT_TRUE(GRPC_SLICE_IS_EMPTY(orig_decoded)); |
||||
gpr_free(b64); |
||||
grpc_slice_unref(orig_decoded); |
||||
} |
||||
|
||||
TEST(B64Test, Rfc4648TestVectors) { |
||||
char* b64; |
||||
|
||||
b64 = grpc_base64_encode("", 0, 0, 0); |
||||
ASSERT_STREQ("", b64); |
||||
gpr_free(b64); |
||||
|
||||
b64 = grpc_base64_encode("f", 1, 0, 0); |
||||
ASSERT_STREQ("Zg==", b64); |
||||
gpr_free(b64); |
||||
|
||||
b64 = grpc_base64_encode("fo", 2, 0, 0); |
||||
ASSERT_STREQ("Zm8=", b64); |
||||
gpr_free(b64); |
||||
|
||||
b64 = grpc_base64_encode("foo", 3, 0, 0); |
||||
ASSERT_STREQ("Zm9v", b64); |
||||
gpr_free(b64); |
||||
|
||||
b64 = grpc_base64_encode("foob", 4, 0, 0); |
||||
ASSERT_STREQ("Zm9vYg==", b64); |
||||
gpr_free(b64); |
||||
|
||||
b64 = grpc_base64_encode("fooba", 5, 0, 0); |
||||
ASSERT_STREQ("Zm9vYmE=", b64); |
||||
gpr_free(b64); |
||||
|
||||
b64 = grpc_base64_encode("foobar", 6, 0, 0); |
||||
ASSERT_STREQ("Zm9vYmFy", b64); |
||||
gpr_free(b64); |
||||
} |
||||
|
||||
TEST(B64Test, UnpaddedDecode) { |
||||
grpc_slice decoded; |
||||
|
||||
grpc_core::ExecCtx exec_ctx; |
||||
decoded = grpc_base64_decode("Zm9vYmFy", 0); |
||||
|
||||
ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "foobar"); |
||||
grpc_slice_unref(decoded); |
||||
|
||||
decoded = grpc_base64_decode("Zm9vYmE", 0); |
||||
ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "fooba"); |
||||
grpc_slice_unref(decoded); |
||||
|
||||
decoded = grpc_base64_decode("Zm9vYg", 0); |
||||
ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "foob"); |
||||
grpc_slice_unref(decoded); |
||||
|
||||
decoded = grpc_base64_decode("Zm9v", 0); |
||||
ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "foo"); |
||||
grpc_slice_unref(decoded); |
||||
|
||||
decoded = grpc_base64_decode("Zm8", 0); |
||||
ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "fo"); |
||||
grpc_slice_unref(decoded); |
||||
|
||||
decoded = grpc_base64_decode("Zg", 0); |
||||
ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "f"); |
||||
grpc_slice_unref(decoded); |
||||
|
||||
decoded = grpc_base64_decode("", 0); |
||||
ASSERT_TRUE(GRPC_SLICE_IS_EMPTY(decoded)); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc::testing::TestEnvironment env(&argc, argv); |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
grpc::testing::TestGrpcScope grpc_scope; |
||||
return RUN_ALL_TESTS(); |
||||
} |
Loading…
Reference in new issue