summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/extension_util_unittest.cc
blob: 261f2f71c1699d4504a187c528b47c3b1cbda85c (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) 2011 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 "chrome/browser/sync/glue/extension_util.h"

#include "base/file_path.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_sync_data.h"
#include "chrome/browser/sync/protocol/extension_specifics.pb.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace browser_sync {

namespace {

#if defined(OS_WIN)
const FilePath::CharType kExtensionFilePath[] = FILE_PATH_LITERAL("c:\\foo");
#elif defined(OS_POSIX)
const FilePath::CharType kExtensionFilePath[] = FILE_PATH_LITERAL("/foo");
#endif

const char kValidId[] = "abcdefghijklmnopabcdefghijklmnop";
const char kValidVersion[] = "0.0.0.0";
const char kVersion1[] = "1.0.0.1";
const char kVersion2[] = "1.0.1.0";
const char kVersion3[] = "1.1.0.0";
const char kValidUpdateUrl1[] =
    "http://clients2.google.com/service/update2/crx";
const char kValidUpdateUrl2[] =
    "https://clients2.google.com/service/update2/crx";
const char kName[] = "MyExtension";
const char kName2[] = "MyExtension2";

class ExtensionUtilTest : public testing::Test {
};

scoped_refptr<Extension> MakeExtension(
    bool is_theme, const GURL& update_url,
    const GURL& launch_url,
    bool converted_from_user_script,
    Extension::Location location, int num_plugins,
    const FilePath& extension_path) {
  DictionaryValue source;
  source.SetString(extension_manifest_keys::kName,
                   "PossiblySyncableExtension");
  source.SetString(extension_manifest_keys::kVersion, "0.0.0.0");
  if (is_theme) {
    source.Set(extension_manifest_keys::kTheme, new DictionaryValue());
  }
  if (!update_url.is_empty()) {
    source.SetString(extension_manifest_keys::kUpdateURL,
                     update_url.spec());
  }
  if (!launch_url.is_empty()) {
    source.SetString(extension_manifest_keys::kLaunchWebURL,
                     launch_url.spec());
  }
  if (!is_theme) {
    source.SetBoolean(extension_manifest_keys::kConvertedFromUserScript,
                      converted_from_user_script);
    ListValue* plugins = new ListValue();
    for (int i = 0; i < num_plugins; ++i) {
      DictionaryValue* plugin = new DictionaryValue();
      plugin->SetString(extension_manifest_keys::kPluginsPath, "");
      plugins->Set(i, plugin);
    }
    source.Set(extension_manifest_keys::kPlugins, plugins);
  }

  std::string error;
  scoped_refptr<Extension> extension = Extension::Create(
      extension_path, location, source, Extension::STRICT_ERROR_CHECKS, &error);
#if defined(OS_CHROMEOS)
  if (num_plugins > 0) {  // plugins are illegal in extensions on chrome os.
    EXPECT_FALSE(extension);
    return NULL;
  }
#endif
  EXPECT_TRUE(extension);
  EXPECT_EQ("", error);
  return extension;
}

TEST_F(ExtensionUtilTest, IsExtensionValid) {
  {
    FilePath file_path(kExtensionFilePath);
    scoped_refptr<Extension> extension(
        MakeExtension(false, GURL(), GURL(), false,
                      Extension::INTERNAL, 0, file_path));
    EXPECT_TRUE(IsExtensionValid(*extension));
  }
  {
    FilePath file_path(kExtensionFilePath);
    scoped_refptr<Extension> extension(
        MakeExtension(false, GURL(kValidUpdateUrl1), GURL(),
                      true, Extension::INTERNAL, 0, file_path));
    EXPECT_TRUE(IsExtensionValid(*extension));
  }
  {
    FilePath file_path(kExtensionFilePath);
    scoped_refptr<Extension> extension(
        MakeExtension(false, GURL(), GURL(), true,
                      Extension::INTERNAL, 0, file_path));
    EXPECT_TRUE(IsExtensionValid(*extension));
  }
  {
    FilePath file_path(kExtensionFilePath);
    scoped_refptr<Extension> extension(
        MakeExtension(true, GURL(), GURL(), false,
                      Extension::INTERNAL, 0, file_path));
    EXPECT_TRUE(IsExtensionValid(*extension));
  }
  {
    FilePath file_path(kExtensionFilePath);
    scoped_refptr<Extension> extension(
        MakeExtension(false, GURL(),
                      GURL("http://www.google.com"), false,
                      Extension::INTERNAL, 0, file_path));
    EXPECT_TRUE(IsExtensionValid(*extension));
  }
  {
    FilePath file_path(kExtensionFilePath);
    scoped_refptr<Extension> extension(
        MakeExtension(false, GURL(), GURL(), false,
                      Extension::EXTERNAL_PREF, 0, file_path));
    EXPECT_FALSE(IsExtensionValid(*extension));
  }
  {
    FilePath file_path(kExtensionFilePath);
    scoped_refptr<Extension> extension(
        MakeExtension(
            false, GURL("http://third-party.update_url.com"), GURL(), true,
            Extension::INTERNAL, 0, file_path));
    EXPECT_FALSE(IsExtensionValid(*extension));
  }
  {
    FilePath file_path(kExtensionFilePath);
    scoped_refptr<Extension> extension(
        MakeExtension(false, GURL(), GURL(), true,
                      Extension::INTERNAL, 1, file_path));
    EXPECT_FALSE(extension && IsExtensionValid(*extension));
  }
  {
    FilePath file_path(kExtensionFilePath);
    scoped_refptr<Extension> extension(
        MakeExtension(false, GURL(), GURL(), true,
                      Extension::INTERNAL, 2, file_path));
    EXPECT_FALSE(extension && IsExtensionValid(*extension));
  }
}

TEST_F(ExtensionUtilTest, SpecificsToSyncData) {
  sync_pb::ExtensionSpecifics specifics;
  specifics.set_id(kValidId);
  specifics.set_update_url(kValidUpdateUrl2);
  specifics.set_enabled(false);
  specifics.set_incognito_enabled(true);
  specifics.set_version(kVersion1);
  specifics.set_name(kName);

  ExtensionSyncData sync_data;
  EXPECT_TRUE(SpecificsToSyncData(specifics, &sync_data));
  EXPECT_EQ(specifics.id(), sync_data.id);
  EXPECT_EQ(specifics.version(), sync_data.version.GetString());
  EXPECT_EQ(specifics.update_url(), sync_data.update_url.spec());
  EXPECT_EQ(specifics.enabled(), sync_data.enabled);
  EXPECT_EQ(specifics.incognito_enabled(), sync_data.incognito_enabled);
  EXPECT_EQ(specifics.name(), sync_data.name);
}

TEST_F(ExtensionUtilTest, SyncDataToSpecifics) {
  ExtensionSyncData sync_data;
  sync_data.id = kValidId;
  sync_data.update_url = GURL(kValidUpdateUrl2);
  sync_data.enabled = true;
  sync_data.incognito_enabled = false;
  {
    scoped_ptr<Version> version(Version::GetVersionFromString(kVersion1));
    sync_data.version = *version;
  }
  sync_data.name = kName;

  sync_pb::ExtensionSpecifics specifics;
  SyncDataToSpecifics(sync_data, &specifics);
  EXPECT_EQ(sync_data.id, specifics.id());
  EXPECT_EQ(sync_data.update_url.spec(), specifics.update_url());
  EXPECT_EQ(sync_data.enabled, specifics.enabled());
  EXPECT_EQ(sync_data.incognito_enabled, specifics.incognito_enabled());
  EXPECT_EQ(sync_data.version.GetString(), specifics.version());
  EXPECT_EQ(sync_data.name, specifics.name());
}

}  // namespace

}  // namespace browser_sync