platform: avoid returning corrupted plugin descriptor

This commit is contained in:
Roman Shevchenko
2014-12-17 17:13:30 +01:00
parent 5b31948e3b
commit be9338022f
3 changed files with 50 additions and 17 deletions
@@ -556,23 +556,27 @@ public class PluginManagerCore {
@Nullable
static IdeaPluginDescriptorImpl loadDescriptorFromDir(@NotNull File file, @NotNull String fileName) {
IdeaPluginDescriptorImpl descriptor = null;
File descriptorFile = new File(file, META_INF + File.separator + fileName);
if (descriptorFile.exists()) {
descriptor = new IdeaPluginDescriptorImpl(file);
try {
IdeaPluginDescriptorImpl descriptor = new IdeaPluginDescriptorImpl(file);
descriptor.readExternal(descriptorFile.toURI().toURL());
return descriptor;
}
catch (Exception e) {
System.err.println("Cannot load: " + descriptorFile.getAbsolutePath());
e.printStackTrace();
catch (XmlSerializationException e) {
getLogger().info("Cannot load " + file, e);
prepareLoadingPluginsErrorMessage("File '" + file.getName() + "' contains invalid plugin descriptor.");
}
catch (Throwable e) {
getLogger().info("Cannot load " + file, e);
}
}
return descriptor;
return null;
}
@Nullable
static IdeaPluginDescriptorImpl loadDescriptorFromJar(@NotNull File file, @NotNull @NonNls String fileName) {
static IdeaPluginDescriptorImpl loadDescriptorFromJar(@NotNull File file, @NotNull String fileName) {
try {
String fileURL = StringUtil.replace(file.toURI().toASCIIString(), "!", "%21");
URL jarURL = new URL("jar:" + fileURL + "!/META-INF/" + fileName);
@@ -593,7 +597,7 @@ public class PluginManagerCore {
}
catch (XmlSerializationException e) {
getLogger().info("Cannot load " + file, e);
prepareLoadingPluginsErrorMessage("Plugin file " + file.getName() + " contains invalid plugin descriptor file.");
prepareLoadingPluginsErrorMessage("File '" + file.getName() + "' contains invalid plugin descriptor.");
}
catch (Throwable e) {
getLogger().info("Cannot load " + file, e);
@@ -607,9 +611,8 @@ public class PluginManagerCore {
return loadDescriptorFromJar(file, PLUGIN_XML);
}
@SuppressWarnings({"HardCodedStringLiteral"})
@Nullable
public static IdeaPluginDescriptorImpl loadDescriptor(final File file, @NonNls final String fileName) {
public static IdeaPluginDescriptorImpl loadDescriptor(@NotNull final File file, @NotNull String fileName) {
IdeaPluginDescriptorImpl descriptor = null;
if (file.isDirectory()) {
@@ -1,21 +1,50 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.ide.plugins;
import com.intellij.openapi.application.ex.PathManagerEx;
import junit.framework.TestCase;
import org.junit.Test;
import java.io.File;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author Dmitry Avdeev
* Date: 7/14/11
*/
public class PluginDescriptorTest extends TestCase {
public class PluginDescriptorTest {
private static String getTestDataPath() {
return PathManagerEx.getTestDataPath() + "/ide/plugins/pluginDescriptor";
}
public void testDescriptorLoading() throws Exception {
String path = PathManagerEx.getTestDataPath().replace(File.separatorChar, '/') + "/ide/plugins/pluginDescriptor";
File file = new File(path + "/asp.jar");
@Test
public void testDescriptorLoading() {
File file = new File(getTestDataPath(), "asp.jar");
assertTrue(file + " not exist", file.exists());
IdeaPluginDescriptorImpl descriptor = PluginManager.loadDescriptorFromJar(file);
IdeaPluginDescriptorImpl descriptor = PluginManagerCore.loadDescriptorFromJar(file);
assertNotNull(descriptor);
}
@Test
public void testInvalidFileDescriptor() {
File file = new File(getTestDataPath(), "malformed");
assertTrue(file + " not exist", file.exists());
IdeaPluginDescriptorImpl descriptor = PluginManagerCore.loadDescriptorFromDir(file, PluginManagerCore.PLUGIN_XML);
assertNull(descriptor);
}
}