Home | History | Annotate | Download | only in token_cache
      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 "base/basictypes.h"
      6 #include "base/compiler_specific.h"
      7 #include "base/time/time.h"
      8 #include "chrome/browser/chrome_notification_types.h"
      9 #include "chrome/browser/extensions/token_cache/token_cache_service.h"
     10 #include "content/public/browser/notification_details.h"
     11 #include "content/public/browser/notification_source.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 using base::Time;
     15 using base::TimeDelta;
     16 
     17 namespace extensions {
     18 
     19 class TokenCacheTest : public testing::Test {
     20  public:
     21   TokenCacheTest() : cache_(NULL) {}
     22   virtual ~TokenCacheTest() {}
     23 
     24   size_t CacheSize() {
     25     return cache_.token_cache_.size();
     26   }
     27 
     28   bool HasMatch(const std::string& token_name) {
     29     return cache_.RetrieveToken(token_name) != std::string();
     30   }
     31 
     32   void InsertExpiredToken(const std::string& token_name,
     33                           const std::string& token_value) {
     34     EXPECT_TRUE(!HasMatch(token_name));
     35 
     36     // Compute a time value for yesterday
     37     Time now = Time::Now();
     38     TimeDelta one_day = one_day.FromDays(1);
     39     Time yesterday = now - one_day;
     40 
     41     TokenCacheService::TokenCacheData token_data;
     42     token_data.token = token_value;
     43     token_data.expiration_time = yesterday;
     44 
     45     cache_.token_cache_[token_name] = token_data;
     46   }
     47 
     48  protected:
     49   TokenCacheService cache_;
     50 };
     51 
     52 TEST_F(TokenCacheTest, SaveTokenTest) {
     53   TimeDelta zero;
     54   cache_.StoreToken("foo", "bar", zero);
     55 
     56   EXPECT_EQ(1U, CacheSize());
     57   EXPECT_TRUE(HasMatch("foo"));
     58 }
     59 
     60 TEST_F(TokenCacheTest, RetrieveTokenTest) {
     61   TimeDelta zero;
     62   cache_.StoreToken("Mozart", "Eine Kleine Nacht Musik", zero);
     63   cache_.StoreToken("Bach", "Brandenburg Concerto #3", zero);
     64   cache_.StoreToken("Beethoven", "Emperor Piano Concerto #5", zero);
     65   cache_.StoreToken("Handel", "Water Music", zero);
     66   cache_.StoreToken("Chopin", "Heroic", zero);
     67 
     68   std::string found_token = cache_.RetrieveToken("Chopin");
     69   EXPECT_EQ("Heroic", found_token);
     70 }
     71 
     72 TEST_F(TokenCacheTest, ReplaceTokenTest) {
     73   TimeDelta zero;
     74   cache_.StoreToken("Chopin", "Heroic", zero);
     75 
     76   std::string found_token = cache_.RetrieveToken("Chopin");
     77   EXPECT_EQ("Heroic", found_token);
     78 
     79   cache_.StoreToken("Chopin", "Military", zero);
     80 
     81   found_token = cache_.RetrieveToken("Chopin");
     82   EXPECT_EQ("Military", found_token);
     83   EXPECT_EQ(1U, CacheSize());
     84 }
     85 
     86 TEST_F(TokenCacheTest, SignoutTest) {
     87   TimeDelta zero;
     88   cache_.StoreToken("foo", "bar", zero);
     89   content::Source<Profile> stub_source(NULL);
     90   content::NotificationDetails stub_details;
     91 
     92   EXPECT_EQ(1U, CacheSize());
     93   EXPECT_TRUE(HasMatch("foo"));
     94 
     95   cache_.Observe(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT,
     96                  stub_source, stub_details);
     97 
     98   EXPECT_EQ(0U, CacheSize());
     99   EXPECT_FALSE(HasMatch("foo"));
    100 }
    101 
    102 TEST_F(TokenCacheTest, TokenExpireTest) {
    103   // Use the fact that we are friends to insert an expired token.
    104   InsertExpiredToken("foo", "bar");
    105 
    106   EXPECT_EQ(1U, CacheSize());
    107 
    108   // If we attempt to find the token, the attempt should fail.
    109   EXPECT_FALSE(HasMatch("foo"));
    110   EXPECT_EQ(0U, CacheSize());
    111 }
    112 
    113 } // namespace extensions
    114