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.
5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h"
7 #include "base/time/time.h"
8 #include "chrome/browser/extensions/token_cache/token_cache_service.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "testing/gtest/include/gtest/gtest.h"
13 using base::TimeDelta
;
15 namespace extensions
{
17 class TokenCacheTest
: public testing::Test
{
19 TokenCacheTest() : cache_(&profile_
) {}
20 virtual ~TokenCacheTest() { cache_
.Shutdown(); }
23 return cache_
.token_cache_
.size();
26 bool HasMatch(const std::string
& token_name
) {
27 return cache_
.RetrieveToken(token_name
) != std::string();
30 void InsertExpiredToken(const std::string
& token_name
,
31 const std::string
& token_value
) {
32 EXPECT_TRUE(!HasMatch(token_name
));
34 // Compute a time value for yesterday
35 Time now
= Time::Now();
36 TimeDelta one_day
= one_day
.FromDays(1);
37 Time yesterday
= now
- one_day
;
39 TokenCacheService::TokenCacheData token_data
;
40 token_data
.token
= token_value
;
41 token_data
.expiration_time
= yesterday
;
43 cache_
.token_cache_
[token_name
] = token_data
;
47 TestingProfile profile_
;
48 TokenCacheService cache_
;
51 TEST_F(TokenCacheTest
, SaveTokenTest
) {
53 cache_
.StoreToken("foo", "bar", zero
);
55 EXPECT_EQ(1U, CacheSize());
56 EXPECT_TRUE(HasMatch("foo"));
59 TEST_F(TokenCacheTest
, RetrieveTokenTest
) {
61 cache_
.StoreToken("Mozart", "Eine Kleine Nacht Musik", zero
);
62 cache_
.StoreToken("Bach", "Brandenburg Concerto #3", zero
);
63 cache_
.StoreToken("Beethoven", "Emperor Piano Concerto #5", zero
);
64 cache_
.StoreToken("Handel", "Water Music", zero
);
65 cache_
.StoreToken("Chopin", "Heroic", zero
);
67 std::string found_token
= cache_
.RetrieveToken("Chopin");
68 EXPECT_EQ("Heroic", found_token
);
71 TEST_F(TokenCacheTest
, ReplaceTokenTest
) {
73 cache_
.StoreToken("Chopin", "Heroic", zero
);
75 std::string found_token
= cache_
.RetrieveToken("Chopin");
76 EXPECT_EQ("Heroic", found_token
);
78 cache_
.StoreToken("Chopin", "Military", zero
);
80 found_token
= cache_
.RetrieveToken("Chopin");
81 EXPECT_EQ("Military", found_token
);
82 EXPECT_EQ(1U, CacheSize());
85 TEST_F(TokenCacheTest
, SignoutTest
) {
87 cache_
.StoreToken("foo", "bar", zero
);
89 EXPECT_EQ(1U, CacheSize());
90 EXPECT_TRUE(HasMatch("foo"));
92 cache_
.GoogleSignedOut("foo");
94 EXPECT_EQ(0U, CacheSize());
95 EXPECT_FALSE(HasMatch("foo"));
98 TEST_F(TokenCacheTest
, TokenExpireTest
) {
99 // Use the fact that we are friends to insert an expired token.
100 InsertExpiredToken("foo", "bar");
102 EXPECT_EQ(1U, CacheSize());
104 // If we attempt to find the token, the attempt should fail.
105 EXPECT_FALSE(HasMatch("foo"));
106 EXPECT_EQ(0U, CacheSize());
109 } // namespace extensions