xref: /aosp_15_r20/external/grpc-grpc/test/core/slice/c_slice_buffer_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <stddef.h>
20 
21 #include "gtest/gtest.h"
22 
23 #include <grpc/slice.h>
24 #include <grpc/slice_buffer.h>
25 #include <grpc/support/alloc.h>
26 
27 #include "src/core/lib/slice/slice_internal.h"
28 #include "test/core/util/test_config.h"
29 
30 static constexpr size_t kTotalDataLength = 4096;
31 
test_slice_buffer_add()32 void test_slice_buffer_add() {
33   grpc_slice_buffer buf;
34   grpc_slice aaa = grpc_slice_from_copied_string("aaa");
35   grpc_slice bb = grpc_slice_from_copied_string("bb");
36   size_t i;
37 
38   grpc_slice_buffer_init(&buf);
39   for (i = 0; i < 10; i++) {
40     grpc_slice_ref(aaa);
41     grpc_slice_ref(bb);
42     grpc_slice_buffer_add(&buf, aaa);
43     grpc_slice_buffer_add(&buf, bb);
44   }
45   ASSERT_GT(buf.count, 0);
46   ASSERT_EQ(buf.length, 50);
47   grpc_slice_buffer_reset_and_unref(&buf);
48   ASSERT_EQ(buf.count, 0);
49   ASSERT_EQ(buf.length, 0);
50   for (i = 0; i < 10; i++) {
51     grpc_slice_ref(aaa);
52     grpc_slice_ref(bb);
53     grpc_slice_buffer_add(&buf, aaa);
54     grpc_slice_buffer_add(&buf, bb);
55   }
56   ASSERT_GT(buf.count, 0);
57   ASSERT_EQ(buf.length, 50);
58   for (i = 0; i < 10; i++) {
59     grpc_slice_buffer_pop(&buf);
60     grpc_slice_unref(aaa);
61     grpc_slice_unref(bb);
62   }
63   ASSERT_EQ(buf.count, 0);
64   ASSERT_EQ(buf.length, 0);
65   grpc_slice_buffer_destroy(&buf);
66 }
67 
free_data(void * data,size_t len)68 static void free_data(void* data, size_t len) {
69   ASSERT_EQ(len, kTotalDataLength);
70   gpr_free(data);
71 }
72 
test_slice_buffer_add_contiguous_slices()73 void test_slice_buffer_add_contiguous_slices() {
74   grpc_slice_buffer buf;
75   grpc_slice_buffer_init(&buf);
76   char* data = reinterpret_cast<char*>(gpr_malloc(kTotalDataLength));
77   ASSERT_NE(data, nullptr);
78   grpc_slice a = grpc_slice_new_with_len(data, kTotalDataLength, free_data);
79   grpc_slice s1 = grpc_slice_split_head(&a, kTotalDataLength / 4);
80   grpc_slice s2 = grpc_slice_split_head(&a, kTotalDataLength / 4);
81   grpc_slice s3 = grpc_slice_split_head(&a, kTotalDataLength / 4);
82   grpc_slice_buffer_add(&buf, s1);
83   ASSERT_EQ(buf.count, 1);
84   ASSERT_EQ(buf.length, kTotalDataLength / 4);
85   grpc_slice_buffer_add(&buf, s2);
86   ASSERT_EQ(buf.count, 1);
87   ASSERT_EQ(buf.length, kTotalDataLength / 2);
88   grpc_slice_buffer_add(&buf, s3);
89   ASSERT_EQ(buf.count, 1);
90   ASSERT_EQ(buf.length, 3 * kTotalDataLength / 4);
91   grpc_slice_buffer_add(&buf, a);
92   ASSERT_EQ(buf.count, 1);
93   ASSERT_EQ(buf.length, kTotalDataLength);
94   grpc_slice_buffer_destroy(&buf);
95 }
96 
test_slice_buffer_move_first()97 void test_slice_buffer_move_first() {
98   grpc_slice slices[3];
99   grpc_slice_buffer src;
100   grpc_slice_buffer dst;
101   int idx = 0;
102   size_t src_len = 0;
103   size_t dst_len = 0;
104 
105   slices[0] = grpc_slice_from_copied_string("aaa");
106   slices[1] = grpc_slice_from_copied_string("bbbb");
107   slices[2] = grpc_slice_from_copied_string("ccc");
108 
109   grpc_slice_buffer_init(&src);
110   grpc_slice_buffer_init(&dst);
111   for (idx = 0; idx < 3; idx++) {
112     grpc_slice_ref(slices[idx]);
113     // For this test, it is important that we add each slice at a new
114     // slice index
115     grpc_slice_buffer_add_indexed(&src, slices[idx]);
116     grpc_slice_buffer_add_indexed(&dst, slices[idx]);
117   }
118 
119   // Case 1: Move more than the first slice's length from src to dst
120   src_len = src.length;
121   dst_len = dst.length;
122   grpc_slice_buffer_move_first(&src, 4, &dst);
123   src_len -= 4;
124   dst_len += 4;
125   ASSERT_EQ(src.length, src_len);
126   ASSERT_EQ(dst.length, dst_len);
127 
128   // src now has two slices ["bbb"] and  ["ccc"]
129   // Case 2: Move the first slice from src to dst
130   grpc_slice_buffer_move_first(&src, 3, &dst);
131   src_len -= 3;
132   dst_len += 3;
133   ASSERT_EQ(src.length, src_len);
134   ASSERT_EQ(dst.length, dst_len);
135 
136   // src now has one slice ["ccc"]
137   // Case 3: Move less than the first slice's length from src to dst
138   grpc_slice_buffer_move_first(&src, 2, &dst);
139   src_len -= 2;
140   dst_len += 2;
141   ASSERT_EQ(src.length, src_len);
142   ASSERT_EQ(dst.length, dst_len);
143 }
144 
test_slice_buffer_first()145 void test_slice_buffer_first() {
146   grpc_slice slices[3];
147   slices[0] = grpc_slice_from_copied_string("aaa");
148   slices[1] = grpc_slice_from_copied_string("bbbb");
149   slices[2] = grpc_slice_from_copied_string("ccccc");
150 
151   grpc_slice_buffer buf;
152   grpc_slice_buffer_init(&buf);
153   for (int idx = 0; idx < 3; ++idx) {
154     grpc_slice_ref(slices[idx]);
155     grpc_slice_buffer_add_indexed(&buf, slices[idx]);
156   }
157 
158   grpc_slice* first = grpc_slice_buffer_peek_first(&buf);
159   ASSERT_EQ(GRPC_SLICE_LENGTH(*first), GRPC_SLICE_LENGTH(slices[0]));
160   ASSERT_EQ(buf.count, 3);
161   ASSERT_EQ(buf.length, 12);
162 
163   grpc_slice_buffer_sub_first(&buf, 1, 2);
164   first = grpc_slice_buffer_peek_first(&buf);
165   ASSERT_EQ(GRPC_SLICE_LENGTH(*first), 1);
166   ASSERT_EQ(buf.count, 3);
167   ASSERT_EQ(buf.length, 10);
168 
169   grpc_slice_buffer_remove_first(&buf);
170   first = grpc_slice_buffer_peek_first(&buf);
171   ASSERT_EQ(GRPC_SLICE_LENGTH(*first), GRPC_SLICE_LENGTH(slices[1]));
172   ASSERT_EQ(buf.count, 2);
173   ASSERT_EQ(buf.length, 9);
174 
175   grpc_slice_buffer_remove_first(&buf);
176   first = grpc_slice_buffer_peek_first(&buf);
177   ASSERT_EQ(GRPC_SLICE_LENGTH(*first), GRPC_SLICE_LENGTH(slices[2]));
178   ASSERT_EQ(buf.count, 1);
179   ASSERT_EQ(buf.length, 5);
180 
181   grpc_slice_buffer_remove_first(&buf);
182   ASSERT_EQ(buf.count, 0);
183   ASSERT_EQ(buf.length, 0);
184 }
185 
main(int argc,char ** argv)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 }
192