diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2020-03-19 12:41:40 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-03-23 20:03:41 +0000 |
commit | a5e56c8d403ba0bfdb36e94acb8def5ceb065524 (patch) | |
tree | 184bce441b640428b2bc3f1bb6e176e9f8a2d6cd /sonar-plugin-api/src/test/java/org/sonar | |
parent | 0c8d18b4ed3e08536eef559153c62fb80cffa253 (diff) | |
download | sonarqube-a5e56c8d403ba0bfdb36e94acb8def5ceb065524.tar.gz sonarqube-a5e56c8d403ba0bfdb36e94acb8def5ceb065524.zip |
SONAR-13214 Remove org.sonar.api.config.Settings from the API
Diffstat (limited to 'sonar-plugin-api/src/test/java/org/sonar')
3 files changed, 9 insertions, 258 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java index a17031fb5af..ffbb82611e1 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java @@ -22,7 +22,7 @@ package org.sonar.api.batch.bootstrap; import java.io.File; import org.junit.Test; import org.sonar.api.batch.bootstrap.internal.ProjectBuilderContext; -import org.sonar.api.config.Settings; +import org.sonar.api.config.Configuration; import org.sonar.api.config.internal.MapSettings; import static org.assertj.core.api.Assertions.assertThat; @@ -36,8 +36,11 @@ public class ProjectBuilderTest { // this reactor is created and provided by Sonar final ProjectReactor projectReactor = new ProjectReactor(ProjectDefinition.create()); - ProjectBuilder builder = new ProjectBuilderSample(new MapSettings()); - builder.build(new ProjectBuilderContext(projectReactor)); + ProjectBuilder builder = new ProjectBuilderSample(); + final MapSettings settings = new MapSettings(); + settings.setProperty("foo", "bar"); + final Configuration configuration = settings.asConfig(); + builder.build(new ProjectBuilderContext(projectReactor, configuration)); assertThat(projectReactor.getProjects().size(), is(2)); ProjectDefinition root = projectReactor.getRoot(); @@ -47,17 +50,12 @@ public class ProjectBuilderTest { } final static class ProjectBuilderSample extends ProjectBuilder { - private Settings conf; - - public ProjectBuilderSample(Settings conf) { - // A real implementation should for example use the settings - this.conf = conf; - } @Override - protected void build(ProjectReactor reactor) { + public void build(Context context) { + assertThat(context.config().get("foo")).contains("bar"); // change name of root project - ProjectDefinition root = reactor.getRoot(); + ProjectDefinition root = context.projectReactor().getRoot(); root.setName("Name changed by plugin"); // add sub-project diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/AesCipherTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/AesCipherTest.java deleted file mode 100644 index ce1f64ae858..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/AesCipherTest.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2020 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.config; - -import org.apache.commons.codec.binary.Base64; -import org.apache.commons.lang.StringUtils; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import javax.crypto.BadPaddingException; - -import java.io.File; -import java.net.URL; -import java.security.InvalidKeyException; -import java.security.Key; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; - -public class AesCipherTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void generateRandomSecretKey() { - AesCipher cipher = new AesCipher(null); - - String key = cipher.generateRandomSecretKey(); - - assertThat(StringUtils.isNotBlank(key)).isTrue(); - assertThat(Base64.isArrayByteBase64(key.getBytes())).isTrue(); - } - - @Test - public void encrypt() throws Exception { - AesCipher cipher = new AesCipher(pathToSecretKey()); - - String encryptedText = cipher.encrypt("this is a secret"); - - assertThat(StringUtils.isNotBlank(encryptedText)).isTrue(); - assertThat(Base64.isArrayByteBase64(encryptedText.getBytes())).isTrue(); - } - - @Test - public void encrypt_bad_key() throws Exception { - thrown.expect(RuntimeException.class); - thrown.expectMessage("Invalid AES key"); - - URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/bad_secret_key.txt"); - AesCipher cipher = new AesCipher(new File(resource.toURI()).getCanonicalPath()); - - cipher.encrypt("this is a secret"); - } - - @Test - public void decrypt() throws Exception { - AesCipher cipher = new AesCipher(pathToSecretKey()); - - // the following value has been encrypted with the key /org/sonar/api/config/AesCipherTest/aes_secret_key.txt - String clearText = cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY="); - - assertThat(clearText).isEqualTo("this is a secret"); - } - - @Test - public void decrypt_bad_key() throws Exception { - URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/bad_secret_key.txt"); - AesCipher cipher = new AesCipher(new File(resource.toURI()).getCanonicalPath()); - - try { - cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY="); - fail(); - - } catch (RuntimeException e) { - assertThat(e.getCause()).isInstanceOf(InvalidKeyException.class); - } - } - - @Test - public void decrypt_other_key() throws Exception { - URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/other_secret_key.txt"); - AesCipher cipher = new AesCipher(new File(resource.toURI()).getCanonicalPath()); - - try { - // text encrypted with another key - cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY="); - fail(); - - } catch (RuntimeException e) { - assertThat(e.getCause()).isInstanceOf(BadPaddingException.class); - } - } - - @Test - public void encryptThenDecrypt() throws Exception { - AesCipher cipher = new AesCipher(pathToSecretKey()); - - assertThat(cipher.decrypt(cipher.encrypt("foo"))).isEqualTo("foo"); - } - - @Test - public void testDefaultPathToSecretKey() { - AesCipher cipher = new AesCipher(null); - - String path = cipher.getPathToSecretKey(); - - assertThat(StringUtils.isNotBlank(path)).isTrue(); - assertThat(new File(path).getName()).isEqualTo("sonar-secret.txt"); - } - - @Test - public void loadSecretKeyFromFile() throws Exception { - AesCipher cipher = new AesCipher(null); - Key secretKey = cipher.loadSecretFileFromFile(pathToSecretKey()); - assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); - assertThat(secretKey.getEncoded().length).isGreaterThan(10); - } - - @Test - public void loadSecretKeyFromFile_trim_content() throws Exception { - URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/non_trimmed_secret_key.txt"); - String path = new File(resource.toURI()).getCanonicalPath(); - AesCipher cipher = new AesCipher(null); - - Key secretKey = cipher.loadSecretFileFromFile(path); - - assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); - assertThat(secretKey.getEncoded().length).isGreaterThan(10); - } - - @Test - public void loadSecretKeyFromFile_file_does_not_exist() throws Exception { - thrown.expect(IllegalStateException.class); - - AesCipher cipher = new AesCipher(null); - cipher.loadSecretFileFromFile("/file/does/not/exist"); - } - - @Test - public void loadSecretKeyFromFile_no_property() throws Exception { - thrown.expect(IllegalStateException.class); - - AesCipher cipher = new AesCipher(null); - cipher.loadSecretFileFromFile(null); - } - - @Test - public void hasSecretKey() throws Exception { - AesCipher cipher = new AesCipher(pathToSecretKey()); - - assertThat(cipher.hasSecretKey()).isTrue(); - } - - @Test - public void doesNotHaveSecretKey() { - AesCipher cipher = new AesCipher("/my/twitter/id/is/SimonBrandhof"); - - assertThat(cipher.hasSecretKey()).isFalse(); - } - - private String pathToSecretKey() throws Exception { - URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/aes_secret_key.txt"); - return new File(resource.toURI()).getCanonicalPath(); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/EncryptionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/EncryptionTest.java deleted file mode 100644 index e73b91fe9c7..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/EncryptionTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2020 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.config; - -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class EncryptionTest { - - @Test - public void isEncrypted() { - Encryption encryption = new Encryption(null); - assertThat(encryption.isEncrypted("{aes}ADASDASAD")).isTrue(); - assertThat(encryption.isEncrypted("{b64}ADASDASAD")).isTrue(); - assertThat(encryption.isEncrypted("{abc}ADASDASAD")).isTrue(); - - assertThat(encryption.isEncrypted("{}")).isFalse(); - assertThat(encryption.isEncrypted("{foo")).isFalse(); - assertThat(encryption.isEncrypted("foo{aes}")).isFalse(); - } - - @Test - public void scramble() { - Encryption encryption = new Encryption(null); - assertThat(encryption.scramble("foo")).isEqualTo("{b64}Zm9v"); - } - - @Test - public void decrypt() { - Encryption encryption = new Encryption(null); - assertThat(encryption.decrypt("{b64}Zm9v")).isEqualTo("foo"); - } - - @Test - public void decrypt_unknown_algorithm() { - Encryption encryption = new Encryption(null); - assertThat(encryption.decrypt("{xxx}Zm9v")).isEqualTo("{xxx}Zm9v"); - } - - @Test - public void decrypt_uncrypted_text() { - Encryption encryption = new Encryption(null); - assertThat(encryption.decrypt("foo")).isEqualTo("foo"); - } -} |