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/mutation.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "storage/config_cache.h"
23 #include "storage/device.h"
24
25 namespace testing {
26
27 using bluetooth::storage::ConfigCache;
28 using bluetooth::storage::Device;
29 using bluetooth::storage::Mutation;
30 using bluetooth::storage::MutationEntry;
31
TEST(MutationTest,simple_sequence_test)32 TEST(MutationTest, simple_sequence_test) {
33 ConfigCache config(100, Device::kLinkKeyProperties);
34 ConfigCache memory_only_config(100, {});
35 config.SetProperty("A", "B", "C");
36 config.SetProperty("AA:BB:CC:DD:EE:FF", "B", "C");
37 config.SetProperty("AA:BB:CC:DD:EE:FF", "C", "D");
38 config.SetProperty("CC:DD:EE:FF:00:11", "LinkKey", "AABBAABBCCDDEE");
39 Mutation mutation(&config, &memory_only_config);
40 mutation.Add(MutationEntry::Set(MutationEntry::PropertyType::NORMAL, "AA:BB:CC:DD:EE:FF",
41 "LinkKey", "CCDDEEFFGG"));
42 mutation.Add(MutationEntry::Remove(MutationEntry::PropertyType::NORMAL, "AA:BB:CC:DD:EE:FF",
43 "LinkKey"));
44 mutation.Commit();
45 ASSERT_THAT(config.GetPersistentSections(), ElementsAre("CC:DD:EE:FF:00:11"));
46 Mutation mutation2(&config, &memory_only_config);
47 mutation2.Add(MutationEntry::Set(MutationEntry::PropertyType::NORMAL, "AA:BB:CC:DD:EE:FF",
48 "LinkKey", "CCDDEEFFGG"));
49 mutation2.Commit();
50 ASSERT_THAT(config.GetPersistentSections(),
51 ElementsAre("CC:DD:EE:FF:00:11", "AA:BB:CC:DD:EE:FF"));
52 }
53
TEST(MutationTest,remove_property_and_section)54 TEST(MutationTest, remove_property_and_section) {
55 ConfigCache config(100, Device::kLinkKeyProperties);
56 ConfigCache memory_only_config(100, {});
57 config.SetProperty("A", "B", "C");
58 config.SetProperty("A", "C", "D");
59 config.SetProperty("B", "B", "C");
60 config.SetProperty("B", "C", "D");
61 ASSERT_TRUE(config.HasSection("A"));
62 ASSERT_TRUE(config.HasProperty("A", "B"));
63 ASSERT_TRUE(config.HasProperty("A", "C"));
64 ASSERT_TRUE(config.HasSection("B"));
65 ASSERT_TRUE(config.HasProperty("B", "B"));
66 ASSERT_TRUE(config.HasProperty("B", "C"));
67 {
68 Mutation mutation(&config, &memory_only_config);
69 mutation.Add(MutationEntry::Remove(MutationEntry::PropertyType::NORMAL, "A", "B"));
70 mutation.Commit();
71 }
72 ASSERT_TRUE(config.HasSection("A"));
73 ASSERT_FALSE(config.HasProperty("A", "B"));
74 ASSERT_TRUE(config.HasProperty("A", "C"));
75 ASSERT_TRUE(config.HasSection("B"));
76 ASSERT_TRUE(config.HasProperty("B", "B"));
77 ASSERT_TRUE(config.HasProperty("B", "C"));
78 {
79 Mutation mutation(&config, &memory_only_config);
80 mutation.Add(MutationEntry::Remove(MutationEntry::PropertyType::NORMAL, "B"));
81 mutation.Commit();
82 }
83 ASSERT_TRUE(config.HasSection("A"));
84 ASSERT_FALSE(config.HasProperty("A", "B"));
85 ASSERT_TRUE(config.HasProperty("A", "C"));
86 ASSERT_FALSE(config.HasSection("B"));
87 ASSERT_FALSE(config.HasProperty("B", "B"));
88 ASSERT_FALSE(config.HasProperty("B", "C"));
89 {
90 Mutation mutation(&config, &memory_only_config);
91 mutation.Add(MutationEntry::Remove(MutationEntry::PropertyType::NORMAL, "A", "C"));
92 mutation.Commit();
93 }
94 ASSERT_FALSE(config.HasSection("A"));
95 ASSERT_FALSE(config.HasProperty("A", "B"));
96 ASSERT_FALSE(config.HasProperty("A", "C"));
97 ASSERT_FALSE(config.HasSection("B"));
98 ASSERT_FALSE(config.HasProperty("B", "B"));
99 ASSERT_FALSE(config.HasProperty("B", "C"));
100 }
101
TEST(MutationTest,add_and_remove_cancel_each_other)102 TEST(MutationTest, add_and_remove_cancel_each_other) {
103 ConfigCache config(100, Device::kLinkKeyProperties);
104 ConfigCache memory_only_config(100, {});
105 ASSERT_FALSE(config.HasSection("A"));
106 Mutation mutation(&config, &memory_only_config);
107 mutation.Add(MutationEntry::Set(MutationEntry::PropertyType::NORMAL, "A", "B", "C"));
108 mutation.Add(MutationEntry::Remove(MutationEntry::PropertyType::NORMAL, "A", "B"));
109 mutation.Commit();
110 ASSERT_FALSE(config.HasSection("A"));
111 }
112
TEST(MutationTest,add_to_different_configs)113 TEST(MutationTest, add_to_different_configs) {
114 ConfigCache config(100, Device::kLinkKeyProperties);
115 ConfigCache memory_only_config(100, {});
116 ASSERT_FALSE(config.HasSection("A"));
117 Mutation mutation(&config, &memory_only_config);
118 mutation.Add(MutationEntry::Set(MutationEntry::PropertyType::NORMAL, "A", "B", "C"));
119 mutation.Add(MutationEntry::Set(MutationEntry::PropertyType::MEMORY_ONLY, "A", "D", "Hello"));
120 mutation.Commit();
121 ASSERT_TRUE(config.HasProperty("A", "B"));
122 ASSERT_FALSE(config.HasProperty("A", "D"));
123 ASSERT_THAT(config.GetProperty("A", "B"), Optional(StrEq("C")));
124 ASSERT_FALSE(memory_only_config.HasProperty("A", "B"));
125 ASSERT_TRUE(memory_only_config.HasProperty("A", "D"));
126 ASSERT_THAT(memory_only_config.GetProperty("A", "D"), Optional(StrEq("Hello")));
127 }
128
129 } // namespace testing
130