OLD | NEW |
(Empty) | |
| 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.h" |
| 8 #include "chrome/browser/token_cache/token_cache.h" |
| 9 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using base::Time; |
| 13 using base::TimeDelta; |
| 14 |
| 15 class TokenCacheTest : public testing::Test { |
| 16 public: |
| 17 TokenCacheTest() {} |
| 18 virtual ~TokenCacheTest() {} |
| 19 |
| 20 // Methods from testing::Test. |
| 21 virtual void SetUp() OVERRIDE {} |
| 22 virtual void TearDown() OVERRIDE { |
| 23 cache_.token_cache_.clear(); |
| 24 } |
| 25 |
| 26 size_t CacheSize() { |
| 27 return cache_.token_cache_.size(); |
| 28 } |
| 29 |
| 30 bool HasMatch(std::string token_name) { |
| 31 return cache_.FindMatch(token_name) != cache_.token_cache_.end(); |
| 32 } |
| 33 |
| 34 void InsertExpiredToken(std::string token_name, std::string token_value) { |
| 35 |
| 36 EXPECT_TRUE(!HasMatch(token_name)); |
| 37 |
| 38 // Compute a time value for yesterday |
| 39 Time now = Time::Now(); |
| 40 TimeDelta one_day = one_day.FromDays(1); |
| 41 Time yesterday = now - one_day; |
| 42 |
| 43 TokenCacheData token_data; |
| 44 token_data.token_name = token_name; |
| 45 token_data.token = token_value; |
| 46 token_data.expiration_time = yesterday; |
| 47 |
| 48 cache_.token_cache_.push_back(token_data); |
| 49 } |
| 50 |
| 51 protected: |
| 52 TokenCacheService cache_; |
| 53 |
| 54 }; |
| 55 |
| 56 TEST_F(TokenCacheTest, SaveTokenTest) { |
| 57 |
| 58 cache_.StoreToken("foo", "bar", 0); |
| 59 |
| 60 EXPECT_EQ(CacheSize(), (size_t) 1); |
| 61 EXPECT_TRUE(HasMatch("foo")); |
| 62 } |
| 63 |
| 64 TEST_F(TokenCacheTest, RetrieveTokenTest) { |
| 65 // TODO: does it make the test nicer to make these std::string and not rely on |
| 66 // autoconverting? |
| 67 cache_.StoreToken("Mozart", "Eine Kleine Nacht Musik", 0); |
| 68 cache_.StoreToken("Bach", "Brandenburg Concerto #3", 0); |
| 69 cache_.StoreToken("Beethoven", "Emperor Piano Concerto #5", 0); |
| 70 cache_.StoreToken("Handel", "Water Music", 0); |
| 71 cache_.StoreToken("Chopin", "Heroic", 0); |
| 72 |
| 73 std::string found_token = cache_.RetrieveToken("Chopin"); |
| 74 EXPECT_EQ("Heroic", found_token); |
| 75 } |
| 76 |
| 77 TEST_F(TokenCacheTest, ReplaceTokenTest) { |
| 78 cache_.StoreToken("Chopin", "Heroic", 0); |
| 79 |
| 80 std::string found_token = cache_.RetrieveToken("Chopin"); |
| 81 EXPECT_EQ("Heroic", found_token); |
| 82 |
| 83 cache_.StoreToken("Chopin", "Military", 0); |
| 84 |
| 85 found_token = cache_.RetrieveToken("Chopin"); |
| 86 EXPECT_EQ("Military", found_token); |
| 87 EXPECT_EQ(CacheSize(), (size_t) 1); |
| 88 } |
| 89 |
| 90 TEST_F(TokenCacheTest, SignoutTest) { |
| 91 cache_.StoreToken("foo", "bar", 0); |
| 92 content::Source<TokenCacheService> stub_source(&cache_); |
| 93 content::NotificationDetails stub_details; |
| 94 |
| 95 EXPECT_EQ(CacheSize(), (size_t) 1); |
| 96 EXPECT_TRUE(HasMatch("foo")); |
| 97 |
| 98 cache_.Observe(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
| 99 stub_source, stub_details); |
| 100 |
| 101 EXPECT_EQ(CacheSize(), (size_t) 0); |
| 102 EXPECT_FALSE(HasMatch("foo")); |
| 103 } |
| 104 |
| 105 TEST_F(TokenCacheTest, ShutdownTest) { |
| 106 cache_.StoreToken("foo", "bar", 0); |
| 107 |
| 108 EXPECT_EQ(CacheSize(), (size_t) 1); |
| 109 EXPECT_TRUE(HasMatch("foo")); |
| 110 |
| 111 cache_.Shutdown(); |
| 112 |
| 113 EXPECT_EQ(CacheSize(), (size_t) 0); |
| 114 EXPECT_FALSE(HasMatch("foo")); |
| 115 } |
| 116 |
| 117 TEST_F(TokenCacheTest, ExpiredToken) { |
| 118 |
| 119 cache_.StoreToken("foo", "bar", -500); |
| 120 |
| 121 // Since the token is expired, we expect the insert to fail. |
| 122 EXPECT_EQ(CacheSize(), (size_t) 0); |
| 123 EXPECT_FALSE(HasMatch("foo")); |
| 124 } |
| 125 |
| 126 TEST_F(TokenCacheTest, TokenExpireTest) { |
| 127 // Use the fact that we are friends to insert an expired token. |
| 128 InsertExpiredToken("foo", "bar"); |
| 129 |
| 130 EXPECT_EQ(CacheSize(), (size_t) 1); |
| 131 |
| 132 // If we attempt to find the token, the attempt should fail. |
| 133 EXPECT_FALSE(HasMatch("foo")); |
| 134 EXPECT_EQ(CacheSize(), (size_t) 0); |
| 135 |
| 136 } |
OLD | NEW |