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 "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
14 using base::TimeDelta
;
16 namespace extensions
{
18 class TokenCacheTest
: public testing::Test
{
20 TokenCacheTest() : cache_(&profile_
) {}
21 ~TokenCacheTest() override
{ cache_
.Shutdown(); }
24 return cache_
.token_cache_
.size();
27 bool HasMatch(const std::string
& token_name
) {
28 return cache_
.RetrieveToken(token_name
) != std::string();
31 void InsertExpiredToken(const std::string
& token_name
,
32 const std::string
& token_value
) {
33 EXPECT_TRUE(!HasMatch(token_name
));
35 // Compute a time value for yesterday
36 Time now
= Time::Now();
37 TimeDelta one_day
= one_day
.FromDays(1);
38 Time yesterday
= now
- one_day
;
40 TokenCacheService::TokenCacheData token_data
;
41 token_data
.token
= token_value
;
42 token_data
.expiration_time
= yesterday
;
44 cache_
.token_cache_
[token_name
] = token_data
;
48 content::TestBrowserThreadBundle thread_bundle_
;
49 TestingProfile profile_
;
50 TokenCacheService cache_
;
53 TEST_F(TokenCacheTest
, SaveTokenTest
) {
55 cache_
.StoreToken("foo", "bar", zero
);
57 EXPECT_EQ(1U, CacheSize());
58 EXPECT_TRUE(HasMatch("foo"));
61 TEST_F(TokenCacheTest
, RetrieveTokenTest
) {
63 cache_
.StoreToken("Mozart", "Eine Kleine Nacht Musik", zero
);
64 cache_
.StoreToken("Bach", "Brandenburg Concerto #3", zero
);
65 cache_
.StoreToken("Beethoven", "Emperor Piano Concerto #5", zero
);
66 cache_
.StoreToken("Handel", "Water Music", zero
);
67 cache_
.StoreToken("Chopin", "Heroic", zero
);
69 std::string found_token
= cache_
.RetrieveToken("Chopin");
70 EXPECT_EQ("Heroic", found_token
);
73 TEST_F(TokenCacheTest
, ReplaceTokenTest
) {
75 cache_
.StoreToken("Chopin", "Heroic", zero
);
77 std::string found_token
= cache_
.RetrieveToken("Chopin");
78 EXPECT_EQ("Heroic", found_token
);
80 cache_
.StoreToken("Chopin", "Military", zero
);
82 found_token
= cache_
.RetrieveToken("Chopin");
83 EXPECT_EQ("Military", found_token
);
84 EXPECT_EQ(1U, CacheSize());
87 TEST_F(TokenCacheTest
, SignoutTest
) {
89 cache_
.StoreToken("foo", "bar", zero
);
91 EXPECT_EQ(1U, CacheSize());
92 EXPECT_TRUE(HasMatch("foo"));
94 cache_
.GoogleSignedOut("foo", "foo");
96 EXPECT_EQ(0U, CacheSize());
97 EXPECT_FALSE(HasMatch("foo"));
100 TEST_F(TokenCacheTest
, TokenExpireTest
) {
101 // Use the fact that we are friends to insert an expired token.
102 InsertExpiredToken("foo", "bar");
104 EXPECT_EQ(1U, CacheSize());
106 // If we attempt to find the token, the attempt should fail.
107 EXPECT_FALSE(HasMatch("foo"));
108 EXPECT_EQ(0U, CacheSize());
111 } // namespace extensions