Path: blob/main/src/vs/platform/extensionManagement/test/common/allowedExtensionsService.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as assert from 'assert';6import { AllowedExtensionsService } from '../../common/allowedExtensionsService.js';7import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';8import { IProductService } from '../../../product/common/productService.js';9import { TestConfigurationService } from '../../../configuration/test/common/testConfigurationService.js';10import { AllowedExtensionsConfigKey, IGalleryExtension, ILocalExtension } from '../../common/extensionManagement.js';11import { ExtensionType, IExtensionManifest, TargetPlatform } from '../../../extensions/common/extensions.js';12import { Event } from '../../../../base/common/event.js';13import { ConfigurationTarget } from '../../../configuration/common/configuration.js';14import { getGalleryExtensionId } from '../../common/extensionManagementUtil.js';15import { generateUuid } from '../../../../base/common/uuid.js';16import { URI } from '../../../../base/common/uri.js';1718suite('AllowedExtensionsService', () => {1920const disposables = ensureNoDisposablesAreLeakedInTestSuite();2122const configurationService = new TestConfigurationService();2324setup(() => {25configurationService.setUserConfiguration(AllowedExtensionsConfigKey, '*');26});2728test('should allow all extensions if no allowed extensions are configured', () => {29const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));30assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, true);31});3233test('should not allow specific extension if not in allowed list', () => {34configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': false });35const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));36assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, false);37});3839test('should allow specific extension if in allowed list', () => {40configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': true });41const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));42assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, true);43});4445test('should not allow pre-release extension if only stable is allowed', () => {46configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': 'stable' });47const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));48assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, prerelease: true }) === true, false);49});5051test('should allow pre-release extension if pre-release is allowed', () => {52configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': true });53const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));54assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, prerelease: true }) === true, true);55});5657test('should allow specific version of an extension when configured to that version', () => {58configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3'] });59const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));60assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3' }) === true, true);61});6263test('should allow any version of an extension when a specific version is configured', () => {64configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3'] });65const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));66assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, true);67});6869test('should allow any version of an extension when stable is configured', () => {70configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': 'stable' });71const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));72assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, true);73});7475test('should allow a version of an extension when stable is configured', () => {76configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': 'stable' });77const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));78assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3' }) === true, true);79});8081test('should allow a pre-release version of an extension when stable is configured', () => {82configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': 'stable' });83const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));84assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', prerelease: true }) === true, false);85});8687test('should allow specific version of an extension when configured to multiple versions', () => {88configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3', '2.0.1', '3.1.2'] });89const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));90assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3' }) === true, true);91});9293test('should allow platform specific version of an extension when configured to platform specific version', () => {94configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3@darwin-x64'] });95const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));96assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', targetPlatform: TargetPlatform.DARWIN_X64 }) === true, true);97});9899test('should allow universal platform specific version of an extension when configured to platform specific version', () => {100configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3@darwin-x64'] });101const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));102assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', targetPlatform: TargetPlatform.UNIVERSAL }) === true, true);103});104105test('should allow specific version of an extension when configured to platform specific version', () => {106configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3@darwin-x64'] });107const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));108assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3' }) === true, true);109});110111test('should allow platform specific version of an extension when configured to multiple versions', () => {112configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.0.0', '1.2.3@darwin-x64', '1.2.3@darwin-arm64'] });113const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));114assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', targetPlatform: TargetPlatform.DARWIN_X64 }) === true, true);115});116117test('should not allow platform specific version of an extension when configured to different platform specific version', () => {118configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.2.3@darwin-x64'] });119const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));120assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.2.3', targetPlatform: TargetPlatform.DARWIN_ARM64 }) === true, false);121});122123test('should specific version of an extension when configured to different versions', () => {124configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test.extension': ['1.0.0', '1.2.3@darwin-x64', '1.2.3@darwin-arm64'] });125const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));126assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, version: '1.0.1' }) === true, false);127});128129test('should allow extension if publisher is in allowed list', () => {130configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test': true });131const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));132assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }), true);133});134135test('should allow extension if publisher is not in allowed list and has publisher mapping', () => {136configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'hello': true });137const testObject = disposables.add(new AllowedExtensionsService(aProductService(['hello']), configurationService));138assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: 'Hello' }), true);139});140141test('should allow extension if publisher is not in allowed list and has different publisher mapping', () => {142configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'hello': true });143const testObject = disposables.add(new AllowedExtensionsService(aProductService(['bar']), configurationService));144assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: 'Hello' }) === true, false);145});146147test('should not allow extension if publisher is not in allowed list', () => {148configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test': false });149const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));150assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, false);151});152153test('should not allow prerelease extension if publisher is allowed only to stable', () => {154configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test': 'stable' });155const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));156assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, prerelease: true }) === true, false);157});158159test('should allow extension if publisher is set to random value', () => {160configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'test': 'hello' });161const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));162assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined, prerelease: true }) === true, true);163});164165test('should allow extension if only wildcard is in allowed list', () => {166configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { '*': true });167const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));168assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }), true);169});170171test('should allow extension if wildcard is in allowed list', () => {172configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { '*': true, 'hello': false });173const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));174assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }), true);175});176177test('should not allow extension if wildcard is not in allowed list', () => {178configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { '*': false, 'hello': true });179const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));180assert.strictEqual(testObject.isAllowed({ id: 'test.extension', publisherDisplayName: undefined }) === true, false);181});182183test('should allow a gallery extension', () => {184configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'pub': true });185const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));186assert.strictEqual(testObject.isAllowed(aGalleryExtension('name')) === true, true);187});188189test('should allow a local extension', () => {190configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { 'pub': true });191const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));192assert.strictEqual(testObject.isAllowed(aLocalExtension('pub.name')) === true, true);193});194195test('should trigger change event when allowed list change', async () => {196configurationService.setUserConfiguration(AllowedExtensionsConfigKey, { '*': false });197const testObject = disposables.add(new AllowedExtensionsService(aProductService(), configurationService));198const promise = Event.toPromise(testObject.onDidChangeAllowedExtensionsConfigValue);199configurationService.onDidChangeConfigurationEmitter.fire({ affectsConfiguration: () => true, affectedKeys: new Set([AllowedExtensionsConfigKey]), change: { keys: [], overrides: [] }, source: ConfigurationTarget.USER });200await promise;201});202203function aProductService(extensionPublisherOrgs?: string[]): IProductService {204return {205_serviceBrand: undefined,206extensionPublisherOrgs207} as IProductService;208}209210function aGalleryExtension(name: string, properties: any = {}, galleryExtensionProperties: any = {}): IGalleryExtension {211const galleryExtension = <IGalleryExtension>Object.create({ type: 'gallery', name, publisher: 'pub', publisherDisplayName: 'Pub', version: '1.0.0', allTargetPlatforms: [TargetPlatform.UNIVERSAL], properties: {}, assets: {}, isSigned: true, ...properties });212galleryExtension.properties = { ...galleryExtension.properties, dependencies: [], ...galleryExtensionProperties };213galleryExtension.identifier = { id: getGalleryExtensionId(galleryExtension.publisher, galleryExtension.name), uuid: generateUuid() };214return <IGalleryExtension>galleryExtension;215}216217function aLocalExtension(id: string, manifest: Partial<IExtensionManifest> = {}, properties: any = {}): ILocalExtension {218const [publisher, name] = id.split('.');219manifest = { name, publisher, ...manifest };220properties = {221identifier: { id },222location: URI.file(`pub.${name}`),223galleryIdentifier: { id, uuid: undefined },224type: ExtensionType.User,225...properties,226isValid: properties.isValid ?? true,227};228properties.isBuiltin = properties.type === ExtensionType.System;229return <ILocalExtension>Object.create({ manifest, ...properties });230}231});232233234