added unit test for storing and loading of default cipher key

This commit is contained in:
Sebastian Sdorra
2016-11-29 20:46:30 +01:00
parent 634061a91d
commit 0a47d5946a

View File

@@ -31,15 +31,62 @@
package sonia.scm.security; package sonia.scm.security;
import java.io.File;
import java.io.IOException;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
import sonia.scm.SCMContextProvider;
/** /**
* Unit tests for {@link DefaultCipherHandler}. * Unit tests for {@link DefaultCipherHandler}.
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
*/ */
@RunWith(MockitoJUnitRunner.class)
public class DefaultCipherHandlerTest { public class DefaultCipherHandlerTest {
@Mock
private SCMContextProvider context;
@Mock
private KeyGenerator keyGenerator;
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
/**
* Tests loading and storing default key.
*
* @throws IOException
*/
@Test
public void testLoadingAndStoringDefaultKey() throws IOException {
File baseDirectory = tempFolder.newFolder();
when(context.getBaseDirectory()).thenReturn(baseDirectory);
when(keyGenerator.createKey()).thenReturn("secret");
DefaultCipherHandler cipher = new DefaultCipherHandler(context, keyGenerator);
File configDirectory = new File(baseDirectory, "config");
assertTrue(new File(configDirectory, DefaultCipherHandler.CIPHERKEY_FILENAME).exists());
// plain text for assertion
String plain = "hallo123";
// encrypt value with new generated key
String encrypted = cipher.encode(plain);
// load key from disk
cipher = new DefaultCipherHandler(context, keyGenerator);
// decrypt with loaded key
assertEquals(plain, cipher.decode(encrypted));
}
/** /**
* Test encode and decode method with a separate key. * Test encode and decode method with a separate key.