OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
7 #include "components/update_client/utils.h" | 7 #include "components/update_client/utils.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "url/gurl.h" | 9 #include "url/gurl.h" |
10 | 10 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 EXPECT_FALSE(IsValidBrand(std::string("T"))); // Too short. | 68 EXPECT_FALSE(IsValidBrand(std::string("T"))); // Too short. |
69 EXPECT_FALSE(IsValidBrand(std::string("TE"))); // | 69 EXPECT_FALSE(IsValidBrand(std::string("TE"))); // |
70 EXPECT_FALSE(IsValidBrand(std::string("TES"))); // | 70 EXPECT_FALSE(IsValidBrand(std::string("TES"))); // |
71 EXPECT_FALSE(IsValidBrand(std::string("TESTS"))); // Too long. | 71 EXPECT_FALSE(IsValidBrand(std::string("TESTS"))); // Too long. |
72 EXPECT_FALSE(IsValidBrand(std::string("TES1"))); // Has digit. | 72 EXPECT_FALSE(IsValidBrand(std::string("TES1"))); // Has digit. |
73 EXPECT_FALSE(IsValidBrand(std::string(" TES"))); // Begins with white space. | 73 EXPECT_FALSE(IsValidBrand(std::string(" TES"))); // Begins with white space. |
74 EXPECT_FALSE(IsValidBrand(std::string("TES "))); // Ends with white space. | 74 EXPECT_FALSE(IsValidBrand(std::string("TES "))); // Ends with white space. |
75 EXPECT_FALSE(IsValidBrand(std::string("T ES"))); // Contains white space. | 75 EXPECT_FALSE(IsValidBrand(std::string("T ES"))); // Contains white space. |
76 EXPECT_FALSE(IsValidBrand(std::string("<TE"))); // Has <. | 76 EXPECT_FALSE(IsValidBrand(std::string("<TE"))); // Has <. |
77 EXPECT_FALSE(IsValidBrand(std::string("TE>"))); // Has >. | 77 EXPECT_FALSE(IsValidBrand(std::string("TE>"))); // Has >. |
78 EXPECT_FALSE(IsValidAp(std::string("\""))); // Has " | 78 EXPECT_FALSE(IsValidBrand(std::string("\""))); // Has " |
79 EXPECT_FALSE(IsValidAp(std::string("\\"))); // Has backslash. | 79 EXPECT_FALSE(IsValidBrand(std::string("\\"))); // Has backslash. |
80 EXPECT_FALSE(IsValidBrand(std::string("\xaa"))); // Has non-ASCII char. | 80 EXPECT_FALSE(IsValidBrand(std::string("\xaa"))); // Has non-ASCII char. |
81 } | 81 } |
82 | 82 |
83 // Tests that the ap matches ^[-+_=a-zA-Z0-9]{0,256}$ | 83 // Tests that the name of an InstallerAttribute matches ^[-_=a-zA-Z0-9]{1,256}$ |
84 TEST(UpdateClientUtils, IsValidAp) { | 84 TEST(UpdateClientUtils, IsValidInstallerAttributeName) { |
85 EXPECT_TRUE(IsValidAp(std::string("a=1"))); | 85 // Test the length boundaries. |
86 EXPECT_TRUE(IsValidAp(std::string(""))); | 86 EXPECT_FALSE(IsValidInstallerAttribute( |
87 EXPECT_TRUE(IsValidAp(std::string("A"))); | 87 make_pair(std::string(0, 'a'), std::string("value")))); |
88 EXPECT_TRUE(IsValidAp(std::string("Z"))); | 88 EXPECT_TRUE(IsValidInstallerAttribute( |
89 EXPECT_TRUE(IsValidAp(std::string("a"))); | 89 make_pair(std::string(1, 'a'), std::string("value")))); |
90 EXPECT_TRUE(IsValidAp(std::string("z"))); | 90 EXPECT_TRUE(IsValidInstallerAttribute( |
91 EXPECT_TRUE(IsValidAp(std::string("0"))); | 91 make_pair(std::string(256, 'a'), std::string("value")))); |
92 EXPECT_TRUE(IsValidAp(std::string("9"))); | 92 EXPECT_FALSE(IsValidInstallerAttribute( |
93 EXPECT_TRUE(IsValidAp(std::string(256, 'a'))); | 93 make_pair(std::string(257, 'a'), std::string("value")))); |
94 EXPECT_TRUE(IsValidAp(std::string("-+_="))); | |
95 | 94 |
96 EXPECT_FALSE(IsValidAp(std::string(257, 'a'))); // Too long. | 95 const char* const valid_names[] = {"A", "Z", "a", "a-b", "A_B", |
97 EXPECT_FALSE(IsValidAp(std::string(" ap"))); // Begins with white space. | 96 "z", "0", "9", "-_"}; |
98 EXPECT_FALSE(IsValidAp(std::string("ap "))); // Ends with white space. | 97 for (const auto& name : valid_names) |
99 EXPECT_FALSE(IsValidAp(std::string("a p"))); // Contains white space. | 98 EXPECT_TRUE(IsValidInstallerAttribute( |
100 EXPECT_FALSE(IsValidAp(std::string("<ap"))); // Has <. | 99 make_pair(std::string(name), std::string("value")))); |
101 EXPECT_FALSE(IsValidAp(std::string("ap>"))); // Has >. | 100 |
102 EXPECT_FALSE(IsValidAp(std::string("\""))); // Has " | 101 const char* const invalid_names[] = { |
103 EXPECT_FALSE(IsValidAp(std::string("\\"))); // Has backspace. | 102 "", "a=1", " name", "name ", "na me", "<name", "name>", |
104 EXPECT_FALSE(IsValidAp(std::string("\xaa"))); // Has non-ASCII char. | 103 "\"", "\\", "\xaa", ".", ",", ";", "+"}; |
| 104 for (const auto& name : invalid_names) |
| 105 EXPECT_FALSE(IsValidInstallerAttribute( |
| 106 make_pair(std::string(name), std::string("value")))); |
| 107 } |
| 108 |
| 109 // Tests that the value of an InstallerAttribute matches |
| 110 // ^[-.,;+_=a-zA-Z0-9]{0,256}$ |
| 111 TEST(UpdateClientUtils, IsValidInstallerAttributeValue) { |
| 112 // Test the length boundaries. |
| 113 EXPECT_TRUE(IsValidInstallerAttribute( |
| 114 make_pair(std::string("name"), std::string(0, 'a')))); |
| 115 EXPECT_TRUE(IsValidInstallerAttribute( |
| 116 make_pair(std::string("name"), std::string(256, 'a')))); |
| 117 EXPECT_FALSE(IsValidInstallerAttribute( |
| 118 make_pair(std::string("name"), std::string(257, 'a')))); |
| 119 |
| 120 const char* const valid_values[] = {"", "a=1", "A", "Z", "a", |
| 121 "z", "0", "9", "-.,;+_="}; |
| 122 for (const auto& value : valid_values) |
| 123 EXPECT_TRUE(IsValidInstallerAttribute( |
| 124 make_pair(std::string("name"), std::string(value)))); |
| 125 |
| 126 const char* const invalid_values[] = {" ap", "ap ", "a p", "<ap", |
| 127 "ap>", "\"", "\\", "\xaa"}; |
| 128 for (const auto& value : invalid_values) |
| 129 EXPECT_FALSE(IsValidInstallerAttribute( |
| 130 make_pair(std::string("name"), std::string(value)))); |
105 } | 131 } |
106 | 132 |
107 TEST(UpdateClientUtils, RemoveUnsecureUrls) { | 133 TEST(UpdateClientUtils, RemoveUnsecureUrls) { |
108 const GURL test1[] = {GURL("http://foo"), GURL("https://foo")}; | 134 const GURL test1[] = {GURL("http://foo"), GURL("https://foo")}; |
109 std::vector<GURL> urls(std::begin(test1), std::end(test1)); | 135 std::vector<GURL> urls(std::begin(test1), std::end(test1)); |
110 RemoveUnsecureUrls(&urls); | 136 RemoveUnsecureUrls(&urls); |
111 EXPECT_EQ(1u, urls.size()); | 137 EXPECT_EQ(1u, urls.size()); |
112 EXPECT_EQ(urls[0], GURL("https://foo")); | 138 EXPECT_EQ(urls[0], GURL("https://foo")); |
113 | 139 |
114 const GURL test2[] = {GURL("https://foo"), GURL("http://foo")}; | 140 const GURL test2[] = {GURL("https://foo"), GURL("http://foo")}; |
(...skipping 14 matching lines...) Expand all Loading... |
129 RemoveUnsecureUrls(&urls); | 155 RemoveUnsecureUrls(&urls); |
130 EXPECT_EQ(0u, urls.size()); | 156 EXPECT_EQ(0u, urls.size()); |
131 | 157 |
132 const GURL test5[] = {GURL("http://foo"), GURL("http://bar")}; | 158 const GURL test5[] = {GURL("http://foo"), GURL("http://bar")}; |
133 urls.assign(std::begin(test5), std::end(test5)); | 159 urls.assign(std::begin(test5), std::end(test5)); |
134 RemoveUnsecureUrls(&urls); | 160 RemoveUnsecureUrls(&urls); |
135 EXPECT_EQ(0u, urls.size()); | 161 EXPECT_EQ(0u, urls.size()); |
136 } | 162 } |
137 | 163 |
138 } // namespace update_client | 164 } // namespace update_client |
OLD | NEW |