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/extensions/token_cache/token_cache.h" | |
9 #include "chrome/common/chrome_notification_types.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 // Methods from testing::Test. | |
25 virtual void SetUp() OVERRIDE {} | |
26 virtual void TearDown() OVERRIDE { | |
27 cache_.token_cache_.clear(); | |
dcheng
2013/03/06 00:26:35
Do we really need to do this? If not, just remove
Pete Williamson
2013/03/06 00:50:06
Done.
| |
28 } | |
29 | |
30 size_t CacheSize() { | |
31 return cache_.token_cache_.size(); | |
32 } | |
33 | |
34 bool HasMatch(const std::string& token_name) { | |
35 return cache_.FindMatch(token_name) != cache_.token_cache_.end(); | |
36 } | |
37 | |
38 void InsertExpiredToken(const std::string& token_name, | |
39 const std::string& token_value) { | |
40 EXPECT_TRUE(!HasMatch(token_name)); | |
41 | |
42 // Compute a time value for yesterday | |
43 Time now = Time::Now(); | |
44 TimeDelta one_day = one_day.FromDays(1); | |
45 Time yesterday = now - one_day; | |
46 | |
47 TokenCacheData token_data; | |
48 token_data.token = token_value; | |
49 token_data.expiration_time = yesterday; | |
50 | |
51 cache_.token_cache_[token_name] = token_data; | |
52 } | |
53 | |
54 protected: | |
55 TokenCacheService cache_; | |
56 }; | |
57 | |
58 TEST_F(TokenCacheTest, SaveTokenTest) { | |
59 TimeDelta zero; | |
60 cache_.StoreToken("foo", "bar", zero); | |
61 | |
62 EXPECT_EQ(CacheSize(), (size_t) 1); | |
dcheng
2013/03/06 00:26:35
1U.
Pete Williamson
2013/03/06 00:50:06
Done.
| |
63 EXPECT_TRUE(HasMatch("foo")); | |
64 } | |
65 | |
66 TEST_F(TokenCacheTest, RetrieveTokenTest) { | |
67 TimeDelta zero; | |
68 cache_.StoreToken("Mozart", "Eine Kleine Nacht Musik", zero); | |
69 cache_.StoreToken("Bach", "Brandenburg Concerto #3", zero); | |
70 cache_.StoreToken("Beethoven", "Emperor Piano Concerto #5", zero); | |
71 cache_.StoreToken("Handel", "Water Music", zero); | |
72 cache_.StoreToken("Chopin", "Heroic", zero); | |
73 | |
74 std::string found_token = cache_.RetrieveToken("Chopin"); | |
75 EXPECT_EQ("Heroic", found_token); | |
76 } | |
77 | |
78 TEST_F(TokenCacheTest, ReplaceTokenTest) { | |
79 TimeDelta zero; | |
80 cache_.StoreToken("Chopin", "Heroic", zero); | |
81 | |
82 std::string found_token = cache_.RetrieveToken("Chopin"); | |
83 EXPECT_EQ("Heroic", found_token); | |
84 | |
85 cache_.StoreToken("Chopin", "Military", zero); | |
86 | |
87 found_token = cache_.RetrieveToken("Chopin"); | |
88 EXPECT_EQ("Military", found_token); | |
89 EXPECT_EQ(CacheSize(), 1U); | |
90 } | |
91 | |
92 TEST_F(TokenCacheTest, SignoutTest) { | |
93 TimeDelta zero; | |
94 cache_.StoreToken("foo", "bar", zero); | |
95 content::Source<TokenCacheService> stub_source(&cache_); | |
96 content::NotificationDetails stub_details; | |
97 | |
98 EXPECT_EQ(CacheSize(), 1U); | |
99 EXPECT_TRUE(HasMatch("foo")); | |
100 | |
101 cache_.Observe(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | |
102 stub_source, stub_details); | |
103 | |
104 EXPECT_EQ(CacheSize(), 0U); | |
105 EXPECT_FALSE(HasMatch("foo")); | |
106 } | |
107 | |
108 TEST_F(TokenCacheTest, TokenExpireTest) { | |
109 // Use the fact that we are friends to insert an expired token. | |
110 InsertExpiredToken("foo", "bar"); | |
111 | |
112 EXPECT_EQ(CacheSize(), 1U); | |
113 | |
114 // If we attempt to find the token, the attempt should fail. | |
115 EXPECT_FALSE(HasMatch("foo")); | |
116 EXPECT_EQ(CacheSize(), 0U); | |
117 } | |
118 | |
119 } // namespace extensions | |
OLD | NEW |