utils_unittest.cc 7.4 KB

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