sorin | 590586c | 2016-01-26 20:09:40 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 5 | #include "base/files/file_path.h" |
| 6 | #include "base/path_service.h" |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 7 | #include "components/update_client/updater_state.h" |
sorin | 590586c | 2016-01-26 20:09:40 | [diff] [blame] | 8 | #include "components/update_client/utils.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | #include "url/gurl.h" |
| 11 | |
| 12 | using std::string; |
| 13 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | base::FilePath MakeTestFilePath(const char* file) { |
| 17 | base::FilePath path; |
Avi Drissman | f617d01 | 2018-05-02 18:48:53 | [diff] [blame] | 18 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 19 | return path.AppendASCII("components/test/data/update_client") |
| 20 | .AppendASCII(file); |
| 21 | } |
| 22 | |
| 23 | } // namespace |
| 24 | |
sorin | 590586c | 2016-01-26 20:09:40 | [diff] [blame] | 25 | namespace update_client { |
| 26 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 27 | TEST(UpdateClientUtils, VerifyFileHash256) { |
| 28 | EXPECT_TRUE(VerifyFileHash256( |
| 29 | MakeTestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), |
| 30 | std::string( |
| 31 | "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"))); |
| 32 | |
| 33 | EXPECT_FALSE(VerifyFileHash256( |
| 34 | MakeTestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), |
| 35 | std::string(""))); |
| 36 | |
| 37 | EXPECT_FALSE(VerifyFileHash256( |
| 38 | MakeTestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), |
| 39 | std::string("abcd"))); |
| 40 | |
| 41 | EXPECT_FALSE(VerifyFileHash256( |
| 42 | MakeTestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), |
| 43 | std::string( |
| 44 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))); |
| 45 | } |
| 46 | |
sorin | af402ee | 2016-04-09 01:02:06 | [diff] [blame] | 47 | // Tests that the brand matches ^[a-zA-Z]{4}?$ |
sorin | a1fafb7d | 2016-03-23 17:54:42 | [diff] [blame] | 48 | TEST(UpdateClientUtils, IsValidBrand) { |
sorin | af402ee | 2016-04-09 01:02:06 | [diff] [blame] | 49 | // The valid brand code must be empty or exactly 4 chars long. |
sorin | a1fafb7d | 2016-03-23 17:54:42 | [diff] [blame] | 50 | EXPECT_TRUE(IsValidBrand(std::string(""))); |
sorin | a1fafb7d | 2016-03-23 17:54:42 | [diff] [blame] | 51 | EXPECT_TRUE(IsValidBrand(std::string("TEST"))); |
sorin | a1fafb7d | 2016-03-23 17:54:42 | [diff] [blame] | 52 | EXPECT_TRUE(IsValidBrand(std::string("test"))); |
| 53 | EXPECT_TRUE(IsValidBrand(std::string("TEst"))); |
| 54 | |
sorin | af402ee | 2016-04-09 01:02:06 | [diff] [blame] | 55 | EXPECT_FALSE(IsValidBrand(std::string("T"))); // Too short. |
| 56 | EXPECT_FALSE(IsValidBrand(std::string("TE"))); // |
| 57 | EXPECT_FALSE(IsValidBrand(std::string("TES"))); // |
sorin | a1fafb7d | 2016-03-23 17:54:42 | [diff] [blame] | 58 | EXPECT_FALSE(IsValidBrand(std::string("TESTS"))); // Too long. |
| 59 | EXPECT_FALSE(IsValidBrand(std::string("TES1"))); // Has digit. |
| 60 | EXPECT_FALSE(IsValidBrand(std::string(" TES"))); // Begins with white space. |
| 61 | EXPECT_FALSE(IsValidBrand(std::string("TES "))); // Ends with white space. |
| 62 | EXPECT_FALSE(IsValidBrand(std::string("T ES"))); // Contains white space. |
| 63 | EXPECT_FALSE(IsValidBrand(std::string("<TE"))); // Has <. |
| 64 | EXPECT_FALSE(IsValidBrand(std::string("TE>"))); // Has >. |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 65 | EXPECT_FALSE(IsValidBrand(std::string("\""))); // Has " |
| 66 | EXPECT_FALSE(IsValidBrand(std::string("\\"))); // Has backslash. |
sorin | a1fafb7d | 2016-03-23 17:54:42 | [diff] [blame] | 67 | EXPECT_FALSE(IsValidBrand(std::string("\xaa"))); // Has non-ASCII char. |
| 68 | } |
| 69 | |
David 'Digit' Turner | 2ccae250 | 2017-11-10 17:16:04 | [diff] [blame] | 70 | TEST(UpdateClientUtils, GetCrxComponentId) { |
| 71 | static const uint8_t kHash[16] = { |
| 72 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
| 73 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
| 74 | }; |
| 75 | CrxComponent component; |
| 76 | component.pk_hash.assign(kHash, kHash + sizeof(kHash)); |
| 77 | |
| 78 | EXPECT_EQ(std::string("abcdefghijklmnopabcdefghijklmnop"), |
| 79 | GetCrxComponentID(component)); |
| 80 | } |
| 81 | |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 82 | // Tests that the name of an InstallerAttribute matches ^[-_=a-zA-Z0-9]{1,256}$ |
| 83 | TEST(UpdateClientUtils, IsValidInstallerAttributeName) { |
| 84 | // Test the length boundaries. |
| 85 | EXPECT_FALSE(IsValidInstallerAttribute( |
| 86 | make_pair(std::string(0, 'a'), std::string("value")))); |
| 87 | EXPECT_TRUE(IsValidInstallerAttribute( |
| 88 | make_pair(std::string(1, 'a'), std::string("value")))); |
| 89 | EXPECT_TRUE(IsValidInstallerAttribute( |
| 90 | make_pair(std::string(256, 'a'), std::string("value")))); |
| 91 | EXPECT_FALSE(IsValidInstallerAttribute( |
| 92 | make_pair(std::string(257, 'a'), std::string("value")))); |
sorin | 2b864bc7 | 2016-04-08 22:14:47 | [diff] [blame] | 93 | |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 94 | const char* const valid_names[] = {"A", "Z", "a", "a-b", "A_B", |
| 95 | "z", "0", "9", "-_"}; |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 96 | for (const char* name : valid_names) |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 97 | EXPECT_TRUE(IsValidInstallerAttribute( |
| 98 | make_pair(std::string(name), std::string("value")))); |
| 99 | |
| 100 | const char* const invalid_names[] = { |
| 101 | "", "a=1", " name", "name ", "na me", "<name", "name>", |
| 102 | "\"", "\\", "\xaa", ".", ",", ";", "+"}; |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 103 | for (const char* name : invalid_names) |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 104 | EXPECT_FALSE(IsValidInstallerAttribute( |
| 105 | make_pair(std::string(name), std::string("value")))); |
| 106 | } |
| 107 | |
| 108 | // Tests that the value of an InstallerAttribute matches |
| 109 | // ^[-.,;+_=a-zA-Z0-9]{0,256}$ |
| 110 | TEST(UpdateClientUtils, IsValidInstallerAttributeValue) { |
| 111 | // Test the length boundaries. |
| 112 | EXPECT_TRUE(IsValidInstallerAttribute( |
| 113 | make_pair(std::string("name"), std::string(0, 'a')))); |
| 114 | EXPECT_TRUE(IsValidInstallerAttribute( |
| 115 | make_pair(std::string("name"), std::string(256, 'a')))); |
| 116 | EXPECT_FALSE(IsValidInstallerAttribute( |
| 117 | make_pair(std::string("name"), std::string(257, 'a')))); |
| 118 | |
| 119 | const char* const valid_values[] = {"", "a=1", "A", "Z", "a", |
| 120 | "z", "0", "9", "-.,;+_="}; |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 121 | for (const char* value : valid_values) |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 122 | EXPECT_TRUE(IsValidInstallerAttribute( |
| 123 | make_pair(std::string("name"), std::string(value)))); |
| 124 | |
| 125 | const char* const invalid_values[] = {" ap", "ap ", "a p", "<ap", |
| 126 | "ap>", "\"", "\\", "\xaa"}; |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 127 | for (const char* value : invalid_values) |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 128 | EXPECT_FALSE(IsValidInstallerAttribute( |
| 129 | make_pair(std::string("name"), std::string(value)))); |
sorin | 2b864bc7 | 2016-04-08 22:14:47 | [diff] [blame] | 130 | } |
| 131 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 132 | TEST(UpdateClientUtils, RemoveUnsecureUrls) { |
| 133 | const GURL test1[] = {GURL("http://foo"), GURL("https://foo")}; |
| 134 | std::vector<GURL> urls(std::begin(test1), std::end(test1)); |
| 135 | RemoveUnsecureUrls(&urls); |
| 136 | EXPECT_EQ(1u, urls.size()); |
| 137 | EXPECT_EQ(urls[0], GURL("https://foo")); |
| 138 | |
| 139 | const GURL test2[] = {GURL("https://foo"), GURL("http://foo")}; |
| 140 | urls.assign(std::begin(test2), std::end(test2)); |
| 141 | RemoveUnsecureUrls(&urls); |
| 142 | EXPECT_EQ(1u, urls.size()); |
| 143 | EXPECT_EQ(urls[0], GURL("https://foo")); |
| 144 | |
| 145 | const GURL test3[] = {GURL("https://foo"), GURL("https://bar")}; |
| 146 | urls.assign(std::begin(test3), std::end(test3)); |
| 147 | RemoveUnsecureUrls(&urls); |
| 148 | EXPECT_EQ(2u, urls.size()); |
| 149 | EXPECT_EQ(urls[0], GURL("https://foo")); |
| 150 | EXPECT_EQ(urls[1], GURL("https://bar")); |
| 151 | |
| 152 | const GURL test4[] = {GURL("http://foo")}; |
| 153 | urls.assign(std::begin(test4), std::end(test4)); |
| 154 | RemoveUnsecureUrls(&urls); |
| 155 | EXPECT_EQ(0u, urls.size()); |
| 156 | |
| 157 | const GURL test5[] = {GURL("http://foo"), GURL("http://bar")}; |
| 158 | urls.assign(std::begin(test5), std::end(test5)); |
| 159 | RemoveUnsecureUrls(&urls); |
| 160 | EXPECT_EQ(0u, urls.size()); |
| 161 | } |
| 162 | |
Minh X. Nguyen | ac280f2 | 2018-05-24 17:41:29 | [diff] [blame] | 163 | TEST(UpdateClientUtils, ToInstallerResult) { |
| 164 | enum EnumA { |
| 165 | ENTRY0 = 10, |
| 166 | ENTRY1 = 20, |
| 167 | }; |
| 168 | |
| 169 | enum class EnumB { |
| 170 | ENTRY0 = 0, |
| 171 | ENTRY1, |
| 172 | }; |
| 173 | |
| 174 | const auto result1 = ToInstallerResult(EnumA::ENTRY0); |
| 175 | EXPECT_EQ(110, result1.error); |
| 176 | EXPECT_EQ(0, result1.extended_error); |
| 177 | |
| 178 | const auto result2 = ToInstallerResult(ENTRY1, 10000); |
| 179 | EXPECT_EQ(120, result2.error); |
| 180 | EXPECT_EQ(10000, result2.extended_error); |
| 181 | |
| 182 | const auto result3 = ToInstallerResult(EnumB::ENTRY0); |
| 183 | EXPECT_EQ(100, result3.error); |
| 184 | EXPECT_EQ(0, result3.extended_error); |
| 185 | |
| 186 | const auto result4 = ToInstallerResult(EnumB::ENTRY1, 20000); |
| 187 | EXPECT_EQ(101, result4.error); |
| 188 | EXPECT_EQ(20000, result4.extended_error); |
| 189 | } |
| 190 | |
sorin | 590586c | 2016-01-26 20:09:40 | [diff] [blame] | 191 | } // namespace update_client |