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"), Optional(StrEq(std::to_string(std::numeric_limits<int>::max()) + "0")));
56 ASSERT_THAT(ConfigCacheHelper(config).GetUint64("A", "B"), Optional(Eq(num)));
57 // zero
58 ConfigCacheHelper(config).SetUint64("A", "B", 0);
59 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("0")));
60 ASSERT_THAT(ConfigCacheHelper(config).GetUint64("A", "B"), Optional(Eq(0)));
61 }
62
TEST(ConfigCacheHelperTest,set_get_uint32_test)63 TEST(ConfigCacheHelperTest, set_get_uint32_test) {
64 ConfigCache config(100, Device::kLinkKeyProperties);
65 // small
66 ConfigCacheHelper(config).SetUint32("A", "B", 123);
67 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("123")));
68 ASSERT_THAT(ConfigCacheHelper(config).GetUint64("A", "B"), Optional(Eq(uint32_t(123))));
69 // big
70 uint64_t num = std::numeric_limits<uint32_t>::max();
71 num *= 10;
72 ConfigCacheHelper(config).SetUint64("A", "B", num);
73 ASSERT_THAT(
74 config.GetProperty("A", "B"), Optional(StrEq(std::to_string(std::numeric_limits<uint32_t>::max()) + "0")));
75 ASSERT_FALSE(ConfigCacheHelper(config).GetUint32("A", "B"));
76 // zero
77 ConfigCacheHelper(config).SetUint32("A", "B", 0);
78 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("0")));
79 ASSERT_THAT(ConfigCacheHelper(config).GetUint32("A", "B"), Optional(Eq(0)));
80 }
81
TEST(ConfigCacheHelperTest,set_get_int64_test)82 TEST(ConfigCacheHelperTest, set_get_int64_test) {
83 ConfigCache config(100, Device::kLinkKeyProperties);
84 // positive
85 int64_t num = std::numeric_limits<int32_t>::max();
86 num *= 10;
87 ConfigCacheHelper(config).SetInt64("A", "B", num);
88 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq(std::to_string(std::numeric_limits<int32_t>::max()) + "0")));
89 ASSERT_THAT(ConfigCacheHelper(config).GetInt64("A", "B"), Optional(Eq(int64_t(num))));
90 // negative
91 ConfigCacheHelper(config).SetInt64("A", "B", -1 * num);
92 ASSERT_THAT(
93 config.GetProperty("A", "B"), Optional(StrEq("-" + std::to_string(std::numeric_limits<int32_t>::max()) + "0")));
94 ASSERT_THAT(ConfigCacheHelper(config).GetInt64("A", "B"), Optional(Eq(-1 * num)));
95 // zero
96 ConfigCacheHelper(config).SetInt("A", "B", 0);
97 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("0")));
98 ASSERT_THAT(ConfigCacheHelper(config).GetInt64("A", "B"), Optional(Eq(int64_t(0))));
99 }
100
TEST(ConfigCacheHelperTest,set_get_int_test)101 TEST(ConfigCacheHelperTest, set_get_int_test) {
102 ConfigCache config(100, Device::kLinkKeyProperties);
103 // positive
104 ConfigCacheHelper(config).SetInt("A", "B", 123);
105 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("123")));
106 ASSERT_THAT(ConfigCacheHelper(config).GetInt("A", "B"), Optional(Eq(int(123))));
107 // negative
108 ConfigCacheHelper(config).SetInt("A", "B", -123);
109 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("-123")));
110 ASSERT_THAT(ConfigCacheHelper(config).GetInt("A", "B"), Optional(Eq(int(-123))));
111 // zero
112 ConfigCacheHelper(config).SetInt("A", "B", 0);
113 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("0")));
114 ASSERT_THAT(ConfigCacheHelper(config).GetInt("A", "B"), Optional(Eq(int(0))));
115 // big
116 int64_t num = std::numeric_limits<int32_t>::max();
117 num *= 10;
118 ConfigCacheHelper(config).SetInt64("A", "B", num);
119 ASSERT_FALSE(ConfigCacheHelper(config).GetInt("A", "B"));
120 }
121
TEST(ConfigCacheHelperTest,set_get_bin_test)122 TEST(ConfigCacheHelperTest, set_get_bin_test) {
123 ConfigCache config(100, Device::kLinkKeyProperties);
124 // empty
125 std::vector<uint8_t> data;
126 ConfigCacheHelper(config).SetBin("A", "B", data);
127 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("")));
128 ASSERT_THAT(ConfigCacheHelper(config).GetBin("A", "B"), Optional(ContainerEq(data)));
129 // non-empty
130 std::vector<uint8_t> data2 = {0xAB, 0x5D, 0x42};
131 ConfigCacheHelper(config).SetBin("A", "B", data2);
132 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("ab5d42")));
133 ASSERT_THAT(ConfigCacheHelper(config).GetBin("A", "B"), Optional(ContainerEq(data2)));
134 }
135
136 } // namespace testing