OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/time/time.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" | 8 #include "chrome/browser/extensions/token_cache/token_cache_service.h" |
10 #include "content/public/browser/notification_details.h" | 9 #include "chrome/test/base/testing_profile.h" |
11 #include "content/public/browser/notification_source.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
13 | 11 |
14 using base::Time; | 12 using base::Time; |
15 using base::TimeDelta; | 13 using base::TimeDelta; |
16 | 14 |
17 namespace extensions { | 15 namespace extensions { |
18 | 16 |
19 class TokenCacheTest : public testing::Test { | 17 class TokenCacheTest : public testing::Test { |
20 public: | 18 public: |
21 TokenCacheTest() : cache_(NULL) {} | 19 TokenCacheTest() : cache_(&profile_) {} |
22 virtual ~TokenCacheTest() {} | 20 virtual ~TokenCacheTest() { cache_.Shutdown(); } |
23 | 21 |
24 size_t CacheSize() { | 22 size_t CacheSize() { |
25 return cache_.token_cache_.size(); | 23 return cache_.token_cache_.size(); |
26 } | 24 } |
27 | 25 |
28 bool HasMatch(const std::string& token_name) { | 26 bool HasMatch(const std::string& token_name) { |
29 return cache_.RetrieveToken(token_name) != std::string(); | 27 return cache_.RetrieveToken(token_name) != std::string(); |
30 } | 28 } |
31 | 29 |
32 void InsertExpiredToken(const std::string& token_name, | 30 void InsertExpiredToken(const std::string& token_name, |
33 const std::string& token_value) { | 31 const std::string& token_value) { |
34 EXPECT_TRUE(!HasMatch(token_name)); | 32 EXPECT_TRUE(!HasMatch(token_name)); |
35 | 33 |
36 // Compute a time value for yesterday | 34 // Compute a time value for yesterday |
37 Time now = Time::Now(); | 35 Time now = Time::Now(); |
38 TimeDelta one_day = one_day.FromDays(1); | 36 TimeDelta one_day = one_day.FromDays(1); |
39 Time yesterday = now - one_day; | 37 Time yesterday = now - one_day; |
40 | 38 |
41 TokenCacheService::TokenCacheData token_data; | 39 TokenCacheService::TokenCacheData token_data; |
42 token_data.token = token_value; | 40 token_data.token = token_value; |
43 token_data.expiration_time = yesterday; | 41 token_data.expiration_time = yesterday; |
44 | 42 |
45 cache_.token_cache_[token_name] = token_data; | 43 cache_.token_cache_[token_name] = token_data; |
46 } | 44 } |
47 | 45 |
48 protected: | 46 protected: |
| 47 TestingProfile profile_; |
49 TokenCacheService cache_; | 48 TokenCacheService cache_; |
50 }; | 49 }; |
51 | 50 |
52 TEST_F(TokenCacheTest, SaveTokenTest) { | 51 TEST_F(TokenCacheTest, SaveTokenTest) { |
53 TimeDelta zero; | 52 TimeDelta zero; |
54 cache_.StoreToken("foo", "bar", zero); | 53 cache_.StoreToken("foo", "bar", zero); |
55 | 54 |
56 EXPECT_EQ(1U, CacheSize()); | 55 EXPECT_EQ(1U, CacheSize()); |
57 EXPECT_TRUE(HasMatch("foo")); | 56 EXPECT_TRUE(HasMatch("foo")); |
58 } | 57 } |
(...skipping 20 matching lines...) Expand all Loading... |
79 cache_.StoreToken("Chopin", "Military", zero); | 78 cache_.StoreToken("Chopin", "Military", zero); |
80 | 79 |
81 found_token = cache_.RetrieveToken("Chopin"); | 80 found_token = cache_.RetrieveToken("Chopin"); |
82 EXPECT_EQ("Military", found_token); | 81 EXPECT_EQ("Military", found_token); |
83 EXPECT_EQ(1U, CacheSize()); | 82 EXPECT_EQ(1U, CacheSize()); |
84 } | 83 } |
85 | 84 |
86 TEST_F(TokenCacheTest, SignoutTest) { | 85 TEST_F(TokenCacheTest, SignoutTest) { |
87 TimeDelta zero; | 86 TimeDelta zero; |
88 cache_.StoreToken("foo", "bar", zero); | 87 cache_.StoreToken("foo", "bar", zero); |
89 content::Source<Profile> stub_source(NULL); | |
90 content::NotificationDetails stub_details; | |
91 | 88 |
92 EXPECT_EQ(1U, CacheSize()); | 89 EXPECT_EQ(1U, CacheSize()); |
93 EXPECT_TRUE(HasMatch("foo")); | 90 EXPECT_TRUE(HasMatch("foo")); |
94 | 91 |
95 cache_.Observe(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | 92 cache_.GoogleSignedOut("foo"); |
96 stub_source, stub_details); | |
97 | 93 |
98 EXPECT_EQ(0U, CacheSize()); | 94 EXPECT_EQ(0U, CacheSize()); |
99 EXPECT_FALSE(HasMatch("foo")); | 95 EXPECT_FALSE(HasMatch("foo")); |
100 } | 96 } |
101 | 97 |
102 TEST_F(TokenCacheTest, TokenExpireTest) { | 98 TEST_F(TokenCacheTest, TokenExpireTest) { |
103 // Use the fact that we are friends to insert an expired token. | 99 // Use the fact that we are friends to insert an expired token. |
104 InsertExpiredToken("foo", "bar"); | 100 InsertExpiredToken("foo", "bar"); |
105 | 101 |
106 EXPECT_EQ(1U, CacheSize()); | 102 EXPECT_EQ(1U, CacheSize()); |
107 | 103 |
108 // If we attempt to find the token, the attempt should fail. | 104 // If we attempt to find the token, the attempt should fail. |
109 EXPECT_FALSE(HasMatch("foo")); | 105 EXPECT_FALSE(HasMatch("foo")); |
110 EXPECT_EQ(0U, CacheSize()); | 106 EXPECT_EQ(0U, CacheSize()); |
111 } | 107 } |
112 | 108 |
113 } // namespace extensions | 109 } // namespace extensions |
OLD | NEW |