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