Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/extensionManagement/test/common/allowedExtensionsService.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as assert from 'assert';
7
import { AllowedExtensionsService } from '../../common/allowedExtensionsService.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
9
import { IProductService } from '../../../product/common/productService.js';
10
import { TestConfigurationService } from '../../../configuration/test/common/testConfigurationService.js';
11
import { AllowedExtensionsConfigKey, IGalleryExtension, ILocalExtension } from '../../common/extensionManagement.js';
12
import { ExtensionType, IExtensionManifest, TargetPlatform } from '../../../extensions/common/extensions.js';
13
import { Event } from '../../../../base/common/event.js';
14
import { ConfigurationTarget } from '../../../configuration/common/configuration.js';
15
import { getGalleryExtensionId } from '../../common/extensionManagementUtil.js';
16
import { generateUuid } from '../../../../base/common/uuid.js';
17
import { URI } from '../../../../base/common/uri.js';
18
19
suite('AllowedExtensionsService', () => {
20
21
const disposables = ensureNoDisposablesAreLeakedInTestSuite();
22
23
const configurationService = new TestConfigurationService();
24
25
setup(() => {
26
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, '*');
27
});
28
29
test('should allow all extensions if no allowed extensions are configured', () => {
30
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
31
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, true);
32
});
33
34
test('should not allow specific extension if not in allowed list', () => {
35
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': false });
36
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
37
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, false);
38
});
39
40
test('should allow specific extension if in allowed list', () => {
41
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': true });
42
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
43
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, true);
44
});
45
46
test('should not allow pre-release extension if only stable is allowed', () => {
47
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': 'stable' });
48
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
49
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, prerelease: true }) === true, false);
50
});
51
52
test('should allow pre-release extension if pre-release is allowed', () => {
53
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': true });
54
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
55
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, prerelease: true }) === true, true);
56
});
57
58
test('should allow specific version of an extension when configured to that version', () => {
59
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3'] });
60
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
61
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3' }) === true, true);
62
});
63
64
test('should allow any version of an extension when a specific version is configured', () => {
65
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3'] });
66
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
67
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, true);
68
});
69
70
test('should allow any version of an extension when stable is configured', () => {
71
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': 'stable' });
72
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
73
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, true);
74
});
75
76
test('should allow a version of an extension when stable is configured', () => {
77
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': 'stable' });
78
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
79
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3' }) === true, true);
80
});
81
82
test('should allow a pre-release version of an extension when stable is configured', () => {
83
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': 'stable' });
84
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
85
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', prerelease: true }) === true, false);
86
});
87
88
test('should allow specific version of an extension when configured to multiple versions', () => {
89
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3', '2.0.1', '3.1.2'] });
90
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
91
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3' }) === true, true);
92
});
93
94
test('should allow platform specific version of an extension when configured to platform specific version', () => {
95
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3@darwin-x64'] });
96
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
97
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', targetPlatform: TargetPlatform.DARWIN_X64 }) === true, true);
98
});
99
100
test('should allow universal platform specific version of an extension when configured to platform specific version', () => {
101
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3@darwin-x64'] });
102
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
103
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', targetPlatform: TargetPlatform.UNIVERSAL }) === true, true);
104
});
105
106
test('should allow specific version of an extension when configured to platform specific version', () => {
107
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3@darwin-x64'] });
108
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
109
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3' }) === true, true);
110
});
111
112
test('should allow platform specific version of an extension when configured to multiple versions', () => {
113
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.0.0', '1.2.3@darwin-x64', '1.2.3@darwin-arm64'] });
114
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
115
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', targetPlatform: TargetPlatform.DARWIN_X64 }) === true, true);
116
});
117
118
test('should not allow platform specific version of an extension when configured to different platform specific version', () => {
119
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3@darwin-x64'] });
120
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
121
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', targetPlatform: TargetPlatform.DARWIN_ARM64 }) === true, false);
122
});
123
124
test('should specific version of an extension when configured to different versions', () => {
125
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.0.0', '1.2.3@darwin-x64', '1.2.3@darwin-arm64'] });
126
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
127
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.0.1' }) === true, false);
128
});
129
130
test('should allow extension if publisher is in allowed list', () => {
131
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test': true });
132
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
133
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }), true);
134
});
135
136
test('should allow extension if publisher is not in allowed list and has publisher mapping', () => {
137
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'hello': true });
138
const testObject = disposables.add(new AllowedExtensionsService(aProductService(['hello']), configurationService));
139
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: 'Hello' }), true);
140
});
141
142
test('should allow extension if publisher is not in allowed list and has different publisher mapping', () => {
143
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'hello': true });
144
const testObject = disposables.add(new AllowedExtensionsService(aProductService(['bar']), configurationService));
145
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: 'Hello' }) === true, false);
146
});
147
148
test('should not allow extension if publisher is not in allowed list', () => {
149
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test': false });
150
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
151
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, false);
152
});
153
154
test('should not allow prerelease extension if publisher is allowed only to stable', () => {
155
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test': 'stable' });
156
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
157
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, prerelease: true }) === true, false);
158
});
159
160
test('should allow extension if publisher is set to random value', () => {
161
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test': 'hello' });
162
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
163
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, prerelease: true }) === true, true);
164
});
165
166
test('should allow extension if only wildcard is in allowed list', () => {
167
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { '*': true });
168
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
169
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }), true);
170
});
171
172
test('should allow extension if wildcard is in allowed list', () => {
173
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { '*': true, 'hello': false });
174
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
175
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }), true);
176
});
177
178
test('should not allow extension if wildcard is not in allowed list', () => {
179
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { '*': false, 'hello': true });
180
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
181
assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, false);
182
});
183
184
test('should allow a gallery extension', () => {
185
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'pub': true });
186
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
187
assert.strictEqual(testObject.isAllowed(aGalleryExtension('name')) === true, true);
188
});
189
190
test('should allow a local extension', () => {
191
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'pub': true });
192
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
193
assert.strictEqual(testObject.isAllowed(aLocalExtension('pub.name')) === true, true);
194
});
195
196
test('should trigger change event when allowed list change', async () => {
197
configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { '*': false });
198
const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));
199
const promise = Event.toPromise(testObject.onDidChangeAllowedExtensionsConfigValue);
200
configurationService.onDidChangeConfigurationEmitter.fire({ affectsConfiguration: () => true, affectedKeys: new Set([AllowedExtensionsConfigKey]), change: { keys: [], overrides: [] }, source: ConfigurationTarget.USER });
201
await promise;
202
});
203
204
function aProductService(extensionPublisherOrgs?: string[]): IProductService {
205
return {
206
_serviceBrand: undefined,
207
extensionPublisherOrgs
208
} as IProductService;
209
}
210
211
function aGalleryExtension(name: string, properties: any = {}, galleryExtensionProperties: any = {}): IGalleryExtension {
212
const galleryExtension = <IGalleryExtension>Object.create({ type: 'gallery', name, publisher: 'pub', publisherDisplayName: 'Pub', version: '1.0.0', allTargetPlatforms: [TargetPlatform.UNIVERSAL], properties: {}, assets: {}, isSigned: true, ...properties });
213
galleryExtension.properties = { ...galleryExtension.properties, dependencies: [], ...galleryExtensionProperties };
214
galleryExtension.identifier = { id: getGalleryExtensionId(galleryExtension.publisher, galleryExtension.name), uuid: generateUuid() };
215
return <IGalleryExtension>galleryExtension;
216
}
217
218
function aLocalExtension(id: string, manifest: Partial<IExtensionManifest> = {}, properties: any = {}): ILocalExtension {
219
const [publisher, name] = id.split('.');
220
manifest = { name, publisher, ...manifest };
221
properties = {
222
identifier: { id },
223
location: URI.file(`pub.${name}`),
224
galleryIdentifier: { id, uuid: undefined },
225
type: ExtensionType.User,
226
...properties,
227
isValid: properties.isValid ?? true,
228
};
229
properties.isBuiltin = properties.type === ExtensionType.System;
230
return <ILocalExtension>Object.create({ manifest, ...properties });
231
}
232
});
233
234