• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <map>
32 #include <memory>
33 #include <unordered_map>
34 
35 #include <google/protobuf/stubs/logging.h>
36 #include <google/protobuf/stubs/common.h>
37 #include <google/protobuf/arena_test_util.h>
38 #include <google/protobuf/map_test_util.h>
39 #include <google/protobuf/map_unittest.pb.h>
40 #include <google/protobuf/unittest.pb.h>
41 #include <google/protobuf/arena.h>
42 #include <google/protobuf/map.h>
43 #include <google/protobuf/map_field_inl.h>
44 #include <google/protobuf/message.h>
45 #include <google/protobuf/repeated_field.h>
46 #include <gtest/gtest.h>
47 
48 namespace google {
49 namespace protobuf {
50 
51 namespace internal {
52 
53 using unittest::TestAllTypes;
54 
55 class MapFieldBaseStub : public MapFieldBase {
56  public:
57   typedef void InternalArenaConstructable_;
58   typedef void DestructorSkippable_;
MapFieldBaseStub()59   MapFieldBaseStub() {}
MapFieldBaseStub(Arena * arena)60   explicit MapFieldBaseStub(Arena* arena) : MapFieldBase(arena) {}
61   // Get underlined repeated field without synchronizing map.
InternalRepeatedField()62   RepeatedPtrField<Message>* InternalRepeatedField() { return repeated_field_; }
IsMapClean()63   bool IsMapClean() {
64     return state_.load(std::memory_order_relaxed) != STATE_MODIFIED_MAP;
65   }
IsRepeatedClean()66   bool IsRepeatedClean() {
67     return state_.load(std::memory_order_relaxed) != STATE_MODIFIED_REPEATED;
68   }
SetMapDirty()69   void SetMapDirty() {
70     state_.store(STATE_MODIFIED_MAP, std::memory_order_relaxed);
71   }
SetRepeatedDirty()72   void SetRepeatedDirty() {
73     state_.store(STATE_MODIFIED_REPEATED, std::memory_order_relaxed);
74   }
ContainsMapKey(const MapKey & map_key) const75   bool ContainsMapKey(const MapKey& map_key) const override { return false; }
InsertOrLookupMapValue(const MapKey & map_key,MapValueRef * val)76   bool InsertOrLookupMapValue(const MapKey& map_key,
77                               MapValueRef* val) override {
78     return false;
79   }
DeleteMapValue(const MapKey & map_key)80   bool DeleteMapValue(const MapKey& map_key) override { return false; }
EqualIterator(const MapIterator & a,const MapIterator & b) const81   bool EqualIterator(const MapIterator& a,
82                      const MapIterator& b) const override {
83     return false;
84   }
size() const85   int size() const override { return 0; }
Clear()86   void Clear() override {}
MapBegin(MapIterator * map_iter) const87   void MapBegin(MapIterator* map_iter) const override {}
MapEnd(MapIterator * map_iter) const88   void MapEnd(MapIterator* map_iter) const override {}
MergeFrom(const MapFieldBase & other)89   void MergeFrom(const MapFieldBase& other) override {}
Swap(MapFieldBase * other)90   void Swap(MapFieldBase* other) override {}
InitializeIterator(MapIterator * map_iter) const91   void InitializeIterator(MapIterator* map_iter) const override {}
DeleteIterator(MapIterator * map_iter) const92   void DeleteIterator(MapIterator* map_iter) const override {}
CopyIterator(MapIterator * this_iterator,const MapIterator & other_iterator) const93   void CopyIterator(MapIterator* this_iterator,
94                     const MapIterator& other_iterator) const override {}
IncreaseIterator(MapIterator * map_iter) const95   void IncreaseIterator(MapIterator* map_iter) const override {}
96 };
97 
98 class MapFieldBasePrimitiveTest : public ::testing::Test {
99  protected:
100   typedef unittest::TestMap_MapInt32Int32Entry_DoNotUse EntryType;
101   typedef MapField<EntryType, int32, int32, WireFormatLite::TYPE_INT32,
102                    WireFormatLite::TYPE_INT32, false>
103       MapFieldType;
104 
MapFieldBasePrimitiveTest()105   MapFieldBasePrimitiveTest() {
106     // Get descriptors
107     map_descriptor_ = unittest::TestMap::descriptor()
108                           ->FindFieldByName("map_int32_int32")
109                           ->message_type();
110     key_descriptor_ = map_descriptor_->FindFieldByName("key");
111     value_descriptor_ = map_descriptor_->FindFieldByName("value");
112 
113     // Build map field
114     map_field_.reset(new MapFieldType);
115     map_field_base_ = map_field_.get();
116     map_ = map_field_->MutableMap();
117     initial_value_map_[0] = 100;
118     initial_value_map_[1] = 101;
119     map_->insert(initial_value_map_.begin(), initial_value_map_.end());
120     EXPECT_EQ(2, map_->size());
121   }
122 
123   std::unique_ptr<MapFieldType> map_field_;
124   MapFieldBase* map_field_base_;
125   Map<int32, int32>* map_;
126   const Descriptor* map_descriptor_;
127   const FieldDescriptor* key_descriptor_;
128   const FieldDescriptor* value_descriptor_;
129   std::map<int32, int32> initial_value_map_;  // copy of initial values inserted
130 };
131 
TEST_F(MapFieldBasePrimitiveTest,SpaceUsedExcludingSelf)132 TEST_F(MapFieldBasePrimitiveTest, SpaceUsedExcludingSelf) {
133   EXPECT_LT(0, map_field_base_->SpaceUsedExcludingSelf());
134 }
135 
TEST_F(MapFieldBasePrimitiveTest,GetRepeatedField)136 TEST_F(MapFieldBasePrimitiveTest, GetRepeatedField) {
137   const RepeatedPtrField<Message>& repeated =
138       reinterpret_cast<const RepeatedPtrField<Message>&>(
139           map_field_base_->GetRepeatedField());
140   EXPECT_EQ(2, repeated.size());
141   for (int i = 0; i < repeated.size(); i++) {
142     const Message& message = repeated.Get(i);
143     int key = message.GetReflection()->GetInt32(message, key_descriptor_);
144     int value = message.GetReflection()->GetInt32(message, value_descriptor_);
145     EXPECT_EQ(value, initial_value_map_[key]);
146   }
147 }
148 
TEST_F(MapFieldBasePrimitiveTest,MutableRepeatedField)149 TEST_F(MapFieldBasePrimitiveTest, MutableRepeatedField) {
150   RepeatedPtrField<Message>* repeated =
151       reinterpret_cast<RepeatedPtrField<Message>*>(
152           map_field_base_->MutableRepeatedField());
153   EXPECT_EQ(2, repeated->size());
154   for (int i = 0; i < repeated->size(); i++) {
155     const Message& message = repeated->Get(i);
156     int key = message.GetReflection()->GetInt32(message, key_descriptor_);
157     int value = message.GetReflection()->GetInt32(message, value_descriptor_);
158     EXPECT_EQ(value, initial_value_map_[key]);
159   }
160 }
161 
TEST_F(MapFieldBasePrimitiveTest,Arena)162 TEST_F(MapFieldBasePrimitiveTest, Arena) {
163   // Allocate a large initial block to avoid mallocs during hooked test.
164   std::vector<char> arena_block(128 * 1024);
165   ArenaOptions options;
166   options.initial_block = &arena_block[0];
167   options.initial_block_size = arena_block.size();
168   Arena arena(options);
169 
170   {
171     // TODO(liujisi): Re-write the test to ensure the memory for the map and
172     // repeated fields are allocated from arenas.
173     // NoHeapChecker no_heap;
174 
175     MapFieldType* map_field = Arena::CreateMessage<MapFieldType>(&arena);
176 
177     // Set content in map
178     (*map_field->MutableMap())[100] = 101;
179 
180     // Trigger conversion to repeated field.
181     map_field->GetRepeatedField();
182   }
183 
184   {
185     // TODO(liujisi): Re-write the test to ensure the memory for the map and
186     // repeated fields are allocated from arenas.
187     // NoHeapChecker no_heap;
188 
189     MapFieldBaseStub* map_field =
190         Arena::CreateMessage<MapFieldBaseStub>(&arena);
191 
192     // Trigger conversion to repeated field.
193     EXPECT_TRUE(map_field->MutableRepeatedField() != NULL);
194   }
195 }
196 
197 namespace {
198 enum State { CLEAN, MAP_DIRTY, REPEATED_DIRTY };
199 }  // anonymous namespace
200 
201 class MapFieldStateTest : public testing::TestWithParam<State> {
202  public:
203  protected:
204   typedef unittest::TestMap_MapInt32Int32Entry_DoNotUse EntryType;
205   typedef MapField<EntryType, int32, int32, WireFormatLite::TYPE_INT32,
206                    WireFormatLite::TYPE_INT32, false>
207       MapFieldType;
MapFieldStateTest()208   MapFieldStateTest() : state_(GetParam()) {
209     // Build map field
210     map_field_.reset(new MapFieldType());
211     map_field_base_ = map_field_.get();
212 
213     Expect(map_field_.get(), MAP_DIRTY, 0, 0, true);
214     switch (state_) {
215       case CLEAN:
216         AddOneStillClean(map_field_.get());
217         break;
218       case MAP_DIRTY:
219         MakeMapDirty(map_field_.get());
220         break;
221       case REPEATED_DIRTY:
222         MakeRepeatedDirty(map_field_.get());
223         break;
224       default:
225         break;
226     }
227   }
228 
AddOneStillClean(MapFieldType * map_field)229   void AddOneStillClean(MapFieldType* map_field) {
230     MapFieldBase* map_field_base = map_field;
231     Map<int32, int32>* map = map_field->MutableMap();
232     (*map)[0] = 0;
233     map_field_base->GetRepeatedField();
234     Expect(map_field, CLEAN, 1, 1, false);
235   }
236 
MakeMapDirty(MapFieldType * map_field)237   void MakeMapDirty(MapFieldType* map_field) {
238     Map<int32, int32>* map = map_field->MutableMap();
239     (*map)[0] = 0;
240     Expect(map_field, MAP_DIRTY, 1, 0, true);
241   }
242 
MakeRepeatedDirty(MapFieldType * map_field)243   void MakeRepeatedDirty(MapFieldType* map_field) {
244     MakeMapDirty(map_field);
245     MapFieldBase* map_field_base = map_field;
246     map_field_base->MutableRepeatedField();
247     // We use MutableMap on impl_ because we don't want to disturb the syncing
248     Map<int32, int32>* map = map_field->impl_.MutableMap();
249     map->clear();
250 
251     Expect(map_field, REPEATED_DIRTY, 0, 1, false);
252   }
253 
Expect(MapFieldType * map_field,State state,int map_size,int repeated_size,bool is_repeated_null)254   void Expect(MapFieldType* map_field, State state, int map_size,
255               int repeated_size, bool is_repeated_null) {
256     MapFieldBase* map_field_base = map_field;
257     MapFieldBaseStub* stub =
258         reinterpret_cast<MapFieldBaseStub*>(map_field_base);
259 
260     // We use MutableMap on impl_ because we don't want to disturb the syncing
261     Map<int32, int32>* map = map_field->impl_.MutableMap();
262     RepeatedPtrField<Message>* repeated_field = stub->InternalRepeatedField();
263 
264     switch (state) {
265       case MAP_DIRTY:
266         EXPECT_FALSE(stub->IsMapClean());
267         EXPECT_TRUE(stub->IsRepeatedClean());
268         break;
269       case REPEATED_DIRTY:
270         EXPECT_TRUE(stub->IsMapClean());
271         EXPECT_FALSE(stub->IsRepeatedClean());
272         break;
273       case CLEAN:
274         EXPECT_TRUE(stub->IsMapClean());
275         EXPECT_TRUE(stub->IsRepeatedClean());
276         break;
277       default:
278         FAIL();
279     }
280 
281     EXPECT_EQ(map_size, map->size());
282     if (is_repeated_null) {
283       EXPECT_TRUE(repeated_field == NULL);
284     } else {
285       if (repeated_field == nullptr) {
286         EXPECT_EQ(repeated_size, 0);
287       } else {
288         EXPECT_EQ(repeated_size, repeated_field->size());
289       }
290     }
291   }
292 
293   std::unique_ptr<MapFieldType> map_field_;
294   MapFieldBase* map_field_base_;
295   State state_;
296 };
297 
298 INSTANTIATE_TEST_SUITE_P(MapFieldStateTestInstance, MapFieldStateTest,
299                          ::testing::Values(CLEAN, MAP_DIRTY, REPEATED_DIRTY));
300 
TEST_P(MapFieldStateTest,GetMap)301 TEST_P(MapFieldStateTest, GetMap) {
302   map_field_->GetMap();
303   if (state_ != MAP_DIRTY) {
304     Expect(map_field_.get(), CLEAN, 1, 1, false);
305   } else {
306     Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
307   }
308 }
309 
TEST_P(MapFieldStateTest,MutableMap)310 TEST_P(MapFieldStateTest, MutableMap) {
311   map_field_->MutableMap();
312   if (state_ != MAP_DIRTY) {
313     Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
314   } else {
315     Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
316   }
317 }
318 
TEST_P(MapFieldStateTest,MergeFromClean)319 TEST_P(MapFieldStateTest, MergeFromClean) {
320   MapFieldType other;
321   AddOneStillClean(&other);
322 
323   map_field_->MergeFrom(other);
324 
325   if (state_ != MAP_DIRTY) {
326     Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
327   } else {
328     Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
329   }
330 
331   Expect(&other, CLEAN, 1, 1, false);
332 }
333 
TEST_P(MapFieldStateTest,MergeFromMapDirty)334 TEST_P(MapFieldStateTest, MergeFromMapDirty) {
335   MapFieldType other;
336   MakeMapDirty(&other);
337 
338   map_field_->MergeFrom(other);
339 
340   if (state_ != MAP_DIRTY) {
341     Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
342   } else {
343     Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
344   }
345 
346   Expect(&other, MAP_DIRTY, 1, 0, true);
347 }
348 
TEST_P(MapFieldStateTest,MergeFromRepeatedDirty)349 TEST_P(MapFieldStateTest, MergeFromRepeatedDirty) {
350   MapFieldType other;
351   MakeRepeatedDirty(&other);
352 
353   map_field_->MergeFrom(other);
354 
355   if (state_ != MAP_DIRTY) {
356     Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
357   } else {
358     Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
359   }
360 
361   Expect(&other, CLEAN, 1, 1, false);
362 }
363 
TEST_P(MapFieldStateTest,SwapClean)364 TEST_P(MapFieldStateTest, SwapClean) {
365   MapFieldType other;
366   AddOneStillClean(&other);
367 
368   map_field_->Swap(&other);
369 
370   Expect(map_field_.get(), CLEAN, 1, 1, false);
371 
372   switch (state_) {
373     case CLEAN:
374       Expect(&other, CLEAN, 1, 1, false);
375       break;
376     case MAP_DIRTY:
377       Expect(&other, MAP_DIRTY, 1, 0, true);
378       break;
379     case REPEATED_DIRTY:
380       Expect(&other, REPEATED_DIRTY, 0, 1, false);
381       break;
382     default:
383       break;
384   }
385 }
386 
TEST_P(MapFieldStateTest,SwapMapDirty)387 TEST_P(MapFieldStateTest, SwapMapDirty) {
388   MapFieldType other;
389   MakeMapDirty(&other);
390 
391   map_field_->Swap(&other);
392 
393   Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
394 
395   switch (state_) {
396     case CLEAN:
397       Expect(&other, CLEAN, 1, 1, false);
398       break;
399     case MAP_DIRTY:
400       Expect(&other, MAP_DIRTY, 1, 0, true);
401       break;
402     case REPEATED_DIRTY:
403       Expect(&other, REPEATED_DIRTY, 0, 1, false);
404       break;
405     default:
406       break;
407   }
408 }
409 
TEST_P(MapFieldStateTest,SwapRepeatedDirty)410 TEST_P(MapFieldStateTest, SwapRepeatedDirty) {
411   MapFieldType other;
412   MakeRepeatedDirty(&other);
413 
414   map_field_->Swap(&other);
415 
416   Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
417 
418   switch (state_) {
419     case CLEAN:
420       Expect(&other, CLEAN, 1, 1, false);
421       break;
422     case MAP_DIRTY:
423       Expect(&other, MAP_DIRTY, 1, 0, true);
424       break;
425     case REPEATED_DIRTY:
426       Expect(&other, REPEATED_DIRTY, 0, 1, false);
427       break;
428     default:
429       break;
430   }
431 }
432 
TEST_P(MapFieldStateTest,Clear)433 TEST_P(MapFieldStateTest, Clear) {
434   map_field_->Clear();
435 
436   Expect(map_field_.get(), MAP_DIRTY, 0, 0, false);
437 }
438 
TEST_P(MapFieldStateTest,SpaceUsedExcludingSelf)439 TEST_P(MapFieldStateTest, SpaceUsedExcludingSelf) {
440   map_field_base_->SpaceUsedExcludingSelf();
441 
442   switch (state_) {
443     case CLEAN:
444       Expect(map_field_.get(), CLEAN, 1, 1, false);
445       break;
446     case MAP_DIRTY:
447       Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
448       break;
449     case REPEATED_DIRTY:
450       Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
451       break;
452     default:
453       break;
454   }
455 }
456 
TEST_P(MapFieldStateTest,GetMapField)457 TEST_P(MapFieldStateTest, GetMapField) {
458   map_field_base_->GetRepeatedField();
459 
460   if (state_ != REPEATED_DIRTY) {
461     Expect(map_field_.get(), CLEAN, 1, 1, false);
462   } else {
463     Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
464   }
465 }
466 
TEST_P(MapFieldStateTest,MutableMapField)467 TEST_P(MapFieldStateTest, MutableMapField) {
468   map_field_base_->MutableRepeatedField();
469 
470   if (state_ != REPEATED_DIRTY) {
471     Expect(map_field_.get(), REPEATED_DIRTY, 1, 1, false);
472   } else {
473     Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
474   }
475 }
476 
477 
478 }  // namespace internal
479 }  // namespace protobuf
480 }  // namespace google
481