You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AesCipherTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.process;
  21. import com.google.common.io.Resources;
  22. import org.apache.commons.codec.binary.Base64;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import javax.crypto.BadPaddingException;
  28. import java.io.File;
  29. import java.security.InvalidKeyException;
  30. import java.security.Key;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.junit.Assert.fail;
  33. public class AesCipherTest {
  34. @Rule
  35. public ExpectedException thrown = ExpectedException.none();
  36. @Test
  37. public void generateRandomSecretKey() {
  38. AesCipher cipher = new AesCipher(null);
  39. String key = cipher.generateRandomSecretKey();
  40. assertThat(StringUtils.isNotBlank(key)).isTrue();
  41. assertThat(Base64.isBase64(key.getBytes())).isTrue();
  42. }
  43. @Test
  44. public void encrypt() {
  45. AesCipher cipher = new AesCipher(pathToSecretKey());
  46. String encryptedText = cipher.encrypt("this is a secret");
  47. assertThat(StringUtils.isNotBlank(encryptedText)).isTrue();
  48. assertThat(Base64.isBase64(encryptedText.getBytes())).isTrue();
  49. }
  50. @Test
  51. public void encrypt_bad_key() {
  52. thrown.expect(RuntimeException.class);
  53. thrown.expectMessage("Invalid AES key");
  54. AesCipher cipher = new AesCipher(getPath("bad_secret_key.txt"));
  55. cipher.encrypt("this is a secret");
  56. }
  57. @Test
  58. public void decrypt() {
  59. AesCipher cipher = new AesCipher(pathToSecretKey());
  60. // the following value has been encrypted with the key /org/sonar/api/config/AesCipherTest/aes_secret_key.txt
  61. String clearText = cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY=");
  62. assertThat(clearText).isEqualTo("this is a secret");
  63. }
  64. @Test
  65. public void decrypt_bad_key() {
  66. AesCipher cipher = new AesCipher(getPath("bad_secret_key.txt"));
  67. try {
  68. cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY=");
  69. fail();
  70. } catch (RuntimeException e) {
  71. assertThat(e.getCause()).isInstanceOf(InvalidKeyException.class);
  72. }
  73. }
  74. @Test
  75. public void decrypt_other_key() {
  76. AesCipher cipher = new AesCipher(getPath("other_secret_key.txt"));
  77. try {
  78. // text encrypted with another key
  79. cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY=");
  80. fail();
  81. } catch (RuntimeException e) {
  82. assertThat(e.getCause()).isInstanceOf(BadPaddingException.class);
  83. }
  84. }
  85. @Test
  86. public void encryptThenDecrypt() {
  87. AesCipher cipher = new AesCipher(pathToSecretKey());
  88. assertThat(cipher.decrypt(cipher.encrypt("foo"))).isEqualTo("foo");
  89. }
  90. @Test
  91. public void testDefaultPathToSecretKey() {
  92. AesCipher cipher = new AesCipher(null);
  93. String path = cipher.getPathToSecretKey();
  94. assertThat(StringUtils.isNotBlank(path)).isTrue();
  95. assertThat(new File(path).getName()).isEqualTo("sonar-secret.txt");
  96. }
  97. @Test
  98. public void loadSecretKeyFromFile() throws Exception {
  99. AesCipher cipher = new AesCipher(null);
  100. Key secretKey = cipher.loadSecretFileFromFile(pathToSecretKey());
  101. assertThat(secretKey.getAlgorithm()).isEqualTo("AES");
  102. assertThat(secretKey.getEncoded().length).isGreaterThan(10);
  103. }
  104. @Test
  105. public void loadSecretKeyFromFile_trim_content() throws Exception {
  106. String path = getPath("non_trimmed_secret_key.txt");
  107. AesCipher cipher = new AesCipher(null);
  108. Key secretKey = cipher.loadSecretFileFromFile(path);
  109. assertThat(secretKey.getAlgorithm()).isEqualTo("AES");
  110. assertThat(secretKey.getEncoded().length).isGreaterThan(10);
  111. }
  112. @Test
  113. public void loadSecretKeyFromFile_file_does_not_exist() throws Exception {
  114. thrown.expect(IllegalStateException.class);
  115. AesCipher cipher = new AesCipher(null);
  116. cipher.loadSecretFileFromFile("/file/does/not/exist");
  117. }
  118. @Test
  119. public void loadSecretKeyFromFile_no_property() throws Exception {
  120. thrown.expect(IllegalStateException.class);
  121. AesCipher cipher = new AesCipher(null);
  122. cipher.loadSecretFileFromFile(null);
  123. }
  124. @Test
  125. public void hasSecretKey() {
  126. AesCipher cipher = new AesCipher(pathToSecretKey());
  127. assertThat(cipher.hasSecretKey()).isTrue();
  128. }
  129. @Test
  130. public void doesNotHaveSecretKey() {
  131. AesCipher cipher = new AesCipher("/my/twitter/id/is/SimonBrandhof");
  132. assertThat(cipher.hasSecretKey()).isFalse();
  133. }
  134. private static String getPath(String file) {
  135. return Resources.getResource(AesCipherTest.class, "AesCipherTest/" + file).getPath();
  136. }
  137. private static String pathToSecretKey() {
  138. return getPath("aes_secret_key.txt");
  139. }
  140. }