Debian Codegraph @

test/core/slice/b64_test.cc

raw
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 <stdint.h>
22 #include <string.h>
23
24 #include "absl/strings/string_view.h"
25 #include "gtest/gtest.h"
26
27 #include <grpc/slice.h>
28 #include <grpc/support/alloc.h>
29
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "src/core/lib/slice/slice_internal.h"
32 #include "test/core/util/test_config.h"
33
34 static void test_simple_encode_decode_b64(int url_safe, int multiline) {
35 const char* hello = "hello";
36 char* hello_b64 =
37 grpc_base64_encode(hello, strlen(hello), url_safe, multiline);
38 grpc_core::ExecCtx exec_ctx;
39 grpc_slice hello_slice = grpc_base64_decode(hello_b64, url_safe);
40 ASSERT_EQ(grpc_core::StringViewFromSlice(hello_slice),
41 absl::string_view(hello));
42 grpc_slice_unref(hello_slice);
43
44 gpr_free(hello_b64);
45 }
46
47 static void test_full_range_encode_decode_b64(int url_safe, int multiline) {
48 unsigned char orig[256];
49 size_t i;
50 char* b64;
51 grpc_slice orig_decoded;
52 for (i = 0; i < sizeof(orig); i++) orig[i] = static_cast<uint8_t>(i);
53
54 /* Try all the different paddings. */
55 for (i = 0; i < 3; i++) {
56 grpc_core::ExecCtx exec_ctx;
57 b64 = grpc_base64_encode(orig, sizeof(orig) - i, url_safe, multiline);
58 orig_decoded = grpc_base64_decode(b64, url_safe);
59 ASSERT_EQ(
60 grpc_core::StringViewFromSlice(orig_decoded),
61 absl::string_view(reinterpret_cast<char*>(orig), sizeof(orig) - i));
62 grpc_slice_unref(orig_decoded);
63 gpr_free(b64);
64 }
65 }
66
67 TEST(B64Test, SimpleEncodeDecodeB64NoMultiline) {
68 test_simple_encode_decode_b64(0, 0);
69 }
70
71 TEST(B64Test, SimpleEncodeDecodeB64Multiline) {
72 test_simple_encode_decode_b64(0, 1);
73 }
74
75 TEST(B64Test, SimpleEncodeDecodeB64UrlsafeNoMultiline) {
76 test_simple_encode_decode_b64(1, 0);
77 }
78
79 TEST(B64Test, SimpleEncodeDecodeB64UrlsafeMultiline) {
80 test_simple_encode_decode_b64(1, 1);
81 }
82
83 TEST(B64Test, FullRangeEncodeDecodeB64NoMultiline) {
84 test_full_range_encode_decode_b64(0, 0);
85 }
86
87 TEST(B64Test, FullRangeEncodeDecodeB64Multiline) {
88 test_full_range_encode_decode_b64(0, 1);
89 }
90
91 TEST(B64Test, FullRangeEncodeDecodeB64UrlsafeNoMultiline) {
92 test_full_range_encode_decode_b64(1, 0);
93 }
94
95 TEST(B64Test, FullRangeEncodeDecodeB64UrlsafeMultiline) {
96 test_full_range_encode_decode_b64(1, 1);
97 }
98
99 TEST(B64Test, UrlSafeUnsafeMismatchFailure) {
100 unsigned char orig[256];
101 size_t i;
102 char* b64;
103 grpc_slice orig_decoded;
104 int url_safe = 1;
105 for (i = 0; i < sizeof(orig); i++) orig[i] = static_cast<uint8_t>(i);
106
107 grpc_core::ExecCtx exec_ctx;
108 b64 = grpc_base64_encode(orig, sizeof(orig), url_safe, 0);
109 orig_decoded = grpc_base64_decode(b64, !url_safe);
110 ASSERT_TRUE(GRPC_SLICE_IS_EMPTY(orig_decoded));
111 gpr_free(b64);
112 grpc_slice_unref(orig_decoded);
113
114 b64 = grpc_base64_encode(orig, sizeof(orig), !url_safe, 0);
115 orig_decoded = grpc_base64_decode(b64, url_safe);
116 ASSERT_TRUE(GRPC_SLICE_IS_EMPTY(orig_decoded));
117 gpr_free(b64);
118 grpc_slice_unref(orig_decoded);
119 }
120
121 TEST(B64Test, Rfc4648TestVectors) {
122 char* b64;
123
124 b64 = grpc_base64_encode("", 0, 0, 0);
125 ASSERT_STREQ("", b64);
126 gpr_free(b64);
127
128 b64 = grpc_base64_encode("f", 1, 0, 0);
129 ASSERT_STREQ("Zg==", b64);
130 gpr_free(b64);
131
132 b64 = grpc_base64_encode("fo", 2, 0, 0);
133 ASSERT_STREQ("Zm8=", b64);
134 gpr_free(b64);
135
136 b64 = grpc_base64_encode("foo", 3, 0, 0);
137 ASSERT_STREQ("Zm9v", b64);
138 gpr_free(b64);
139
140 b64 = grpc_base64_encode("foob", 4, 0, 0);
141 ASSERT_STREQ("Zm9vYg==", b64);
142 gpr_free(b64);
143
144 b64 = grpc_base64_encode("fooba", 5, 0, 0);
145 ASSERT_STREQ("Zm9vYmE=", b64);
146 gpr_free(b64);
147
148 b64 = grpc_base64_encode("foobar", 6, 0, 0);
149 ASSERT_STREQ("Zm9vYmFy", b64);
150 gpr_free(b64);
151 }
152
153 TEST(B64Test, UnpaddedDecode) {
154 grpc_slice decoded;
155
156 grpc_core::ExecCtx exec_ctx;
157 decoded = grpc_base64_decode("Zm9vYmFy", 0);
158
159 ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "foobar");
160 grpc_slice_unref(decoded);
161
162 decoded = grpc_base64_decode("Zm9vYmE", 0);
163 ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "fooba");
164 grpc_slice_unref(decoded);
165
166 decoded = grpc_base64_decode("Zm9vYg", 0);
167 ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "foob");
168 grpc_slice_unref(decoded);
169
170 decoded = grpc_base64_decode("Zm9v", 0);
171 ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "foo");
172 grpc_slice_unref(decoded);
173
174 decoded = grpc_base64_decode("Zm8", 0);
175 ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "fo");
176 grpc_slice_unref(decoded);
177
178 decoded = grpc_base64_decode("Zg", 0);
179 ASSERT_EQ(grpc_core::StringViewFromSlice(decoded), "f");
180 grpc_slice_unref(decoded);
181
182 decoded = grpc_base64_decode("", 0);
183 ASSERT_TRUE(GRPC_SLICE_IS_EMPTY(decoded));
184 }
185
186 int main(int argc, char** argv) {
187 grpc::testing::TestEnvironment env(&argc, argv);
188 ::testing::InitGoogleTest(&argc, argv);
189 grpc::testing::TestGrpcScope grpc_scope;
190 return RUN_ALL_TESTS();
191 }