Commit c8c6cd13 authored by maksim.sisov's avatar maksim.sisov Committed by Commit bot

Make callers of FromUTC(Local)Exploded in net/ use new time API.

Use new time conversion API in accordance with
https://codereview.chromium.org/1988663002/

BUG=601905,601903,601900

Review-Url: https://codereview.chromium.org/2090713003
Cr-Commit-Position: refs/heads/master@{#439789}
parent e064666e
......@@ -85,11 +85,11 @@ class CTPolicyEnforcerTest : public ::testing::Test {
sct->log_id = std::string(crypto::kSHA256Length, static_cast<char>(i));
if (timestamp_past_enforcement_date) {
sct->timestamp =
base::Time::FromUTCExploded({2015, 8, 0, 15, 0, 0, 0, 0});
EXPECT_TRUE(base::Time::FromUTCExploded({2015, 8, 0, 15, 0, 0, 0, 0},
&sct->timestamp));
} else {
sct->timestamp =
base::Time::FromUTCExploded({2015, 6, 0, 15, 0, 0, 0, 0});
EXPECT_TRUE(base::Time::FromUTCExploded({2015, 6, 0, 15, 0, 0, 0, 0},
&sct->timestamp));
}
verified_scts->push_back(sct);
......@@ -111,10 +111,11 @@ class CTPolicyEnforcerTest : public ::testing::Test {
sct->origin = desired_origin;
sct->log_id = std::string(kCertlyLogID, crypto::kSHA256Length);
if (timestamp_after_disqualification_date) {
sct->timestamp =
base::Time::FromUTCExploded({2016, 4, 0, 16, 0, 0, 0, 0});
EXPECT_TRUE(base::Time::FromUTCExploded({2016, 4, 0, 16, 0, 0, 0, 0},
&sct->timestamp));
} else {
sct->timestamp = base::Time::FromUTCExploded({2016, 4, 0, 1, 0, 0, 0, 0});
EXPECT_TRUE(base::Time::FromUTCExploded({2016, 4, 0, 1, 0, 0, 0, 0},
&sct->timestamp));
}
verified_scts->push_back(sct);
......@@ -130,6 +131,14 @@ class CTPolicyEnforcerTest : public ::testing::Test {
verified_scts);
}
base::Time CreateTime(const base::Time::Exploded& exploded) {
base::Time result;
if (!base::Time::FromUTCExploded(exploded, &result)) {
ADD_FAILURE() << "Failed FromUTCExploded";
}
return result;
}
protected:
std::unique_ptr<CTPolicyEnforcer> policy_enforcer_;
scoped_refptr<X509Certificate> chain_;
......@@ -450,38 +459,48 @@ TEST_F(CTPolicyEnforcerTest,
ASSERT_TRUE(private_key);
// Test multiple validity periods
base::Time time_2015_3_0_25_11_25_0_0 =
CreateTime({2015, 3, 0, 25, 11, 25, 0, 0});
base::Time time_2016_6_0_6_11_25_0_0 =
CreateTime({2016, 6, 0, 6, 11, 25, 0, 0});
base::Time time_2016_6_0_25_11_25_0_0 =
CreateTime({2016, 6, 0, 25, 11, 25, 0, 0});
base::Time time_2016_6_0_27_11_25_0_0 =
CreateTime({2016, 6, 0, 27, 11, 25, 0, 0});
base::Time time_2017_6_0_25_11_25_0_0 =
CreateTime({2017, 6, 0, 25, 11, 25, 0, 0});
base::Time time_2017_6_0_28_11_25_0_0 =
CreateTime({2017, 6, 0, 28, 11, 25, 0, 0});
base::Time time_2018_6_0_25_11_25_0_0 =
CreateTime({2018, 6, 0, 25, 11, 25, 0, 0});
base::Time time_2018_6_0_27_11_25_0_0 =
CreateTime({2018, 6, 0, 27, 11, 25, 0, 0});
const struct TestData {
base::Time validity_start;
base::Time validity_end;
size_t scts_required;
} kTestData[] = {{// Cert valid for 14 months, needs 2 SCTs.
base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}),
base::Time::FromUTCExploded({2016, 6, 0, 6, 11, 25, 0, 0}),
2},
time_2015_3_0_25_11_25_0_0, time_2016_6_0_6_11_25_0_0, 2},
{// Cert valid for exactly 15 months, needs 3 SCTs.
base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}),
base::Time::FromUTCExploded({2016, 6, 0, 25, 11, 25, 0, 0}),
3},
time_2015_3_0_25_11_25_0_0, time_2016_6_0_25_11_25_0_0, 3},
{// Cert valid for over 15 months, needs 3 SCTs.
base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}),
base::Time::FromUTCExploded({2016, 6, 0, 27, 11, 25, 0, 0}),
3},
time_2015_3_0_25_11_25_0_0, time_2016_6_0_27_11_25_0_0, 3},
{// Cert valid for exactly 27 months, needs 3 SCTs.
base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}),
base::Time::FromUTCExploded({2017, 6, 0, 25, 11, 25, 0, 0}),
3},
time_2015_3_0_25_11_25_0_0, time_2017_6_0_25_11_25_0_0, 3},
{// Cert valid for over 27 months, needs 4 SCTs.
base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}),
base::Time::FromUTCExploded({2017, 6, 0, 28, 11, 25, 0, 0}),
4},
time_2015_3_0_25_11_25_0_0, time_2017_6_0_28_11_25_0_0, 4},
{// Cert valid for exactly 39 months, needs 4 SCTs.
base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}),
base::Time::FromUTCExploded({2018, 6, 0, 25, 11, 25, 0, 0}),
4},
time_2015_3_0_25_11_25_0_0, time_2018_6_0_25_11_25_0_0, 4},
{// Cert valid for over 39 months, needs 5 SCTs.
base::Time::FromUTCExploded({2015, 3, 0, 25, 11, 25, 0, 0}),
base::Time::FromUTCExploded({2018, 6, 0, 27, 11, 25, 0, 0}),
5}};
time_2015_3_0_25_11_25_0_0, time_2018_6_0_27_11_25_0_0, 5}};
for (size_t i = 0; i < arraysize(kTestData); ++i) {
SCOPED_TRACE(i);
......
......@@ -71,13 +71,9 @@ bool ParseCertificateDate(const base::StringPiece& raw_date,
if (valid && year_length == 2)
exploded.year += exploded.year < 50 ? 2000 : 1900;
valid &= exploded.HasValidValues();
if (!valid)
return false;
*time = base::Time::FromUTCExploded(exploded);
return true;
return base::Time::FromUTCExploded(exploded, time);
}
} // namespace net
......@@ -182,18 +182,21 @@ const struct CertDateTestData {
"20120101123000Z",
true,
{2012, 1, 0, 1, 12, 30, 0}},
// test 31st of April
{CERT_DATE_FORMAT_GENERALIZED_TIME, "20160431121000Z", false, {0}},
// test 31st of February
{CERT_DATE_FORMAT_GENERALIZED_TIME, "20160231121000Z", false, {0}},
};
// GTest pretty printer.
void PrintTo(const CertDateTestData& data, std::ostream* os) {
base::Time out_time;
bool result = base::Time::FromUTCExploded(data.expected_result, &out_time);
*os << " format: " << data.format
<< "; date string: " << base::StringPiece(data.date_string)
<< "; valid: " << data.is_valid
<< "; expected date: "
<< (data.is_valid ?
base::Time::FromUTCExploded(data.expected_result)
.ToInternalValue() :
0U);
<< "; valid: " << data.is_valid << "; expected date: "
<< (data.is_valid ? out_time.ToInternalValue() : 0U)
<< "; FromUTCExploded conversion result: " << result;
}
class X509CertTypesDateTest : public testing::TestWithParam<CertDateTestData> {
......@@ -202,24 +205,30 @@ class X509CertTypesDateTest : public testing::TestWithParam<CertDateTestData> {
void SetUp() override { test_data_ = GetParam(); }
protected:
CertDateTestData test_data_;
CertDateTestData test_data_;
};
TEST_P(X509CertTypesDateTest, Parse) {
base::Time parsed_date;
bool parsed = ParseCertificateDate(
test_data_.date_string, test_data_.format, &parsed_date);
EXPECT_EQ(test_data_.is_valid, parsed);
if (!parsed && test_data_.is_valid &&
test_data_.expected_result.year >= 2038 && sizeof(time_t) == 4) {
// Some of the valid test data will fail on 32-bit POSIX systems
return;
}
if (!test_data_.is_valid)
return;
// Convert the expected value to a base::Time(). This ensures that systems
// Convert the expected value to a base::Time(). This ensures that
// systems that only support 32-bit times will pass the tests, by ensuring at
// least that the times have the same truncating behaviour.
// least that the times have the same truncating behavior.
// Note: Compared as internal values so that mismatches can be cleanly
// printed by GTest (eg: without PrintTo overrides).
EXPECT_EQ(base::Time::FromUTCExploded(test_data_.expected_result)
.ToInternalValue(),
parsed_date.ToInternalValue());
// printed by GTest (e.g.: without PrintTo overrides).
base::Time out_time;
EXPECT_TRUE(
base::Time::FromUTCExploded(test_data_.expected_result, &out_time));
EXPECT_EQ(out_time.ToInternalValue(), parsed_date.ToInternalValue());
}
INSTANTIATE_TEST_CASE_P(,
X509CertTypesDateTest,
......
......@@ -275,12 +275,10 @@ void Eviction::ReportTrimTimes(EntryImpl* entry) {
backend_->FirstEviction();
} else {
// This is an old file, but we may want more reports from this user so
// lets save some create_time.
Time::Exploded old = {0};
old.year = 2009;
old.month = 3;
old.day_of_month = 1;
header_->create_time = Time::FromLocalExploded(old).ToInternalValue();
// lets save some create_time. Conversion cannot fail here.
const base::Time time_2009_3_1 =
base::Time::FromInternalValue(12985574400000000);
header_->create_time = time_2009_3_1.ToInternalValue();
}
}
}
......
......@@ -84,7 +84,9 @@ class SQLiteChannelIDStoreTest : public testing::Test {
exploded_time.minute = 22;
exploded_time.second = 39;
exploded_time.millisecond = 0;
return base::Time::FromUTCExploded(exploded_time);
base::Time out_time;
EXPECT_TRUE(base::Time::FromUTCExploded(exploded_time, &out_time));
return out_time;
}
static base::Time GetTestCertCreationTime() {
......@@ -98,7 +100,9 @@ class SQLiteChannelIDStoreTest : public testing::Test {
exploded_time.minute = 22;
exploded_time.second = 39;
exploded_time.millisecond = 0;
return base::Time::FromUTCExploded(exploded_time);
base::Time out_time;
EXPECT_TRUE(base::Time::FromUTCExploded(exploded_time, &out_time));
return out_time;
}
void SetUp() override {
......
......@@ -53,8 +53,7 @@ bool TwoColumnDateListingToTime(const base::string16& date,
return false;
// We don't know the time zone of the server, so just use UTC.
*result = base::Time::FromUTCExploded(time_exploded);
return true;
return base::Time::FromUTCExploded(time_exploded, result);
}
// Returns the column index of the end of the date listing and detected
......
......@@ -48,8 +48,9 @@ TEST_P(FtpDirectoryListingParserTest, Parse) {
mock_current_time_exploded.day_of_month = 15;
mock_current_time_exploded.hour = 12;
mock_current_time_exploded.minute = 45;
base::Time mock_current_time(
base::Time::FromUTCExploded(mock_current_time_exploded));
base::Time mock_current_time;
EXPECT_TRUE(base::Time::FromUTCExploded(mock_current_time_exploded,
&mock_current_time));
SCOPED_TRACE(base::StringPrintf("Test case: %s", param.name));
......
......@@ -68,7 +68,11 @@ class FtpDirectoryListingParserTest : public testing::Test {
mock_current_time_exploded.day_of_month = 15;
mock_current_time_exploded.hour = 12;
mock_current_time_exploded.minute = 45;
return base::Time::FromUTCExploded(mock_current_time_exploded);
base::Time out_time;
EXPECT_TRUE(
base::Time::FromUTCExploded(mock_current_time_exploded, &out_time));
return out_time;
}
};
......
......@@ -193,8 +193,7 @@ bool VmsDateListingToTime(const std::vector<base::string16>& columns,
return false;
// We don't know the time zone of the server, so just use UTC.
*time = base::Time::FromUTCExploded(time_exploded);
return true;
return base::Time::FromUTCExploded(time_exploded, time);
}
} // namespace
......
......@@ -286,8 +286,7 @@ bool FtpUtil::LsDateListingToTime(const base::string16& month,
}
// We don't know the time zone of the listing, so just use UTC.
*result = base::Time::FromUTCExploded(time_exploded);
return true;
return base::Time::FromUTCExploded(time_exploded, result);
}
// static
......@@ -349,8 +348,7 @@ bool FtpUtil::WindowsDateListingToTime(const base::string16& date,
}
// We don't know the time zone of the server, so just use UTC.
*result = base::Time::FromUTCExploded(time_exploded);
return true;
return base::Time::FromUTCExploded(time_exploded, result);
}
// static
......
......@@ -1040,10 +1040,12 @@ TEST(HttpUtilTest, ParseContentRangeHeader) {
TEST(HttpUtilTest, ParseRetryAfterHeader) {
base::Time::Exploded now_exploded = {2014, 11, 4, 5, 22, 39, 30, 0};
base::Time now = base::Time::FromUTCExploded(now_exploded);
base::Time now;
EXPECT_TRUE(base::Time::FromUTCExploded(now_exploded, &now));
base::Time::Exploded later_exploded = {2015, 1, 5, 1, 12, 34, 56, 0};
base::Time later = base::Time::FromUTCExploded(later_exploded);
base::Time later;
EXPECT_TRUE(base::Time::FromUTCExploded(later_exploded, &later));
const struct {
const char* retry_after_string;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment