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