| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ios/net/cookies/cookie_creation_time_manager.h" | 5 #include "ios/net/cookies/cookie_creation_time_manager.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 #include <stdint.h> |
| 8 | 9 |
| 9 #include "base/ios/ios_util.h" | 10 #include "base/ios/ios_util.h" |
| 10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace net { | 14 namespace net { |
| 14 | 15 |
| 15 class CookieCreationTimeManagerTest : public testing::Test { | 16 class CookieCreationTimeManagerTest : public testing::Test { |
| 16 public: | 17 public: |
| 17 NSHTTPCookie* GetCookie(NSString* cookie_line) { | 18 NSHTTPCookie* GetCookie(NSString* cookie_line) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 38 | 39 |
| 39 TEST_F(CookieCreationTimeManagerTest, GetFromSystemCookie) { | 40 TEST_F(CookieCreationTimeManagerTest, GetFromSystemCookie) { |
| 40 NSHTTPCookie* cookie = GetCookie(@"A=B"); | 41 NSHTTPCookie* cookie = GetCookie(@"A=B"); |
| 41 | 42 |
| 42 // The creation time of a cookie that was never set through the | 43 // The creation time of a cookie that was never set through the |
| 43 // CookieCreationTimeManager should be retrieved from the system with 1 | 44 // CookieCreationTimeManager should be retrieved from the system with 1 |
| 44 // second precision. | 45 // second precision. |
| 45 base::Time time = creation_time_manager_.GetCreationTime(cookie); | 46 base::Time time = creation_time_manager_.GetCreationTime(cookie); |
| 46 base::Time now = base::Time::Now(); | 47 base::Time now = base::Time::Now(); |
| 47 ASSERT_FALSE(time.is_null()); | 48 ASSERT_FALSE(time.is_null()); |
| 48 int64 delta = (now - time).InMilliseconds(); | 49 int64_t delta = (now - time).InMilliseconds(); |
| 49 // On iOS 8, the range is (0, 1000) ms, but on earlier iOS versions the range | 50 // 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 | 51 // is (-500, 500) ms. The intervals tested are actually 1200 ms to allow some |
| 51 // imprecision. | 52 // imprecision. |
| 52 if (base::ios::IsRunningOnIOS8OrLater()) { | 53 if (base::ios::IsRunningOnIOS8OrLater()) { |
| 53 EXPECT_GT(delta, -100); | 54 EXPECT_GT(delta, -100); |
| 54 EXPECT_LT(delta, 1100); | 55 EXPECT_LT(delta, 1100); |
| 55 } else { | 56 } else { |
| 56 EXPECT_GT(delta, -600); | 57 EXPECT_GT(delta, -600); |
| 57 EXPECT_LT(delta, 600); | 58 EXPECT_LT(delta, 600); |
| 58 } | 59 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 creation_time_manager_.SetCreationTime(GetCookie(@"F=G"), creation_time); | 93 creation_time_manager_.SetCreationTime(GetCookie(@"F=G"), creation_time); |
| 93 // Delete all creation times. | 94 // Delete all creation times. |
| 94 creation_time_manager_.Clear(); | 95 creation_time_manager_.Clear(); |
| 95 // |now| is available again because all creation times were cleared. | 96 // |now| is available again because all creation times were cleared. |
| 96 creation_time = creation_time_manager_.MakeUniqueCreationTime(now); | 97 creation_time = creation_time_manager_.MakeUniqueCreationTime(now); |
| 97 EXPECT_EQ(now, creation_time); | 98 EXPECT_EQ(now, creation_time); |
| 98 } | 99 } |
| 99 | 100 |
| 100 TEST_F(CookieCreationTimeManagerTest, MakeUniqueCreationTimeConflicts) { | 101 TEST_F(CookieCreationTimeManagerTest, MakeUniqueCreationTimeConflicts) { |
| 101 base::Time creation_time = base::Time::Now(); | 102 base::Time creation_time = base::Time::Now(); |
| 102 int64 time_internal_value = creation_time.ToInternalValue(); | 103 int64_t time_internal_value = creation_time.ToInternalValue(); |
| 103 // Insert two cookies with consecutive times. | 104 // Insert two cookies with consecutive times. |
| 104 creation_time_manager_.SetCreationTime(GetCookie(@"A=B"), creation_time); | 105 creation_time_manager_.SetCreationTime(GetCookie(@"A=B"), creation_time); |
| 105 creation_time_manager_.SetCreationTime( | 106 creation_time_manager_.SetCreationTime( |
| 106 GetCookie(@"C=D"), | 107 GetCookie(@"C=D"), |
| 107 base::Time::FromInternalValue(time_internal_value + 1)); | 108 base::Time::FromInternalValue(time_internal_value + 1)); |
| 108 | 109 |
| 109 // MakeUniqueCreationTime() should insert at |time_internal_value + 2|. | 110 // MakeUniqueCreationTime() should insert at |time_internal_value + 2|. |
| 110 base::Time time = | 111 base::Time time = |
| 111 creation_time_manager_.MakeUniqueCreationTime(creation_time); | 112 creation_time_manager_.MakeUniqueCreationTime(creation_time); |
| 112 EXPECT_EQ(time_internal_value + 2, time.ToInternalValue()); | 113 EXPECT_EQ(time_internal_value + 2, time.ToInternalValue()); |
| 113 creation_time_manager_.SetCreationTime(GetCookie(@"E=F"), time); | 114 creation_time_manager_.SetCreationTime(GetCookie(@"E=F"), time); |
| 114 | 115 |
| 115 // Leave an available slot at |time_internal_value + 3| and insert another | 116 // Leave an available slot at |time_internal_value + 3| and insert another |
| 116 // cookie at |time_internal_value + 4|. | 117 // cookie at |time_internal_value + 4|. |
| 117 creation_time_manager_.SetCreationTime( | 118 creation_time_manager_.SetCreationTime( |
| 118 GetCookie(@"G=H"), | 119 GetCookie(@"G=H"), |
| 119 base::Time::FromInternalValue(time_internal_value + 4)); | 120 base::Time::FromInternalValue(time_internal_value + 4)); |
| 120 | 121 |
| 121 // MakeUniqueCreationTime() should use the available slot. | 122 // MakeUniqueCreationTime() should use the available slot. |
| 122 time = creation_time_manager_.MakeUniqueCreationTime(creation_time); | 123 time = creation_time_manager_.MakeUniqueCreationTime(creation_time); |
| 123 EXPECT_EQ(time_internal_value + 3, time.ToInternalValue()); | 124 EXPECT_EQ(time_internal_value + 3, time.ToInternalValue()); |
| 124 } | 125 } |
| 125 | 126 |
| 126 } // namespace net | 127 } // namespace net |
| OLD | NEW |