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 <gtest/gtest.h>
20
21 #include <grpc++/support/slice.h>
22 #include <grpc/grpc.h>
23 #include <grpc/slice.h>
24 #include <grpcpp/impl/grpc_library.h>
25
26 #include "test/core/util/test_config.h"
27
28 namespace grpc {
29
30 static internal::GrpcLibraryInitializer g_gli_initializer;
31
32 namespace {
33
34 const char* kContent = "hello xxxxxxxxxxxxxxxxxxxx world";
35
36 class SliceTest : public ::testing::Test {
37 protected:
SetUpTestCase()38 static void SetUpTestCase() { grpc_init(); }
39
TearDownTestCase()40 static void TearDownTestCase() { grpc_shutdown(); }
41
CheckSliceSize(const Slice & s,const std::string & content)42 void CheckSliceSize(const Slice& s, const std::string& content) {
43 EXPECT_EQ(content.size(), s.size());
44 }
CheckSlice(const Slice & s,const std::string & content)45 void CheckSlice(const Slice& s, const std::string& content) {
46 EXPECT_EQ(content.size(), s.size());
47 EXPECT_EQ(content,
48 std::string(reinterpret_cast<const char*>(s.begin()), s.size()));
49 }
50 };
51
TEST_F(SliceTest,Empty)52 TEST_F(SliceTest, Empty) {
53 Slice empty_slice;
54 CheckSlice(empty_slice, "");
55 }
56
TEST_F(SliceTest,Sized)57 TEST_F(SliceTest, Sized) {
58 Slice sized_slice(strlen(kContent));
59 CheckSliceSize(sized_slice, kContent);
60 }
61
TEST_F(SliceTest,String)62 TEST_F(SliceTest, String) {
63 Slice spp(kContent);
64 CheckSlice(spp, kContent);
65 }
66
TEST_F(SliceTest,Buf)67 TEST_F(SliceTest, Buf) {
68 Slice spp(kContent, strlen(kContent));
69 CheckSlice(spp, kContent);
70 }
71
TEST_F(SliceTest,StaticBuf)72 TEST_F(SliceTest, StaticBuf) {
73 Slice spp(kContent, strlen(kContent), Slice::STATIC_SLICE);
74 CheckSlice(spp, kContent);
75 }
76
TEST_F(SliceTest,SliceNew)77 TEST_F(SliceTest, SliceNew) {
78 char* x = new char[strlen(kContent) + 1];
79 strcpy(x, kContent);
80 Slice spp(x, strlen(x), [](void* p) { delete[] static_cast<char*>(p); });
81 CheckSlice(spp, kContent);
82 }
83
TEST_F(SliceTest,SliceNewDoNothing)84 TEST_F(SliceTest, SliceNewDoNothing) {
85 Slice spp(const_cast<char*>(kContent), strlen(kContent), [](void* /*p*/) {});
86 CheckSlice(spp, kContent);
87 }
88
TEST_F(SliceTest,SliceNewWithUserData)89 TEST_F(SliceTest, SliceNewWithUserData) {
90 struct stest {
91 char* x;
92 int y;
93 };
94 auto* t = new stest;
95 t->x = new char[strlen(kContent) + 1];
96 strcpy(t->x, kContent);
97 Slice spp(
98 t->x, strlen(t->x),
99 [](void* p) {
100 auto* t = static_cast<stest*>(p);
101 delete[] t->x;
102 delete t;
103 },
104 t);
105 CheckSlice(spp, kContent);
106 }
107
TEST_F(SliceTest,SliceNewLen)108 TEST_F(SliceTest, SliceNewLen) {
109 Slice spp(const_cast<char*>(kContent), strlen(kContent),
110 [](void* /*p*/, size_t l) { EXPECT_EQ(l, strlen(kContent)); });
111 CheckSlice(spp, kContent);
112 }
113
TEST_F(SliceTest,Steal)114 TEST_F(SliceTest, Steal) {
115 grpc_slice s = grpc_slice_from_copied_string(kContent);
116 Slice spp(s, Slice::STEAL_REF);
117 CheckSlice(spp, kContent);
118 }
119
TEST_F(SliceTest,Add)120 TEST_F(SliceTest, Add) {
121 grpc_slice s = grpc_slice_from_copied_string(kContent);
122 Slice spp(s, Slice::ADD_REF);
123 grpc_slice_unref(s);
124 CheckSlice(spp, kContent);
125 }
126
TEST_F(SliceTest,Sub)127 TEST_F(SliceTest, Sub) {
128 Slice spp("0123456789");
129 Slice sub = spp.sub(1, 9);
130 CheckSlice(sub, "12345678");
131 }
132
TEST_F(SliceTest,Cslice)133 TEST_F(SliceTest, Cslice) {
134 grpc_slice s = grpc_slice_from_copied_string(kContent);
135 Slice spp(s, Slice::STEAL_REF);
136 CheckSlice(spp, kContent);
137 grpc_slice c_slice = spp.c_slice();
138 EXPECT_EQ(GRPC_SLICE_START_PTR(s), GRPC_SLICE_START_PTR(c_slice));
139 EXPECT_EQ(GRPC_SLICE_END_PTR(s), GRPC_SLICE_END_PTR(c_slice));
140 grpc_slice_unref(c_slice);
141 }
142
143 } // namespace
144 } // namespace grpc
145
main(int argc,char ** argv)146 int main(int argc, char** argv) {
147 grpc::testing::TestEnvironment env(argc, argv);
148 ::testing::InitGoogleTest(&argc, argv);
149 int ret = RUN_ALL_TESTS();
150 return ret;
151 }
152