This source file includes following definitions.
- Matches
- TEST
- TEST
#include "components/policy/core/common/cloud/resource_cache.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/test_simple_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
namespace {
const char kKey1[] = "key 1";
const char kKey2[] = "key 2";
const char kKey3[] = "key 3";
const char kSubA[] = "a";
const char kSubB[] = "bb";
const char kSubC[] = "ccc";
const char kSubD[] = "dddd";
const char kSubE[] = "eeeee";
const char kData0[] = "{ \"key\": \"value\" }";
const char kData1[] = "{}";
bool Matches(const std::string& expected, const std::string& subkey) {
return subkey == expected;
}
}
TEST(ResourceCacheTest, StoreAndLoad) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
ResourceCache cache(temp_dir.path(),
make_scoped_refptr(new base::TestSimpleTaskRunner));
std::string data;
EXPECT_FALSE(cache.Load(kKey1, kSubA, &data));
EXPECT_TRUE(cache.Store(kKey1, kSubA, kData0));
EXPECT_TRUE(cache.Load(kKey1, kSubA, &data));
EXPECT_EQ(kData0, data);
EXPECT_TRUE(cache.Store(kKey1, kSubB, kData1));
EXPECT_TRUE(cache.Store(kKey2, kSubA, kData0));
EXPECT_TRUE(cache.Store(kKey2, kSubB, kData1));
EXPECT_TRUE(cache.Store(kKey3, kSubA, kData0));
EXPECT_TRUE(cache.Store(kKey3, kSubB, kData1));
std::map<std::string, std::string> contents;
cache.LoadAllSubkeys(kKey1, &contents);
EXPECT_EQ(2u, contents.size());
EXPECT_EQ(kData0, contents[kSubA]);
EXPECT_EQ(kData1, contents[kSubB]);
EXPECT_TRUE(cache.Store(kKey1, kSubC, kData1));
EXPECT_TRUE(cache.Store(kKey1, kSubD, kData1));
EXPECT_TRUE(cache.Store(kKey1, kSubE, kData1));
std::set<std::string> keep;
keep.insert(kSubB);
keep.insert(kSubD);
cache.PurgeOtherSubkeys(kKey1, keep);
cache.LoadAllSubkeys(kKey1, &contents);
EXPECT_EQ(2u, contents.size());
EXPECT_EQ(kData1, contents[kSubB]);
EXPECT_EQ(kData1, contents[kSubD]);
cache.Delete(kKey1, kSubB);
cache.Delete(kKey1, kSubD);
cache.LoadAllSubkeys(kKey1, &contents);
EXPECT_EQ(0u, contents.size());
cache.LoadAllSubkeys(kKey2, &contents);
EXPECT_EQ(2u, contents.size());
EXPECT_EQ(kData0, contents[kSubA]);
EXPECT_EQ(kData1, contents[kSubB]);
cache.LoadAllSubkeys(kKey3, &contents);
EXPECT_EQ(2u, contents.size());
EXPECT_EQ(kData0, contents[kSubA]);
EXPECT_EQ(kData1, contents[kSubB]);
keep.clear();
keep.insert(kKey3);
cache.PurgeOtherKeys(keep);
cache.LoadAllSubkeys(kKey1, &contents);
EXPECT_EQ(0u, contents.size());
cache.LoadAllSubkeys(kKey1, &contents);
EXPECT_EQ(0u, contents.size());
cache.LoadAllSubkeys(kKey3, &contents);
EXPECT_EQ(2u, contents.size());
EXPECT_EQ(kData0, contents[kSubA]);
EXPECT_EQ(kData1, contents[kSubB]);
}
TEST(ResourceCacheTest, FilterSubkeys) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
ResourceCache cache(temp_dir.path(),
make_scoped_refptr(new base::TestSimpleTaskRunner));
EXPECT_TRUE(cache.Store(kKey1, kSubA, kData0));
EXPECT_TRUE(cache.Store(kKey1, kSubB, kData1));
EXPECT_TRUE(cache.Store(kKey1, kSubC, kData0));
EXPECT_TRUE(cache.Store(kKey2, kSubA, kData0));
EXPECT_TRUE(cache.Store(kKey2, kSubB, kData1));
EXPECT_TRUE(cache.Store(kKey3, kSubA, kData0));
EXPECT_TRUE(cache.Store(kKey3, kSubB, kData1));
std::map<std::string, std::string> contents;
cache.LoadAllSubkeys(kKey1, &contents);
EXPECT_EQ(3u, contents.size());
EXPECT_EQ(kData0, contents[kSubA]);
EXPECT_EQ(kData1, contents[kSubB]);
EXPECT_EQ(kData0, contents[kSubC]);
cache.FilterSubkeys(kKey1, base::Bind(&Matches, kSubA));
cache.LoadAllSubkeys(kKey1, &contents);
EXPECT_EQ(2u, contents.size());
EXPECT_EQ(kData1, contents[kSubB]);
EXPECT_EQ(kData0, contents[kSubC]);
cache.LoadAllSubkeys(kKey2, &contents);
EXPECT_EQ(2u, contents.size());
cache.LoadAllSubkeys(kKey3, &contents);
EXPECT_EQ(2u, contents.size());
}
}