summaryrefslogtreecommitdiffstats
path: root/extensions/common/manifest_handlers/shared_module_manifest_unittest.cc
blob: 63ff88e381610bad1f0b1201cb1b7c5e25287b04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/macros.h"
#include "base/version.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_handlers/shared_module_info.h"
#include "extensions/common/manifest_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const char* kValidImportPath =
    "_modules/abcdefghijklmnopabcdefghijklmnop/foo/bar.html";
const char* kValidImportPathID = "abcdefghijklmnopabcdefghijklmnop";
const char* kValidImportPathRelative = "foo/bar.html";
const char* kInvalidImportPath = "_modules/abc/foo.html";
const char* kImportId1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const char* kImportId2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
const char* kNoImport = "cccccccccccccccccccccccccccccccc";

}  // namespace

namespace extensions {

class SharedModuleManifestTest : public ManifestTest {
};

TEST_F(SharedModuleManifestTest, ExportsAll) {
  ManifestData manifest("shared_module_export.json");

  scoped_refptr<Extension> extension = LoadAndExpectSuccess(manifest);

  EXPECT_TRUE(SharedModuleInfo::IsSharedModule(extension.get()))
      << manifest.name();
  EXPECT_FALSE(SharedModuleInfo::ImportsModules(extension.get()))
      << manifest.name();

  EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
                  kImportId1)) << manifest.name();
  EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
                  kImportId2)) << manifest.name();
  EXPECT_FALSE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
                  kNoImport)) << manifest.name();
}

TEST_F(SharedModuleManifestTest, ExportWhitelistAll) {
  ManifestData manifest("shared_module_export_no_whitelist.json");

  scoped_refptr<Extension> extension = LoadAndExpectSuccess(manifest);

  EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
                  kImportId1)) << manifest.name();
  EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
                  kImportId2)) << manifest.name();
  EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
                  kNoImport)) << manifest.name();
}

TEST_F(SharedModuleManifestTest, ExportParseErrors) {
  Testcase testcases[] = {
    Testcase("shared_module_export_and_import.json",
             "Simultaneous 'import' and 'export' are not allowed."),
    Testcase("shared_module_export_not_dict.json",
             "Invalid value for 'export'."),
    Testcase("shared_module_export_whitelist_item_not_id.json",
             "Invalid value for 'export.whitelist[0]'."),
    Testcase("shared_module_export_whitelist_item_not_string.json",
             "Invalid value for 'export.whitelist[0]'."),
    Testcase("shared_module_export_whitelist_not_list.json",
             "Invalid value for 'export.whitelist'."),
  };
  RunTestcases(testcases, arraysize(testcases), EXPECT_TYPE_ERROR);
}

TEST_F(SharedModuleManifestTest, SharedModuleStaticFunctions) {
  EXPECT_TRUE(SharedModuleInfo::IsImportedPath(kValidImportPath));
  EXPECT_FALSE(SharedModuleInfo::IsImportedPath(kInvalidImportPath));

  std::string id;
  std::string relative;
  SharedModuleInfo::ParseImportedPath(kValidImportPath, &id, &relative);
  EXPECT_EQ(id, kValidImportPathID);
  EXPECT_EQ(relative, kValidImportPathRelative);
}

TEST_F(SharedModuleManifestTest, Import) {
  ManifestData manifest("shared_module_import.json");

  scoped_refptr<Extension> extension = LoadAndExpectSuccess(manifest);

  EXPECT_FALSE(SharedModuleInfo::IsSharedModule(extension.get()))
      << manifest.name();
  EXPECT_TRUE(SharedModuleInfo::ImportsModules(extension.get()))
      << manifest.name();
  const std::vector<SharedModuleInfo::ImportInfo>& imports =
      SharedModuleInfo::GetImports(extension.get());
  ASSERT_EQ(2U, imports.size());
  EXPECT_EQ(imports[0].extension_id, kImportId1);
  EXPECT_EQ(imports[0].minimum_version, "");
  EXPECT_EQ(imports[1].extension_id, kImportId2);
  EXPECT_TRUE(base::Version(imports[1].minimum_version).IsValid());
  EXPECT_TRUE(
      SharedModuleInfo::ImportsExtensionById(extension.get(), kImportId1));
  EXPECT_TRUE(
      SharedModuleInfo::ImportsExtensionById(extension.get(), kImportId2));
  EXPECT_FALSE(
      SharedModuleInfo::ImportsExtensionById(extension.get(), kNoImport));
}

TEST_F(SharedModuleManifestTest, ImportParseErrors) {
  Testcase testcases[] = {
    Testcase("shared_module_import_not_list.json",
             "Invalid value for 'import'."),
    Testcase("shared_module_import_invalid_id.json",
             "Invalid value for 'import[0].id'."),
    Testcase("shared_module_import_invalid_version.json",
             "Invalid value for 'import[0].minimum_version'."),
  };
  RunTestcases(testcases, arraysize(testcases), EXPECT_TYPE_ERROR);
}

}  // namespace extensions