OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/policy/cloud/resource_cache.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/bind.h" | |
9 #include "base/callback.h" | |
10 #include "base/files/scoped_temp_dir.h" | |
11 #include "base/test/test_simple_task_runner.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace policy { | |
15 | |
16 namespace { | |
17 | |
18 const char kKey1[] = "key 1"; | |
19 const char kKey2[] = "key 2"; | |
20 const char kKey3[] = "key 3"; | |
21 const char kSubA[] = "a"; | |
22 const char kSubB[] = "bb"; | |
23 const char kSubC[] = "ccc"; | |
24 const char kSubD[] = "dddd"; | |
25 const char kSubE[] = "eeeee"; | |
26 | |
27 const char kData0[] = "{ \"key\": \"value\" }"; | |
28 const char kData1[] = "{}"; | |
29 | |
30 bool Matches(const std::string& expected, const std::string& subkey) { | |
31 return subkey == expected; | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 TEST(ResourceCacheTest, StoreAndLoad) { | |
37 base::ScopedTempDir temp_dir; | |
38 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
39 ResourceCache cache(temp_dir.path(), | |
40 make_scoped_refptr(new base::TestSimpleTaskRunner)); | |
41 | |
42 // No data initially. | |
43 std::string data; | |
44 EXPECT_FALSE(cache.Load(kKey1, kSubA, &data)); | |
45 | |
46 // Store some data and load it. | |
47 EXPECT_TRUE(cache.Store(kKey1, kSubA, kData0)); | |
48 EXPECT_TRUE(cache.Load(kKey1, kSubA, &data)); | |
49 EXPECT_EQ(kData0, data); | |
50 | |
51 // Store more data in another subkey. | |
52 EXPECT_TRUE(cache.Store(kKey1, kSubB, kData1)); | |
53 | |
54 // Write subkeys to two other keys. | |
55 EXPECT_TRUE(cache.Store(kKey2, kSubA, kData0)); | |
56 EXPECT_TRUE(cache.Store(kKey2, kSubB, kData1)); | |
57 EXPECT_TRUE(cache.Store(kKey3, kSubA, kData0)); | |
58 EXPECT_TRUE(cache.Store(kKey3, kSubB, kData1)); | |
59 | |
60 // Enumerate all the subkeys. | |
61 std::map<std::string, std::string> contents; | |
62 cache.LoadAllSubkeys(kKey1, &contents); | |
63 EXPECT_EQ(2u, contents.size()); | |
64 EXPECT_EQ(kData0, contents[kSubA]); | |
65 EXPECT_EQ(kData1, contents[kSubB]); | |
66 | |
67 // Store more subkeys. | |
68 EXPECT_TRUE(cache.Store(kKey1, kSubC, kData1)); | |
69 EXPECT_TRUE(cache.Store(kKey1, kSubD, kData1)); | |
70 EXPECT_TRUE(cache.Store(kKey1, kSubE, kData1)); | |
71 | |
72 // Now purge some of them. | |
73 std::set<std::string> keep; | |
74 keep.insert(kSubB); | |
75 keep.insert(kSubD); | |
76 cache.PurgeOtherSubkeys(kKey1, keep); | |
77 | |
78 // Enumerate all the remaining subkeys. | |
79 cache.LoadAllSubkeys(kKey1, &contents); | |
80 EXPECT_EQ(2u, contents.size()); | |
81 EXPECT_EQ(kData1, contents[kSubB]); | |
82 EXPECT_EQ(kData1, contents[kSubD]); | |
83 | |
84 // Delete subkeys directly. | |
85 cache.Delete(kKey1, kSubB); | |
86 cache.Delete(kKey1, kSubD); | |
87 cache.LoadAllSubkeys(kKey1, &contents); | |
88 EXPECT_EQ(0u, contents.size()); | |
89 | |
90 // The other two keys were not affected. | |
91 cache.LoadAllSubkeys(kKey2, &contents); | |
92 EXPECT_EQ(2u, contents.size()); | |
93 EXPECT_EQ(kData0, contents[kSubA]); | |
94 EXPECT_EQ(kData1, contents[kSubB]); | |
95 cache.LoadAllSubkeys(kKey3, &contents); | |
96 EXPECT_EQ(2u, contents.size()); | |
97 EXPECT_EQ(kData0, contents[kSubA]); | |
98 EXPECT_EQ(kData1, contents[kSubB]); | |
99 | |
100 // Now purge all keys except the third. | |
101 keep.clear(); | |
102 keep.insert(kKey3); | |
103 cache.PurgeOtherKeys(keep); | |
104 | |
105 // The first two keys are empty. | |
106 cache.LoadAllSubkeys(kKey1, &contents); | |
107 EXPECT_EQ(0u, contents.size()); | |
108 cache.LoadAllSubkeys(kKey1, &contents); | |
109 EXPECT_EQ(0u, contents.size()); | |
110 | |
111 // The third key is unaffected. | |
112 cache.LoadAllSubkeys(kKey3, &contents); | |
113 EXPECT_EQ(2u, contents.size()); | |
114 EXPECT_EQ(kData0, contents[kSubA]); | |
115 EXPECT_EQ(kData1, contents[kSubB]); | |
116 } | |
117 | |
118 TEST(ResourceCacheTest, FilterSubkeys) { | |
119 base::ScopedTempDir temp_dir; | |
120 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
121 ResourceCache cache(temp_dir.path(), | |
122 make_scoped_refptr(new base::TestSimpleTaskRunner)); | |
123 | |
124 // Store some data. | |
125 EXPECT_TRUE(cache.Store(kKey1, kSubA, kData0)); | |
126 EXPECT_TRUE(cache.Store(kKey1, kSubB, kData1)); | |
127 EXPECT_TRUE(cache.Store(kKey1, kSubC, kData0)); | |
128 EXPECT_TRUE(cache.Store(kKey2, kSubA, kData0)); | |
129 EXPECT_TRUE(cache.Store(kKey2, kSubB, kData1)); | |
130 EXPECT_TRUE(cache.Store(kKey3, kSubA, kData0)); | |
131 EXPECT_TRUE(cache.Store(kKey3, kSubB, kData1)); | |
132 | |
133 // Check the contents of kKey1. | |
134 std::map<std::string, std::string> contents; | |
135 cache.LoadAllSubkeys(kKey1, &contents); | |
136 EXPECT_EQ(3u, contents.size()); | |
137 EXPECT_EQ(kData0, contents[kSubA]); | |
138 EXPECT_EQ(kData1, contents[kSubB]); | |
139 EXPECT_EQ(kData0, contents[kSubC]); | |
140 | |
141 // Filter some subkeys. | |
142 cache.FilterSubkeys(kKey1, base::Bind(&Matches, kSubA)); | |
143 | |
144 // Check the contents of kKey1 again. | |
145 cache.LoadAllSubkeys(kKey1, &contents); | |
146 EXPECT_EQ(2u, contents.size()); | |
147 EXPECT_EQ(kData1, contents[kSubB]); | |
148 EXPECT_EQ(kData0, contents[kSubC]); | |
149 | |
150 // Other keys weren't affected. | |
151 cache.LoadAllSubkeys(kKey2, &contents); | |
152 EXPECT_EQ(2u, contents.size()); | |
153 cache.LoadAllSubkeys(kKey3, &contents); | |
154 EXPECT_EQ(2u, contents.size()); | |
155 } | |
156 | |
157 } // namespace policy | |
OLD | NEW |