• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/byte_buffer.h>
20 #include <grpc/byte_buffer_reader.h>
21 #include <grpc/grpc.h>
22 #include <grpc/slice.h>
23 
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/time.h>
27 
28 #include "src/core/lib/compression/message_compress.h"
29 #include "src/core/lib/gprpp/thd.h"
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "test/core/util/test_config.h"
32 
33 #include <string.h>
34 
35 #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
36 
test_read_one_slice(void)37 static void test_read_one_slice(void) {
38   grpc_slice slice;
39   grpc_byte_buffer* buffer;
40   grpc_byte_buffer_reader reader;
41   grpc_slice first_slice, second_slice;
42   int first_code, second_code;
43 
44   LOG_TEST("test_read_one_slice");
45   slice = grpc_slice_from_copied_string("test");
46   buffer = grpc_raw_byte_buffer_create(&slice, 1);
47   grpc_slice_unref(slice);
48   GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, buffer) &&
49              "Couldn't init byte buffer reader");
50   first_code = grpc_byte_buffer_reader_next(&reader, &first_slice);
51   GPR_ASSERT(first_code != 0);
52   GPR_ASSERT(memcmp(GRPC_SLICE_START_PTR(first_slice), "test", 4) == 0);
53   grpc_slice_unref(first_slice);
54   second_code = grpc_byte_buffer_reader_next(&reader, &second_slice);
55   GPR_ASSERT(second_code == 0);
56   grpc_byte_buffer_destroy(buffer);
57 }
58 
test_read_one_slice_malloc(void)59 static void test_read_one_slice_malloc(void) {
60   grpc_slice slice;
61   grpc_byte_buffer* buffer;
62   grpc_byte_buffer_reader reader;
63   grpc_slice first_slice, second_slice;
64   int first_code, second_code;
65 
66   LOG_TEST("test_read_one_slice_malloc");
67   slice = grpc_slice_malloc(4);
68   memcpy(GRPC_SLICE_START_PTR(slice), "test", 4);
69   buffer = grpc_raw_byte_buffer_create(&slice, 1);
70   grpc_slice_unref(slice);
71   GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, buffer) &&
72              "Couldn't init byte buffer reader");
73   first_code = grpc_byte_buffer_reader_next(&reader, &first_slice);
74   GPR_ASSERT(first_code != 0);
75   GPR_ASSERT(memcmp(GRPC_SLICE_START_PTR(first_slice), "test", 4) == 0);
76   grpc_slice_unref(first_slice);
77   second_code = grpc_byte_buffer_reader_next(&reader, &second_slice);
78   GPR_ASSERT(second_code == 0);
79   grpc_byte_buffer_destroy(buffer);
80 }
81 
test_read_none_compressed_slice(void)82 static void test_read_none_compressed_slice(void) {
83   grpc_slice slice;
84   grpc_byte_buffer* buffer;
85   grpc_byte_buffer_reader reader;
86   grpc_slice first_slice, second_slice;
87   int first_code, second_code;
88 
89   LOG_TEST("test_read_none_compressed_slice");
90   slice = grpc_slice_from_copied_string("test");
91   buffer = grpc_raw_byte_buffer_create(&slice, 1);
92   grpc_slice_unref(slice);
93   GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, buffer) &&
94              "Couldn't init byte buffer reader");
95   first_code = grpc_byte_buffer_reader_next(&reader, &first_slice);
96   GPR_ASSERT(first_code != 0);
97   GPR_ASSERT(memcmp(GRPC_SLICE_START_PTR(first_slice), "test", 4) == 0);
98   grpc_slice_unref(first_slice);
99   second_code = grpc_byte_buffer_reader_next(&reader, &second_slice);
100   GPR_ASSERT(second_code == 0);
101   grpc_byte_buffer_destroy(buffer);
102 }
103 
test_read_corrupted_slice(void)104 static void test_read_corrupted_slice(void) {
105   grpc_slice slice;
106   grpc_byte_buffer* buffer;
107   grpc_byte_buffer_reader reader;
108 
109   LOG_TEST("test_read_corrupted_slice");
110   slice = grpc_slice_from_copied_string("test");
111   buffer = grpc_raw_byte_buffer_create(&slice, 1);
112   buffer->data.raw.compression = GRPC_COMPRESS_GZIP; /* lies! */
113   grpc_slice_unref(slice);
114   GPR_ASSERT(!grpc_byte_buffer_reader_init(&reader, buffer));
115   grpc_byte_buffer_destroy(buffer);
116 }
117 
read_compressed_slice(grpc_compression_algorithm algorithm,size_t input_size)118 static void read_compressed_slice(grpc_compression_algorithm algorithm,
119                                   size_t input_size) {
120   grpc_slice input_slice;
121   grpc_slice_buffer sliceb_in;
122   grpc_slice_buffer sliceb_out;
123   grpc_byte_buffer* buffer;
124   grpc_byte_buffer_reader reader;
125   grpc_slice read_slice;
126   size_t read_count = 0;
127 
128   grpc_slice_buffer_init(&sliceb_in);
129   grpc_slice_buffer_init(&sliceb_out);
130 
131   input_slice = grpc_slice_malloc(input_size);
132   memset(GRPC_SLICE_START_PTR(input_slice), 'a', input_size);
133   grpc_slice_buffer_add(&sliceb_in, input_slice); /* takes ownership */
134   {
135     grpc_core::ExecCtx exec_ctx;
136     GPR_ASSERT(grpc_msg_compress(
137 
138         grpc_compression_algorithm_to_message_compression_algorithm(algorithm),
139         &sliceb_in, &sliceb_out));
140   }
141 
142   buffer = grpc_raw_compressed_byte_buffer_create(sliceb_out.slices,
143                                                   sliceb_out.count, algorithm);
144   GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, buffer) &&
145              "Couldn't init byte buffer reader");
146 
147   while (grpc_byte_buffer_reader_next(&reader, &read_slice)) {
148     GPR_ASSERT(memcmp(GRPC_SLICE_START_PTR(read_slice),
149                       GRPC_SLICE_START_PTR(input_slice) + read_count,
150                       GRPC_SLICE_LENGTH(read_slice)) == 0);
151     read_count += GRPC_SLICE_LENGTH(read_slice);
152     grpc_slice_unref(read_slice);
153   }
154   GPR_ASSERT(read_count == input_size);
155   grpc_byte_buffer_reader_destroy(&reader);
156   grpc_byte_buffer_destroy(buffer);
157   grpc_slice_buffer_destroy(&sliceb_out);
158   grpc_slice_buffer_destroy(&sliceb_in);
159 }
160 
test_read_gzip_compressed_slice(void)161 static void test_read_gzip_compressed_slice(void) {
162   const size_t INPUT_SIZE = 2048;
163   LOG_TEST("test_read_gzip_compressed_slice");
164   read_compressed_slice(GRPC_COMPRESS_GZIP, INPUT_SIZE);
165 }
166 
test_read_deflate_compressed_slice(void)167 static void test_read_deflate_compressed_slice(void) {
168   const size_t INPUT_SIZE = 2048;
169   LOG_TEST("test_read_deflate_compressed_slice");
170   read_compressed_slice(GRPC_COMPRESS_DEFLATE, INPUT_SIZE);
171 }
172 
test_byte_buffer_from_reader(void)173 static void test_byte_buffer_from_reader(void) {
174   grpc_slice slice;
175   grpc_byte_buffer *buffer, *buffer_from_reader;
176   grpc_byte_buffer_reader reader;
177 
178   LOG_TEST("test_byte_buffer_from_reader");
179   slice = grpc_slice_malloc(4);
180   memcpy(GRPC_SLICE_START_PTR(slice), "test", 4);
181   buffer = grpc_raw_byte_buffer_create(&slice, 1);
182   grpc_slice_unref(slice);
183   GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, buffer) &&
184              "Couldn't init byte buffer reader");
185 
186   buffer_from_reader = grpc_raw_byte_buffer_from_reader(&reader);
187   GPR_ASSERT(buffer->type == buffer_from_reader->type);
188   GPR_ASSERT(buffer_from_reader->data.raw.compression == GRPC_COMPRESS_NONE);
189   GPR_ASSERT(buffer_from_reader->data.raw.slice_buffer.count == 1);
190   GPR_ASSERT(memcmp(GRPC_SLICE_START_PTR(
191                         buffer_from_reader->data.raw.slice_buffer.slices[0]),
192                     "test", 4) == 0);
193 
194   grpc_byte_buffer_destroy(buffer);
195   grpc_byte_buffer_destroy(buffer_from_reader);
196 }
197 
test_readall(void)198 static void test_readall(void) {
199   char* lotsa_as[512];
200   char* lotsa_bs[1024];
201   grpc_slice slices[2];
202   grpc_byte_buffer* buffer;
203   grpc_byte_buffer_reader reader;
204   grpc_slice slice_out;
205 
206   LOG_TEST("test_readall");
207 
208   memset(lotsa_as, 'a', 512 * sizeof(lotsa_as[0]));
209   memset(lotsa_bs, 'b', 1024 * sizeof(lotsa_bs[0]));
210   /* use slices large enough to overflow inlining */
211   slices[0] = grpc_slice_malloc(512);
212   memcpy(GRPC_SLICE_START_PTR(slices[0]), lotsa_as, 512);
213   slices[1] = grpc_slice_malloc(1024);
214   memcpy(GRPC_SLICE_START_PTR(slices[1]), lotsa_bs, 1024);
215 
216   buffer = grpc_raw_byte_buffer_create(slices, 2);
217   grpc_slice_unref(slices[0]);
218   grpc_slice_unref(slices[1]);
219 
220   GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, buffer) &&
221              "Couldn't init byte buffer reader");
222   slice_out = grpc_byte_buffer_reader_readall(&reader);
223 
224   GPR_ASSERT(GRPC_SLICE_LENGTH(slice_out) == 512 + 1024);
225   GPR_ASSERT(memcmp(GRPC_SLICE_START_PTR(slice_out), lotsa_as, 512) == 0);
226   GPR_ASSERT(memcmp(&(GRPC_SLICE_START_PTR(slice_out)[512]), lotsa_bs, 1024) ==
227              0);
228   grpc_slice_unref(slice_out);
229   grpc_byte_buffer_destroy(buffer);
230 }
231 
test_byte_buffer_copy(void)232 static void test_byte_buffer_copy(void) {
233   char* lotsa_as[512];
234   char* lotsa_bs[1024];
235   grpc_slice slices[2];
236   grpc_byte_buffer* buffer;
237   grpc_byte_buffer* copied_buffer;
238   grpc_byte_buffer_reader reader;
239   grpc_slice slice_out;
240 
241   LOG_TEST("test_byte_buffer_copy");
242 
243   memset(lotsa_as, 'a', 512 * sizeof(lotsa_as[0]));
244   memset(lotsa_bs, 'b', 1024 * sizeof(lotsa_bs[0]));
245   /* use slices large enough to overflow inlining */
246   slices[0] = grpc_slice_malloc(512);
247   memcpy(GRPC_SLICE_START_PTR(slices[0]), lotsa_as, 512);
248   slices[1] = grpc_slice_malloc(1024);
249   memcpy(GRPC_SLICE_START_PTR(slices[1]), lotsa_bs, 1024);
250 
251   buffer = grpc_raw_byte_buffer_create(slices, 2);
252   grpc_slice_unref(slices[0]);
253   grpc_slice_unref(slices[1]);
254   copied_buffer = grpc_byte_buffer_copy(buffer);
255 
256   GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, buffer) &&
257              "Couldn't init byte buffer reader");
258   slice_out = grpc_byte_buffer_reader_readall(&reader);
259 
260   GPR_ASSERT(GRPC_SLICE_LENGTH(slice_out) == 512 + 1024);
261   GPR_ASSERT(memcmp(GRPC_SLICE_START_PTR(slice_out), lotsa_as, 512) == 0);
262   GPR_ASSERT(memcmp(&(GRPC_SLICE_START_PTR(slice_out)[512]), lotsa_bs, 1024) ==
263              0);
264   grpc_slice_unref(slice_out);
265   grpc_byte_buffer_destroy(buffer);
266   grpc_byte_buffer_destroy(copied_buffer);
267 }
268 
main(int argc,char ** argv)269 int main(int argc, char** argv) {
270   grpc_test_init(argc, argv);
271   test_read_one_slice();
272   test_read_one_slice_malloc();
273   test_read_none_compressed_slice();
274   test_read_gzip_compressed_slice();
275   test_read_deflate_compressed_slice();
276   test_read_corrupted_slice();
277   test_byte_buffer_from_reader();
278   test_byte_buffer_copy();
279   test_readall();
280   return 0;
281 }
282