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