Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / token_cache / token_cache_service_unittest.cc
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/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"
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_(&profile_) {}
20   ~TokenCacheTest() override { cache_.Shutdown(); }
21
22   size_t CacheSize() {
23     return cache_.token_cache_.size();
24   }
25
26   bool HasMatch(const std::string& token_name) {
27     return cache_.RetrieveToken(token_name) != std::string();
28   }
29
30   void InsertExpiredToken(const std::string& token_name,
31                           const std::string& token_value) {
32     EXPECT_TRUE(!HasMatch(token_name));
33
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;
38
39     TokenCacheService::TokenCacheData token_data;
40     token_data.token = token_value;
41     token_data.expiration_time = yesterday;
42
43     cache_.token_cache_[token_name] = token_data;
44   }
45
46  protected:
47   TestingProfile profile_;
48   TokenCacheService cache_;
49 };
50
51 TEST_F(TokenCacheTest, SaveTokenTest) {
52   TimeDelta zero;
53   cache_.StoreToken("foo", "bar", zero);
54
55   EXPECT_EQ(1U, CacheSize());
56   EXPECT_TRUE(HasMatch("foo"));
57 }
58
59 TEST_F(TokenCacheTest, RetrieveTokenTest) {
60   TimeDelta zero;
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);
66
67   std::string found_token = cache_.RetrieveToken("Chopin");
68   EXPECT_EQ("Heroic", found_token);
69 }
70
71 TEST_F(TokenCacheTest, ReplaceTokenTest) {
72   TimeDelta zero;
73   cache_.StoreToken("Chopin", "Heroic", zero);
74
75   std::string found_token = cache_.RetrieveToken("Chopin");
76   EXPECT_EQ("Heroic", found_token);
77
78   cache_.StoreToken("Chopin", "Military", zero);
79
80   found_token = cache_.RetrieveToken("Chopin");
81   EXPECT_EQ("Military", found_token);
82   EXPECT_EQ(1U, CacheSize());
83 }
84
85 TEST_F(TokenCacheTest, SignoutTest) {
86   TimeDelta zero;
87   cache_.StoreToken("foo", "bar", zero);
88
89   EXPECT_EQ(1U, CacheSize());
90   EXPECT_TRUE(HasMatch("foo"));
91
92   cache_.GoogleSignedOut("foo", "foo");
93
94   EXPECT_EQ(0U, CacheSize());
95   EXPECT_FALSE(HasMatch("foo"));
96 }
97
98 TEST_F(TokenCacheTest, TokenExpireTest) {
99   // Use the fact that we are friends to insert an expired token.
100   InsertExpiredToken("foo", "bar");
101
102   EXPECT_EQ(1U, CacheSize());
103
104   // If we attempt to find the token, the attempt should fail.
105   EXPECT_FALSE(HasMatch("foo"));
106   EXPECT_EQ(0U, CacheSize());
107 }
108
109 } // namespace extensions