2017-04-06 14:53:49 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the GNU Public License v3.0
|
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
|
* http://www.gnu.org/licenses/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.etesync.syncadapter.journalmanager;
|
|
|
|
|
|
|
|
|
|
import com.etesync.syncadapter.utils.Base64;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.codec.Charsets;
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
|
|
|
|
public class EncryptionTest {
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() throws IOException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() throws IOException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testDerivePassword() {
|
2017-04-06 15:39:01 +00:00
|
|
|
|
String key = Crypto.deriveKey(Helpers.USER, Helpers.PASSWORD);
|
|
|
|
|
assertEquals(key, Helpers.keyBase64);
|
2017-04-06 14:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testCryptoV1() throws Exceptions.IntegrityException, Exceptions.GenericCryptoException {
|
2017-04-06 15:39:01 +00:00
|
|
|
|
Crypto.CryptoManager cryptoManager = new Crypto.CryptoManager(1, Helpers.keyBase64, "TestSaltShouldBeJournalId");
|
2017-04-06 14:53:49 +00:00
|
|
|
|
|
|
|
|
|
String clearText = "This Is Some Test Cleartext.";
|
|
|
|
|
byte[] cipher = cryptoManager.encrypt(clearText.getBytes(Charsets.UTF_8));
|
|
|
|
|
assertEquals(clearText, new String(cryptoManager.decrypt(cipher), Charsets.UTF_8));
|
|
|
|
|
|
|
|
|
|
String expected = "Lz+HUFzh1HdjxuGdQrBwBG1IzHT0ug6mO8fwePSbXtc=";
|
|
|
|
|
assertEquals(expected, Base64.encodeToString(cryptoManager.hmac("Some test data".getBytes(Charsets.UTF_8)), Base64.NO_WRAP));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testCryptoV2() throws Exceptions.IntegrityException, Exceptions.GenericCryptoException {
|
2017-04-06 15:39:01 +00:00
|
|
|
|
Crypto.CryptoManager cryptoManager = new Crypto.CryptoManager(2, Helpers.keyBase64, "TestSaltShouldBeJournalId");
|
2017-04-06 14:53:49 +00:00
|
|
|
|
|
|
|
|
|
String clearText = "This Is Some Test Cleartext.";
|
|
|
|
|
byte[] cipher = cryptoManager.encrypt(clearText.getBytes(Charsets.UTF_8));
|
|
|
|
|
assertEquals(clearText, new String(cryptoManager.decrypt(cipher), Charsets.UTF_8));
|
|
|
|
|
|
|
|
|
|
String expected = "XQ/A0gentOaE98R9wzf3zEIAHj4OH1GF8J4C6JiJupo=";
|
|
|
|
|
assertEquals(expected, Base64.encodeToString(cryptoManager.hmac("Some test data".getBytes(Charsets.UTF_8)), Base64.NO_WRAP));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=Exceptions.VersionTooNewException.class)
|
|
|
|
|
public void testCryptoVersionTooNew() throws Exceptions.IntegrityException, Exceptions.VersionTooNewException {
|
2017-04-06 15:39:01 +00:00
|
|
|
|
new Crypto.CryptoManager(120, Helpers.keyBase64, "TestSaltShouldBeJournalId");
|
2017-04-06 14:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected=Exceptions.IntegrityException.class)
|
|
|
|
|
public void testCryptoVersionOutOfRange() throws Exceptions.IntegrityException, Exceptions.VersionTooNewException {
|
2017-04-06 15:39:01 +00:00
|
|
|
|
new Crypto.CryptoManager(999, Helpers.keyBase64, "TestSaltShouldBeJournalId");
|
2017-04-06 14:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|