GpgServiceTest.java (5068B)
1 package com.wimdupont.service; 2 3 4 import org.bouncycastle.openpgp.PGPException; 5 import org.junit.jupiter.api.Assertions; 6 import org.junit.jupiter.api.BeforeAll; 7 import org.junit.jupiter.api.BeforeEach; 8 import org.junit.jupiter.api.Test; 9 import org.junit.jupiter.api.extension.ExtendWith; 10 import org.mockito.Mock; 11 import org.mockito.MockedStatic; 12 import org.mockito.Mockito; 13 import org.mockito.junit.jupiter.MockitoExtension; 14 15 import java.io.FileNotFoundException; 16 import java.io.IOException; 17 import java.nio.charset.Charset; 18 import java.nio.file.Paths; 19 20 import static org.mockito.Mockito.mockStatic; 21 22 @ExtendWith(MockitoExtension.class) 23 public class GpgServiceTest { 24 25 private static final String TOTP_SECRET_FILE = "encrypted-totp-secret"; 26 private static final char[] PASSWORD = {'s', 'e', 'c', 'r', 'e', 't', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; 27 private static String dirPath; 28 private static String secretFile; 29 30 private GpgService gpgService; 31 @Mock 32 private ApplicationProperties applicationProperties; 33 34 @BeforeAll 35 public static void setupClass() { 36 var resourceDirectory = Paths.get("src", "test", "resources") 37 .toAbsolutePath() 38 .toString(); 39 dirPath = String.format("%s/totpfiles/", resourceDirectory); 40 secretFile = String.format("%s/private-key.asc", resourceDirectory); 41 } 42 43 @BeforeEach 44 public void setup() { 45 gpgService = new GpgService(applicationProperties); 46 } 47 48 @Test 49 public void decryptWithCorrectPasswordShouldReturnCorrectSecret() throws PGPException, IOException { 50 Mockito.when(applicationProperties.getDirPath()).thenReturn(dirPath); 51 Mockito.when(applicationProperties.getSecretFile()).thenReturn(secretFile); 52 try (MockedStatic<PasswordReader> mockedPasswordReader = mockStatic(PasswordReader.class)) { 53 mockedPasswordReader.when(PasswordReader::getPassword).thenReturn(PASSWORD); 54 55 var result = gpgService.decrypt(TOTP_SECRET_FILE); 56 57 Assertions.assertNotNull(result); 58 Assertions.assertEquals("This is the big secret key", new String(result, Charset.defaultCharset())); 59 } 60 } 61 62 @Test 63 public void decryptWithWrongPasswordShouldThrowPgpException() { 64 Mockito.when(applicationProperties.getDirPath()).thenReturn(dirPath); 65 Mockito.when(applicationProperties.getSecretFile()).thenReturn(secretFile); 66 try (MockedStatic<PasswordReader> mockedPasswordReader = mockStatic(PasswordReader.class)) { 67 mockedPasswordReader.when(PasswordReader::getPassword).thenReturn(new char[]{'w', 'r', 'o', 'n', 'g'}); 68 69 Exception exception = Assertions.assertThrows(PGPException.class, () -> 70 gpgService.decrypt(TOTP_SECRET_FILE) 71 ); 72 73 Assertions.assertEquals(PGPException.class, exception.getClass()); 74 } 75 } 76 77 @Test 78 public void decryptUnknownFileNameArgShouldThrowException() { 79 Mockito.when(applicationProperties.getDirPath()).thenReturn(dirPath); 80 try (MockedStatic<PasswordReader> mockedPasswordReader = mockStatic(PasswordReader.class)) { 81 mockedPasswordReader.when(PasswordReader::getPassword).thenReturn(PASSWORD); 82 83 Exception exception = Assertions.assertThrows(FileNotFoundException.class, () -> 84 gpgService.decrypt("unexisting") 85 ); 86 87 Assertions.assertEquals(FileNotFoundException.class, exception.getClass()); 88 Assertions.assertEquals("File \"unexisting.gpg\" not found", exception.getMessage()); 89 } 90 } 91 92 @Test 93 public void decryptUnknownFileFromDirPathShouldThrowException() { 94 Mockito.when(applicationProperties.getDirPath()).thenReturn("/wrong/path/"); 95 try (MockedStatic<PasswordReader> mockedPasswordReader = mockStatic(PasswordReader.class)) { 96 mockedPasswordReader.when(PasswordReader::getPassword).thenReturn(PASSWORD); 97 98 Exception exception = Assertions.assertThrows(FileNotFoundException.class, () -> 99 gpgService.decrypt(TOTP_SECRET_FILE) 100 ); 101 102 Assertions.assertEquals(FileNotFoundException.class, exception.getClass()); 103 Assertions.assertEquals("File \"encrypted-totp-secret.gpg\" not found", exception.getMessage()); 104 } 105 } 106 107 @Test 108 public void decryptUnknownFileFromSecretFileShouldThrowException() { 109 Mockito.when(applicationProperties.getDirPath()).thenReturn(dirPath); 110 Mockito.when(applicationProperties.getSecretFile()).thenReturn("/wrong/unexisting.asc"); 111 try (MockedStatic<PasswordReader> mockedPasswordReader = mockStatic(PasswordReader.class)) { 112 mockedPasswordReader.when(PasswordReader::getPassword).thenReturn(PASSWORD); 113 114 Exception exception = Assertions.assertThrows(FileNotFoundException.class, () -> 115 gpgService.decrypt(TOTP_SECRET_FILE) 116 ); 117 118 Assertions.assertEquals(FileNotFoundException.class, exception.getClass()); 119 } 120 } 121 }