1 /*
2 * Copyright 2020 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 "storage/config_cache_helper.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include <limits>
23 #include <vector>
24
25 #include "storage/device.h"
26
27 namespace testing {
28
29 using bluetooth::storage::ConfigCache;
30 using bluetooth::storage::ConfigCacheHelper;
31 using bluetooth::storage::Device;
32
TEST(ConfigCacheHelperTest,set_get_bool_test)33 TEST(ConfigCacheHelperTest, set_get_bool_test) {
34 ConfigCache config(100, Device::kLinkKeyProperties);
35 // true
36 ConfigCacheHelper(config).SetBool("A", "B", true);
37 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("true")));
38 ASSERT_THAT(ConfigCacheHelper(config).GetBool("A", "B"), Optional(IsTrue()));
39 // false
40 ConfigCacheHelper(config).SetBool("A", "B", false);
41 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("false")));
42 ASSERT_THAT(ConfigCacheHelper(config).GetBool("A", "B"), Optional(IsFalse()));
43 }
44
TEST(ConfigCacheHelperTest,set_get_uint64_test)45 TEST(ConfigCacheHelperTest, set_get_uint64_test) {
46 ConfigCache config(100, Device::kLinkKeyProperties);
47 // small
48 ConfigCacheHelper(config).SetUint64("A", "B", 123);
49 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("123")));
50 ASSERT_THAT(ConfigCacheHelper(config).GetUint64("A", "B"), Optional(Eq(uint64_t(123))));
51 // big
52 uint64_t num = std::numeric_limits<int>::max();
53 num = num * 10;
54 ConfigCacheHelper(config).SetUint64("A", "B", num);
55 ASSERT_THAT(config.GetProperty("A", "B"),
56 Optional(StrEq(std::to_string(std::numeric_limits<int>::max()) + "0")));
57 ASSERT_THAT(ConfigCacheHelper(config).GetUint64("A", "B"), Optional(Eq(num)));
58 // zero
59 ConfigCacheHelper(config).SetUint64("A", "B", 0);
60 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("0")));
61 ASSERT_THAT(ConfigCacheHelper(config).GetUint64("A", "B"), Optional(Eq(0ul)));
62 }
63
TEST(ConfigCacheHelperTest,set_get_uint32_test)64 TEST(ConfigCacheHelperTest, set_get_uint32_test) {
65 ConfigCache config(100, Device::kLinkKeyProperties);
66 // small
67 ConfigCacheHelper(config).SetUint32("A", "B", 123);
68 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("123")));
69 ASSERT_THAT(ConfigCacheHelper(config).GetUint64("A", "B"), Optional(Eq(uint32_t(123))));
70 // big
71 uint64_t num = std::numeric_limits<uint32_t>::max();
72 num *= 10;
73 ConfigCacheHelper(config).SetUint64("A", "B", num);
74 ASSERT_THAT(config.GetProperty("A", "B"),
75 Optional(StrEq(std::to_string(std::numeric_limits<uint32_t>::max()) + "0")));
76 ASSERT_FALSE(ConfigCacheHelper(config).GetUint32("A", "B"));
77 // zero
78 ConfigCacheHelper(config).SetUint32("A", "B", 0);
79 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("0")));
80 ASSERT_THAT(ConfigCacheHelper(config).GetUint32("A", "B"), Optional(Eq(0u)));
81 }
82
TEST(ConfigCacheHelperTest,set_get_int64_test)83 TEST(ConfigCacheHelperTest, set_get_int64_test) {
84 ConfigCache config(100, Device::kLinkKeyProperties);
85 // positive
86 int64_t num = std::numeric_limits<int32_t>::max();
87 num *= 10;
88 ConfigCacheHelper(config).SetInt64("A", "B", num);
89 ASSERT_THAT(config.GetProperty("A", "B"),
90 Optional(StrEq(std::to_string(std::numeric_limits<int32_t>::max()) + "0")));
91 ASSERT_THAT(ConfigCacheHelper(config).GetInt64("A", "B"), Optional(Eq(int64_t(num))));
92 // negative
93 ConfigCacheHelper(config).SetInt64("A", "B", -1 * num);
94 ASSERT_THAT(config.GetProperty("A", "B"),
95 Optional(StrEq("-" + std::to_string(std::numeric_limits<int32_t>::max()) + "0")));
96 ASSERT_THAT(ConfigCacheHelper(config).GetInt64("A", "B"), Optional(Eq(-1 * num)));
97 // zero
98 ConfigCacheHelper(config).SetInt("A", "B", 0);
99 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("0")));
100 ASSERT_THAT(ConfigCacheHelper(config).GetInt64("A", "B"), Optional(Eq(int64_t(0))));
101 }
102
TEST(ConfigCacheHelperTest,set_get_int_test)103 TEST(ConfigCacheHelperTest, set_get_int_test) {
104 ConfigCache config(100, Device::kLinkKeyProperties);
105 // positive
106 ConfigCacheHelper(config).SetInt("A", "B", 123);
107 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("123")));
108 ASSERT_THAT(ConfigCacheHelper(config).GetInt("A", "B"), Optional(Eq(int(123))));
109 // negative
110 ConfigCacheHelper(config).SetInt("A", "B", -123);
111 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("-123")));
112 ASSERT_THAT(ConfigCacheHelper(config).GetInt("A", "B"), Optional(Eq(int(-123))));
113 // zero
114 ConfigCacheHelper(config).SetInt("A", "B", 0);
115 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("0")));
116 ASSERT_THAT(ConfigCacheHelper(config).GetInt("A", "B"), Optional(Eq(int(0))));
117 // big
118 int64_t num = std::numeric_limits<int32_t>::max();
119 num *= 10;
120 ConfigCacheHelper(config).SetInt64("A", "B", num);
121 ASSERT_FALSE(ConfigCacheHelper(config).GetInt("A", "B"));
122 }
123
TEST(ConfigCacheHelperTest,set_get_bin_test)124 TEST(ConfigCacheHelperTest, set_get_bin_test) {
125 ConfigCache config(100, Device::kLinkKeyProperties);
126 // empty
127 std::vector<uint8_t> data;
128 ConfigCacheHelper(config).SetBin("A", "B", data);
129 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("")));
130 ASSERT_THAT(ConfigCacheHelper(config).GetBin("A", "B"), Optional(ContainerEq(data)));
131 // non-empty
132 std::vector<uint8_t> data2 = {0xAB, 0x5D, 0x42};
133 ConfigCacheHelper(config).SetBin("A", "B", data2);
134 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("ab5d42")));
135 ASSERT_THAT(ConfigCacheHelper(config).GetBin("A", "B"), Optional(ContainerEq(data2)));
136 }
137
138 } // namespace testing
139