This source file includes following definitions.
- task_runner_
- SetUp
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/chromeos/policy/cloud_external_data_store.h"
#include "base/compiler_specific.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/test/test_simple_task_runner.h"
#include "components/policy/core/common/cloud/resource_cache.h"
#include "crypto/sha2.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
namespace {
const char kKey1[] = "Key 1";
const char kKey2[] = "Key 2";
const char kPolicy1[] = "Test policy 1";
const char kPolicy2[] = "Test policy 2";
const char kData1[] = "Testing data 1";
const char kData2[] = "Testing data 2";
const char kURL[] = "http://localhost";
const size_t kMaxSize = 100;
}
class CouldExternalDataStoreTest : public testing::Test {
public:
CouldExternalDataStoreTest();
virtual void SetUp() OVERRIDE;
protected:
const std::string kData1Hash;
const std::string kData2Hash;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
base::ScopedTempDir temp_dir_;
scoped_ptr<ResourceCache> resource_cache_;
DISALLOW_COPY_AND_ASSIGN(CouldExternalDataStoreTest);
};
CouldExternalDataStoreTest::CouldExternalDataStoreTest()
: kData1Hash(crypto::SHA256HashString(kData1)),
kData2Hash(crypto::SHA256HashString(kData2)),
task_runner_(new base::TestSimpleTaskRunner) {
}
void CouldExternalDataStoreTest::SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
resource_cache_.reset(new ResourceCache(temp_dir_.path(), task_runner_));
}
TEST_F(CouldExternalDataStoreTest, StoreAndLoad) {
CloudExternalDataStore store(kKey1, task_runner_, resource_cache_.get());
EXPECT_TRUE(store.Store(kPolicy1, kData1Hash, kData1));
std::string data;
EXPECT_FALSE(store.Load(kPolicy1, kData2Hash, kMaxSize, &data));
EXPECT_TRUE(store.Load(kPolicy1, kData1Hash, kMaxSize, &data));
EXPECT_EQ(kData1, data);
}
TEST_F(CouldExternalDataStoreTest, StoreTooLargeAndLoad) {
CloudExternalDataStore store(kKey1, task_runner_, resource_cache_.get());
EXPECT_TRUE(store.Store(kPolicy1, kData1Hash, kData2));
std::map<std::string, std::string> contents;
resource_cache_->LoadAllSubkeys(kKey1, &contents);
ASSERT_EQ(1u, contents.size());
EXPECT_EQ(kData2, contents.begin()->second);
std::string data;
EXPECT_FALSE(store.Load(kPolicy1, kData1Hash, 1, &data));
resource_cache_->LoadAllSubkeys(kKey1, &contents);
EXPECT_TRUE(contents.empty());
}
TEST_F(CouldExternalDataStoreTest, StoreInvalidAndLoad) {
CloudExternalDataStore store(kKey1, task_runner_, resource_cache_.get());
EXPECT_TRUE(store.Store(kPolicy1, kData1Hash, kData2));
std::map<std::string, std::string> contents;
resource_cache_->LoadAllSubkeys(kKey1, &contents);
ASSERT_EQ(1u, contents.size());
EXPECT_EQ(kData2, contents.begin()->second);
std::string data;
EXPECT_FALSE(store.Load(kPolicy1, kData1Hash, kMaxSize, &data));
resource_cache_->LoadAllSubkeys(kKey1, &contents);
EXPECT_TRUE(contents.empty());
}
TEST_F(CouldExternalDataStoreTest, Prune) {
CloudExternalDataStore store(kKey1, task_runner_, resource_cache_.get());
EXPECT_TRUE(store.Store(kPolicy1, kData1Hash, kData1));
EXPECT_TRUE(store.Store(kPolicy2, kData2Hash, kData2));
std::string data;
EXPECT_TRUE(store.Load(kPolicy1, kData1Hash, kMaxSize, &data));
EXPECT_EQ(kData1, data);
EXPECT_TRUE(store.Load(kPolicy2, kData2Hash, kMaxSize, &data));
EXPECT_EQ(kData2, data);
CloudExternalDataManager::Metadata metadata;
metadata[kPolicy1] =
CloudExternalDataManager::MetadataEntry(kURL, kData1Hash);
store.Prune(metadata);
std::map<std::string, std::string> contents;
resource_cache_->LoadAllSubkeys(kKey1, &contents);
ASSERT_EQ(1u, contents.size());
EXPECT_EQ(kData1, contents.begin()->second);
metadata[kPolicy1] =
CloudExternalDataManager::MetadataEntry(kURL, kData2Hash);
store.Prune(metadata);
resource_cache_->LoadAllSubkeys(kKey1, &contents);
EXPECT_TRUE(contents.empty());
}
TEST_F(CouldExternalDataStoreTest, SharedCache) {
CloudExternalDataStore store1(kKey1, task_runner_, resource_cache_.get());
EXPECT_TRUE(store1.Store(kPolicy1, kData1Hash, kData1));
CloudExternalDataStore store2(kKey2, task_runner_, resource_cache_.get());
EXPECT_TRUE(store2.Store(kPolicy2, kData2Hash, kData2));
std::map<std::string, std::string> contents;
resource_cache_->LoadAllSubkeys(kKey1, &contents);
ASSERT_EQ(1u, contents.size());
EXPECT_EQ(kData1, contents.begin()->second);
resource_cache_->LoadAllSubkeys(kKey2, &contents);
ASSERT_EQ(1u, contents.size());
EXPECT_EQ(kData2, contents.begin()->second);
std::string data;
EXPECT_TRUE(store1.Load(kPolicy1, kData1Hash, kMaxSize, &data));
EXPECT_EQ(kData1, data);
EXPECT_FALSE(store1.Load(kPolicy2, kData2Hash, kMaxSize, &data));
EXPECT_FALSE(store2.Load(kPolicy1, kData1Hash, kMaxSize, &data));
EXPECT_TRUE(store2.Load(kPolicy2, kData2Hash, kMaxSize, &data));
EXPECT_EQ(kData2, data);
CloudExternalDataManager::Metadata metadata;
store1.Prune(metadata);
resource_cache_->LoadAllSubkeys(kKey1, &contents);
EXPECT_TRUE(contents.empty());
resource_cache_->LoadAllSubkeys(kKey2, &contents);
ASSERT_EQ(1u, contents.size());
EXPECT_EQ(kData2, contents.begin()->second);
}
}