[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 1 | // Copyright 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. |
| 4 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 5 | #include "base/macros.h" |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 6 | #include "net/cookies/cookie_constants.h" |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | |
| 9 | namespace net { |
| 10 | |
| 11 | TEST(CookieConstantsTest, TestCookiePriority) { |
| 12 | // Basic cases. |
| 13 | EXPECT_EQ("low", CookiePriorityToString(COOKIE_PRIORITY_LOW)); |
| 14 | EXPECT_EQ("medium", CookiePriorityToString(COOKIE_PRIORITY_MEDIUM)); |
| 15 | EXPECT_EQ("high", CookiePriorityToString(COOKIE_PRIORITY_HIGH)); |
| 16 | |
| 17 | EXPECT_EQ(COOKIE_PRIORITY_LOW, StringToCookiePriority("low")); |
| 18 | EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, StringToCookiePriority("medium")); |
| 19 | EXPECT_EQ(COOKIE_PRIORITY_HIGH, StringToCookiePriority("high")); |
| 20 | |
| 21 | // Case Insensitivity of StringToCookiePriority(). |
| 22 | EXPECT_EQ(COOKIE_PRIORITY_LOW, StringToCookiePriority("LOW")); |
| 23 | EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, StringToCookiePriority("Medium")); |
| 24 | EXPECT_EQ(COOKIE_PRIORITY_HIGH, StringToCookiePriority("hiGH")); |
| 25 | |
| 26 | // Value of default priority. |
| 27 | EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, COOKIE_PRIORITY_MEDIUM); |
| 28 | |
| 29 | // Numeric values. |
| 30 | EXPECT_LT(COOKIE_PRIORITY_LOW, COOKIE_PRIORITY_MEDIUM); |
| 31 | EXPECT_LT(COOKIE_PRIORITY_MEDIUM, COOKIE_PRIORITY_HIGH); |
| 32 | |
| 33 | // Unrecognized tokens are interpreted as COOKIE_PRIORITY_DEFAULT. |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 34 | const char* const bad_tokens[] = { |
| 35 | "", "lo", "lowerest", "high ", " high", "0"}; |
Ryan Sleevi | 435a3a2 | 2018-05-15 02:16:07 | [diff] [blame] | 36 | for (const auto* bad_token : bad_tokens) { |
| 37 | EXPECT_EQ(COOKIE_PRIORITY_DEFAULT, StringToCookiePriority(bad_token)); |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Lily Chen | 9a9c08a9 | 2019-09-18 00:18:16 | [diff] [blame] | 41 | // TODO(crbug.com/996811): Add tests for multiple possibly-invalid attributes. |
Lily Chen | 324657c | 2019-04-19 14:09:44 | [diff] [blame] | 42 | TEST(CookieConstantsTest, TestCookieSameSite) { |
| 43 | // Test case insensitivity |
| 44 | EXPECT_EQ(CookieSameSite::NO_RESTRICTION, StringToCookieSameSite("None")); |
| 45 | EXPECT_EQ(CookieSameSite::NO_RESTRICTION, StringToCookieSameSite("none")); |
| 46 | EXPECT_EQ(CookieSameSite::NO_RESTRICTION, StringToCookieSameSite("NONE")); |
| 47 | EXPECT_EQ(CookieSameSite::LAX_MODE, StringToCookieSameSite("Lax")); |
| 48 | EXPECT_EQ(CookieSameSite::LAX_MODE, StringToCookieSameSite("LAX")); |
| 49 | EXPECT_EQ(CookieSameSite::LAX_MODE, StringToCookieSameSite("lAx")); |
| 50 | EXPECT_EQ(CookieSameSite::STRICT_MODE, StringToCookieSameSite("Strict")); |
| 51 | EXPECT_EQ(CookieSameSite::STRICT_MODE, StringToCookieSameSite("STRICT")); |
| 52 | EXPECT_EQ(CookieSameSite::STRICT_MODE, StringToCookieSameSite("sTrIcT")); |
Caleb Raitto | f1af4b3 | 2019-10-28 20:02:31 | [diff] [blame] | 53 | EXPECT_EQ(CookieSameSite::UNSPECIFIED, StringToCookieSameSite("extended")); |
| 54 | EXPECT_EQ(CookieSameSite::UNSPECIFIED, StringToCookieSameSite("EXTENDED")); |
| 55 | EXPECT_EQ(CookieSameSite::UNSPECIFIED, StringToCookieSameSite("ExtenDED")); |
Lily Chen | 324657c | 2019-04-19 14:09:44 | [diff] [blame] | 56 | |
| 57 | // Unrecognized tokens are interpreted as UNSPECIFIED. |
| 58 | const char* const bad_tokens[] = {"", "foo", "none ", |
| 59 | "strictest", " none", "0"}; |
| 60 | for (const auto* bad_token : bad_tokens) { |
| 61 | EXPECT_EQ(CookieSameSite::UNSPECIFIED, StringToCookieSameSite(bad_token)); |
| 62 | } |
| 63 | } |
| 64 | |
Steven Bingler | 190f130 | 2020-11-11 16:32:45 | [diff] [blame] | 65 | TEST(CookieConstantsTest, TestReducePortRangeForCookieHistogram) { |
| 66 | struct TestData { |
| 67 | int input_port; |
| 68 | CookiePort expected_enum; |
| 69 | }; |
| 70 | |
| 71 | const TestData kTestValues[] = { |
| 72 | {-1234 /* Invalid port. */, CookiePort::kOther}, |
| 73 | {0 /* Invalid port. */, CookiePort::kOther}, |
| 74 | {1 /* Valid but outside range. */, CookiePort::kOther}, |
| 75 | {79 /* Valid but outside range. */, CookiePort::kOther}, |
| 76 | {80, CookiePort::k80}, |
| 77 | {445, CookiePort::k445}, |
| 78 | {3001, CookiePort::k3001}, |
| 79 | {4200, CookiePort::k4200}, |
| 80 | {5002, CookiePort::k5002}, |
| 81 | {7003, CookiePort::k7003}, |
| 82 | {8001, CookiePort::k8001}, |
| 83 | {8080, CookiePort::k8080}, |
| 84 | {8086 /* Valid but outside range. */, CookiePort::kOther}, |
| 85 | {8095, CookiePort::k8095}, |
| 86 | {8100, CookiePort::k8100}, |
| 87 | {8201, CookiePort::k8201}, |
| 88 | {8445, CookiePort::k8445}, |
| 89 | {8888, CookiePort::k8888}, |
| 90 | {9004, CookiePort::k9004}, |
| 91 | {9091, CookiePort::k9091}, |
| 92 | {65535 /* Valid but outside range. */, CookiePort::kOther}, |
| 93 | {655356 /* Invalid port. */, CookiePort::kOther}, |
| 94 | }; |
| 95 | |
| 96 | for (const auto& value : kTestValues) { |
| 97 | EXPECT_EQ(value.expected_enum, |
| 98 | ReducePortRangeForCookieHistogram(value.input_port)); |
| 99 | } |
| 100 | } |
| 101 | |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 102 | } // namespace net |