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/slice_string_helpers.h"
20
21 #include <grpc/support/alloc.h>
22
23 #include <memory>
24
25 #include "absl/log/log.h"
26 #include "gtest/gtest.h"
27 #include "src/core/util/string.h"
28
expect_slice_dump(grpc_slice slice,uint32_t flags,const char * result)29 static void expect_slice_dump(grpc_slice slice, uint32_t flags,
30 const char* result) {
31 char* got = grpc_dump_slice(slice, flags);
32 ASSERT_STREQ(got, result);
33 gpr_free(got);
34 grpc_slice_unref(slice);
35 }
36
TEST(SliceStringHelpersTest,DumpSlice)37 TEST(SliceStringHelpersTest, DumpSlice) {
38 static const char* text = "HELLO WORLD!";
39 static const char* long_text =
40 "It was a bright cold day in April, and the clocks were striking "
41 "thirteen. Winston Smith, his chin nuzzled into his breast in an effort "
42 "to escape the vile wind, slipped quickly through the glass doors of "
43 "Victory Mansions, though not quickly enough to prevent a swirl of "
44 "gritty dust from entering along with him.";
45
46 LOG(INFO) << "test_dump_slice";
47
48 expect_slice_dump(grpc_slice_from_copied_string(text), GPR_DUMP_ASCII, text);
49 expect_slice_dump(grpc_slice_from_copied_string(long_text), GPR_DUMP_ASCII,
50 long_text);
51 expect_slice_dump(grpc_slice_from_copied_buffer("\x01", 1), GPR_DUMP_HEX,
52 "01");
53 expect_slice_dump(grpc_slice_from_copied_buffer("\x01", 1),
54 GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'");
55 }
56
main(int argc,char ** argv)57 int main(int argc, char** argv) {
58 ::testing::InitGoogleTest(&argc, argv);
59 return RUN_ALL_TESTS();
60 }
61