Search in sources :

Example 1 with ValidationResult

use of org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult in project eclipse.jdt.ls by eclipse.

the class WrapperValidatorTest method testMissingSha256.

@Test
public void testMissingSha256() throws Exception {
    WrapperValidator wrapperValidator = new WrapperValidator(100);
    Set<String> allowed = WrapperValidator.getAllowed();
    Set<String> disallowed = WrapperValidator.getDisallowed();
    File file = new File(getSourceProjectDirectory(), "gradle/gradle-4.0");
    wrapperValidator.checkWrapper(file.getAbsolutePath());
    int size = WrapperValidator.size();
    List<String> sha256 = new ArrayList<>();
    try {
        sha256.add("41c8aa7a337a44af18d8cda0d632ebba469aef34f3041827624ef5c1a4e4419d");
        WrapperValidator.clear();
        WrapperValidator.disallow(sha256);
        assertTrue(file.isDirectory());
        ValidationResult result = wrapperValidator.checkWrapper(file.getAbsolutePath());
        assertFalse(result.isValid());
        assertNotNull(result.getChecksum());
        WrapperValidator.clear();
        WrapperValidator.allow(sha256);
        result = wrapperValidator.checkWrapper(file.getAbsolutePath());
        assertTrue(result.isValid());
    } finally {
        WrapperValidator.clear();
        WrapperValidator.allow(allowed);
        WrapperValidator.disallow(disallowed);
        wrapperValidator.checkWrapper(file.getAbsolutePath());
        assertEquals(size, WrapperValidator.size());
    }
}
Also used : WrapperValidator(org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator) ArrayList(java.util.ArrayList) ValidationResult(org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult) File(java.io.File) Test(org.junit.Test)

Example 2 with ValidationResult

use of org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult in project eclipse.jdt.ls by eclipse.

the class GradlePreferenceChangeListener method updateProject.

private void updateProject(ProjectsManager projectsManager, IProject project, boolean gradleJavaHomeChanged) {
    String projectDir = project.getLocation().toFile().getAbsolutePath();
    Path projectPath = Paths.get(projectDir);
    if (gradleJavaHomeChanged || Files.exists(projectPath.resolve("gradlew"))) {
        ProjectConfiguration configuration = CorePlugin.configurationManager().loadProjectConfiguration(project);
        GradleDistribution distribution = configuration.getBuildConfiguration().getGradleDistribution();
        if (gradleJavaHomeChanged || !(distribution instanceof WrapperGradleDistribution)) {
            projectsManager.updateProject(project, true);
        } else {
            try {
                ValidationResult result = new WrapperValidator().checkWrapper(projectDir);
                if (!result.isValid()) {
                    projectsManager.updateProject(project, true);
                }
            } catch (CoreException e) {
                JavaLanguageServerPlugin.logException(e.getMessage(), e);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) GradleDistribution(org.eclipse.buildship.core.GradleDistribution) CoreException(org.eclipse.core.runtime.CoreException) WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) WrapperValidator(org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator) ValidationResult(org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult) ProjectConfiguration(org.eclipse.buildship.core.internal.configuration.ProjectConfiguration)

Example 3 with ValidationResult

use of org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult in project eclipse.jdt.ls by eclipse.

the class GradleProjectImporter method getGradleDistribution.

public static GradleDistribution getGradleDistribution(Path rootFolder) {
    PreferenceManager preferencesManager = JavaLanguageServerPlugin.getPreferencesManager();
    Preferences preferences = getPreferences();
    if (preferencesManager != null && preferences.isGradleWrapperEnabled() && Files.exists(rootFolder.resolve("gradlew"))) {
        WrapperValidator validator = new WrapperValidator();
        try {
            ValidationResult result = validator.checkWrapper(rootFolder.toFile().getAbsolutePath());
            if (result.isValid()) {
                WrapperGradleDistribution gradleDistribution = GradleDistribution.fromBuild();
                return gradleDistribution;
            } else {
                if (!WrapperValidator.contains(result.getChecksum())) {
                    ProjectsManager pm = JavaLanguageServerPlugin.getProjectsManager();
                    if (pm != null && pm.getConnection() != null) {
                        if (preferencesManager.getClientPreferences().isGradleChecksumWrapperPromptSupport()) {
                            String id = "gradle/checksum/prompt";
                            ExecuteCommandParams params = new ExecuteCommandParams(id, asList(result.getWrapperJar(), result.getChecksum()));
                            pm.getConnection().sendNotification(params);
                        } else {
                            // @formatter:off
                            String message = GRADLE_WRAPPER_CHEKSUM_WARNING_TEMPLATE.replaceAll("@wrapper@", result.getWrapperJar()).replaceAll("@checksum@", result.getChecksum());
                            // @formatter:on
                            pm.getConnection().showMessage(new MessageParams(MessageType.Error, message));
                        }
                    }
                }
            }
        } catch (CoreException e) {
            JavaLanguageServerPlugin.logException(e.getMessage(), e);
        }
    }
    if (StringUtils.isNotBlank(preferences.getGradleVersion())) {
        List<GradleVersion> versions = CorePlugin.publishedGradleVersions().getVersions();
        GradleVersion gradleVersion = null;
        String versionString = preferences.getGradleVersion();
        GradleVersion requiredVersion = GradleVersion.version(versionString);
        for (GradleVersion version : versions) {
            if (version.compareTo(requiredVersion) == 0) {
                gradleVersion = version;
            }
        }
        if (gradleVersion != null) {
            return GradleDistribution.forVersion(gradleVersion.getVersion());
        } else {
            JavaLanguageServerPlugin.logInfo("Invalid gradle version" + versionString);
        }
    }
    File gradleHomeFile = getGradleHomeFile();
    if (gradleHomeFile != null) {
        return GradleDistribution.forLocalInstallation(gradleHomeFile);
    }
    return DEFAULT_DISTRIBUTION;
}
Also used : WrapperGradleDistribution(org.eclipse.buildship.core.WrapperGradleDistribution) WrapperValidator(org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator) MessageParams(org.eclipse.lsp4j.MessageParams) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) ValidationResult(org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) CoreException(org.eclipse.core.runtime.CoreException) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) GradleVersion(org.eclipse.buildship.core.internal.util.gradle.GradleVersion) File(java.io.File)

