1 // Copyright 2014 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 "ios/net/cookies/cookie_creation_time_manager.h"
7 #import <Foundation/Foundation.h>
9 #include "base/ios/ios_util.h"
10 #include "base/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class CookieCreationTimeManagerTest : public testing::Test {
17 NSHTTPCookie* GetCookie(NSString* cookie_line) {
18 NSArray* cookies = [NSHTTPCookie
19 cookiesWithResponseHeaderFields:@{ @"Set-Cookie" : cookie_line }
20 forURL:[NSURL URLWithString:@"http://foo"]];
21 if ([cookies count] != 1)
24 return [cookies objectAtIndex:0];
28 CookieCreationTimeManager creation_time_manager_;
31 TEST_F(CookieCreationTimeManagerTest, SetAndGet) {
32 NSHTTPCookie* cookie = GetCookie(@"A=B");
34 base::Time creation_time = base::Time::Now();
35 creation_time_manager_.SetCreationTime(cookie, creation_time);
36 EXPECT_EQ(creation_time, creation_time_manager_.GetCreationTime(cookie));
39 TEST_F(CookieCreationTimeManagerTest, GetFromSystemCookie) {
40 NSHTTPCookie* cookie = GetCookie(@"A=B");
42 // The creation time of a cookie that was never set through the
43 // CookieCreationTimeManager should be retrieved from the system with 1
45 base::Time time = creation_time_manager_.GetCreationTime(cookie);
46 base::Time now = base::Time::Now();
47 ASSERT_FALSE(time.is_null());
48 int64 delta = (now - time).InMilliseconds();
49 // On iOS 8, the range is (0, 1000) ms, but on earlier iOS versions the range
50 // is (-500, 500) ms. The intervals tested are actually 1200 ms to allow some
52 if (base::ios::IsRunningOnIOS8OrLater()) {
53 EXPECT_GT(delta, -100);
54 EXPECT_LT(delta, 1100);
56 EXPECT_GT(delta, -600);
57 EXPECT_LT(delta, 600);
61 TEST_F(CookieCreationTimeManagerTest, MakeUniqueCreationTime) {
62 base::Time now = base::Time::Now();
64 // |now| is not used yet, so MakeUniqueCreationTime() should return that.
65 base::Time creation_time = creation_time_manager_.MakeUniqueCreationTime(now);
66 EXPECT_EQ(now, creation_time);
68 NSHTTPCookie* cookie1 = GetCookie(@"A=B");
70 creation_time_manager_.SetCreationTime(cookie1, creation_time);
71 // |now| is used by cookie1, MakeUniqueCreationTime() should return the
73 creation_time = creation_time_manager_.MakeUniqueCreationTime(now);
74 EXPECT_EQ(base::Time::FromInternalValue(now.ToInternalValue() + 1),
78 creation_time_manager_.DeleteCreationTime(cookie1);
79 // |now| is available again because |cookie1| was deleted.
80 creation_time = creation_time_manager_.MakeUniqueCreationTime(now);
81 EXPECT_EQ(now, creation_time);
83 creation_time_manager_.SetCreationTime(GetCookie(@"C=D"), creation_time);
84 // Override |C| with a cookie that has a different time, to make |now|
86 creation_time_manager_.SetCreationTime(
87 GetCookie(@"C=E"), now - base::TimeDelta::FromMilliseconds(1));
88 // |now| is available again because |C| was overriden.
89 creation_time = creation_time_manager_.MakeUniqueCreationTime(now);
90 EXPECT_EQ(now, creation_time);
92 creation_time_manager_.SetCreationTime(GetCookie(@"F=G"), creation_time);
93 // Delete all creation times.
94 creation_time_manager_.Clear();
95 // |now| is available again because all creation times were cleared.
96 creation_time = creation_time_manager_.MakeUniqueCreationTime(now);
97 EXPECT_EQ(now, creation_time);
100 TEST_F(CookieCreationTimeManagerTest, MakeUniqueCreationTimeConflicts) {
101 base::Time creation_time = base::Time::Now();
102 int64 time_internal_value = creation_time.ToInternalValue();
103 // Insert two cookies with consecutive times.
104 creation_time_manager_.SetCreationTime(GetCookie(@"A=B"), creation_time);
105 creation_time_manager_.SetCreationTime(
107 base::Time::FromInternalValue(time_internal_value + 1));
109 // MakeUniqueCreationTime() should insert at |time_internal_value + 2|.
111 creation_time_manager_.MakeUniqueCreationTime(creation_time);
112 EXPECT_EQ(time_internal_value + 2, time.ToInternalValue());
113 creation_time_manager_.SetCreationTime(GetCookie(@"E=F"), time);
115 // Leave an available slot at |time_internal_value + 3| and insert another
116 // cookie at |time_internal_value + 4|.
117 creation_time_manager_.SetCreationTime(
119 base::Time::FromInternalValue(time_internal_value + 4));
121 // MakeUniqueCreationTime() should use the available slot.
122 time = creation_time_manager_.MakeUniqueCreationTime(creation_time);
123 EXPECT_EQ(time_internal_value + 3, time.ToInternalValue());