use of org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator 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());
}
}
use of org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator 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);
}
}
}
}
use of org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator 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;
}
use of org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator 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());
}
}
use of org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator 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);
}
Aggregations