1 // Copyright 2018 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 "chrome/browser/extensions/api/cookies/cookies_helpers.h"
6
7 #include <limits>
8 #include <memory>
9
10 #include "net/cookies/canonical_cookie.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace extensions {
14
15 // Tests that cookies with an expiration date too far in the future to represent
16 // with base::Time serialize gracefully.
17 // Regression test for https://crbug.com/848221.
TEST(CookiesHelperUnittest,CookieConversionWithInfiniteExpirationDate)18 TEST(CookiesHelperUnittest, CookieConversionWithInfiniteExpirationDate) {
19 // Set a cookie to expire at base::Time::Max(). This can happen when the
20 // cookie is set to expire farther in the future than we can accurately
21 // represent with base::Time(). Note that, in practice, this is really only
22 // applicable on 32-bit machines, but we can fake it a bit for cross-platform
23 // testing by just setting the expiration date directly.
24 const base::Time kExpirationDate = base::Time::Max();
25 net::CanonicalCookie cookie("cookiename", "cookievalue", "example.com", "/",
26 base::Time::Now(), kExpirationDate, base::Time(),
27 false, false, net::CookieSameSite::NO_RESTRICTION,
28 net::COOKIE_PRIORITY_DEFAULT);
29
30 // Serialize the cookie to JSON. We need to gracefully handle the infinite
31 // expiration date, which should be converted to the maximum value.
32 api::cookies::Cookie serialized_cookie =
33 cookies_helpers::CreateCookie(cookie, "1");
34 std::unique_ptr<base::Value> value_cookie = serialized_cookie.ToValue();
35 ASSERT_TRUE(value_cookie);
36 base::Value* expiration_time =
37 value_cookie->FindKeyOfType("expirationDate", base::Value::Type::DOUBLE);
38 ASSERT_TRUE(expiration_time);
39 EXPECT_EQ(std::numeric_limits<double>::max(), expiration_time->GetDouble());
40 }
41
42 } // namespace extensions
43