extension_set_unittest.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2013 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 "extensions/common/extension_set.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/files/file_path.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/values.h"
  10. #include "build/build_config.h"
  11. #include "extensions/common/extension.h"
  12. #include "extensions/common/extension_builder.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace extensions {
  15. namespace {
  16. scoped_refptr<Extension> CreateTestExtension(const std::string& name,
  17. const std::string& launch_url,
  18. const std::string& extent) {
  19. #if BUILDFLAG(IS_WIN)
  20. base::FilePath path(FILE_PATH_LITERAL("c:\\"));
  21. #else
  22. base::FilePath path(FILE_PATH_LITERAL("/"));
  23. #endif
  24. path = path.AppendASCII(name);
  25. base::DictionaryValue manifest;
  26. manifest.SetStringKey("name", name);
  27. manifest.SetStringKey("version", "1");
  28. manifest.SetIntKey("manifest_version", 2);
  29. if (!launch_url.empty())
  30. manifest.SetStringPath("app.launch.web_url", launch_url);
  31. if (!extent.empty()) {
  32. base::Value urls(base::Value::Type::LIST);
  33. urls.Append(extent);
  34. manifest.SetPath("app.urls", std::move(urls));
  35. }
  36. std::string error;
  37. scoped_refptr<Extension> extension(
  38. Extension::Create(path, mojom::ManifestLocation::kInternal, manifest,
  39. Extension::NO_FLAGS, &error));
  40. EXPECT_TRUE(extension.get()) << error;
  41. return extension;
  42. }
  43. } // namespace
  44. TEST(ExtensionSetTest, ExtensionSet) {
  45. scoped_refptr<Extension> ext1(CreateTestExtension(
  46. "a", "https://chrome.google.com/launch", "https://chrome.google.com/"));
  47. scoped_refptr<Extension> ext2(CreateTestExtension(
  48. "a", "http://code.google.com/p/chromium",
  49. "http://code.google.com/p/chromium/"));
  50. scoped_refptr<Extension> ext3(CreateTestExtension(
  51. "b", "http://dev.chromium.org/", "http://dev.chromium.org/"));
  52. scoped_refptr<Extension> ext4(
  53. CreateTestExtension("c", std::string(), std::string()));
  54. ASSERT_TRUE(ext1.get() && ext2.get() && ext3.get() && ext4.get());
  55. ExtensionSet extensions;
  56. // Add an extension.
  57. EXPECT_TRUE(extensions.Insert(ext1));
  58. EXPECT_EQ(1u, extensions.size());
  59. EXPECT_EQ(ext1.get(), extensions.GetByID(ext1->id()));
  60. // Since extension2 has same ID, it should overwrite extension1.
  61. EXPECT_FALSE(extensions.Insert(ext2));
  62. EXPECT_EQ(1u, extensions.size());
  63. EXPECT_EQ(ext2.get(), extensions.GetByID(ext1->id()));
  64. // Add the other extensions.
  65. EXPECT_TRUE(extensions.Insert(ext3));
  66. EXPECT_TRUE(extensions.Insert(ext4));
  67. EXPECT_EQ(3u, extensions.size());
  68. // Get extension by its chrome-extension:// URL
  69. EXPECT_EQ(
  70. ext2.get(),
  71. extensions.GetExtensionOrAppByURL(ext2->GetResourceURL("test.html")));
  72. EXPECT_EQ(
  73. ext3.get(),
  74. extensions.GetExtensionOrAppByURL(ext3->GetResourceURL("test.html")));
  75. EXPECT_EQ(
  76. ext4.get(),
  77. extensions.GetExtensionOrAppByURL(ext4->GetResourceURL("test.html")));
  78. // Get extension by a filesystem or blob URL within it.
  79. GURL ext2_filesystem_url =
  80. GURL("filesystem:" + ext2->GetResourceURL("test.html").spec());
  81. EXPECT_EQ(ext2.get(), extensions.GetExtensionOrAppByURL(ext2_filesystem_url));
  82. EXPECT_EQ(ext2->id(),
  83. extensions.GetExtensionOrAppIDByURL(ext2_filesystem_url));
  84. GURL ext3_blob_url = GURL("blob:" + ext3->GetResourceURL("test.html").spec());
  85. EXPECT_EQ(ext3.get(), extensions.GetExtensionOrAppByURL(ext3_blob_url));
  86. EXPECT_EQ(ext3->id(), extensions.GetExtensionOrAppIDByURL(ext3_blob_url));
  87. // Get extension by web extent.
  88. EXPECT_EQ(ext2.get(),
  89. extensions.GetExtensionOrAppByURL(
  90. GURL("http://code.google.com/p/chromium/monkey")));
  91. EXPECT_EQ(ext3.get(),
  92. extensions.GetExtensionOrAppByURL(
  93. GURL("http://dev.chromium.org/design-docs/")));
  94. EXPECT_FALSE(extensions.GetExtensionOrAppByURL(
  95. GURL("http://blog.chromium.org/")));
  96. // Get extension by web extent with filesystem URL. Paths still matter.
  97. EXPECT_EQ(ext3.get(), extensions.GetExtensionOrAppByURL(
  98. GURL("filesystem:http://dev.chromium.org/foo")));
  99. EXPECT_EQ(ext3->id(), extensions.GetExtensionOrAppIDByURL(
  100. GURL("filesystem:http://dev.chromium.org/foo")));
  101. EXPECT_EQ(nullptr, extensions.GetExtensionOrAppByURL(
  102. GURL("filesystem:http://code.google.com/foo")));
  103. // TODO(crbug/852162): Support blob URLs. This should return ext3.
  104. EXPECT_EQ(nullptr, extensions.GetExtensionOrAppByURL(
  105. GURL("blob:http://dev.chromium.org/abcd")));
  106. // Test InSameExtent().
  107. EXPECT_TRUE(extensions.InSameExtent(
  108. GURL("http://code.google.com/p/chromium/monkey/"),
  109. GURL("http://code.google.com/p/chromium/")));
  110. EXPECT_FALSE(extensions.InSameExtent(
  111. GURL("http://code.google.com/p/chromium/"),
  112. GURL("https://code.google.com/p/chromium/")));
  113. EXPECT_FALSE(extensions.InSameExtent(
  114. GURL("http://code.google.com/p/chromium/"),
  115. GURL("http://dev.chromium.org/design-docs/")));
  116. // Both of these should be NULL, which mean true for InSameExtent.
  117. EXPECT_TRUE(extensions.InSameExtent(
  118. GURL("http://www.google.com/"),
  119. GURL("http://blog.chromium.org/")));
  120. // Remove one of the extensions.
  121. EXPECT_TRUE(extensions.Remove(ext2->id()));
  122. EXPECT_EQ(2u, extensions.size());
  123. EXPECT_FALSE(extensions.GetByID(ext2->id()));
  124. // Make a union of a set with 3 more extensions (only 2 are new).
  125. scoped_refptr<Extension> ext5(
  126. CreateTestExtension("d", std::string(), std::string()));
  127. scoped_refptr<Extension> ext6(
  128. CreateTestExtension("e", std::string(), std::string()));
  129. ASSERT_TRUE(ext5.get() && ext6.get());
  130. std::unique_ptr<ExtensionSet> to_add(new ExtensionSet());
  131. // |ext3| is already in |extensions|, should not affect size.
  132. EXPECT_TRUE(to_add->Insert(ext3));
  133. EXPECT_TRUE(to_add->Insert(ext5));
  134. EXPECT_TRUE(to_add->Insert(ext6));
  135. ASSERT_TRUE(extensions.Contains(ext3->id()));
  136. ASSERT_TRUE(extensions.InsertAll(*to_add));
  137. EXPECT_EQ(4u, extensions.size());
  138. ASSERT_FALSE(extensions.InsertAll(*to_add)); // Re-adding same set no-ops.
  139. EXPECT_EQ(4u, extensions.size());
  140. }
  141. TEST(ExtensionSetTest, TestInsert) {
  142. ExtensionSet set;
  143. std::string id_a(32, 'a');
  144. std::string id_b(32, 'b');
  145. scoped_refptr<const Extension> extension_a_v1 =
  146. ExtensionBuilder("A").SetID(id_a).SetVersion("0.1").Build();
  147. scoped_refptr<const Extension> extension_a_v2 =
  148. ExtensionBuilder("A").SetID(id_a).SetVersion("0.2").Build();
  149. scoped_refptr<const Extension> extension_b =
  150. ExtensionBuilder("B").SetID(id_b).SetVersion("1").Build();
  151. // Inserting a new extension should return true.
  152. EXPECT_TRUE(set.Insert(extension_a_v1));
  153. EXPECT_EQ(1u, set.size());
  154. EXPECT_EQ("0.1", set.GetByID(id_a)->version().GetString());
  155. // Inserting a new version of an extension already in the set should replace
  156. // the current entry, and return false.
  157. EXPECT_FALSE(set.Insert(extension_a_v2));
  158. EXPECT_EQ(1u, set.size());
  159. // Verify the entry was updated.
  160. EXPECT_EQ("0.2", set.GetByID(id_a)->version().GetString());
  161. // Inserting a second new extension should return true.
  162. EXPECT_TRUE(set.Insert(extension_b));
  163. EXPECT_EQ(2u, set.size());
  164. }
  165. } // namespace extensions