• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "google/protobuf/arena_test_util.h"
9 #include "google/protobuf/map_lite_test_util.h"
10 #include <gtest/gtest.h>
11 
12 
13 namespace google {
14 namespace protobuf {
15 namespace {
16 
17 class LiteArenaTest : public testing::Test {
18  protected:
LiteArenaTest()19   LiteArenaTest() {
20     ArenaOptions options;
21     options.start_block_size = 128 * 1024;
22     options.max_block_size = 128 * 1024;
23     arena_.reset(new Arena(options));
24     // Trigger the allocation of the first arena block, so that further use of
25     // the arena will not require any heap allocations.
26     Arena::CreateArray<char>(arena_.get(), 1);
27   }
28 
29   std::unique_ptr<Arena> arena_;
30 };
31 
TEST_F(LiteArenaTest,MapNoHeapAllocation)32 TEST_F(LiteArenaTest, MapNoHeapAllocation) {
33   std::string data;
34   data.reserve(128 * 1024);
35 
36   {
37     // TODO: Enable no heap check when ArenaStringPtr is used in
38     // Map.
39     // internal::NoHeapChecker no_heap;
40 
41     protobuf_unittest::TestArenaMapLite* from =
42         Arena::Create<protobuf_unittest::TestArenaMapLite>(arena_.get());
43     MapLiteTestUtil::SetArenaMapFields(from);
44     from->SerializeToString(&data);
45 
46     protobuf_unittest::TestArenaMapLite* to =
47         Arena::Create<protobuf_unittest::TestArenaMapLite>(arena_.get());
48     to->ParseFromString(data);
49     MapLiteTestUtil::ExpectArenaMapFieldsSet(*to);
50   }
51 }
52 
TEST_F(LiteArenaTest,UnknownFieldMemLeak)53 TEST_F(LiteArenaTest, UnknownFieldMemLeak) {
54   protobuf_unittest::ForeignMessageArenaLite* message =
55       Arena::Create<protobuf_unittest::ForeignMessageArenaLite>(arena_.get());
56   std::string data = "\012\000";
57   int original_capacity = data.capacity();
58   while (data.capacity() <= original_capacity) {
59     data.append("a");
60   }
61   data[1] = data.size() - 2;
62   message->ParseFromString(data);
63 }
64 
65 }  // namespace
66 }  // namespace protobuf
67 }  // namespace google
68 
69