Commit 85511ccf authored by Devlin Cronin's avatar Devlin Cronin Committed by Commit Bot

[base] Add explicit tests for leading zeros in base::Version

base::Version is interesting. It is treated as (up to) four dot-
separated integers. However, because these are parsed individually,
leading zeros in each component are ignored. This leads to some
interesting (potentially unexpected) behavior:
- v1.01 is equivalent to v1.1
- v1.01's string representation is "1.1"
- v1.02 is greater than v1.1

Additionally, leading zeros are not allowed in the first component,
though they are allowed in subsequent components. That is, v01.1 is
invalid.

Add unittests to cover these cases, and make them more explicit.

Bug: None
Change-Id: Ia8cc6ec988fdef7f6c2b41c93e7468781a458591
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2233495Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#775808}
parent 51d5e92a
......@@ -196,4 +196,28 @@ TEST(VersionTest, IsValidWildcardString) {
}
}
TEST(VersionTest, LeadingZeros) {
{
// Leading zeros in the first component are not allowed.
base::Version v("01.1");
EXPECT_FALSE(v.IsValid());
}
{
// Leading zeros in subsequent components are allowed (and this behavior is
// now important for compatibility with existing modules, like extensions),
// but are ignored because the value is parsed as an integer...
base::Version v1("1.01");
EXPECT_TRUE(v1.IsValid());
// ...and as a result, v1.01 == v1.1.
EXPECT_EQ("1.1", v1.GetString());
base::Version v2("1.1");
EXPECT_EQ(v1, v2);
}
// Similarly, since leading zeros are ignored, v1.02 > v1.1 (because
// v1.02 is translated to 1.2).
EXPECT_GT(base::Version("1.02"), base::Version("1.1"));
}
} // namespace
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