NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / token_cache / token_cache_service_unittest.cc
blobfe549317180551818cffdb85d262f4178a0d6456
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/chrome_notification_types.h"
9 #include "chrome/browser/extensions/token_cache/token_cache_service.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"
14 using base::Time;
15 using base::TimeDelta;
17 namespace extensions {
19 class TokenCacheTest : public testing::Test {
20 public:
21 TokenCacheTest() : cache_(NULL) {}
22 virtual ~TokenCacheTest() {}
24 size_t CacheSize() {
25 return cache_.token_cache_.size();
28 bool HasMatch(const std::string& token_name) {
29 return cache_.RetrieveToken(token_name) != std::string();
32 void InsertExpiredToken(const std::string& token_name,
33 const std::string& token_value) {
34 EXPECT_TRUE(!HasMatch(token_name));
36 // Compute a time value for yesterday
37 Time now = Time::Now();
38 TimeDelta one_day = one_day.FromDays(1);
39 Time yesterday = now - one_day;
41 TokenCacheService::TokenCacheData token_data;
42 token_data.token = token_value;
43 token_data.expiration_time = yesterday;
45 cache_.token_cache_[token_name] = token_data;
48 protected:
49 TokenCacheService cache_;
52 TEST_F(TokenCacheTest, SaveTokenTest) {
53 TimeDelta zero;
54 cache_.StoreToken("foo", "bar", zero);
56 EXPECT_EQ(1U, CacheSize());
57 EXPECT_TRUE(HasMatch("foo"));
60 TEST_F(TokenCacheTest, RetrieveTokenTest) {
61 TimeDelta zero;
62 cache_.StoreToken("Mozart", "Eine Kleine Nacht Musik", zero);
63 cache_.StoreToken("Bach", "Brandenburg Concerto #3", zero);
64 cache_.StoreToken("Beethoven", "Emperor Piano Concerto #5", zero);
65 cache_.StoreToken("Handel", "Water Music", zero);
66 cache_.StoreToken("Chopin", "Heroic", zero);
68 std::string found_token = cache_.RetrieveToken("Chopin");
69 EXPECT_EQ("Heroic", found_token);
72 TEST_F(TokenCacheTest, ReplaceTokenTest) {
73 TimeDelta zero;
74 cache_.StoreToken("Chopin", "Heroic", zero);
76 std::string found_token = cache_.RetrieveToken("Chopin");
77 EXPECT_EQ("Heroic", found_token);
79 cache_.StoreToken("Chopin", "Military", zero);
81 found_token = cache_.RetrieveToken("Chopin");
82 EXPECT_EQ("Military", found_token);
83 EXPECT_EQ(1U, CacheSize());
86 TEST_F(TokenCacheTest, SignoutTest) {
87 TimeDelta zero;
88 cache_.StoreToken("foo", "bar", zero);
89 content::Source<Profile> stub_source(NULL);
90 content::NotificationDetails stub_details;
92 EXPECT_EQ(1U, CacheSize());
93 EXPECT_TRUE(HasMatch("foo"));
95 cache_.Observe(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT,
96 stub_source, stub_details);
98 EXPECT_EQ(0U, CacheSize());
99 EXPECT_FALSE(HasMatch("foo"));
102 TEST_F(TokenCacheTest, TokenExpireTest) {
103 // Use the fact that we are friends to insert an expired token.
104 InsertExpiredToken("foo", "bar");
106 EXPECT_EQ(1U, CacheSize());
108 // If we attempt to find the token, the attempt should fail.
109 EXPECT_FALSE(HasMatch("foo"));
110 EXPECT_EQ(0U, CacheSize());
113 } // namespace extensions