Example 4 with ValidationResult

use of org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult in project eclipse.jdt.ls by eclipse.

the class WrapperValidatorTest method testPreferences.

@Test
public void testPreferences() throws Exception {
    WrapperValidator wrapperValidator = new WrapperValidator(100);
    Set<String> allowed = WrapperValidator.getAllowed();
    Set<String> disallowed = WrapperValidator.getDisallowed();
    File file = new File(getSourceProjectDirectory(), "gradle/gradle-4.0");
    wrapperValidator.checkWrapper(file.getAbsolutePath());
    int size = WrapperValidator.size();
    List list = new ArrayList();
    Map map = new HashMap();
    map.put("sha256", "41c8aa7a337a44af18d8cda0d632ebba469aef34f3041827624ef5c1a4e4419d");
    map.put("allowed", Boolean.TRUE);
    list.add(map);
    try {
        ValidationResult result = wrapperValidator.checkWrapper(file.getAbsolutePath());
        assertFalse(result.isValid());
        assertNotNull(result.getChecksum());
        WrapperValidator.clear();
        WrapperValidator.putSha256(list);
        result = wrapperValidator.checkWrapper(file.getAbsolutePath());
        assertTrue(result.isValid());
    } finally {
        WrapperValidator.clear();
        WrapperValidator.allow(allowed);
        WrapperValidator.disallow(disallowed);
        wrapperValidator.checkWrapper(file.getAbsolutePath());
        assertEquals(size, WrapperValidator.size());
    }
}
Also used : HashMap(java.util.HashMap) WrapperValidator(org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ValidationResult(org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with ValidationResult

use of org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult in project eclipse.jdt.ls by eclipse.

the class WrapperValidatorTest method testGradleWrapper.

@Test
public void testGradleWrapper() throws Exception {
    File file = new File(getSourceProjectDirectory(), "gradle/simple-gradle");
    assertTrue(file.isDirectory());
    File sha256Directory = WrapperValidator.getSha256CacheFile();
    assertTrue(sha256Directory.isDirectory());
    ValidationResult result = new WrapperValidator(100).checkWrapper(file.getAbsolutePath());
    assertTrue(result.isValid());
    // test cache
    assertTrue(sha256Directory.isDirectory());
    String fileName = "gradle-6.3-wrapper.jar.sha256";
    Bundle bundle = Platform.getBundle(IConstants.PLUGIN_ID);
    URL url = FileLocator.find(bundle, new org.eclipse.core.runtime.Path(WrapperValidator.GRADLE_CHECKSUMS));
    String sha256 = null;
    if (url == null) {
        String message = Files.list(Paths.get(sha256Directory.getAbsolutePath())).collect(Collectors.toList()).toString();
        file = new File(sha256Directory, fileName);
        if (file.isFile()) {
            assertTrue(message, file.isFile());
            sha256 = Files.lines(Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8).findFirst().get();
        }
    } else {
        try (InputStream inputStream = url.openStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            Reader reader = new BufferedReader(inputStreamReader)) {
            JsonElement jsonElement = new JsonParser().parse(reader);
            if (jsonElement instanceof JsonArray) {
                JsonArray array = (JsonArray) jsonElement;
                for (JsonElement json : array) {
                    String wrapperChecksumUrl = json.getAsJsonObject().get("wrapperChecksumUrl").getAsString();
                    if (wrapperChecksumUrl != null && wrapperChecksumUrl.endsWith("/" + fileName)) {
                        sha256 = json.getAsJsonObject().get("sha256").getAsString();
                        break;
                    }
                }
            }
        } catch (IOException e) {
            JavaLanguageServerPlugin.logException(e.getMessage(), e);
        }
    }
    assertEquals("1cef53de8dc192036e7b0cc47584449b0cf570a00d560bfaa6c9eabe06e1fc06", sha256);
}
Also used : InputStreamReader(java.io.InputStreamReader) Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) WrapperValidator(org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ValidationResult(org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult) URL(java.net.URL) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) BufferedReader(java.io.BufferedReader) File(java.io.File) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Aggregations

ValidationResult (org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult)5 WrapperValidator (org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator)5 File (java.io.File)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 WrapperGradleDistribution (org.eclipse.buildship.core.WrapperGradleDistribution)2 CoreException (org.eclipse.core.runtime.CoreException)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1