Add tests for redirect responses from SafeBrowsingProtocolManager.
[chromium-blink-merge.git] / base / version.cc
blobb7db4b570d67334a64a890b31a14da8b775d3d8e
1 // Copyright (c) 2012 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.
5 #include "base/version.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/string_number_conversions.h"
11 #include "base/string_split.h"
12 #include "base/string_util.h"
14 namespace {
16 // Parses the |numbers| vector representing the different numbers
17 // inside the version string and constructs a vector of valid integers. It stops
18 // when it reaches an invalid item (including the wildcard character). |parsed|
19 // is the resulting integer vector. Function returns true if all numbers were
20 // parsed successfully, false otherwise.
21 bool ParseVersionNumbers(const std::string& version_str,
22 std::vector<uint16>* parsed) {
23 std::vector<std::string> numbers;
24 base::SplitString(version_str, '.', &numbers);
25 if (numbers.empty())
26 return false;
28 for (std::vector<std::string>::const_iterator it = numbers.begin();
29 it != numbers.end(); ++it) {
30 int num;
31 if (!base::StringToInt(*it, &num))
32 return false;
34 if (num < 0)
35 return false;
37 const uint16 max = 0xFFFF;
38 if (num > max)
39 return false;
41 // This throws out things like +3, or 032.
42 if (base::IntToString(num) != *it)
43 return false;
45 parsed->push_back(static_cast<uint16>(num));
47 return true;
50 // Compares version components in |components1| with components in
51 // |components2|. Returns -1, 0 or 1 if |components1| is greater than, equal to,
52 // or less than |components2|, respectively.
53 int CompareVersionComponents(const std::vector<uint16>& components1,
54 const std::vector<uint16>& components2) {
55 const size_t count = std::min(components1.size(), components2.size());
56 for (size_t i = 0; i < count; ++i) {
57 if (components1[i] > components2[i])
58 return 1;
59 if (components1[i] < components2[i])
60 return -1;
62 if (components1.size() > components2.size()) {
63 for (size_t i = count; i < components1.size(); ++i) {
64 if (components1[i] > 0)
65 return 1;
67 } else if (components1.size() < components2.size()) {
68 for (size_t i = count; i < components2.size(); ++i) {
69 if (components2[i] > 0)
70 return -1;
73 return 0;
76 } // namespace
78 Version::Version() {
81 Version::~Version() {
84 Version::Version(const std::string& version_str) {
85 std::vector<uint16> parsed;
86 if (!ParseVersionNumbers(version_str, &parsed))
87 return;
89 components_.swap(parsed);
92 bool Version::IsValid() const {
93 return (!components_.empty());
96 // static
97 bool Version::IsValidWildcardString(const std::string& wildcard_string) {
98 std::string version_string = wildcard_string;
99 if (EndsWith(wildcard_string.c_str(), ".*", false))
100 version_string = wildcard_string.substr(0, wildcard_string.size() - 2);
102 Version version(version_string);
103 return version.IsValid();
106 bool Version::IsOlderThan(const std::string& version_str) const {
107 Version proposed_ver(version_str);
108 if (!proposed_ver.IsValid())
109 return false;
110 return (CompareTo(proposed_ver) < 0);
113 int Version::CompareToWildcardString(const std::string& wildcard_string) const {
114 DCHECK(IsValid());
115 DCHECK(Version::IsValidWildcardString(wildcard_string));
117 // Default behavior if the string doesn't end with a wildcard.
118 if (!EndsWith(wildcard_string.c_str(), ".*", false)) {
119 Version version(wildcard_string);
120 DCHECK(version.IsValid());
121 return CompareTo(version);
124 std::vector<uint16> parsed;
125 const bool success = ParseVersionNumbers(
126 wildcard_string.substr(0, wildcard_string.length() - 2), &parsed);
127 DCHECK(success);
128 const int comparison = CompareVersionComponents(components_, parsed);
129 // If the version is smaller than the wildcard version's |parsed| vector,
130 // then the wildcard has no effect (e.g. comparing 1.2.3 and 1.3.*) and the
131 // version is still smaller. Same logic for equality (e.g. comparing 1.2.2 to
132 // 1.2.2.* is 0 regardless of the wildcard). Under this logic,
133 // 1.2.0.0.0.0 compared to 1.2.* is 0.
134 if (comparison == -1 || comparison == 0)
135 return comparison;
137 // Catch the case where the digits of |parsed| are found in |components_|,
138 // which means that the two are equal since |parsed| has a trailing "*".
139 // (e.g. 1.2.3 vs. 1.2.* will return 0). All other cases return 1 since
140 // components is greater (e.g. 3.2.3 vs 1.*).
141 DCHECK_GT(parsed.size(), 0UL);
142 const size_t min_num_comp = std::min(components_.size(), parsed.size());
143 for (size_t i = 0; i < min_num_comp; ++i) {
144 if (components_[i] != parsed[i])
145 return 1;
147 return 0;
150 bool Version::Equals(const Version& that) const {
151 DCHECK(IsValid());
152 DCHECK(that.IsValid());
153 return (CompareTo(that) == 0);
156 int Version::CompareTo(const Version& other) const {
157 DCHECK(IsValid());
158 DCHECK(other.IsValid());
159 return CompareVersionComponents(components_, other.components_);
162 const std::string Version::GetString() const {
163 DCHECK(IsValid());
164 std::string version_str;
165 size_t count = components_.size();
166 for (size_t i = 0; i < count - 1; ++i) {
167 version_str.append(base::IntToString(components_[i]));
168 version_str.append(".");
170 version_str.append(base::IntToString(components_[count - 1]));
171 return version_str;