byte_buffer_test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include <cstring>
  19. #include <vector>
  20. #include <gtest/gtest.h>
  21. #include <grpc++/support/byte_buffer.h>
  22. #include <grpc/grpc.h>
  23. #include <grpc/slice.h>
  24. #include <grpcpp/impl/grpc_library.h>
  25. #include <grpcpp/support/slice.h>
  26. #include "test/core/util/test_config.h"
  27. namespace grpc {
  28. static internal::GrpcLibraryInitializer g_gli_initializer;
  29. namespace {
  30. const char* kContent1 = "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  31. const char* kContent2 = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy world";
  32. class ByteBufferTest : public ::testing::Test {
  33. protected:
  34. static void SetUpTestCase() { grpc_init(); }
  35. static void TearDownTestCase() { grpc_shutdown(); }
  36. };
  37. TEST_F(ByteBufferTest, CopyCtor) {
  38. ByteBuffer buffer1;
  39. EXPECT_FALSE(buffer1.Valid());
  40. const ByteBuffer& buffer2 = buffer1;
  41. EXPECT_FALSE(buffer2.Valid());
  42. }
  43. TEST_F(ByteBufferTest, CreateFromSingleSlice) {
  44. Slice s(kContent1);
  45. ByteBuffer buffer(&s, 1);
  46. EXPECT_EQ(strlen(kContent1), buffer.Length());
  47. }
  48. TEST_F(ByteBufferTest, CreateFromVector) {
  49. std::vector<Slice> slices;
  50. slices.emplace_back(kContent1);
  51. slices.emplace_back(kContent2);
  52. ByteBuffer buffer(&slices[0], 2);
  53. EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
  54. }
  55. TEST_F(ByteBufferTest, Clear) {
  56. Slice s(kContent1);
  57. ByteBuffer buffer(&s, 1);
  58. buffer.Clear();
  59. EXPECT_EQ(static_cast<size_t>(0), buffer.Length());
  60. }
  61. TEST_F(ByteBufferTest, Length) {
  62. std::vector<Slice> slices;
  63. slices.emplace_back(kContent1);
  64. slices.emplace_back(kContent2);
  65. ByteBuffer buffer(&slices[0], 2);
  66. EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
  67. }
  68. bool SliceEqual(const Slice& a, grpc_slice b) {
  69. if (a.size() != GRPC_SLICE_LENGTH(b)) {
  70. return false;
  71. }
  72. for (size_t i = 0; i < a.size(); i++) {
  73. if (a.begin()[i] != GRPC_SLICE_START_PTR(b)[i]) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. TEST_F(ByteBufferTest, Dump) {
  80. grpc_slice hello = grpc_slice_from_copied_string(kContent1);
  81. grpc_slice world = grpc_slice_from_copied_string(kContent2);
  82. std::vector<Slice> slices;
  83. slices.push_back(Slice(hello, Slice::STEAL_REF));
  84. slices.push_back(Slice(world, Slice::STEAL_REF));
  85. ByteBuffer buffer(&slices[0], 2);
  86. slices.clear();
  87. (void)buffer.Dump(&slices);
  88. EXPECT_TRUE(SliceEqual(slices[0], hello));
  89. EXPECT_TRUE(SliceEqual(slices[1], world));
  90. }
  91. TEST_F(ByteBufferTest, SerializationMakesCopy) {
  92. grpc_slice hello = grpc_slice_from_copied_string(kContent1);
  93. grpc_slice world = grpc_slice_from_copied_string(kContent2);
  94. std::vector<Slice> slices;
  95. slices.push_back(Slice(hello, Slice::STEAL_REF));
  96. slices.push_back(Slice(world, Slice::STEAL_REF));
  97. ByteBuffer send_buffer;
  98. bool owned = false;
  99. ByteBuffer buffer(&slices[0], 2);
  100. slices.clear();
  101. auto status = SerializationTraits<ByteBuffer, void>::Serialize(
  102. buffer, &send_buffer, &owned);
  103. EXPECT_TRUE(status.ok());
  104. EXPECT_TRUE(owned);
  105. EXPECT_TRUE(send_buffer.Valid());
  106. }
  107. TEST_F(ByteBufferTest, TrySingleSliceWithSingleSlice) {
  108. std::vector<Slice> slices;
  109. slices.emplace_back(kContent1);
  110. ByteBuffer buffer(&slices[0], 1);
  111. Slice slice;
  112. EXPECT_TRUE(buffer.TrySingleSlice(&slice).ok());
  113. EXPECT_EQ(slice.size(), slices[0].size());
  114. EXPECT_EQ(memcmp(slice.begin(), slices[0].begin(), slice.size()), 0);
  115. }
  116. TEST_F(ByteBufferTest, TrySingleSliceWithMultipleSlices) {
  117. std::vector<Slice> slices;
  118. slices.emplace_back(kContent1);
  119. slices.emplace_back(kContent2);
  120. ByteBuffer buffer(&slices[0], 2);
  121. Slice slice;
  122. EXPECT_FALSE(buffer.TrySingleSlice(&slice).ok());
  123. }
  124. TEST_F(ByteBufferTest, DumpToSingleSlice) {
  125. std::vector<Slice> slices;
  126. slices.emplace_back(kContent1);
  127. slices.emplace_back(kContent2);
  128. ByteBuffer buffer(&slices[0], 2);
  129. Slice slice;
  130. EXPECT_TRUE(buffer.DumpToSingleSlice(&slice).ok());
  131. EXPECT_EQ(strlen(kContent1) + strlen(kContent2), slice.size());
  132. }
  133. } // namespace
  134. } // namespace grpc
  135. int main(int argc, char** argv) {
  136. grpc::testing::TestEnvironment env(argc, argv);
  137. ::testing::InitGoogleTest(&argc, argv);
  138. int ret = RUN_ALL_TESTS();
  139. return ret;
  140. }