summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/net/cookie_util_unittest.mm
blob: c2611d7c70c70e24feeeda865c1b7b73f50f8a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/chrome/browser/net/cookie_util.h"

#import <Foundation/Foundation.h>

#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Date of the last cookie deletion.
NSString* const kLastCookieDeletionDate = @"LastCookieDeletionDate";

}  // namespace

TEST(CookieUtil, ShouldClearSessionCookies) {
  time_t start_test_time;
  time(&start_test_time);
  NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  // Delete cookies if the key is not present.
  [defaults removeObjectForKey:kLastCookieDeletionDate];
  EXPECT_TRUE(cookie_util::ShouldClearSessionCookies());
  // The deletion time should be created.
  time_t deletion_time = [defaults integerForKey:kLastCookieDeletionDate];
  time_t now;
  time(&now);
  EXPECT_LE(start_test_time, deletion_time);
  EXPECT_LE(deletion_time, now);
  // Cookies are not deleted again.
  EXPECT_FALSE(cookie_util::ShouldClearSessionCookies());

  // Set the deletion time before the machine was started.
  // Sometime in year 1980.
  [defaults setInteger:328697227 forKey:kLastCookieDeletionDate];
  EXPECT_TRUE(cookie_util::ShouldClearSessionCookies());
  EXPECT_LE(now, [defaults integerForKey:kLastCookieDeletionDate]);
}