Home | History | Annotate | Download | only in cloud
      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/files/scoped_temp_dir.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace policy {
     12 
     13 namespace {
     14 
     15 const char kKey1[] = "key 1";
     16 const char kKey2[] = "key 2";
     17 const char kKey3[] = "key 3";
     18 const char kSubA[] = "a";
     19 const char kSubB[] = "bb";
     20 const char kSubC[] = "ccc";
     21 const char kSubD[] = "dddd";
     22 const char kSubE[] = "eeeee";
     23 
     24 const char kData0[] = "{ \"key\": \"value\" }";
     25 const char kData1[] = "{}";
     26 
     27 }  // namespace
     28 
     29 TEST(ResourceCacheTest, StoreAndLoad) {
     30   base::ScopedTempDir temp_dir;
     31   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
     32   ResourceCache cache(temp_dir.path());
     33 
     34   // No data initially.
     35   std::string data;
     36   EXPECT_FALSE(cache.Load(kKey1, kSubA, &data));
     37 
     38   // Store some data and load it.
     39   EXPECT_TRUE(cache.Store(kKey1, kSubA, kData0));
     40   EXPECT_TRUE(cache.Load(kKey1, kSubA, &data));
     41   EXPECT_EQ(kData0, data);
     42 
     43   // Store more data in another subkey.
     44   EXPECT_TRUE(cache.Store(kKey1, kSubB, kData1));
     45 
     46   // Write subkeys to two other keys.
     47   EXPECT_TRUE(cache.Store(kKey2, kSubA, kData0));
     48   EXPECT_TRUE(cache.Store(kKey2, kSubB, kData1));
     49   EXPECT_TRUE(cache.Store(kKey3, kSubA, kData0));
     50   EXPECT_TRUE(cache.Store(kKey3, kSubB, kData1));
     51 
     52   // Enumerate all the subkeys.
     53   std::map<std::string, std::string> contents;
     54   cache.LoadAllSubkeys(kKey1, &contents);
     55   EXPECT_EQ(2u, contents.size());
     56   EXPECT_EQ(kData0, contents[kSubA]);
     57   EXPECT_EQ(kData1, contents[kSubB]);
     58 
     59   // Store more subkeys.
     60   EXPECT_TRUE(cache.Store(kKey1, kSubC, kData1));
     61   EXPECT_TRUE(cache.Store(kKey1, kSubD, kData1));
     62   EXPECT_TRUE(cache.Store(kKey1, kSubE, kData1));
     63 
     64   // Now purge some of them.
     65   std::set<std::string> keep;
     66   keep.insert(kSubB);
     67   keep.insert(kSubD);
     68   cache.PurgeOtherSubkeys(kKey1, keep);
     69 
     70   // Enumerate all the remaining subkeys.
     71   cache.LoadAllSubkeys(kKey1, &contents);
     72   EXPECT_EQ(2u, contents.size());
     73   EXPECT_EQ(kData1, contents[kSubB]);
     74   EXPECT_EQ(kData1, contents[kSubD]);
     75 
     76   // Delete subkeys directly.
     77   cache.Delete(kKey1, kSubB);
     78   cache.Delete(kKey1, kSubD);
     79   cache.LoadAllSubkeys(kKey1, &contents);
     80   EXPECT_EQ(0u, contents.size());
     81 
     82   // The other two keys were not affected.
     83   cache.LoadAllSubkeys(kKey2, &contents);
     84   EXPECT_EQ(2u, contents.size());
     85   EXPECT_EQ(kData0, contents[kSubA]);
     86   EXPECT_EQ(kData1, contents[kSubB]);
     87   cache.LoadAllSubkeys(kKey3, &contents);
     88   EXPECT_EQ(2u, contents.size());
     89   EXPECT_EQ(kData0, contents[kSubA]);
     90   EXPECT_EQ(kData1, contents[kSubB]);
     91 
     92   // Now purge all keys except the third.
     93   keep.clear();
     94   keep.insert(kKey3);
     95   cache.PurgeOtherKeys(keep);
     96 
     97   // The first two keys are empty.
     98   cache.LoadAllSubkeys(kKey1, &contents);
     99   EXPECT_EQ(0u, contents.size());
    100   cache.LoadAllSubkeys(kKey1, &contents);
    101   EXPECT_EQ(0u, contents.size());
    102 
    103   // The third key is unaffected.
    104   cache.LoadAllSubkeys(kKey3, &contents);
    105   EXPECT_EQ(2u, contents.size());
    106   EXPECT_EQ(kData0, contents[kSubA]);
    107   EXPECT_EQ(kData1, contents[kSubB]);
    108 }
    109 
    110 }  // namespace policy
    111