1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/key_value_store.h"
18
19 #include <gtest/gtest.h>
20
21 using std::map;
22 using std::string;
23 using std::vector;
24 using testing::Test;
25
26 namespace shill {
27
28 class KeyValueStoreTest : public Test {
29 public:
KeyValueStoreTest()30 KeyValueStoreTest() {}
31
32 protected:
33 KeyValueStore store_;
34 };
35
TEST_F(KeyValueStoreTest,Any)36 TEST_F(KeyValueStoreTest, Any) {
37 const string kKey("foo");
38 const string kValue("baz");
39 EXPECT_FALSE(store_.Contains(kKey));
40 store_.Set(kKey, brillo::Any(kValue));
41 EXPECT_TRUE(store_.Contains(kKey));
42 EXPECT_EQ(kValue, store_.Get(kKey).Get<string>());
43 store_.Remove(kKey);
44 EXPECT_FALSE(store_.Contains(kKey));
45 }
46
TEST_F(KeyValueStoreTest,Bool)47 TEST_F(KeyValueStoreTest, Bool) {
48 const string kKey("foo");
49 const bool kDefaultValue = true;
50 const bool kValue = false;
51 EXPECT_FALSE(store_.ContainsBool(kKey));
52 EXPECT_EQ(kDefaultValue, store_.LookupBool(kKey, kDefaultValue));
53 store_.SetBool(kKey, kValue);
54 EXPECT_TRUE(store_.ContainsBool(kKey));
55 // TODO(shenhan): investigate if a newer version of gtest handles EXPECT_EQ
56 // for bools in a manner that gcc 4.7 is happy with. (Improper conversion from
57 // "false" to "NULL").
58 EXPECT_EQ(static_cast<int>(kValue),
59 static_cast<int>(store_.LookupBool(kKey, kDefaultValue)));
60 EXPECT_EQ(static_cast<int>(kValue),
61 static_cast<int>(store_.GetBool(kKey)));
62 }
63
TEST_F(KeyValueStoreTest,ByteArrays)64 TEST_F(KeyValueStoreTest, ByteArrays) {
65 const string kKey("foo");
66 const vector<vector<uint8_t>> kValue{ {1, 2, 3 } };
67 EXPECT_FALSE(store_.ContainsByteArrays(kKey));
68 store_.SetByteArrays(kKey, kValue);
69 EXPECT_TRUE(store_.ContainsByteArrays(kKey));
70 EXPECT_EQ(kValue, store_.GetByteArrays(kKey));
71 store_.RemoveByteArrays(kKey);
72 EXPECT_FALSE(store_.ContainsByteArrays(kKey));
73 }
74
TEST_F(KeyValueStoreTest,Int)75 TEST_F(KeyValueStoreTest, Int) {
76 const string kKey("foo");
77 const int kValue = 456;
78 EXPECT_FALSE(store_.ContainsInt(kKey));
79 const int kDefaultValue = 789;
80 EXPECT_EQ(kDefaultValue, store_.LookupInt(kKey, kDefaultValue));
81 store_.SetInt(kKey, kValue);
82 EXPECT_TRUE(store_.ContainsInt(kKey));
83 EXPECT_EQ(kValue, store_.GetInt(kKey));
84 EXPECT_EQ(kValue, store_.LookupInt(kKey, kDefaultValue));
85 store_.RemoveInt(kKey);
86 EXPECT_FALSE(store_.ContainsInt(kKey));
87 }
88
TEST_F(KeyValueStoreTest,Int16)89 TEST_F(KeyValueStoreTest, Int16) {
90 const string kKey("foo");
91 const int16_t kValue = 123;
92 EXPECT_FALSE(store_.ContainsInt16(kKey));
93 store_.SetInt16(kKey, kValue);
94 EXPECT_TRUE(store_.ContainsInt16(kKey));
95 EXPECT_EQ(kValue, store_.GetInt16(kKey));
96 store_.RemoveInt16(kKey);
97 EXPECT_FALSE(store_.ContainsInt16(kKey));
98 }
99
TEST_F(KeyValueStoreTest,KeyValueStore)100 TEST_F(KeyValueStoreTest, KeyValueStore) {
101 const string kSubKey("foo");
102 const map<string, string> kSubValue{ { "bar0", "baz0" }, { "bar1", "baz1" } };
103 KeyValueStore value;
104 value.SetStringmap(kSubKey, kSubValue);
105 const string kKey("foo");
106 EXPECT_FALSE(store_.ContainsKeyValueStore(kKey));
107 store_.SetKeyValueStore(kKey, value);
108 EXPECT_TRUE(store_.ContainsKeyValueStore(kKey));
109 EXPECT_EQ(value, store_.GetKeyValueStore(kKey));
110 store_.RemoveKeyValueStore(kKey);
111 EXPECT_FALSE(store_.ContainsKeyValueStore(kKey));
112 }
113
TEST_F(KeyValueStoreTest,RpcIdentifier)114 TEST_F(KeyValueStoreTest, RpcIdentifier) {
115 const string kKey("foo");
116 const string kValue("baz");
117 EXPECT_FALSE(store_.ContainsRpcIdentifier(kKey));
118 store_.SetRpcIdentifier(kKey, kValue);
119 EXPECT_TRUE(store_.ContainsRpcIdentifier(kKey));
120 EXPECT_EQ(kValue, store_.GetRpcIdentifier(kKey));
121 store_.RemoveRpcIdentifier(kKey);
122 EXPECT_FALSE(store_.ContainsRpcIdentifier(kKey));
123 }
124
TEST_F(KeyValueStoreTest,RpcIdentifiers)125 TEST_F(KeyValueStoreTest, RpcIdentifiers) {
126 const string kKey("foo");
127 const vector<string> kValue{ "baz0", "baz1", "baz2" };
128 EXPECT_FALSE(store_.ContainsRpcIdentifiers(kKey));
129 store_.SetRpcIdentifiers(kKey, kValue);
130 EXPECT_TRUE(store_.ContainsRpcIdentifiers(kKey));
131 EXPECT_EQ(kValue, store_.GetRpcIdentifiers(kKey));
132 store_.Remove(kKey);
133 EXPECT_FALSE(store_.ContainsRpcIdentifiers(kKey));
134 }
135
TEST_F(KeyValueStoreTest,String)136 TEST_F(KeyValueStoreTest, String) {
137 const string kKey("foo");
138 const string kDefaultValue("bar");
139 const string kValue("baz");
140 EXPECT_FALSE(store_.ContainsString(kKey));
141 EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue));
142 store_.SetString(kKey, kValue);
143 EXPECT_TRUE(store_.ContainsString(kKey));
144 EXPECT_EQ(kValue, store_.LookupString(kKey, kDefaultValue));
145 EXPECT_EQ(kValue, store_.GetString(kKey));
146 store_.RemoveString(kKey);
147 EXPECT_FALSE(store_.ContainsString(kKey));
148 EXPECT_EQ(kDefaultValue, store_.LookupString(kKey, kDefaultValue));
149 }
150
TEST_F(KeyValueStoreTest,Stringmap)151 TEST_F(KeyValueStoreTest, Stringmap) {
152 const string kKey("foo");
153 const map<string, string> kValue{ { "bar0", "baz0" }, { "bar1", "baz1" } };
154 EXPECT_FALSE(store_.ContainsStringmap(kKey));
155 store_.SetStringmap(kKey, kValue);
156 EXPECT_TRUE(store_.ContainsStringmap(kKey));
157 EXPECT_EQ(kValue, store_.GetStringmap(kKey));
158 store_.RemoveStringmap(kKey);
159 EXPECT_FALSE(store_.ContainsStringmap(kKey));
160 }
161
TEST_F(KeyValueStoreTest,Strings)162 TEST_F(KeyValueStoreTest, Strings) {
163 const string kKey("foo");
164 const vector<string> kValue{ "baz0", "baz1", "baz2" };
165 EXPECT_FALSE(store_.ContainsStrings(kKey));
166 store_.SetStrings(kKey, kValue);
167 EXPECT_TRUE(store_.ContainsStrings(kKey));
168 EXPECT_EQ(kValue, store_.GetStrings(kKey));
169 store_.RemoveStrings(kKey);
170 EXPECT_FALSE(store_.ContainsStrings(kKey));
171 }
172
TEST_F(KeyValueStoreTest,Uint)173 TEST_F(KeyValueStoreTest, Uint) {
174 const string kKey("foo");
175 const uint32_t kValue = 456;
176 EXPECT_FALSE(store_.ContainsUint(kKey));
177 store_.SetUint(kKey, kValue);
178 EXPECT_TRUE(store_.ContainsUint(kKey));
179 EXPECT_EQ(kValue, store_.GetUint(kKey));
180 }
181
TEST_F(KeyValueStoreTest,Uint16)182 TEST_F(KeyValueStoreTest, Uint16) {
183 const string kKey("foo");
184 const uint16_t kValue = 456;
185 EXPECT_FALSE(store_.ContainsUint16(kKey));
186 store_.SetUint16(kKey, kValue);
187 EXPECT_TRUE(store_.ContainsUint16(kKey));
188 EXPECT_EQ(kValue, store_.GetUint16(kKey));
189 }
190
TEST_F(KeyValueStoreTest,Uint8)191 TEST_F(KeyValueStoreTest, Uint8) {
192 const string kKey("foo");
193 const uint8_t kValue = 123;
194 EXPECT_FALSE(store_.ContainsUint8(kKey));
195 store_.SetUint8(kKey, kValue);
196 EXPECT_TRUE(store_.ContainsUint8(kKey));
197 EXPECT_EQ(kValue, store_.GetUint8(kKey));
198 store_.RemoveUint8(kKey);
199 EXPECT_FALSE(store_.ContainsUint8(kKey));
200 }
201
TEST_F(KeyValueStoreTest,Uint8s)202 TEST_F(KeyValueStoreTest, Uint8s) {
203 const string kKey("foo");
204 const vector<uint8_t> kValue{ 1, 2, 3 };
205 EXPECT_FALSE(store_.ContainsUint8s(kKey));
206 store_.SetUint8s(kKey, kValue);
207 EXPECT_TRUE(store_.ContainsUint8s(kKey));
208 EXPECT_EQ(kValue, store_.GetUint8s(kKey));
209 store_.RemoveUint8s(kKey);
210 EXPECT_FALSE(store_.ContainsUint8s(kKey));
211 }
212
TEST_F(KeyValueStoreTest,Uint32s)213 TEST_F(KeyValueStoreTest, Uint32s) {
214 const string kKey("foo");
215 const vector<uint32_t> kValue{ 1, 2, 3 };
216 EXPECT_FALSE(store_.ContainsUint32s(kKey));
217 store_.SetUint32s(kKey, kValue);
218 EXPECT_TRUE(store_.ContainsUint32s(kKey));
219 EXPECT_EQ(kValue, store_.GetUint32s(kKey));
220 store_.RemoveUint32s(kKey);
221 EXPECT_FALSE(store_.ContainsUint32s(kKey));
222 }
223
TEST_F(KeyValueStoreTest,DoubleRemove)224 TEST_F(KeyValueStoreTest, DoubleRemove) {
225 const string kKey("foo");
226 // Make sure we don't get an exception/infinite loop if we do a
227 // "Remove()" when the key does not exist.
228 store_.RemoveInt(kKey);
229 store_.RemoveInt(kKey);
230 store_.RemoveString(kKey);
231 store_.RemoveString(kKey);
232 }
233
TEST_F(KeyValueStoreTest,Clear)234 TEST_F(KeyValueStoreTest, Clear) {
235 EXPECT_TRUE(store_.IsEmpty());
236 const string kBoolKey("foo");
237 const bool kBoolValue = true;
238 store_.SetBool(kBoolKey, kBoolValue);
239 const string kByteArraysKey("bytearrays");
240 const vector<vector<uint8_t>> kByteArraysValue{ {1, 2} };
241 store_.SetByteArrays(kByteArraysKey, kByteArraysValue);
242 const string kIntKey("bar");
243 const int kIntValue = 123;
244 store_.SetInt(kIntKey, kIntValue);
245 const string kInt16Key("int16");
246 const int16_t kInt16Value = 123;
247 store_.SetInt16(kInt16Key, kInt16Value);
248 const string kKeyValueStoreKey("bear");
249 const KeyValueStore kKeyValueStoreValue;
250 store_.SetKeyValueStore(kKeyValueStoreKey, kKeyValueStoreValue);
251 const string kRpcIdentifierKey("rpcid");
252 const string kRpcIdentifierValue("rpc_identifier");
253 store_.SetRpcIdentifier(kRpcIdentifierKey, kRpcIdentifierValue);
254 const string kStringKey("baz");
255 const string kStringValue("string");
256 store_.SetString(kStringKey, kStringValue);
257 const string kStringmapKey("stringMapKey");
258 const map<string, string> kStringmapValue;
259 store_.SetStringmap(kStringmapKey, kStringmapValue);
260 const string kStringsKey("stringsKey");
261 const vector<string> kStringsValue;
262 store_.SetStrings(kStringsKey, kStringsValue);
263 const string kUintKey("bun");
264 const uint32_t kUintValue = 456;
265 store_.SetUint(kUintKey, kUintValue);
266 const string kUint16Key("uint16");
267 const uint16_t kUint16Value = 123;
268 store_.SetUint16(kUint16Key, kUint16Value);
269 const string kUint8sKey("uint8s");
270 const vector<uint8_t> kUint8sValue{ 1, 2, 3 };
271 store_.SetUint8s(kUint8sKey, kUint8sValue);
272 const string kUint32sKey("uint32s");
273 const vector<uint32_t> kUint32sValue{ 1, 2, 3 };
274 store_.SetUint32s(kUint32sKey, kUint32sValue);
275
276 EXPECT_TRUE(store_.ContainsBool(kBoolKey));
277 EXPECT_TRUE(store_.ContainsByteArrays(kByteArraysKey));
278 EXPECT_TRUE(store_.ContainsInt(kIntKey));
279 EXPECT_TRUE(store_.ContainsInt16(kInt16Key));
280 EXPECT_TRUE(store_.ContainsKeyValueStore(kKeyValueStoreKey));
281 EXPECT_TRUE(store_.ContainsRpcIdentifier(kRpcIdentifierKey));
282 EXPECT_TRUE(store_.ContainsString(kStringKey));
283 EXPECT_TRUE(store_.ContainsStringmap(kStringmapKey));
284 EXPECT_TRUE(store_.ContainsStrings(kStringsKey));
285 EXPECT_TRUE(store_.ContainsUint(kUintKey));
286 EXPECT_TRUE(store_.ContainsUint16(kUint16Key));
287 EXPECT_TRUE(store_.ContainsUint8s(kUint8sKey));
288 EXPECT_TRUE(store_.ContainsUint32s(kUint32sKey));
289 EXPECT_FALSE(store_.IsEmpty());
290 store_.Clear();
291 EXPECT_TRUE(store_.IsEmpty());
292 EXPECT_FALSE(store_.ContainsBool(kBoolKey));
293 EXPECT_FALSE(store_.ContainsByteArrays(kByteArraysKey));
294 EXPECT_FALSE(store_.ContainsInt(kIntKey));
295 EXPECT_FALSE(store_.ContainsInt16(kInt16Key));
296 EXPECT_FALSE(store_.ContainsInt(kKeyValueStoreKey));
297 EXPECT_FALSE(store_.ContainsRpcIdentifier(kRpcIdentifierKey));
298 EXPECT_FALSE(store_.ContainsString(kStringKey));
299 EXPECT_FALSE(store_.ContainsStringmap(kStringmapKey));
300 EXPECT_FALSE(store_.ContainsStrings(kStringsKey));
301 EXPECT_FALSE(store_.ContainsUint(kUintKey));
302 EXPECT_FALSE(store_.ContainsUint16(kUint16Key));
303 EXPECT_FALSE(store_.ContainsUint8s(kUint8sKey));
304 EXPECT_FALSE(store_.ContainsUint32s(kUint32sKey));
305 }
306
TEST_F(KeyValueStoreTest,Equals)307 TEST_F(KeyValueStoreTest, Equals) {
308 KeyValueStore first, second;
309
310 first.SetBool("boolKey", true);
311 EXPECT_NE(first, second);
312
313 first.Clear();
314 second.Clear();
315 second.SetBool("boolKey", true);
316 EXPECT_NE(first, second);
317
318 first.Clear();
319 second.Clear();
320 first.SetBool("boolKey", true);
321 second.SetBool("boolOtherKey", true);
322 EXPECT_NE(first, second);
323
324 first.Clear();
325 second.Clear();
326 first.SetBool("boolKey", true);
327 second.SetBool("boolKey", false);
328 EXPECT_NE(first, second);
329
330 const vector<vector<uint8_t>> kByteArrays1{ {1, 2} };
331 const vector<vector<uint8_t>> kByteArrays2{ {3, 4} };
332
333 first.Clear();
334 second.Clear();
335 first.SetByteArrays("byteArraysKey", kByteArrays1);
336 second.SetByteArrays("byteArraysOtherKey", kByteArrays1);
337 EXPECT_NE(first, second);
338
339 first.Clear();
340 second.Clear();
341 first.SetByteArrays("byteArraysKey", kByteArrays1);
342 second.SetByteArrays("byteArraysOtherKey", kByteArrays2);
343 EXPECT_NE(first, second);
344
345 first.Clear();
346 second.Clear();
347 first.SetInt("intKey", 123);
348 second.SetInt("intOtherKey", 123);
349 EXPECT_NE(first, second);
350
351 first.Clear();
352 second.Clear();
353 first.SetInt("intKey", 123);
354 second.SetInt("intKey", 456);
355 EXPECT_NE(first, second);
356
357 first.Clear();
358 second.Clear();
359 first.SetInt16("int16Key", 123);
360 second.SetInt16("int16OtherKey", 123);
361 EXPECT_NE(first, second);
362
363 first.Clear();
364 second.Clear();
365 first.SetInt16("int16Key", 123);
366 second.SetInt16("int16Key", 456);
367 EXPECT_NE(first, second);
368
369 KeyValueStore key_value0;
370 key_value0.SetInt("intKey", 123);
371 KeyValueStore key_value1;
372 key_value1.SetInt("intOtherKey", 123);
373
374 first.Clear();
375 second.Clear();
376 first.SetKeyValueStore("keyValueKey", key_value0);
377 second.SetKeyValueStore("keyValueKey", key_value1);
378 EXPECT_NE(first, second);
379
380 first.Clear();
381 second.Clear();
382 first.SetKeyValueStore("keyValueKey", key_value0);
383 second.SetKeyValueStore("keyValueOtherKey", key_value0);
384 EXPECT_NE(first, second);
385
386 first.Clear();
387 second.Clear();
388 first.SetRpcIdentifier("rpcIdentifierKey", "rpcIdentifier");
389 second.SetRpcIdentifier("rpcIdentifierOtherKey", "rpcIdentifier");
390 EXPECT_NE(first, second);
391
392 first.Clear();
393 second.Clear();
394 first.SetRpcIdentifier("rpcIdentifierKey", "rpcIdentifier");
395 second.SetRpcIdentifier("rpcIdentifierKey", "otherRpcIdentifier");
396 EXPECT_NE(first, second);
397
398 first.Clear();
399 second.Clear();
400 first.SetString("stringKey", "string");
401 second.SetString("stringOtherKey", "string");
402 EXPECT_NE(first, second);
403
404 first.Clear();
405 second.Clear();
406 first.SetString("stringKey", "string");
407 second.SetString("stringKey", "otherString");
408 EXPECT_NE(first, second);
409
410
411 const map<string, string> kStringmap1{ { "key", "value" } };
412 const map<string, string> kStringmap2{ { "otherKey", "value" } };
413 const map<string, string> kStringmap3{ { "key", "otherValue" } };
414
415 first.Clear();
416 second.Clear();
417 first.SetStringmap("stringmapKey", kStringmap1);
418 second.SetStringmap("stringmapOtherKey", kStringmap1);
419 EXPECT_NE(first, second);
420
421 first.Clear();
422 second.Clear();
423 first.SetStringmap("stringmapKey", kStringmap1);
424 second.SetStringmap("stringmapKey", kStringmap2);
425 EXPECT_NE(first, second);
426
427 first.Clear();
428 second.Clear();
429 first.SetStringmap("stringmapKey", kStringmap1);
430 second.SetStringmap("stringmapKey", kStringmap3);
431 EXPECT_NE(first, second);
432
433 const vector<string> kStrings1{ "value" };
434 const vector<string> kStrings2{ "otherValue" };
435
436 first.Clear();
437 second.Clear();
438 first.SetStrings("stringsKey", kStrings1);
439 second.SetStrings("stringsOtherKey", kStrings1);
440 EXPECT_NE(first, second);
441
442 first.Clear();
443 second.Clear();
444 first.SetStrings("stringsKey", kStrings1);
445 second.SetStrings("stringsKey", kStrings2);
446 EXPECT_NE(first, second);
447
448 first.Clear();
449 second.Clear();
450 first.SetUint("uintKey", 1);
451 second.SetUint("uintOtherKey", 1);
452 EXPECT_NE(first, second);
453
454 first.Clear();
455 second.Clear();
456 first.SetUint("uintKey", 1);
457 second.SetUint("uintKey", 2);
458 EXPECT_NE(first, second);
459
460 first.Clear();
461 second.Clear();
462 first.SetUint16("uint16Key", 1);
463 second.SetUint16("uint16OtherKey", 1);
464 EXPECT_NE(first, second);
465
466 first.Clear();
467 second.Clear();
468 first.SetUint16("uint16Key", 1);
469 second.SetUint16("uint16Key", 2);
470 EXPECT_NE(first, second);
471
472 const vector<uint8_t> kUint8s1{ 1 };
473 const vector<uint8_t> kUint8s2{ 2 };
474
475 first.Clear();
476 second.Clear();
477 first.SetUint8s("uint8sKey", kUint8s1);
478 second.SetUint8s("uint8sOtherKey", kUint8s1);
479 EXPECT_NE(first, second);
480
481 first.Clear();
482 second.Clear();
483 first.SetUint8s("uint8sKey", kUint8s1);
484 second.SetUint8s("uint8sKey", kUint8s2);
485 EXPECT_NE(first, second);
486
487 const vector<uint32_t> kUint32s1{ 1 };
488 const vector<uint32_t> kUint32s2{ 2 };
489
490 first.Clear();
491 second.Clear();
492 first.SetUint32s("uint32sKey", kUint32s1);
493 second.SetUint32s("uint32sOtherKey", kUint32s1);
494 EXPECT_NE(first, second);
495
496 first.Clear();
497 second.Clear();
498 first.SetUint32s("uint32sKey", kUint32s1);
499 second.SetUint32s("uint32sKey", kUint32s2);
500 EXPECT_NE(first, second);
501
502 first.Clear();
503 second.Clear();
504 first.SetBool("boolKey", true);
505 first.SetByteArrays("byteArraysKey", kByteArrays1);
506 first.SetInt("intKey", 123);
507 first.SetInt("int16Key", 123);
508 first.SetRpcIdentifier("rpcIdentifierKey", "rpcid");
509 first.SetString("stringKey", "value");
510 first.SetStringmap("stringmapKey", kStringmap1);
511 first.SetStrings("stringsKey", kStrings1);
512 first.SetUint("uintKey", 1);
513 first.SetUint16("uint16Key", 1);
514 first.SetUint8s("uint8sKey", kUint8s1);
515 first.SetUint32s("uint32sKey", kUint32s1);
516 second.SetBool("boolKey", true);
517 second.SetByteArrays("byteArraysKey", kByteArrays1);
518 second.SetInt("intKey", 123);
519 second.SetInt("int16Key", 123);
520 second.SetRpcIdentifier("rpcIdentifierKey", "rpcid");
521 second.SetString("stringKey", "value");
522 second.SetStringmap("stringmapKey", kStringmap1);
523 second.SetStrings("stringsKey", kStrings1);
524 second.SetUint("uintKey", 1);
525 second.SetUint16("uint16Key", 1);
526 second.SetUint8s("uint8sKey", kUint8s1);
527 second.SetUint32s("uint32sKey", kUint32s1);
528 EXPECT_EQ(first, second);
529 }
530
TEST_F(KeyValueStoreTest,CopyFrom)531 TEST_F(KeyValueStoreTest, CopyFrom) {
532 KeyValueStore donor;
533 const string kBoolKey("foo");
534 const bool kBoolValue = true;
535 donor.SetBool(kBoolKey, kBoolValue);
536 const string kByteArraysKey("bytearrays");
537 const vector<vector<uint8_t>> kByteArraysValue{ {1} };
538 donor.SetByteArrays(kByteArraysKey, kByteArraysValue);
539 const string kIntKey("bar");
540 const int kIntValue = 123;
541 donor.SetInt(kIntKey, kIntValue);
542 const string kInt16Key("int16");
543 const int16_t kInt16Value = 123;
544 donor.SetInt16(kInt16Key, kInt16Value);
545 const string kKeyValueStoreKey("bear");
546 KeyValueStore keyValueStoreValue;
547 keyValueStoreValue.SetInt(kIntKey, kIntValue);
548 donor.SetKeyValueStore(kKeyValueStoreKey, keyValueStoreValue);
549 const string kRpcIdentifierKey("rpcidentifier");
550 const string kRpcIdentifierValue("rpcid");
551 donor.SetRpcIdentifier(kRpcIdentifierKey, kRpcIdentifierValue);
552 const string kStringKey("baz");
553 const string kStringValue("string");
554 donor.SetString(kStringKey, kStringValue);
555 const string kStringmapKey("stringMapKey");
556 const map<string, string> kStringmapValue{ { "key", "value" } };
557 donor.SetStringmap(kStringmapKey, kStringmapValue);
558 const string kStringsKey("stringsKey");
559 const vector<string> kStringsValue{ "string0", "string1" };
560 donor.SetStrings(kStringsKey, kStringsValue);
561 const string kUintKey("bun");
562 const uint32_t kUintValue = 456;
563 donor.SetUint(kUintKey, kUintValue);
564 const string kUint16Key("uint16");
565 const uint16_t kUint16Value = 456;
566 donor.SetUint16(kUint16Key, kUint16Value);
567 const string kUint8sKey("uint8s");
568 const vector<uint8_t> kUint8sValue{ 1 };
569 donor.SetUint8s(kUint8sKey, kUint8sValue);
570 const string kUint32sKey("uint32s");
571 const vector<uint32_t> kUint32sValue{ 1 };
572 donor.SetUint32s(kUint32sKey, kUint32sValue);
573
574 EXPECT_TRUE(store_.IsEmpty());
575 store_.CopyFrom(donor);
576 EXPECT_FALSE(store_.IsEmpty());
577 EXPECT_EQ(donor, store_);
578 }
579
TEST_F(KeyValueStoreTest,ConvertToVariantDictionary)580 TEST_F(KeyValueStoreTest, ConvertToVariantDictionary) {
581 static const char kStringKey[] = "StringKey";
582 static const char kStringValue[] = "StringValue";
583 static const char kStringmapKey[] = "StringmapKey";
584 const map<string, string> kStringmapValue = { { "key", "value" } };
585 static const char kStringsKey[] = "StringsKey";
586 const vector<string> kStringsValue = {"StringsValue1", "StringsValue2"};
587 static const char kBoolKey[] = "BoolKey";
588 const bool kBoolValue = true;
589 static const char kInt32Key[] = "Int32Key";
590 const int32_t kInt32Value = 123;
591 static const char kUint32Key[] = "Uint32Key";
592 const uint32_t kUint32Value = 654;
593 static const char kByteArraysKey[] = "ByteArraysKey";
594 const vector<vector<uint8_t>> kByteArraysValue{ {1}, {2} };
595 static const char kInt16Key[] = "Int16Key";
596 const int16_t kInt16Value = 123;
597 static const char kRpcIdentifierKey[] = "RpcIdentifierKey";
598 static const char kRpcIdentifierValue[] = "/org/chromium/test";
599 static const char kUint16Key[] = "Uint16Key";
600 const uint16_t kUint16Value = 123;
601 static const char kUint8sKey[] = "Uint8sKey";
602 const vector<uint8_t> kUint8sValue{ 1, 2 };
603 static const char kUint32sKey[] = "Uint32sKey";
604 const vector<uint32_t> kUint32sValue{ 1, 2 };
605 static const char kKeyValueStoreKey[] = "KeyValueStoreKey";
606 static const char kNestedInt32Key[] = "NestedKey32Key";
607 const int32_t kNestedInt32Value = 1;
608 KeyValueStore nested_store;
609 nested_store.SetInt(kNestedInt32Key, kNestedInt32Value);
610
611 KeyValueStore store;
612 store.SetString(kStringKey, kStringValue);
613 store.SetStringmap(kStringmapKey, kStringmapValue);
614 store.SetStrings(kStringsKey, kStringsValue);
615 store.SetBool(kBoolKey, kBoolValue);
616 store.SetInt(kInt32Key, kInt32Value);
617 store.SetUint(kUint32Key, kUint32Value);
618 store.SetByteArrays(kByteArraysKey, kByteArraysValue);
619 store.SetInt16(kInt16Key, kInt16Value);
620 store.SetRpcIdentifier(kRpcIdentifierKey, kRpcIdentifierValue);
621 store.SetUint16(kUint16Key, kUint16Value);
622 store.SetUint8s(kUint8sKey, kUint8sValue);
623 store.SetUint32s(kUint32sKey, kUint32sValue);
624 store.SetKeyValueStore(kKeyValueStoreKey, nested_store);
625
626 brillo::VariantDictionary dict;
627 KeyValueStore::ConvertToVariantDictionary(store, &dict);
628 EXPECT_EQ(13, dict.size());
629 EXPECT_EQ(kStringValue, dict[kStringKey].Get<string>());
630 map<string, string> stringmap_value =
631 dict[kStringmapKey].Get<map<string, string>>();
632 EXPECT_EQ(kStringmapValue, stringmap_value);
633 EXPECT_EQ(kStringsValue, dict[kStringsKey].Get<vector<string>>());
634 EXPECT_EQ(kBoolValue, dict[kBoolKey].Get<bool>());
635 EXPECT_EQ(kInt32Value, dict[kInt32Key].Get<int32_t>());
636 EXPECT_EQ(kUint32Value, dict[kUint32Key].Get<uint32_t>());
637 EXPECT_EQ(kByteArraysValue,
638 dict[kByteArraysKey].Get<vector<vector<uint8_t>>>());
639 EXPECT_EQ(kInt16Value, dict[kInt16Key].Get<int16_t>());
640 EXPECT_EQ(kRpcIdentifierValue,
641 dict[kRpcIdentifierKey].Get<dbus::ObjectPath>().value());
642 EXPECT_EQ(kUint16Value, dict[kUint16Key].Get<uint16_t>());
643 EXPECT_EQ(kUint8sValue, dict[kUint8sKey].Get<vector<uint8_t>>());
644 EXPECT_EQ(kUint32sValue, dict[kUint32sKey].Get<vector<uint32_t>>());
645 brillo::VariantDictionary nested_dict =
646 dict[kKeyValueStoreKey].Get<brillo::VariantDictionary>();
647 EXPECT_EQ(kNestedInt32Value, nested_dict[kNestedInt32Key].Get<int32_t>());
648 }
649
TEST_F(KeyValueStoreTest,ConvertFromVariantDictionary)650 TEST_F(KeyValueStoreTest, ConvertFromVariantDictionary) {
651 static const char kStringKey[] = "StringKey";
652 static const char kStringValue[] = "StringValue";
653 static const char kStringmapKey[] = "StringmapKey";
654 const map<string, string> kStringmapValue = { { "key", "value" } };
655 static const char kStringsKey[] = "StringsKey";
656 const vector<string> kStringsValue = {"StringsValue1", "StringsValue2"};
657 static const char kBoolKey[] = "BoolKey";
658 const bool kBoolValue = true;
659 static const char kInt32Key[] = "Int32Key";
660 const int32_t kInt32Value = 123;
661 static const char kUint32Key[] = "Uint32Key";
662 const uint32_t kUint32Value = 654;
663 static const char kByteArraysKey[] = "ByteArraysKey";
664 const vector<vector<uint8_t>> kByteArraysValue{ {1}, {2} };
665 static const char kInt16Key[] = "Int16Key";
666 const int16_t kInt16Value = 123;
667 static const char kRpcIdentifierKey[] = "RpcIdentifierKey";
668 static const char kRpcIdentifierValue[] = "/org/chromium/test";
669 static const char kUint16Key[] = "Uint16Key";
670 const uint16_t kUint16Value = 123;
671 static const char kUint8sKey[] = "Uint8sKey";
672 const vector<uint8_t> kUint8sValue{ 1, 2 };
673 static const char kUint32sKey[] = "Uint32sKey";
674 const vector<uint32_t> kUint32sValue{ 1, 2 };
675 static const char kKeyValueStoreKey[] = "KeyValueStoreKey";
676 static const char kNestedInt32Key[] = "NestedKey32Key";
677 const int32_t kNestedInt32Value = 1;
678
679 brillo::VariantDictionary dict;
680 dict[kStringKey] = brillo::Any(string(kStringValue));
681 dict[kStringmapKey] = brillo::Any(kStringmapValue);
682 dict[kStringsKey] = brillo::Any(kStringsValue);
683 dict[kBoolKey] = brillo::Any(kBoolValue);
684 dict[kInt32Key] = brillo::Any(kInt32Value);
685 dict[kUint32Key] = brillo::Any(kUint32Value);
686 dict[kByteArraysKey] = brillo::Any(kByteArraysValue);
687 dict[kInt16Key] = brillo::Any(kInt16Value);
688 dict[kRpcIdentifierKey] =
689 brillo::Any(dbus::ObjectPath(kRpcIdentifierValue));
690 dict[kUint16Key] = brillo::Any(kUint16Value);
691 dict[kUint8sKey] = brillo::Any(kUint8sValue);
692 dict[kUint32sKey] = brillo::Any(kUint32sValue);
693 brillo::VariantDictionary nested_dict;
694 nested_dict[kNestedInt32Key] = brillo::Any(kNestedInt32Value);
695 dict[kKeyValueStoreKey] = brillo::Any(nested_dict);
696
697 KeyValueStore store;
698 KeyValueStore::ConvertFromVariantDictionary(dict, &store);
699 EXPECT_TRUE(store.ContainsString(kStringKey));
700 EXPECT_EQ(kStringValue, store.GetString(kStringKey));
701 EXPECT_TRUE(store.ContainsStringmap(kStringmapKey));
702 EXPECT_EQ(kStringmapValue, store.GetStringmap(kStringmapKey));
703 EXPECT_TRUE(store.ContainsStrings(kStringsKey));
704 EXPECT_EQ(kStringsValue, store.GetStrings(kStringsKey));
705 EXPECT_TRUE(store.ContainsBool(kBoolKey));
706 EXPECT_EQ(kBoolValue, store.GetBool(kBoolKey));
707 EXPECT_TRUE(store.ContainsInt(kInt32Key));
708 EXPECT_EQ(kInt32Value, store.GetInt(kInt32Key));
709 EXPECT_TRUE(store.ContainsUint(kUint32Key));
710 EXPECT_EQ(kUint32Value, store.GetUint(kUint32Key));
711 EXPECT_TRUE(store.ContainsByteArrays(kByteArraysKey));
712 EXPECT_EQ(kByteArraysValue, store.GetByteArrays(kByteArraysKey));
713 EXPECT_TRUE(store.ContainsInt16(kInt16Key));
714 EXPECT_EQ(kInt16Value, store.GetInt16(kInt16Key));
715 EXPECT_TRUE(store.ContainsRpcIdentifier(kRpcIdentifierKey));
716 EXPECT_EQ(kRpcIdentifierValue, store.GetRpcIdentifier(kRpcIdentifierKey));
717 EXPECT_TRUE(store.ContainsUint16(kUint16Key));
718 EXPECT_EQ(kUint16Value, store.GetUint16(kUint16Key));
719 EXPECT_TRUE(store.ContainsUint8s(kUint8sKey));
720 EXPECT_EQ(kUint8sValue, store.GetUint8s(kUint8sKey));
721 EXPECT_TRUE(store.ContainsUint32s(kUint32sKey));
722 EXPECT_EQ(kUint32sValue, store.GetUint32s(kUint32sKey));
723 EXPECT_TRUE(store.ContainsKeyValueStore(kKeyValueStoreKey));
724 KeyValueStore nested_store;
725 nested_store.SetInt(kNestedInt32Key, kNestedInt32Value);
726 EXPECT_EQ(nested_store, store.GetKeyValueStore(kKeyValueStoreKey));
727 }
728
TEST_F(KeyValueStoreTest,ConvertPathsToRpcIdentifiers)729 TEST_F(KeyValueStoreTest, ConvertPathsToRpcIdentifiers) {
730 const string kRpcIdentifier1("/test1");
731 const string kRpcIdentifier2("/test2");
732 vector<dbus::ObjectPath> paths;
733 paths.push_back(dbus::ObjectPath(kRpcIdentifier1));
734 paths.push_back(dbus::ObjectPath(kRpcIdentifier2));
735 vector<string> actual_rpc_identifiers;
736 KeyValueStore::ConvertPathsToRpcIdentifiers(paths, &actual_rpc_identifiers);
737 vector<string> expected_rpc_identifiers;
738 expected_rpc_identifiers.push_back(kRpcIdentifier1);
739 expected_rpc_identifiers.push_back(kRpcIdentifier2);
740 EXPECT_EQ(expected_rpc_identifiers, actual_rpc_identifiers);
741 }
742
743 } // namespace shill
744