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.
5 #include "components/policy/core/common/cloud/resource_cache.h"
7 #include "base/basictypes.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"
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";
27 const char kData0
[] = "{ \"key\": \"value\" }";
28 const char kData1
[] = "{}";
30 bool Matches(const std::string
& expected
, const std::string
& subkey
) {
31 return subkey
== expected
;
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
));
44 EXPECT_FALSE(cache
.Load(kKey1
, kSubA
, &data
));
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
);
51 // Store more data in another subkey.
52 EXPECT_TRUE(cache
.Store(kKey1
, kSubB
, kData1
));
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
));
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
]);
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
));
72 // Now purge some of them.
73 std::set
<std::string
> keep
;
76 cache
.PurgeOtherSubkeys(kKey1
, keep
);
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
]);
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());
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
]);
100 // Now purge all keys except the third.
103 cache
.PurgeOtherKeys(keep
);
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());
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
]);
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
));
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
));
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
]);
141 // Filter some subkeys.
142 cache
.FilterSubkeys(kKey1
, base::Bind(&Matches
, kSubA
));
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
]);
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());
157 } // namespace policy